我正在尝试使用libcloud(1.3.0)使用以下代码连接到我的GCE项目:
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
USER_ID = "nnnnnnnn@cloudservices.gserviceaccount.com"
KEYFILE = "./xxxxxxx.json"
PROJECT = "project1"
cls = get_driver(Provider.GCE)
driver = cls(USER_ID, KEYFILE, project = PROJECT)
这只是因为PyCrypto中的AttributeError失败了(我正在运行v2.6.1):
Traceback (most recent call last):
File "g.py", line 9, in <module>
driver = cls(USER_ID, KEYFILE, project = PROJECT)
File "/usr/lib/python2.7/site-packages/libcloud/compute/drivers/gce.py", line 1348, in __init__
super(GCENodeDriver, self).__init__(user_id, key, **kwargs)
File "/usr/lib/python2.7/site-packages/libcloud/common/base.py", line 1179, in __init__
self.connection = self.connectionCls(*args, **conn_kwargs)
File "/usr/lib/python2.7/site-packages/libcloud/compute/drivers/gce.py", line 98, in __init__
credential_file=credential_file, **kwargs)
File "/usr/lib/python2.7/site-packages/libcloud/common/google.py", line 765, in __init__
user_id, key, auth_type, credential_file, scopes, **kwargs)
File "/usr/lib/python2.7/site-packages/libcloud/common/google.py", line 660, in __init__
self.token = self.oauth2_conn.get_new_token()
File "/usr/lib/python2.7/site-packages/libcloud/common/google.py", line 530, in get_new_token
signature = base64.urlsafe_b64encode(signer.sign(hash_func))
File "/usr/lib/python2.7/site-packages/Crypto/Signature/PKCS1_v1_5.py", line 110, in sign
em = EMSA_PKCS1_V1_5_ENCODE(mhash, k)
File "/usr/lib/python2.7/site-packages/Crypto/Signature/PKCS1_v1_5.py", line 211, in EMSA_PKCS1_V1_5_ENCODE
digestAlgo = DerSequence([hash.oid, DerNull().encode()])
AttributeError: oid
我已经按照libcloud GCE驱动程序页面上关于设置服务帐户,下载JSON文件等的所有说明进行操作,因此我认为我正在采取正确的步骤,但我无法解决此问题。
答案 0 :(得分:0)
我刚刚使用PyCrypto 2.6.1在Python 2.7.12上测试过,并且在libcloud 1.3.0上没有看到任何问题。看看哈希,我怀疑这是您的服务帐户配置的问题。
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
USER_ID = "559xxxxxx-compute@developer.gserviceaccount.com"
KEYFILE = "./libcloud-test-xxx.json"
PROJECT = "libcloud-test"
cls = get_driver(Provider.GCE)
driver = cls(USER_ID, KEYFILE, project = PROJECT)
print(driver.auth_type) # should be SA
print(driver.list_nodes())
从API Manager,我去了Credentials,然后选择了“Create Credentials” - &gt; “服务帐户密钥”。 对于服务帐户,我选择了“计算引擎默认服务帐户”和JSON格式。
计算引擎默认服务帐户
在凭据管理器中,服务帐户看起来像这样