if authentication with the session fails try authenticating with hmac
authorBarış Metin <Talip-Baris.Metin@sophia.inria.fr>
Mon, 6 Apr 2009 09:46:34 +0000 (09:46 +0000)
committerBarış Metin <Talip-Baris.Metin@sophia.inria.fr>
Mon, 6 Apr 2009 09:46:34 +0000 (09:46 +0000)
to get a new session.

nm.py
plcapi.py

diff --git a/nm.py b/nm.py
index e9ca6d6..05eaee7 100755 (executable)
--- a/nm.py
+++ b/nm.py
@@ -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)
index 759207e..e38b0a5 100644 (file)
--- a/plcapi.py
+++ b/plcapi.py
@@ -18,14 +18,52 @@ 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:
+                if self.AuthCheck() == 1: return True
+            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):
         """