X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=plcapi.py;h=acccda0e36d19afb3e8b024bdb34602f43808c13;hb=9e6b9c1ea9e020c55c85b433bac47231d63e9ffd;hp=86d04c9839bd03f170a115bfe512ff6b9a556a17;hpb=1d88acf427c819b0dcb408372637f300c4dfdba7;p=nodemanager.git diff --git a/plcapi.py b/plcapi.py index 86d04c9..acccda0 100644 --- a/plcapi.py +++ b/plcapi.py @@ -1,5 +1,10 @@ import safexmlrpc -import hmac, sha +import hmac +try: + from hashlib import sha1 as sha +except ImportError: + import sha +import logger class PLCAPI: """ @@ -12,18 +17,49 @@ class PLCAPI: session => SessionAuth To authenticate using the Boot Manager authentication method, or - the new session-based method. + the new session-based method, respectively. """ - def __init__(self, uri, cacert, auth, timeout = 300, **kwds): + def __init__(self, uri, cacert, auth, timeout = 90, **kwds): + self.uri = uri + self.cacert = cacert + self.timeout = timeout + if isinstance(auth, (tuple, list)): (self.node_id, self.key) = auth self.session = None - else: + elif isinstance(auth, (str, unicode)): self.node_id = self.key = None self.session = auth + else: + self.node_id = self.key = self.session = None + + self.server = safexmlrpc.ServerProxy(self.uri, self.cacert, self.timeout, allow_none = 1, **kwds) + + + def update_session(self, f="/usr/boot/plnode.txt"): + # try authenticatipopulate /etc.planetlab/session + def plnode(key): + try: + return [i[:-1].split('=') for i in open(f).readlines() if i.startswith(key)][0][1].strip('"') + except: + return None + + auth = (int(plnode("NODE_ID")), plnode("NODE_KEY")) + plc = PLCAPI(self.uri, self.cacert, auth, self.timeout) + open("/etc/planetlab/session", 'w').write(plc.GetSession().strip()) + self.session = open("/etc/planetlab/session").read().strip() + + + def check_authentication(self): + authstatus = False + if self.key or self.session: + try: + authstatus = self.AuthCheck() + except: + logger.log_exc("plcapi: failed in plcapi.check_authentication") + return authstatus - self.server = safexmlrpc.ServerProxy(uri, cacert, timeout, allow_none = 1, **kwds) def add_auth(self, function): """ @@ -64,7 +100,8 @@ class PLCAPI: if self.session is not None: # Use session authentication - auth = {'session': self.session} + auth = {'AuthMethod': "session", + 'session': self.session} else: # Yes, this is the "canonicalization" method used. args = canonicalize(params)