Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom Auth handler that the Splunk REST API Modular Input can use to connect to the Cloudstack REST API
from requests.auth import AuthBase
import hmac
import base64
import hashlib
import urlparse
import urllib
class CloudstackAuth(AuthBase):
def __init__(self,**args):
# setup any auth-related data here
self.apikey = args['apikey']
self.secretkey = args['secretkey']
pass
def __call__(self, r):
# modify and return the request
parsed = urlparse.urlparse(r.url)
url = parsed.geturl().split('?',1)[0]
url_params= urlparse.parse_qs(parsed.query)
#normalize the list value
for param in url_params:
url_params[param] = url_params[param][0]
url_params['apikey'] = self.apikey
keys = sorted(url_params.keys())
sig_params = []
for k in keys:
sig_params.append(k + '=' + urllib.quote_plus(url_params[k]).replace("+", "%20"))
query = '&'.join(sig_params)
signature = base64.b64encode(hmac.new(
self.secretkey,
msg=query.lower(),
digestmod=hashlib.sha1
).digest())
query += '&signature=' + urllib.quote_plus(signature)
r.url = url + '?' + query
return r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.