pychef中的ssl证书验证

时间:2015-09-18 06:39:36

标签: python ssl chef pychef

我正在尝试使用python连接到大厨api。我正在使用pychef从python连接到chef。

以下是代码:

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')

我收到以下证书错误:

[root@chef-e 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参数,但收到错误:

TypeError: __init__() got an unexpected keyword argument 'ssl_verify'
来自api.py的

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

关于我的环境的一些信息:

[root@chef-e 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

1 个答案:

答案 0 :(得分: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尚未包含此选项)。因此要么使用主分支中的源,要么唠叨作者推出新版本。