Skip to content
This repository was archived by the owner on May 9, 2020. It is now read-only.

Commit 337d321

Browse files
committed
Replace urllib2 with requests, add an option to disable SSL cert verification
1 parent e42a551 commit 337d321

File tree

2 files changed

+13
-25
lines changed

2 files changed

+13
-25
lines changed

chef/api.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
import six
21
import datetime
32
import logging
43
import os
54
import re
65
import socket
76
import subprocess
87
import threading
9-
import six.moves.urllib.request
10-
import six.moves.urllib.error
11-
import six.moves.urllib.parse
128
import weakref
9+
import six
1310

1411
import pkg_resources
1512

13+
import requests
14+
1615
from chef.auth import sign_request
1716
from chef.exceptions import ChefServerError
1817
from 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-
5440
class 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

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
'Programming Language :: Python',
3030
],
3131
zip_safe = False,
32-
install_requires = ['six>=1.9.0'],
32+
install_requires = ['six>=1.9.0','requests>=2.7.0'],
3333
tests_require = ['unittest2', 'mock'],
3434
test_suite = 'unittest2.collector',
3535
)

0 commit comments

Comments
 (0)