aec4cfde82fdf9ddbc588cb2033f2f679e1918b2
[sfa.git] / geni / util / api.py
1 #
2 # Geniwrapper XML-RPC and SOAP interfaces
3 #
4 #
5
6 import sys
7 import os
8 import traceback
9 import string
10 import xmlrpclib
11 from geni.util.auth import Auth
12 from geni.util.config import *
13 from geni.util.faults import *
14 from geni.util.debug import *
15 from geni.util.rights import *
16 from geni.util.credential import *
17
18 # See "2.2 Characters" in the XML specification:
19 #
20 # #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD]
21 # avoiding
22 # [#x7F-#x84], [#x86-#x9F], [#xFDD0-#xFDDF]
23
24 invalid_xml_ascii = map(chr, range(0x0, 0x8) + [0xB, 0xC] + range(0xE, 0x1F))
25 xml_escape_table = string.maketrans("".join(invalid_xml_ascii), "?" * len(invalid_xml_ascii))
26
27 def xmlrpclib_escape(s, replace = string.replace):
28     """
29     xmlrpclib does not handle invalid 7-bit control characters. This
30     function augments xmlrpclib.escape, which by default only replaces
31     '&', '<', and '>' with entities.
32     """
33
34     # This is the standard xmlrpclib.escape function
35     s = replace(s, "&", "&amp;")
36     s = replace(s, "<", "&lt;")
37     s = replace(s, ">", "&gt;",)
38
39     # Replace invalid 7-bit control characters with '?'
40     return s.translate(xml_escape_table)
41
42 def xmlrpclib_dump(self, value, write):
43     """
44     xmlrpclib cannot marshal instances of subclasses of built-in
45     types. This function overrides xmlrpclib.Marshaller.__dump so that
46     any value that is an instance of one of its acceptable types is
47     marshalled as that type.
48
49     xmlrpclib also cannot handle invalid 7-bit control characters. See
50     above.
51     """
52
53     # Use our escape function
54     args = [self, value, write]
55     if isinstance(value, (str, unicode)):
56         args.append(xmlrpclib_escape)
57
58     try:
59         # Try for an exact match first
60         f = self.dispatch[type(value)]
61     except KeyError:
62         raise
63         # Try for an isinstance() match
64         for Type, f in self.dispatch.iteritems():
65             if isinstance(value, Type):
66                 f(*args)
67                 return
68         raise TypeError, "cannot marshal %s objects" % type(value)
69     else:
70         f(*args)
71
72 # You can't hide from me!
73 xmlrpclib.Marshaller._Marshaller__dump = xmlrpclib_dump
74
75 # SOAP support is optional
76 try:
77     import SOAPpy
78     from SOAPpy.Parser import parseSOAPRPC
79     from SOAPpy.Types import faultType
80     from SOAPpy.NS import NS
81     from SOAPpy.SOAPBuilder import buildSOAP
82 except ImportError:
83     SOAPpy = None
84
85 import geni.methods
86
87 def import_deep(name):
88     mod = __import__(name)
89     components = name.split('.')
90     for comp in components[1:]:
91         mod = getattr(mod, comp)
92     return mod
93
94 class GeniAPI:
95
96     # flat list of method names
97     methods = geni.methods.methods
98     
99     def __init__(self, config = "/usr/share/geniwrapper/geni/util/geni_config", encoding = "utf-8", peer_cert = None, interface = None, key_file = None, cert_file = None):
100         self.encoding = encoding
101
102         # Better just be documenting the API
103         if config is None:
104             return
105
106         # Load configuration
107         self.config = Config(config)
108         self.auth = Auth(peer_cert)
109         self.interface = interface
110         self.key_file = key_file
111         self.cert_file = cert_file
112         self.credential = None
113         self.plshell = self.getPLCShell()
114         self.basedir = self.config.GENI_BASE_DIR + os.sep
115         self.server_basedir = self.basedir + os.sep + "geni" + os.sep
116         self.hrn = self.config.GENI_INTERFACE_HRN
117         self.time_format = "%Y-%m-%d %H:%M:%S"
118
119
120     def getPLCShell(self):
121         self.plauth = {'Username': self.config.GENI_PLC_USER,
122                          'AuthMethod': 'password',
123                          'AuthString': self.config.GENI_PLC_PASSWORD} 
124         try:
125             import PLC.Shell
126             shell = PLC.Shell.Shell(globals = globals())
127             shell.AuthCheck(self.plauth)
128             return shell
129         except ImportError:
130             # connect via xmlrpc
131             plc_host = self.config.GENI_PLC_HOST
132             plc_port = self.config.GENI_PLC_PORT
133             plc_api_path = self.config.GENI_PLC_API_PATH
134             url = "https://%(plc_host)s:%(plc_port)s/%(plc_api_path)s/" % \
135                    locals()
136              
137             shell = xmlrpclib.Server(url, verbose = 0, allow_none = True)
138             shell.AuthCheck(self.plauth)
139             return shell
140
141     def getCredential(self):
142         if self.interface in ['registry']:
143             return self.getCredentialFromLocalRegistry()
144         else:
145             return self.getCredentialFromRegistry()
146     
147
148     def getCredentialFromRegistry(self):
149         """ 
150         Get our credential from a remote registry using a geniclient connection
151         """
152         type = 'sa'
153         cred_filename = ".".join([self.server_basedir, self.interface, self.hrn, type, "cred"]) 
154         try:
155             credential = Credential(filename = cred_filename)
156             return credential
157         except IOError:
158             from geni.registry import Registries
159             registries = Registries(self)
160             registry = registries[self.hrn] 
161             self_cred = registry.get_credential(None, type, self.hrn)
162             cred = registry.get_credential(self_cred, type, self.hrn)
163             cred.save_to_file(cred_filename, save_parents=True)
164
165     def getCredentialFromLocalRegistry(self):
166         """
167         Get our current credential directly from the local registry. 
168         """
169     
170         hrn = self.hrn
171         auth_hrn = self.auth.get_authority(hrn)
172         if not auth_hrn:
173             auth_hrn = hrn
174         auth_info = self.auth.get_auth_info(auth_hrn)
175         table = self.auth.get_auth_table(auth_hrn)
176         records = table.resolve('*', hrn)
177         if not records:
178             raise RecordnotFound
179         record = records[0]
180         type = record.get_type()
181         object_gid = record.get_gid_object()
182         new_cred = Credential(subject = object_gid.get_subject())
183         new_cred.set_gid_caller(object_gid)
184         new_cred.set_gid_object(object_gid)
185         new_cred.set_issuer(key=auth_info.get_pkey_object(), subject=auth_hrn)
186         new_cred.set_pubkey(object_gid.get_pubkey())
187         r1 = determine_rights(type, hrn)
188         new_cred.set_privileges(r1)
189     
190         # determine the type of credential that we want to use as a parent for
191         # this credential.
192
193         if (type == "ma") or (type == "node"):
194             auth_kind = "authority,ma"
195         else: # user, slice, sa
196             auth_kind = "authority,sa"
197
198         new_cred.set_parent(self.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind))
199
200         new_cred.encode()
201         new_cred.sign()
202
203         return new_cred
204    
205
206     def loadCredential(self):
207         """
208         Attempt to load credential from file if it exists. If it doesnt get
209         credential from registry.
210         """
211
212         # see if this file exists
213         # XX This is really the aggregate's credential. Using this is easier than getting
214         # the registry's credential from iteslf (ssl errors).   
215         ma_cred_filename = self.server_basedir + os.sep + self.interface + self.hrn + ".ma.cred"
216         try:
217             self.credential = Credential(filename = ma_cred_filename)
218         except IOError:
219             self.credential = self.getCredentialFromRegistry()
220       
221     ##
222     # Convert geni fields to PLC fields for use when registering up updating
223     # registry record in the PLC database
224     #
225     # @param type type of record (user, slice, ...)
226     # @param hrn human readable name
227     # @param geni_fields dictionary of geni fields
228     # @param pl_fields dictionary of PLC fields (output)
229
230     def geni_fields_to_pl_fields(self, type, hrn, geni_fields, pl_fields):
231         if type == "user":
232             if not "email" in pl_fields:
233                 if not "email" in geni_fields:
234                     raise MissingGeniInfo("email")
235                 pl_fields["email"] = geni_fields["email"]
236
237             if not "first_name" in pl_fields:
238                 pl_fields["first_name"] = "geni"
239
240             if not "last_name" in pl_fields:
241                 pl_fields["last_name"] = hrn
242
243         elif type == "slice":
244             if not "instantiation" in pl_fields:
245                 pl_fields["instantiation"] = "delegated"  # "plc-instantiated"
246             if not "name" in pl_fields:
247                 pl_fields["name"] = hrn_to_pl_slicename(hrn)
248             if not "max_nodes" in pl_fields:
249                 pl_fields["max_nodes"] = 10
250
251         elif type == "node":
252             if not "hostname" in pl_fields:
253                 if not "dns" in geni_fields:
254                     raise MissingGeniInfo("dns")
255                 pl_fields["hostname"] = geni_fields["dns"]
256             if not "model" in pl_fields:
257                 pl_fields["model"] = "geni"
258
259         elif type == "sa":
260             pl_fields["login_base"] = hrn_to_pl_login_base(hrn)
261
262             if not "name" in pl_fields:
263                 pl_fields["name"] = hrn
264
265             if not "abbreviated_name" in pl_fields:
266                 pl_fields["abbreviated_name"] = hrn
267
268             if not "enabled" in pl_fields:
269                 pl_fields["enabled"] = True
270
271             if not "is_public" in pl_fields:
272                 pl_fields["is_public"] = True
273
274
275  
276     def fill_record_pl_info(self, record):
277         """
278         Fill in the planetlab specific fields of a Geni record. This
279         involves calling the appropraite PLC method to retrie the 
280         dtabase record for the object.
281         
282         PLC data is filled into the pl_fino field of the record.
283     
284         @param record record to fill in field (in/out param)     
285         """
286         type = record.get_type()
287         pointer = record.get_pointer()
288
289         # records with pointer==-1 do not have plc info associated with them.
290         # for example, the top level authority records which are
291         # authorities, but not PL "sites"
292         if pointer == -1:
293             record.set_pl_info({})
294             return
295
296         if (type == "sa") or (type == "ma"):
297             pl_res = self.plshell.GetSites(self.plauth, [pointer])
298         elif (type == "slice"):
299             pl_res = self.plshell.GetSlices(self.plauth, [pointer])
300         elif (type == "user"):
301             pl_res = self.plshell.GetPersons(self.plauth, [pointer])
302             key_ids = pl_res[0]['key_ids']
303             keys = self.plshell.GetKeys(self.plauth, key_ids)
304             pubkeys = []
305             if keys:
306                 pubkeys = [key['key'] for key in keys]
307             pl_res[0]['keys'] = pubkeys
308         elif (type == "node"):
309             pl_res = self.plshell.GetNodes(self.plauth, [pointer])
310         else:
311             raise UnknownGeniType(type)
312
313         if not pl_res:
314             # the planetlab record no longer exists
315             # TODO: delete the geni record ?
316             raise PlanetLabRecordDoesNotExist(record.get_name())
317
318         record.set_pl_info(pl_res[0])
319
320
321     def lookup_users(self, auth_table, user_id_list, role="*"):
322         record_list = []
323         for person_id in user_id_list:
324             user_records = auth_table.find("user", person_id, "pointer")
325             for user_record in user_records:
326                 self.fill_record_info(user_record)
327
328                 user_roles = user_record.get_pl_info().get("roles")
329                 if (role=="*") or (role in user_roles):
330                     record_list.append(user_record.get_name())
331         return record_list
332
333     def fill_record_geni_info(self, record):
334         geni_info = {}
335         type = record.get_type()
336
337         if (type == "slice"):
338             auth_table = self.auth.get_auth_table(self.auth.get_authority(record.get_name()))
339             person_ids = record.pl_info.get("person_ids", [])
340             researchers = self.lookup_users(auth_table, person_ids)
341             geni_info['researcher'] = researchers
342
343         elif (type == "sa"):
344             auth_table = self.auth.get_auth_table(record.get_name())
345             person_ids = record.pl_info.get("person_ids", [])
346             pis = self.lookup_users(auth_table, person_ids, "pi")
347             geni_info['pi'] = pis
348             # TODO: OrganizationName
349
350         elif (type == "ma"):
351             auth_table = self.auth.get_auth_table(record.get_name())
352             person_ids = record.pl_info.get("person_ids", [])
353             operators = self.lookup_users(auth_table, person_ids, "tech")
354             geni_info['operator'] = operators
355             # TODO: OrganizationName
356
357             auth_table = self.auth.get_auth_table(record.get_name())
358             person_ids = record.pl_info.get("person_ids", [])
359             owners = self.lookup_users(auth_table, person_ids, "admin")
360             geni_info['owner'] = owners
361
362         elif (type == "node"):
363             geni_info['dns'] = record.pl_info.get("hostname", "")
364             # TODO: URI, LatLong, IP, DNS
365     
366         elif (type == "user"):
367             geni_info['email'] = record.pl_info.get("email", "")
368             # TODO: PostalAddress, Phone
369
370         record.set_geni_info(geni_info)
371
372     def fill_record_info(self, record):
373         """
374         Given a geni record, fill in the PLC specific and Geni specific
375         fields in the record. 
376         """
377         self.fill_record_pl_info(record)
378         self.fill_record_geni_info(record)
379
380     def update_membership_list(self, oldRecord, record, listName, addFunc, delFunc):
381         # get a list of the HRNs tht are members of the old and new records^M
382         if oldRecord:
383             if oldRecord.pl_info == None:
384                 oldRecord.pl_info = {}
385             oldList = oldRecord.get_geni_info().get(listName, [])
386         else:
387             oldList = []
388         newList = record.get_geni_info().get(listName, [])
389
390         # if the lists are the same, then we don't have to update anything
391         if (oldList == newList):
392             return
393
394         # build a list of the new person ids, by looking up each person to get
395         # their pointer
396         newIdList = []
397         for hrn in newList:
398             userRecord = self.resolve_raw("user", hrn)[0]
399             newIdList.append(userRecord.get_pointer())
400
401         # build a list of the old person ids from the person_ids field of the
402         # pl_info
403         if oldRecord:
404             oldIdList = oldRecord.plinfo.get("person_ids", [])
405             containerId = oldRecord.get_pointer()
406         else:
407             # if oldRecord==None, then we are doing a Register, instead of an
408             # update.
409             oldIdList = []
410             containerId = record.get_pointer()
411
412     # add people who are in the new list, but not the oldList
413         for personId in newIdList:
414             if not (personId in oldIdList):
415                 print "adding id", personId, "to", record.get_name()
416                 addFunc(self.plauth, personId, containerId)
417
418         # remove people who are in the old list, but not the new list
419         for personId in oldIdList:
420             if not (personId in newIdList):
421                 print "removing id", personId, "from", record.get_name()
422                 delFunc(self.plauth, personId, containerId)
423
424     def update_membership(self, oldRecord, record):
425         if record.type == "slice":
426             self.update_membership_list(oldRecord, record, 'researcher',
427                                         self.plshell.AddPersonToSlice,
428                                         self.plshell.DeletePersonFromSlice)
429         elif record.type == "sa":
430             # TODO
431             pass
432         elif record.type == "ma":
433             # TODO
434             pass
435  
436
437     def callable(self, method):
438         """
439         Return a new instance of the specified method.
440         """
441         # Look up method
442         if method not in self.methods:
443             raise GeniInvalidAPIMethod, method
444         
445         # Get new instance of method
446         try:
447             classname = method.split(".")[-1]
448             module = __import__("geni.methods." + method, globals(), locals(), [classname])
449             callablemethod = getattr(module, classname)(self)
450             return getattr(module, classname)(self)
451         except ImportError, AttributeError:
452             raise
453             raise GeniInvalidAPIMethod, method
454
455     def call(self, source, method, *args):
456         """
457         Call the named method from the specified source with the
458         specified arguments.
459         """
460         function = self.callable(method)
461         function.source = source
462         return function(*args)
463
464     def handle(self, source, data):
465         """
466         Handle an XML-RPC or SOAP request from the specified source.
467         """
468
469         # Parse request into method name and arguments
470         try:
471             interface = xmlrpclib
472             (args, method) = xmlrpclib.loads(data)
473             methodresponse = True
474         except Exception, e:
475             if SOAPpy is not None:
476                 interface = SOAPpy
477                 (r, header, body, attrs) = parseSOAPRPC(data, header = 1, body = 1, attrs = 1)
478                 method = r._name
479                 args = r._aslist()
480                 # XXX Support named arguments
481             else:
482                 raise e
483
484         try:
485             result = self.call(source, method, *args)
486         except Exception, fault:
487             traceback.print_exc(file = log)
488             # Handle expected faults
489             if interface == xmlrpclib:
490                 result = fault
491                 methodresponse = None
492             elif interface == SOAPpy:
493                 result = faultParameter(NS.ENV_T + ":Server", "Method Failed", method)
494                 result._setDetail("Fault %d: %s" % (fault.faultCode, fault.faultString))
495
496         # Return result
497         if interface == xmlrpclib:
498             if not isinstance(result, GeniFault):
499                 result = (result,)
500
501             data = xmlrpclib.dumps(result, methodresponse = True, encoding = self.encoding, allow_none = 1)
502         elif interface == SOAPpy:
503             data = buildSOAP(kw = {'%sResponse' % method: {'Result': result}}, encoding = self.encoding)
504
505         return data