mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
add apikey/securitykey in cloud-cli
This commit is contained in:
parent
8d4c7208a4
commit
18255e985c
@ -26,20 +26,63 @@ import urllib2
|
||||
import os
|
||||
import xml.dom.minidom
|
||||
import re
|
||||
import base64
|
||||
import hmac
|
||||
import hashlib
|
||||
import httplib
|
||||
|
||||
class CloudAPI:
|
||||
|
||||
@describe("server", "Management Server host name or address")
|
||||
@describe("apikey", "Management Server apiKey")
|
||||
@describe("securitykey", "Management Server securityKey")
|
||||
@describe("responseformat", "Response format: xml or json")
|
||||
@describe("stripxml", "True if xml tags have to be stripped in the output, false otherwise")
|
||||
def __init__(self,
|
||||
server="127.0.0.1:8096",
|
||||
responseformat="xml",
|
||||
stripxml="true"
|
||||
stripxml="true",
|
||||
apiKey=None,
|
||||
securityKey=None
|
||||
):
|
||||
self.__dict__.update(locals())
|
||||
|
||||
def _make_request_with_keys(self,command,requests={}):
|
||||
requests["command"] = command
|
||||
requests["apiKey"] = self.apiKey
|
||||
requests["response"] = "xml"
|
||||
requests = zip(requests.keys(), requests.values())
|
||||
requests.sort(key=lambda x: str.lower(x[0]))
|
||||
|
||||
requestUrl = "&".join(["=".join([request[0], urllib.quote_plus(str(request[1]))]) for request in requests])
|
||||
hashStr = "&".join(["=".join([str.lower(request[0]), urllib.quote_plus(str.lower(str(request[1])))]) for request in requests])
|
||||
|
||||
sig = urllib.quote_plus(base64.encodestring(hmac.new(self.securityKey, hashStr, hashlib.sha1).digest()).strip())
|
||||
|
||||
requestUrl += "&signature=%s"%sig
|
||||
return requestUrl
|
||||
|
||||
|
||||
def _make_request_with_auth(self, command, requests):
|
||||
self.connection = httplib.HTTPConnection("%s"%(self.server))
|
||||
requests["command"] = command
|
||||
requests["apiKey"] = self.apiKey
|
||||
requests["response"] = self.responseformat
|
||||
requests = zip(requests.keys(), requests.values())
|
||||
requests.sort(key=lambda x: str.lower(x[0]))
|
||||
|
||||
requestUrl = "&".join(["=".join([request[0], urllib.quote_plus(str(request[1]))]) for request in requests])
|
||||
hashStr = "&".join(["=".join([str.lower(request[0]), urllib.quote_plus(str.lower(str(request[1])))]) for request in requests])
|
||||
|
||||
sig = urllib.quote_plus(base64.encodestring(hmac.new(self.securityKey, hashStr, hashlib.sha1).digest()).strip())
|
||||
|
||||
requestUrl += "&signature=%s"%sig
|
||||
|
||||
self.connection.request("GET", "/client/api?%s"%requestUrl)
|
||||
return self.connection.getresponse().read()
|
||||
|
||||
def _make_request(self,command,parameters=None):
|
||||
|
||||
'''Command is a string, parameters is a dictionary'''
|
||||
if ":" in self.server:
|
||||
host,port = self.server.split(":")
|
||||
@ -51,9 +94,13 @@ class CloudAPI:
|
||||
url = "http://" + self.server + "/?"
|
||||
|
||||
if not parameters: parameters = {}
|
||||
if self.apiKey is not None and self.securityKey is not None:
|
||||
return self._make_request_with_auth(command, parameters)
|
||||
else:
|
||||
parameters["command"] = command
|
||||
parameters["response"] = self.responseformat
|
||||
querystring = urllib.urlencode(parameters)
|
||||
|
||||
url += querystring
|
||||
|
||||
f = urllib2.urlopen(url)
|
||||
@ -66,9 +113,6 @@ class CloudAPI:
|
||||
data=data.replace("\n<", "\n");
|
||||
data=re.sub("\n.*cloud-stack-version=.*", "", data);
|
||||
data=data.replace("\n\n\n", "\n");
|
||||
else:
|
||||
data="\n"+data+"\n"
|
||||
return data
|
||||
|
||||
return data
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user