9 Wrapper around safexmlrpc.ServerProxy to automagically add an Auth
10 struct as the first argument to every XML-RPC call. Initialize
13 (node_id, key) => BootAuth
15 session => SessionAuth
17 To authenticate using the Boot Manager authentication method, or
18 the new session-based method.
21 def __init__(self, uri, cacert, auth, timeout = 90, **kwds):
24 self.timeout = timeout
26 if isinstance(auth, (tuple, list)):
27 (self.node_id, self.key) = auth
29 elif isinstance(auth, (str, unicode)):
30 self.node_id = self.key = None
33 self.node_id = self.key = self.session = None
35 self.server = safexmlrpc.ServerProxy(self.uri, self.cacert, self.timeout, allow_none = 1, **kwds)
38 def update_session(self, f="/usr/boot/plnode.txt"):
39 # try authenticatipopulate /etc.planetlab/session
42 return [i[:-1].split('=') for i in open(f).readlines() if i.startswith(key)][0][1].strip('"')
46 auth = (int(plnode("NODE_ID")), plnode("NODE_KEY"))
47 plc = PLCAPI(self.uri, self.cacert, auth, self.timeout)
48 open("/etc/planetlab/session", 'w').write(plc.GetSession().strip())
49 self.session = open("/etc/planetlab/session").read().strip()
52 def check_authentication(self):
53 # just a simple call to check authentication
54 return self.AuthCheck(self)
57 def add_auth(self, function):
59 Returns a wrapper which adds an Auth struct as the first
60 argument when the function is called.
63 def canonicalize(args):
65 BootAuth canonicalization method. Parameter values are
66 collected, sorted, converted to strings, then hashed with
73 if isinstance(arg, list) or isinstance(arg, tuple):
74 # The old implementation did not recursively handle
75 # lists of lists. But neither did the old API itself.
76 values += canonicalize(arg)
77 elif isinstance(arg, dict):
78 # Yes, the comments in the old implementation are
79 # misleading. Keys of dicts are not included in the
81 values += canonicalize(arg.values())
83 # We use unicode() instead of str().
84 values.append(unicode(arg))
90 Adds an Auth struct as the first argument when the
94 if self.session is not None:
95 # Use session authentication
96 auth = {'AuthMethod': "session",
97 'session': self.session}
99 # Yes, this is the "canonicalization" method used.
100 args = canonicalize(params)
102 msg = "[" + "".join(args) + "]"
104 # We encode in UTF-8 before calculating the HMAC, which is
105 # an 8-bit algorithm.
106 digest = hmac.new(self.key, msg.encode('utf-8'), sha).hexdigest()
108 auth = {'AuthMethod': "hmac",
109 'node_id': self.node_id,
112 # Automagically add auth struct to every call
113 params = (auth,) + params
115 return function(*params)
119 def __getattr__(self, methodname):
120 function = getattr(self.server, methodname)
121 return self.add_auth(function)