From 7ec76682b323d9168be481921ebe81ccfebd92db Mon Sep 17 00:00:00 2001 From: Faiyaz Ahmed Date: Tue, 26 May 2009 18:47:44 +0000 Subject: [PATCH] Merge changes from trunk. Check auth and recompute session key when out of synch. Also update PLCDefaults. --- nm.py | 6 +++--- plcapi.py | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/nm.py b/nm.py index e9ca6d6..2c05d20 100755 --- a/nm.py +++ b/nm.py @@ -55,8 +55,8 @@ def GetSlivers(plc, config): try: logger.log("Syncing w/ PLC") data = plc.GetSlivers() - getPLCDefaults(data, config) if (options.verbose): logger.log_slivers(data) + getPLCDefaults(data, config) except: logger.log_exc() # XXX So some modules can at least boostrap. @@ -81,7 +81,7 @@ def getPLCDefaults(data, config): for slice in data.get('slivers'): if slice['name'] == config.PLC_SLICE_PREFIX+"_default": attr_dict = {} - for attr in slice.get('attributes'): attr_dict[attr['name']] = attr['value'] + for attr in slice.get('attributes'): attr_dict[attr['tagname']] = attr['value'] if len(attr_dict): logger.verbose("Found default slice overrides.\n %s" % attr_dict) config.OVERRIDES = attr_dict @@ -127,7 +127,7 @@ def run(): if os.path.exists(options.session): session = file(options.session).read().strip() else: - session = options.session + session = None # Initialize XML-RPC client iperiod=int(options.period) diff --git a/plcapi.py b/plcapi.py index 759207e..03a9ca7 100644 --- a/plcapi.py +++ b/plcapi.py @@ -18,14 +18,55 @@ class PLCAPI: """ 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 - - self.server = safexmlrpc.ServerProxy(uri, cacert, timeout, allow_none = 1, **kwds) + else: + self.node_id = self.key = self.session = None + + self.server = safexmlrpc.ServerProxy(self.uri, self.cacert, self.timeout, allow_none = 1, **kwds) + + self.__check_authentication() + + + 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): + # just a simple call to check authentication + def check(): + if (self.node_id and self.key) or self.session: + try: + if self.AuthCheck() == 1: return True + except: + return False + return False + if not check(): + if self.node_id and self.key: + # if hmac fails, just make it fail + raise Exception, "Unable to authenticate with hmac" + else: + self.__update_session() + if not check(): + raise Exception, "Unable to authenticate with session" + def add_auth(self, function): """ -- 2.47.0