cloudnをAPIで利用するために、python2 で書かれているcloudstack-cloudmonkey をpython3で動かそうとしているのですが、途中でつまずいています。
2to3 で変換した後、importとかを修正することで pipインストールして起動するとことろまで行きました。
shell
1cloudmonkey 2☁ Apache CloudStack ???? cloudmonkey 5.3.3. Type help or ? to list commands. 3 4Using management server profile: local 5 6(local) ???? >
秘密鍵の登録まではうまくいくようになったのですが、コマンドを実行するとエラーになってしまいます。
python
1(local) ???? > list virtualmachines 2Traceback (most recent call last): 3 File "/usr/local/bin/cloudmonkey", line 11, in <module> 4 load_entry_point('cloudmonkey', 'console_scripts', 'cloudmonkey')() 5 File "/Users/kentaro/python/cloudstack-cloudmonkey/cloudmonkey/cloudmonkey.py", line 920, in main 6 shell.cmdloop() 7 File "/Users/kentaro/python/cloudstack-cloudmonkey/cloudmonkey/cloudmonkey.py", line 155, in cmdloop 8 super(CloudMonkeyShell, self).cmdloop(intro="") 9 File "/usr/local/Cellar/python3/3.5.2_1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/cmd.py", line 138, in cmdloop 10 stop = self.onecmd(line) 11 File "/usr/local/Cellar/python3/3.5.2_1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/cmd.py", line 217, in onecmd 12 return func(arg) 13 File "/Users/kentaro/python/cloudstack-cloudmonkey/cloudmonkey/cloudmonkey.py", line 208, in grammar_closure 14 self.default("{0} {1}".format(cmd, args)) 15 File "/Users/kentaro/python/cloudstack-cloudmonkey/cloudmonkey/cloudmonkey.py", line 482, in default 16 result = self.make_request(apiname, args_dict, isasync) 17 File "/Users/kentaro/python/cloudstack-cloudmonkey/cloudmonkey/cloudmonkey.py", line 395, in make_request 18 self.signatureversion) 19 File "/Users/kentaro/python/cloudstack-cloudmonkey/cloudmonkey/requester.py", line 262, in monkeyrequest 20 signatureversion) 21 File "/Users/kentaro/python/cloudstack-cloudmonkey/cloudmonkey/requester.py", line 223, in make_request 22 args["signature"] = sign_request(args, credentials['secretkey']) 23 File "/Users/kentaro/python/cloudstack-cloudmonkey/cloudmonkey/requester.py", line 220, in sign_request 24 hashlib.sha1).digest()).strip() 25 File "/usr/local/Cellar/python3/3.5.2_1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/hmac.py", line 144, in new 26 return HMAC(key, msg, digestmod) 27 File "/usr/local/Cellar/python3/3.5.2_1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/hmac.py", line 42, in __init__ 28 raise TypeError("key: expected bytes or bytearray, but got %r" % type(key).__name__) 29TypeError: key: expected bytes or bytearray, but got 'str'
秘密鍵を使用し HMAC SHA-1でハッシュをかけます。
ハッシュにはopensslコマンドを利用し、ハッシュ結果はBASE64で文字列化します。
となっている箇所だと思うのですが、どこをバイナリにすればいいのかよくわかりません。
python
1 def sign_request(params, secret_key): 2 request = list(zip(list(params.keys()), list(params.values()))) 3 request.sort(key=lambda x: x[0].lower()) 4 hash_str = "&".join( 5 ["=".join( 6 [r[0].lower(), 7 urllib.parse.quote_plus(str(r[1]), safe="*").lower() 8 .replace("+", "%20").replace("%3A", ":")] 9 ) for r in request] 10 ) 11 12 return base64.encodestring(hmac.new(secret_key, hash_str, 13 hashlib.sha1).digest()).strip()

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/27 15:35 編集