1- import six
21import datetime
32import logging
43import os
54import re
65import socket
76import subprocess
87import threading
9- import six .moves .urllib .request
10- import six .moves .urllib .error
11- import six .moves .urllib .parse
128import weakref
9+ import six
1310
1411import pkg_resources
1512
13+ import requests
14+
1615from chef .auth import sign_request
1716from chef .exceptions import ChefServerError
1817from chef .rsa import Key
@@ -38,19 +37,6 @@ class UnknownRubyExpression(Exception):
3837 """Token exception for unprocessed Ruby expressions."""
3938
4039
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-
5440class ChefAPI (object ):
5541 """The ChefAPI object is a wrapper for a single Chef server.
5642
@@ -70,6 +56,8 @@ class ChefAPI(object):
7056 env_value_re = re .compile (r'ENV\[(.+)\]' )
7157 ruby_string_re = re .compile (r'^\s*(["\'])(.*?)\1\s*$' )
7258
59+ verify_ssl = True
60+
7361 def __init__ (self , url , key , client , version = '0.10.8' , headers = {}):
7462 self .url = url .rstrip ('/' )
7563 self .parsed_url = six .moves .urllib .parse .urlparse (self .url )
@@ -192,11 +180,8 @@ def __exit__(self, type, value, traceback):
192180 del api_stack_value ()[- 1 ]
193181
194182 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
200185
201186 def request (self , method , path , headers = {}, data = None ):
202187 auth_headers = sign_request (key = self .key , http_method = method ,
@@ -228,13 +213,13 @@ def api_request(self, method, path, headers={}, data=None):
228213 headers ['content-type' ] = 'application/json'
229214 data = json .dumps (data )
230215 response = self .request (method , path , headers , data )
231- return json . loads ( response .decode () )
216+ return response .json ( )
232217
233218 def __getitem__ (self , path ):
234219 return self .api_request ('GET' , path )
235220
236221
237- def autoconfigure (base_path = None ):
222+ def autoconfigure (base_path = None , verify_ssl = True ):
238223 """Try to find a knife or chef-client config file to load parameters from,
239224 starting from either the given base path or the current working directory.
240225
@@ -253,16 +238,19 @@ def autoconfigure(base_path=None):
253238 config_path = os .path .join (path , '.chef' , 'knife.rb' )
254239 api = ChefAPI .from_config_file (config_path )
255240 if api is not None :
241+ api .verify_ssl = verify_ssl
256242 return api
257243
258244 # The walk didn't work, try ~/.chef/knife.rb
259245 config_path = os .path .expanduser (os .path .join ('~' , '.chef' , 'knife.rb' ))
260246 api = ChefAPI .from_config_file (config_path )
261247 if api is not None :
248+ api .verify_ssl = verify_ssl
262249 return api
263250
264251 # Nothing in the home dir, try /etc/chef/client.rb
265252 config_path = os .path .join (os .path .sep , 'etc' , 'chef' , 'client.rb' )
266253 api = ChefAPI .from_config_file (config_path )
267254 if api is not None :
255+ api .verify_ssl = verify_ssl
268256 return api
0 commit comments