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):
54 if self.key or self.session:
56 authstatus = self.AuthCheck()
62 def add_auth(self, function):
64 Returns a wrapper which adds an Auth struct as the first
65 argument when the function is called.
68 def canonicalize(args):
70 BootAuth canonicalization method. Parameter values are
71 collected, sorted, converted to strings, then hashed with
78 if isinstance(arg, list) or isinstance(arg, tuple):
79 # The old implementation did not recursively handle
80 # lists of lists. But neither did the old API itself.
81 values += canonicalize(arg)
82 elif isinstance(arg, dict):
83 # Yes, the comments in the old implementation are
84 # misleading. Keys of dicts are not included in the
86 values += canonicalize(arg.values())
88 # We use unicode() instead of str().
89 values.append(unicode(arg))
95 Adds an Auth struct as the first argument when the
99 if self.session is not None:
100 # Use session authentication
101 auth = {'AuthMethod': "session",
102 'session': self.session}
104 # Yes, this is the "canonicalization" method used.
105 args = canonicalize(params)
107 msg = "[" + "".join(args) + "]"
109 # We encode in UTF-8 before calculating the HMAC, which is
110 # an 8-bit algorithm.
111 digest = hmac.new(self.key, msg.encode('utf-8'), sha).hexdigest()
113 auth = {'AuthMethod': "hmac",
114 'node_id': self.node_id,
117 # Automagically add auth struct to every call
118 params = (auth,) + params
120 return function(*params)
124 def __getattr__(self, methodname):
125 function = getattr(self.server, methodname)
126 return self.add_auth(function)