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,7 +56,7 @@ 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
73
- def __init__ (self , url , key , client , version = '0.10.8' , headers = {}):
59
+ def __init__ (self , url , key , client , version = '0.10.8' , headers = {}, ssl_verify = True ):
74
60
self .url = url .rstrip ('/' )
75
61
self .parsed_url = six .moves .urllib .parse .urlparse (self .url )
76
62
if not isinstance (key , Key ):
@@ -83,6 +69,7 @@ def __init__(self, url, key, client, version='0.10.8', headers={}):
83
69
self .headers = dict ((k .lower (), v ) for k , v in six .iteritems (headers ))
84
70
self .version_parsed = pkg_resources .parse_version (self .version )
85
71
self .platform = self .parsed_url .hostname == 'api.opscode.com'
72
+ self .ssl_verify = ssl_verify
86
73
if not api_stack_value ():
87
74
self .set_default ()
88
75
@@ -97,6 +84,7 @@ def from_config_file(cls, path):
97
84
log .debug ('Unable to read config file "%s"' , path )
98
85
return
99
86
url = key_path = client_name = None
87
+ ssl_verify = True
100
88
for line in open (path ):
101
89
if not line .strip () or line .startswith ('#' ):
102
90
continue # Skip blanks and comments
@@ -107,6 +95,10 @@ def from_config_file(cls, path):
107
95
md = cls .ruby_string_re .search (value )
108
96
if md :
109
97
value = md .group (2 )
98
+ elif key == 'ssl_verify_mode' :
99
+ log .debug ('Found ssl_verify_mode: %r' , value )
100
+ ssl_verify = (value .strip () != ':verify_none' )
101
+ log .debug ('ssl_verify = %s' , ssl_verify )
110
102
else :
111
103
# Not a string, don't even try
112
104
log .debug ('Value for {0} does not look like a string: {1}' .format (key , value ))
@@ -137,6 +129,7 @@ def _ruby_value(match):
137
129
if not os .path .isabs (key_path ):
138
130
# Relative paths are relative to the config file
139
131
key_path = os .path .abspath (os .path .join (os .path .dirname (path ), key_path ))
132
+
140
133
if not (url and client_name and key_path ):
141
134
# No URL, no chance this was valid, try running Ruby
142
135
log .debug ('No Chef server config found, trying Ruby parse' )
@@ -165,7 +158,7 @@ def _ruby_value(match):
165
158
return
166
159
if not client_name :
167
160
client_name = socket .getfqdn ()
168
- return cls (url , key_path , client_name )
161
+ return cls (url , key_path , client_name , ssl_verify = ssl_verify )
169
162
170
163
@staticmethod
171
164
def get_global ():
@@ -192,11 +185,8 @@ def __exit__(self, type, value, traceback):
192
185
del api_stack_value ()[- 1 ]
193
186
194
187
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 ()
188
+ request = requests .api .request (method , url , headers = headers , data = data , verify = self .ssl_verify )
189
+ return request
200
190
201
191
def request (self , method , path , headers = {}, data = None ):
202
192
auth_headers = sign_request (key = self .key , http_method = method ,
@@ -228,7 +218,7 @@ def api_request(self, method, path, headers={}, data=None):
228
218
headers ['content-type' ] = 'application/json'
229
219
data = json .dumps (data )
230
220
response = self .request (method , path , headers , data )
231
- return json . loads ( response .decode () )
221
+ return response .json ( )
232
222
233
223
def __getitem__ (self , path ):
234
224
return self .api_request ('GET' , path )
0 commit comments