reguire gnupg1 on f>=31; sense the system to use gpg1 when installed
[nodemanager.git] / plcapi.py
index fd04b6a..a03e9e0 100644 (file)
--- a/plcapi.py
+++ b/plcapi.py
@@ -1,9 +1,14 @@
-import xmlrpclib
-import hmac, sha
+import safexmlrpc
+import hmac
+try:
+    from hashlib import sha1 as sha
+except ImportError:
+    import sha
+import logger
 
 class PLCAPI:
     """
-    Wrapper around xmlrpclib.ServerProxy to automagically add an Auth
+    Wrapper around safexmlrpc.ServerProxy to automagically add an Auth
     struct as the first argument to every XML-RPC call. Initialize
     auth with either:
 
@@ -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, auth, **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):
             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 = xmlrpclib.ServerProxy(uri, allow_none = 1, **kwds)
 
     def add_auth(self, function):
         """
@@ -44,15 +80,15 @@ class PLCAPI:
                 if isinstance(arg, list) or isinstance(arg, tuple):
                     # The old implementation did not recursively handle
                     # lists of lists. But neither did the old API itself.
-                    values += self.canonicalize(arg)
+                    values += canonicalize(arg)
                 elif isinstance(arg, dict):
                     # Yes, the comments in the old implementation are
                     # misleading. Keys of dicts are not included in the
                     # hash.
-                    values += self.canonicalize(arg.values())
+                    values += canonicalize(list(arg.values()))
                 else:
                     # We use unicode() instead of str().
-                    values.append(unicode(arg))
+                    values.append(str(arg))
 
             return values
 
@@ -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)