|
| 1 | +<?xml version='1.0' encoding='utf-8' ?> |
| 2 | +<!DOCTYPE section PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [ |
| 3 | +<!ENTITY % BOOK_ENTITIES SYSTEM "cloudstack.ent"> |
| 4 | +%BOOK_ENTITIES; |
| 5 | +]> |
| 6 | + |
| 7 | +<!-- Licensed to the Apache Software Foundation (ASF) under one |
| 8 | + or more contributor license agreements. See the NOTICE file |
| 9 | + distributed with this work for additional information |
| 10 | + regarding copyright ownership. The ASF licenses this file |
| 11 | + to you under the Apache License, Version 2.0 (the |
| 12 | + "License"); you may not use this file except in compliance |
| 13 | + with the License. You may obtain a copy of the License at |
| 14 | +
|
| 15 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | +
|
| 17 | + Unless required by applicable law or agreed to in writing, |
| 18 | + software distributed under the License is distributed on an |
| 19 | + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 20 | + KIND, either express or implied. See the License for the |
| 21 | + specific language governing permissions and limitations |
| 22 | + under the License. |
| 23 | +--> |
| 24 | + |
| 25 | +<section id="signing-api-calls-python"> |
| 26 | + <title>How to sign an API call with Python</title> |
| 27 | + <para>To illustrate the procedure used to sign API calls we present a step by step interactive session |
| 28 | + using Python.</para> |
| 29 | + |
| 30 | + <para>First import the required modules:</para> |
| 31 | + <programlisting> |
| 32 | + |
| 33 | + <![CDATA[ |
| 34 | +$python |
| 35 | +Python 2.7.3 (default, Nov 17 2012, 19:54:34) |
| 36 | +[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin |
| 37 | +Type "help", "copyright", "credits" or "license" for more information. |
| 38 | +>>> import urllib2 |
| 39 | +>>> import urllib |
| 40 | +>>> import hashlib |
| 41 | +>>> import hmac |
| 42 | +>>> import base64 |
| 43 | + ]]> |
| 44 | + </programlisting> |
| 45 | + |
| 46 | + <para>Define the endpoint of the Cloud, the command that you want to execute and the keys of the user.</para> |
| 47 | + <programlisting> |
| 48 | + <![CDATA[ |
| 49 | +
|
| 50 | +>>> baseurl='http://localhost:8080/client/api?' |
| 51 | +>>> request={} |
| 52 | +>>> request['command']='listUsers' |
| 53 | +>>> request['response']='json' |
| 54 | +>>> request['apikey']='plgWJfZK4gyS3mOMTVmjUVg-X-jlWlnfaUJ9GAbBbf9EdM-kAYMmAiLqzzq1ElZLYq_u38zCm0bewzGUdP66mg' |
| 55 | +>>> secretkey='VDaACYb0LV9eNjTetIOElcVQkvJck_J_QljX_FcHRj87ZKiy0z0ty0ZsYBkoXkY9b7eq1EhwJaw7FF3akA3KBQ' |
| 56 | + ]]> |
| 57 | + </programlisting> |
| 58 | + <para>Build the request string:</para> |
| 59 | + <programlisting> |
| 60 | + <![CDATA[ |
| 61 | +>>> request_str='&'.join(['='.join([k,urllib.quote_plus(request[k])]) for k in request.keys()]) |
| 62 | +>>> request_str |
| 63 | +'apikey=plgWJfZK4gyS3mOMTVmjUVg-X-jlWlnfaUJ9GAbBbf9EdM-kAYMmAiLqzzq1ElZLYq_u38zCm0bewzGUdP66mg&command=listUsers&response=json' |
| 64 | + ]]> |
| 65 | + </programlisting> |
| 66 | + |
| 67 | + <para>Compute the signature with hmac, do a 64 bit encoding and a url encoding: </para> |
| 68 | + <programlisting> |
| 69 | + <![CDATA[ |
| 70 | +>>> sig_str='&'.join(['='.join([k.lower(),urllib.quote_plus(request[k].lower().replace('+','%20'))])for k in sorted(request.iterkeys())]) |
| 71 | +>>> sig_str |
| 72 | +'apikey=plgwjfzk4gys3momtvmjuvg-x-jlwlnfauj9gabbbf9edm-kaymmailqzzq1elzlyq_u38zcm0bewzgudp66mg&command=listusers&response=json' |
| 73 | +>>> sig=hmac.new(secretkey,sig_str,hashlib.sha1) |
| 74 | +>>> sig |
| 75 | +<hmac.HMAC instance at 0x10d91d680> |
| 76 | +>>> sig=hmac.new(secretkey,sig_str,hashlib.sha1).digest() |
| 77 | +>>> sig |
| 78 | +'M:]\x0e\xaf\xfb\x8f\xf2y\xf1p\x91\x1e\x89\x8a\xa1\x05\xc4A\xdb' |
| 79 | +>>> sig=base64.encodestring(hmac.new(secretkey,sig_str,hashlib.sha1).digest()) |
| 80 | +>>> sig |
| 81 | +'TTpdDq/7j/J58XCRHomKoQXEQds=\n' |
| 82 | +>>> sig=base64.encodestring(hmac.new(secretkey,sig_str,hashlib.sha1).digest()).strip() |
| 83 | +>>> sig |
| 84 | +'TTpdDq/7j/J58XCRHomKoQXEQds=' |
| 85 | +>>> sig=urllib.quote_plus(base64.encodestring(hmac.new(secretkey,sig_str,hashlib.sha1).digest()).strip()) |
| 86 | + ]]> |
| 87 | + </programlisting> |
| 88 | + |
| 89 | + <para>Finally, build the entire string and do an http GET:</para> |
| 90 | + <programlisting> |
| 91 | + <![CDATA[ |
| 92 | +>>> req=baseurl+request_str+'&signature='+sig |
| 93 | +>>> req |
| 94 | +'http://localhost:8080/client/api?apikey=plgWJfZK4gyS3mOMTVmjUVg-X-jlWlnfaUJ9GAbBbf9EdM-kAYMmAiLqzzq1ElZLYq_u38zCm0bewzGUdP66mg&command=listUsers&response=json&signature=TTpdDq%2F7j%2FJ58XCRHomKoQXEQds%3D' |
| 95 | +>>> res=urllib2.urlopen(req) |
| 96 | +>>> res.read() |
| 97 | +'{ "listusersresponse" : { "count":3 ,"user" : [ {"id":"7ed6d5da-93b2-4545-a502-23d20b48ef2a","username":"admin","firstname":"admin","lastname":"cloud","created":"2012-07-05T12:18:27-0700","state":"enabled","account":"admin","accounttype":1,"domainid":"8a111e58-e155-4482-93ce-84efff3c7c77","domain":"ROOT","apikey":"plgWJfZK4gyS3mOMTVmjUVg-X-jlWlnfaUJ9GAbBbf9EdM-kAYMmAiLqzzq1ElZLYq_u38zCm0bewzGUdP66mg","secretkey":"VDaACYb0LV9eNjTetIOElcVQkvJck_J_QljX_FcHRj87ZKiy0z0ty0ZsYBkoXkY9b7eq1EhwJaw7FF3akA3KBQ","accountid":"7548ac03-af1d-4c1c-9064-2f3e2c0eda0d"}, {"id":"1fea6418-5576-4989-a21e-4790787bbee3","username":"runseb","firstname":"foobar","lastname":"goa","email":"joe@smith.com","created":"2013-04-10T16:52:06-0700","state":"enabled","account":"admin","accounttype":1,"domainid":"8a111e58-e155-4482-93ce-84efff3c7c77","domain":"ROOT","apikey":"Xhsb3MewjJQaXXMszRcLvQI9_NPy_UcbDj1QXikkVbDC9MDSPwWdtZ1bUY1H7JBEYTtDDLY3yuchCeW778GkBA","secretkey":"gIsgmi8C5YwxMHjX5o51pSe0kqs6JnKriw0jJBLceY5bgnfzKjL4aM6ctJX-i1ddQIHJLbLJDK9MRzsKk6xZ_w","accountid":"7548ac03-af1d-4c1c-9064-2f3e2c0eda0d"}, {"id":"52f65396-183c-4473-883f-a37e7bb93967","username":"toto","firstname":"john","lastname":"smith","email":"john@smith.com","created":"2013-04-23T04:27:22-0700","state":"enabled","account":"admin","accounttype":1,"domainid":"8a111e58-e155-4482-93ce-84efff3c7c77","domain":"ROOT","apikey":"THaA6fFWS_OmvU8od201omxFC8yKNL_Hc5ZCS77LFCJsRzSx48JyZucbUul6XYbEg-ZyXMl_wuEpECzK-wKnow","secretkey":"O5ywpqJorAsEBKR_5jEvrtGHfWL1Y_j1E4Z_iCr8OKCYcsPIOdVcfzjJQ8YqK0a5EzSpoRrjOFiLsG0hQrYnDA","accountid":"7548ac03-af1d-4c1c-9064-2f3e2c0eda0d"} ] } }' |
| 98 | + ]]> |
| 99 | + </programlisting> |
| 100 | + |
| 101 | + </section> |
0 commit comments