2015-09-18 159 views
1

我正在嘗試使用python連接到廚師api。我使用pychef從python連接到廚師。pychef中的ssl證書驗證

以下是代碼:

import chef 
with chef.ChefAPI('https://chef-e.xxxx.com:443/organizations/xxxx', '/root/.chef/rajgourav.pem', 'rajgourav'): 
    n = chef.Node('chef-e.xxxx.com') 

我得到以下證書錯誤:

[[email protected] py]# /appl/python27/bin/python mychef.py 
Traceback (most recent call last): 
File "mychef.py", line 6, in <module> 
n = chef.Node('chef-e.xxxx.com') 
File "/appl/python27/lib/python2.7/site-packages/chef/base.py", line 58, in __init__ 
data = self.api[self.url] 
File "/appl/python27/lib/python2.7/site-packages/chef/api.py", line 229, in __getitem__ 
return self.api_request('GET', path) 
File "/appl/python27/lib/python2.7/site-packages/chef/api.py", line 225, in api_request 
response = self.request(method, path, headers, data) 
File "/appl/python27/lib/python2.7/site-packages/chef/api.py", line 208, in request 
response = self._request(method, self.url+path, data, dict((k.capitalize(), v) for k, v in request_headers.iteritems())) 
File "/appl/python27/lib/python2.7/site-packages/chef/api.py", line 195, in _request 
return urllib2.urlopen(request).read() 
File "/appl/python27/lib/python2.7/urllib2.py", line 154, in urlopen 
return opener.open(url, data, timeout) 
File "/appl/python27/lib/python2.7/urllib2.py", line 431, in open 
response = self._open(req, data) 
File "/appl/python27/lib/python2.7/urllib2.py", line 449, in _open 
'_open', req) 
File "/appl/python27/lib/python2.7/urllib2.py", line 409, in _call_chain 
result = func(*args) 
File "/appl/python27/lib/python2.7/urllib2.py", line 1240, in https_open 
context=self._context) 
File "/appl/python27/lib/python2.7/urllib2.py", line 1197, in do_open 
raise URLError(err) 
urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)> 

能否請你幫我解決SSL證書錯誤。我知道我必須接受證書並將其添加到可信證書列表中,但不知道如何在python中執行此操作。

P.S.我能夠在沒有任何問題的情況下使用刀。

我嘗試使用ssl_verify PARAM但我得到錯誤:從api.py

TypeError: __init__() got an unexpected keyword argument 'ssl_verify' 

def __init__(self, url, key, client, version='0.10.8', headers={}): 

我的ENV一些信息:

[[email protected] py]# /appl/python27/bin/python 
Python 2.7.10 (default, Aug 8 2015, 06:25:19) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import chef 
>>> chef.__version__ 
(0, 2, 3, 'dev') 
>>> 

謝謝, Rajgourav Jain

+0

我也更新了我的答案,以反映您的修改。 – StephenKing

回答

2

__init__ method of ChefAPI具有以下特徵:

def __init__(self, url, key, client, version='0.10.8', headers={}, ssl_verify=True): 

所以,很顯然,你必須在參數ssl_verify設置爲False

import chef 
with chef.ChefAPI(
    'https://chef-e.xxxx.com:443/organizations/xxxx', 
    '/root/.chef/rajgourav.pem', 
    'rajgourav', 
    ssl_verify=False): 

不過說實在的,這仍然是在documentation失蹤。

編輯:的確,這似乎是一個尚未發佈的變化(v0.2.3 tag尚未包含此選項)。因此,無論是使用主分支的源代碼還是嘮叨作者推出新版本。

+0

如何修復證書的信任而不是禁用驗證? python不能利用SSL_CERT_FILE環境變量(或者給出一個CA列表?)。這意味着將自簽名證書添加到此列表中,[我的關於添加證書的答案](http://stackoverflow.com/questions/25604784/chef-berkshelf3-proxy-settings-windows-platform/25605415#25605415) – Tensibai

+0

Sure這可能是更好的方法。我沒有使用pychef,所以我不知道確切的細節。 – StephenKing

+0

根據堆棧跟蹤它是urllib2的問題,而不是自己做python我只是給出了這個想法,以防萬一你已經處理了它;) – Tensibai