01f8a105f30dc8a05aaafa4fd71dd4126e3f5208
[sfa.git] / registry / registry.py
1 ##
2 # Geni Registry Wrapper
3 #
4 # This wrapper implements the Geni Registry
5 ##
6
7 import tempfile
8 import os
9 import time
10 import sys
11
12 from cert import *
13 from gid import *
14 from geniserver import *
15 from excep import *
16 from trustedroot import *
17 from hierarchy import *
18 from misc import *
19 from record import *
20 from genitable import *
21 from geniticket import *
22
23 ##
24 # Convert geni fields to PLC fields for use when registering up updating
25 # registry record in the PLC database
26 #
27 # @params type type of record (user, slice, ...)
28 # @params hrn human readable name
29 # @params geni_fields dictionary of geni fields
30 # @params pl_fields dictionary of PLC fields (output)
31
32 def geni_fields_to_pl_fields(type, hrn, geni_fields, pl_fields):
33     if type == "user":
34         if not "email" in pl_fields:
35             if not "email" in geni_fields:
36                 raise MissingGeniInfo("email")
37             pl_fields["email"] = geni_fields["email"]
38
39         if not "first_name" in pl_fields:
40             pl_fields["first_name"] = "geni"
41
42         if not "last_name" in pl_fields:
43             pl_fields["last_name"] = hrn
44
45     elif type == "slice":
46         if not "instantiation" in pl_fields:
47             pl_fields["instantiation"] = "plc-instantiated"
48         if not "name" in pl_fields:
49             pl_fields["name"] = hrn_to_pl_slicename(hrn)
50         if not "max_nodes" in pl_fields:
51             pl_fields["max_nodes"] = 10
52
53     elif type == "node":
54         if not "hostname" in pl_fields:
55             if not "dns" in geni_fields:
56                 raise MissingGeniInfo("dns")
57             pl_fields["hostname"] = geni_fields["dns"]
58
59         if not "model" in pl_fields:
60             pl_fields["model"] = "geni"
61
62     elif type == "sa":
63         pl_fields["login_base"] = hrn_to_pl_login_base(hrn)
64
65         if not "name" in pl_fields:
66             pl_fields["name"] = hrn
67
68         if not "abbreviated_name" in pl_fields:
69             pl_fields["abbreviated_name"] = hrn
70
71         if not "enabled" in pl_fields:
72             pl_fields["enabled"] = True
73
74         if not "is_public" in pl_fields:
75             pl_fields["is_public"] = True
76
77 ##
78 # Registry is a GeniServer that serves registry requests. It also serves
79 # component and slice operations that are implemented on the registry
80 # due to SFA engineering decisions
81 #
82
83 class Registry(GeniServer):
84     def __init__(self, ip, port, key_file, cert_file):
85         GeniServer.__init__(self, ip, port, key_file, cert_file)
86
87         # get PL account settings from config module
88         self.pl_auth = get_pl_auth()
89
90         # connect to planetlab
91         if "Url" in self.pl_auth:
92             self.connect_remote_shell()
93         else:
94             self.connect_local_shell()
95
96     ##
97     # Connect to a remote shell via XMLRPC
98
99     def connect_remote_shell(self):
100         import remoteshell
101         self.shell = remoteshell.RemoteShell()
102
103     ##
104     # Connect to a local shell via local API functions
105
106     def connect_local_shell(self):
107         import PLC.Shell
108         self.shell = PLC.Shell.Shell(globals = globals())
109
110     ##
111     # Register the server RPCs for the registry
112
113     def register_functions(self):
114         GeniServer.register_functions(self)
115         # registry interface
116         self.server.register_function(self.create_gid)
117         self.server.register_function(self.get_self_credential)
118         self.server.register_function(self.get_credential)
119         self.server.register_function(self.get_gid)
120         self.server.register_function(self.register)
121         self.server.register_function(self.remove)
122         self.server.register_function(self.update)
123         self.server.register_function(self.list)
124         self.server.register_function(self.resolve)
125         # component interface
126         self.server.register_function(self.get_ticket)
127
128     ##
129     # Given an authority name, return the information for that authority. This
130     # is basically a stub that calls the hierarchy module.
131     #
132     # @param auth_hrn human readable name of authority
133
134     def get_auth_info(self, auth_hrn):
135         return AuthHierarchy.get_auth_info(auth_hrn)
136
137     ##
138     # Given an authority name, return the database table for that authority. If
139     # the database table does not exist, then one will be automatically
140     # created.
141     #
142     # @param auth_name human readable name of authority
143
144     def get_auth_table(self, auth_name):
145         auth_info = self.get_auth_info(auth_name)
146
147         table = GeniTable(hrn=auth_name,
148                           cninfo=auth_info.get_dbinfo())
149
150         # if the table doesn't exist, then it means we haven't put any records
151         # into this authority yet.
152
153         if not table.exists():
154             report.trace("Registry: creating table for authority " + auth_name)
155             table.create()
156
157         return table
158
159     ##
160     # Verify that an authority belongs to this registry. This is basically left
161     # up to the implementation of the hierarchy module. If the specified name
162     # does not belong to this registry, an exception is thrown indicating the
163     # caller should contact someone else.
164     #
165     # @param auth_name human readable name of authority
166
167     def verify_auth_belongs_to_me(self, name):
168         # get_auth_info will throw an exception if the authority does not
169         # exist
170         self.get_auth_info(name)
171
172     ##
173     # Verify that an object belongs to this registry. By extension, this implies
174     # that the authority that owns the object belongs to this registry. If the
175     # object does not belong to this registry, then an exception is thrown.
176     #
177     # @param name human readable name of object
178
179     def verify_object_belongs_to_me(self, name):
180         auth_name = get_authority(name)
181         if not auth_name:
182             # the root authority belongs to the registry by default?
183             # TODO: is this true?
184             return
185         self.verify_auth_belongs_to_me(auth_name)
186
187     ##
188     # Verify that the object_gid that was specified in the credential allows
189     # permission to the object 'name'. This is done by a simple prefix test.
190     # For example, an object_gid for planetlab.us.arizona would match the
191     # objects planetlab.us.arizona.slice1 and planetlab.us.arizona.
192     #
193     # @param name human readable name to test
194
195     def verify_object_permission(self, name):
196         object_hrn = self.object_gid.get_hrn()
197         if object_hrn == name:
198             return
199         if name.startswith(object_hrn + "."):
200             return
201         raise PermissionError(name)
202
203     ##
204     # Fill in the planetlab-specific fields of a Geni record. This involves
205     # calling the appropriate PLC methods to retrieve the database record for
206     # the object.
207     #
208     # PLC data is filled into the pl_info field of the record.
209     #
210     # @param record record to fill in fields (in/out param)
211
212     def fill_record_pl_info(self, record):
213         type = record.get_type()
214         pointer = record.get_pointer()
215
216         # records with pointer==-1 do not have plc info associated with them.
217         # for example, the top level authority records which are
218         # authorities, but not PL "sites"
219         if pointer == -1:
220             return
221
222         if (type == "sa") or (type == "ma"):
223             pl_res = self.shell.GetSites(self.pl_auth, [pointer])
224         elif (type == "slice"):
225             pl_res = self.shell.GetSlices(self.pl_auth, [pointer])
226         elif (type == "user"):
227             pl_res = self.shell.GetPersons(self.pl_auth, [pointer])
228         elif (type == "node"):
229             pl_res = self.shell.GetNodes(self.pl_auth, [pointer])
230         else:
231             raise UnknownGeniType(type)
232
233         if not pl_res:
234             # the planetlab record no longer exists
235             # TODO: delete the geni record ?
236             raise PlanetLabRecordDoesNotExist(record.get_name())
237
238         record.set_pl_info(pl_res[0])
239
240     def lookup_users(self, auth_table, user_id_list, role="*"):
241         record_list = []
242         for person_id in user_id_list:
243             user_records = auth_table.find("user", person_id, "pointer")
244             for user_record in user_records:
245                 self.fill_record_info(user_record)
246
247                 user_roles = user_record.get_pl_info().get("roles")
248                 if (role=="*") or (role in user_roles):
249                     record_list.append(user_record.get_name())
250         return record_list
251
252     ##
253     # Fill in the geni-specific fields of the record.
254     #
255     # Note: It is assumed the fill_record_pl_info() has already been performed
256     # on the record.
257
258     def fill_record_geni_info(self, record):
259         geni_info = {}
260         type = record.get_type()
261
262         if (type == "slice"):
263             auth_table = self.get_auth_table(get_authority(record.get_name()))
264             person_ids = record.pl_info.get("person_ids", [])
265             researchers = self.lookup_users(auth_table, person_ids)
266             geni_info['researcher'] = researchers
267
268         elif (type == "sa"):
269             auth_table = self.get_auth_table(record.get_name())   
270             person_ids = record.pl_info.get("person_ids", [])
271             pis = self.lookup_users(auth_table, person_ids, "pi")
272             geni_info['pi'] = pis
273
274         elif (type == "ma"):
275             auth_table = self.get_auth_table(record.get_name())   
276             person_ids = record.pl_info.get("person_ids", [])
277             operators = self.lookup_users(auth_table, person_ids, "tech")
278             geni_info['operator'] = operators
279
280             auth_table = self.get_auth_table(record.get_name())
281             person_ids = record.pl_info.get("person_ids", [])
282             owners = self.lookup_users(auth_table, person_ids, "admin")
283             geni_info['owner'] = owners
284             pass
285
286         record.set_geni_info(geni_info)
287
288     def fill_record_info(self, record):
289         self.fill_record_pl_info(record)
290         self.fill_record_geni_info(record)
291
292     def register(self, cred, record_dict):
293         self.decode_authentication(cred, "register")
294
295         record = GeniRecord(dict = record_dict)
296         type = record.get_type()
297         name = record.get_name()
298
299         auth_name = get_authority(name)
300         self.verify_object_permission(auth_name)
301         auth_info = self.get_auth_info(auth_name)
302         table = self.get_auth_table(auth_name)
303
304         pkey = None
305
306         # check if record already exists
307         existing_records = table.resolve(type, name)
308         if existing_records:
309             raise ExistingRecord(name)
310
311         if (type == "sa") or (type=="ma"):
312             # update the tree
313             if not AuthHierarchy.auth_exists(name):
314                 AuthHierarchy.create_auth(name)
315
316             # authorities are special since they are managed by the registry
317             # rather than by the caller. We create our own GID for the
318             # authority rather than relying on the caller to supply one.
319
320             # get the GID from the newly created authority
321             child_auth_info = self.get_auth_info(name)
322             gid = auth_info.get_gid_object()
323             record.set_gid(gid.save_to_string(save_parents=True))
324
325             geni_fields = record.get_geni_info()
326             site_fields = record.get_pl_info()
327
328             # if registering a sa, see if a ma already exists
329             # if registering a ma, see if a sa already exists
330             if (type == "sa"):
331                 other_rec = table.resolve("ma", record.get_name())
332             elif (type == "ma"):
333                 other_rec = table.resolve("sa", record.get_name())
334
335             if other_rec:
336                 print "linking ma and sa to the same plc site"
337                 pointer = other_rec[0].get_pointer()
338             else:
339                 geni_fields_to_pl_fields(type, name, geni_fields, site_fields)
340                 print "adding site with fields", site_fields
341                 pointer = self.shell.AddSite(self.pl_auth, site_fields)
342
343             record.set_pointer(pointer)
344
345         elif (type == "slice"):
346             geni_fields = record.get_geni_info()
347             slice_fields = record.get_pl_info()
348
349             geni_fields_to_pl_fields(type, name, geni_fields, slice_fields)
350
351             pointer = self.shell.AddSlice(self.pl_auth, slice_fields)
352             record.set_pointer(pointer)
353
354         elif (type == "user"):
355             geni_fields = record.get_geni_info()
356             user_fields = record.get_pl_info()
357
358             geni_fields_to_pl_fields(type, name, geni_fields, user_fields)
359
360             pointer = self.shell.AddPerson(self.pl_auth, user_fields)
361             record.set_pointer(pointer)
362
363         elif (type == "node"):
364             geni_fields = record.get_geni_info()
365             node_fields = record.get_pl_info()
366
367             geni_fields_to_pl_fields(type, name, geni_fields, node_fields)
368
369             login_base = hrn_to_pl_login_base(auth_name)
370
371             print "calling addnode with", login_base, node_fields
372             pointer = self.shell.AddNode(self.pl_auth, login_base, node_fields)
373             record.set_pointer(pointer)
374
375         else:
376             raise UnknownGeniType(type)
377
378         table.insert(record)
379
380         return record.get_gid_object().save_to_string(save_parents=True)
381
382     def remove(self, cred, record_dict):
383         self.decode_authentication(cred, "remove")
384
385         record = GeniRecord(dict = record_dict)
386         type = record.get_type()
387
388         self.verify_object_permission(record.get_name())
389
390         auth_name = get_authority(record.get_name())
391         table = self.get_auth_table(auth_name)
392
393         # let's not trust that the caller has a well-formed record (a forged
394         # pointer field could be a disaster), so look it up ourselves
395         record_list = table.resolve(type, record.get_name())
396         if not record_list:
397             raise RecordNotFound(name)
398         record = record_list[0]
399
400         # TODO: sa, ma
401         if type == "user":
402             self.shell.DeletePerson(self.pl_auth, record.get_pointer())
403         elif type == "slice":
404             self.shell.DeleteSlice(self.pl_auth, record.get_pointer())
405         elif type == "node":
406             self.shell.DeleteNode(self.pl_auth, record.get_pointer())
407         elif (type == "sa") or (type == "ma"):
408             if (type == "sa"):
409                 other_rec = table.resolve("ma", record.get_name())
410             elif (type == "ma"):
411                 other_rec = table.resolve("sa", record.get_name())
412
413             if other_rec:
414                 # sa and ma both map to a site, so if we are deleting one
415                 # but the other still exists, then do not delete the site
416                 print "not removing site", record.get_name(), "because either sa or ma still exists"
417                 pass
418             else:
419                 print "removing site", record.get_name()
420                 self.shell.DeleteSite(self.pl_auth, record.get_pointer())
421         else:
422             raise UnknownGeniType(type)
423
424         table.remove(record)
425
426         return True
427
428     def update(self, cred, record_dict):
429         self.decode_authentication(cred, "update")
430
431         record = GeniRecord(dict = record_dict)
432         type = record.get_type()
433
434         self.verify_object_permission(record.get_name())
435
436         auth_name = get_authority(record.get_name())
437         table = self.get_auth_table(auth_name)
438
439         # make sure the record exists
440         existing_record_list = table.resolve(type, record.get_name())
441         if not existing_record_list:
442             raise RecordNotFound(record.get_name())
443
444         existing_record = existing_record_list[0]
445         pointer = existing_record.get_pointer()
446
447         if (type == "sa") or (type == "ma"):
448             self.shell.UpdateSite(self.pl_auth, pointer, record.get_pl_info())
449
450         elif type == "slice":
451             self.shell.UpdateSlice(self.pl_auth, pointer, record.get_pl_info())
452
453         elif type == "user":
454             self.shell.UpdatePerson(self.pl_auth, pointer, record.get_pl_info())
455
456         elif type == "node":
457             self.shell.UpdateNode(self.pl_auth, pointer, record.get_pl_info())
458
459         else:
460             raise UnknownGeniType(type)
461
462     # TODO: List doesn't take an hrn and uses the hrn contained in the
463     #    objectGid of the credential. Does this mean the only way to list an
464     #    authority is by having a credential for that authority? 
465     def list(self, cred):
466         self.decode_authentication(cred, "list")
467
468         auth_name = self.object_gid.get_hrn()
469         table = self.get_auth_table(auth_name)
470
471         records = table.list()
472
473         good_records = []
474         for record in records:
475             try:
476                 self.fill_record_info(record)
477                 good_records.append(record)
478             except PlanetLabRecordDoesNotExist:
479                 # silently drop the ones that are missing in PL.
480                 # is this the right thing to do?
481                 report.error("ignoring geni record " + record.get_name() + " because pl record does not exist")
482                 table.remove(record)
483
484         dicts = []
485         for record in good_records:
486             dicts.append(record.as_dict())
487
488         return dicts
489
490         return dict_list
491
492     def resolve_raw(self, type, name, must_exist=True):
493         auth_name = get_authority(name)
494
495         table = self.get_auth_table(auth_name)
496
497         records = table.resolve(type, name)
498
499         if (not records) and must_exist:
500             raise RecordNotFound(name)
501
502         good_records = []
503         for record in records:
504             try:
505                 self.fill_record_info(record)
506                 good_records.append(record)
507             except PlanetLabRecordDoesNotExist:
508                 # silently drop the ones that are missing in PL.
509                 # is this the right thing to do?
510                 report.error("ignoring geni record " + record.get_name() + " because pl record does not exist")
511                 table.remove(record)
512
513         return good_records
514
515     def resolve(self, cred, name):
516         self.decode_authentication(cred, "resolve")
517
518         records = self.resolve_raw("*", name)
519         dicts = []
520         for record in records:
521             dicts.append(record.as_dict())
522
523         return dicts
524
525     def get_gid(self, name):
526         self.verify_object_belongs_to_me(name)
527         records = self.resolve_raw("*", name)
528         gid_string_list = []
529         for record in records:
530             gid = record.get_gid()
531             gid_string_list.append(gid.save_to_string(save_parents=True))
532         return gid_string_list
533
534     def determine_rights(self, type, name):
535         rl = RightList()
536
537         # rights seem to be somewhat redundant with the type of the credential.
538         # For example, a "sa" credential implies the authority right, because
539         # a sa credential cannot be issued to a user who is not an owner of
540         # the authority
541
542         if type == "user":
543             rl.add("refresh")
544             rl.add("resolve")
545             rl.add("info")
546         elif type == "sa":
547             rl.add("authority")
548         elif type == "ma":
549             rl.add("authority")
550         elif type == "slice":
551             rl.add("embed")
552             rl.add("bind")
553             rl.add("control")
554             rl.add("info")
555         elif type == "component":
556             rl.add("operator")
557
558         return rl
559
560
561     def get_self_credential(self, type, name):
562         self.verify_object_belongs_to_me(name)
563
564         auth_hrn = get_authority(name)
565         auth_info = self.get_auth_info(auth_hrn)
566
567         # find a record that matches
568         records = self.resolve_raw(type, name, must_exist=True)
569         record = records[0]
570
571         gid = record.get_gid_object()
572         peer_cert = self.server.peer_cert
573         if not peer_cert.is_pubkey(gid.get_pubkey()):
574            raise ConnectionKeyGIDMismatch(gid.get_subject())
575
576         # create the credential
577         gid = record.get_gid_object()
578         cred = Credential(subject = gid.get_subject())
579         cred.set_gid_caller(gid)
580         cred.set_gid_object(gid)
581         cred.set_issuer(key=auth_info.get_pkey_object(), subject=auth_hrn)
582         cred.set_pubkey(gid.get_pubkey())
583
584         rl = self.determine_rights(type, name)
585         cred.set_privileges(rl)
586
587         cred.set_parent(AuthHierarchy.get_auth_cred(auth_hrn))
588
589         cred.encode()
590         cred.sign()
591
592         return cred.save_to_string(save_parents=True)
593
594     def get_credential(self, cred, type, name):
595         if not cred:
596             return get_self_credential(self, type, name)
597
598         self.decode_authentication(cred, "getcredential")
599
600         self.verify_object_belongs_to_me(name)
601
602         auth_hrn = get_authority(name)
603         auth_info = self.get_auth_info(auth_hrn)
604
605         records = self.resolve_raw(type, name, must_exist=True)
606         record = records[0]
607
608         # TODO: Check permission that self.client_cred can access the object
609
610         object_gid = record.get_gid_object()
611         new_cred = Credential(subject = object_gid.get_subject())
612         new_cred.set_gid_caller(self.client_gid)
613         new_cred.set_gid_object(object_gid)
614         new_cred.set_issuer(key=auth_info.get_pkey_object(), subject=auth_hrn)
615         new_cred.set_pubkey(object_gid.get_pubkey())
616
617         rl = self.determine_rights(type, name)
618         new_cred.set_privileges(rl)
619
620         new_cred.set_parent(AuthHierarchy.get_auth_cred(auth_hrn))
621
622         new_cred.encode()
623         new_cred.sign()
624
625         return new_cred.save_to_string(save_parents=True)
626
627     def create_gid(self, cred, name, uuid, pubkey_str):
628         self.decode_authentication(cred, "getcredential")
629
630         self.verify_object_belongs_to_me(name)
631
632         self.verify_object_permission(name)
633
634         if uuid == None:
635             uuid = create_uuid()
636
637         pkey = Keypair()
638         pkey.load_pubkey_from_string(pubkey_str)
639         gid = AuthHierarchy.create_gid(name, uuid, pkey)
640
641         return gid.save_to_string(save_parents=True)
642
643     # ------------------------------------------------------------------------
644     # Component Interface
645
646     def record_to_slice_info(self, record):
647
648         # get the user keys from the slice
649         keys = []
650         persons = self.shell.GetPersons(self.pl_auth, record.pl_info['person_ids'])
651         for person in persons:
652             person_keys = self.shell.GetKeys(self.pl_auth, person["key_ids"])
653             for person_key in person_keys:
654                 keys = keys + [person_key['key']]
655
656         attributes={}
657         attributes['name'] = record.pl_info['name']
658         attributes['keys'] = keys
659         attributes['instantiation'] = record.pl_info['instantiation']
660         attributes['vref'] = 'default'
661         attributes['timestamp'] = time.time()
662
663         rspec = {}
664
665         # get the PLC attributes and separate them into slice attributes and
666         # rspec attributes
667         filter = {}
668         filter['slice_id'] = record.pl_info['slice_id']
669         plc_attrs = self.shell.GetSliceAttributes(self.pl_auth, filter)
670         for attr in plc_attrs:
671             name = attr['name']
672
673             # initscripts: lookup the contents of the initscript and store it
674             # in the ticket attributes
675             if (name == "initscript"):
676                 filter={'name': attr['value']}
677                 initscripts = self.shell.GetInitScripts(self.pl_auth, filter)
678                 if initscripts:
679                     attributes['initscript'] = initscripts[0]['script']
680             else:
681                 rspec[name] = attr['value']
682
683         return (attributes, rspec)
684
685
686     def get_ticket(self, cred, name, rspec):
687         self.decode_authentication(cred, "getticket")
688
689         self.verify_object_belongs_to_me(name)
690
691         self.verify_object_permission(name)
692
693         # XXX much of this code looks like get_credential... are they so similar
694         # that they should be combined?
695
696         auth_hrn = get_authority(name)
697         auth_info = self.get_auth_info(auth_hrn)
698
699         records = self.resolve_raw("slice", name, must_exist=True)
700         record = records[0]
701
702         object_gid = record.get_gid_object()
703         new_ticket = Ticket(subject = object_gid.get_subject())
704         new_ticket.set_gid_caller(self.client_gid)
705         new_ticket.set_gid_object(object_gid)
706         new_ticket.set_issuer(key=auth_info.get_pkey_object(), subject=auth_hrn)
707         new_ticket.set_pubkey(object_gid.get_pubkey())
708
709         self.fill_record_info(record)
710
711         (attributes, rspec) = self.record_to_slice_info(record)
712
713         new_ticket.set_attributes(attributes)
714         new_ticket.set_rspec(rspec)
715
716         new_ticket.set_parent(AuthHierarchy.get_auth_ticket(auth_hrn))
717
718         new_ticket.encode()
719         new_ticket.sign()
720
721         return new_ticket.save_to_string(save_parents=True)
722
723
724 if __name__ == "__main__":
725     global AuthHierarchy
726     global TrustedRoots
727
728     key_file = "server.key"
729     cert_file = "server.cert"
730
731     # if no key is specified, then make one up
732     if (not os.path.exists(key_file)) or (not os.path.exists(cert_file)):
733         key = Keypair(create=True)
734         key.save_to_file(key_file)
735
736         cert = Certificate(subject="registry")
737         cert.set_issuer(key=key, subject="registry")
738         cert.set_pubkey(key)
739         cert.sign()
740         cert.save_to_file(cert_file)
741
742     AuthHierarchy = Hierarchy()
743
744     TrustedRoots = TrustedRootList()
745
746     s = Registry("", 12345, key_file, cert_file)
747     s.trusted_cert_list = TrustedRoots.get_list()
748     s.run()
749