1
- import six
2
1
import datetime
3
2
import logging
4
3
import os
5
4
import re
6
5
import socket
7
6
import subprocess
8
7
import threading
9
- import six .moves .urllib .request
10
- import six .moves .urllib .error
11
- import six .moves .urllib .parse
12
8
import weakref
9
+ import six
13
10
14
11
import pkg_resources
15
12
13
+ import requests
14
+
16
15
from chef .auth import sign_request
17
16
from chef .exceptions import ChefServerError
18
17
from chef .rsa import Key
@@ -38,19 +37,6 @@ class UnknownRubyExpression(Exception):
38
37
"""Token exception for unprocessed Ruby expressions."""
39
38
40
39
41
- class ChefRequest (six .moves .urllib .request .Request ):
42
- """Workaround for using PUT/DELETE with urllib2."""
43
- def __init__ (self , * args , ** kwargs ):
44
- self ._method = kwargs .pop ('method' , None )
45
- # Request is an old-style class, no super() allowed.
46
- six .moves .urllib .request .Request .__init__ (self , * args , ** kwargs )
47
-
48
- def get_method (self ):
49
- if self ._method :
50
- return self ._method
51
- return six .moves .urllib .request .Request .get_method (self )
52
-
53
-
54
40
class ChefAPI (object ):
55
41
"""The ChefAPI object is a wrapper for a single Chef server.
56
42
@@ -70,6 +56,8 @@ class ChefAPI(object):
70
56
env_value_re = re .compile (r'ENV\[(.+)\]' )
71
57
ruby_string_re = re .compile (r'^\s*(["\'])(.*?)\1\s*$' )
72
58
59
+ verify_ssl = True
60
+
73
61
def __init__ (self , url , key , client , version = '0.10.8' , headers = {}):
74
62
self .url = url .rstrip ('/' )
75
63
self .parsed_url = six .moves .urllib .parse .urlparse (self .url )
@@ -192,11 +180,8 @@ def __exit__(self, type, value, traceback):
192
180
del api_stack_value ()[- 1 ]
193
181
194
182
def _request (self , method , url , data , headers ):
195
- # Testing hook, subclass and override for WSGI intercept
196
- if six .PY3 and data :
197
- data = data .encode ()
198
- request = ChefRequest (url , data , headers , method = method )
199
- return six .moves .urllib .request .urlopen (request ).read ()
183
+ request = requests .api .request (method , url , headers = headers , data = data , verify = self .verify_ssl )
184
+ return request
200
185
201
186
def request (self , method , path , headers = {}, data = None ):
202
187
auth_headers = sign_request (key = self .key , http_method = method ,
@@ -228,13 +213,13 @@ def api_request(self, method, path, headers={}, data=None):
228
213
headers ['content-type' ] = 'application/json'
229
214
data = json .dumps (data )
230
215
response = self .request (method , path , headers , data )
231
- return json . loads ( response .decode () )
216
+ return response .json ( )
232
217
233
218
def __getitem__ (self , path ):
234
219
return self .api_request ('GET' , path )
235
220
236
221
237
- def autoconfigure (base_path = None ):
222
+ def autoconfigure (base_path = None , verify_ssl = True ):
238
223
"""Try to find a knife or chef-client config file to load parameters from,
239
224
starting from either the given base path or the current working directory.
240
225
@@ -253,16 +238,19 @@ def autoconfigure(base_path=None):
253
238
config_path = os .path .join (path , '.chef' , 'knife.rb' )
254
239
api = ChefAPI .from_config_file (config_path )
255
240
if api is not None :
241
+ api .verify_ssl = verify_ssl
256
242
return api
257
243
258
244
# The walk didn't work, try ~/.chef/knife.rb
259
245
config_path = os .path .expanduser (os .path .join ('~' , '.chef' , 'knife.rb' ))
260
246
api = ChefAPI .from_config_file (config_path )
261
247
if api is not None :
248
+ api .verify_ssl = verify_ssl
262
249
return api
263
250
264
251
# Nothing in the home dir, try /etc/chef/client.rb
265
252
config_path = os .path .join (os .path .sep , 'etc' , 'chef' , 'client.rb' )
266
253
api = ChefAPI .from_config_file (config_path )
267
254
if api is not None :
255
+ api .verify_ssl = verify_ssl
268
256
return api
0 commit comments