1e43bf09a81b5553aa4a23e4428de0011efcfe3f
[sfa.git] / sfa / managers / registry_manager_pl.py
1 import types
2 import time 
3 from sfa.server.registry import Registries
4 from sfa.util.prefixTree import prefixTree
5 from sfa.util.record import SfaRecord
6 from sfa.util.table import SfaTable
7 from sfa.util.record import SfaRecord
8 from sfa.trust.gid import GID 
9 from sfa.util.namespace import *
10 from sfa.trust.credential import *
11 from sfa.trust.certificate import *
12 from sfa.util.faults import *
13
14 def get_credential(api, xrn, type, is_self=False):
15     # convert xrn to hrn     
16     if type:
17         hrn = urn_to_hrn(xrn)[0]
18     else:
19         hrn, type = urn_to_hrn(xrn)
20     # Is this a root or sub authority
21     auth_hrn = api.auth.get_authority(hrn)
22     if not auth_hrn or hrn == api.config.SFA_INTERFACE_HRN:
23         auth_hrn = hrn
24     # get record info
25     auth_info = api.auth.get_auth_info(auth_hrn)
26     table = SfaTable()
27     records = table.findObjects({'type': type, 'hrn': hrn})
28     if not records:
29         raise RecordNotFound(hrn)
30     record = records[0]
31
32     # verify_cancreate_credential requires that the member lists
33     # (researchers, pis, etc) be filled in
34     api.fill_record_info(record)
35     if not record['enabled']:
36         raise AccountNotEnabled(": PlanetLab account %s is not enabled. Please contact your site PI" %(record['email']))
37
38     # get the callers gid
39     # if this is a self cred the record's gid is the caller's gid
40     if is_self:
41         caller_hrn = hrn
42         caller_gid = record.get_gid_object()
43     else:
44         caller_gid = api.auth.client_cred.get_gid_caller() 
45         caller_hrn = caller_gid.get_hrn()
46     
47     object_hrn = record.get_gid_object().get_hrn()
48     rights = api.auth.determine_user_rights(caller_hrn, record)
49     # make sure caller has rights to this object
50     if rights.is_empty():
51         raise PermissionError(caller_hrn + " has no rights to " + record['name'])
52
53     object_gid = GID(string=record['gid'])
54     new_cred = Credential(subject = object_gid.get_subject())
55     new_cred.set_gid_caller(caller_gid)
56     new_cred.set_gid_object(object_gid)
57     new_cred.set_issuer(key=auth_info.get_pkey_object(), subject=auth_hrn)
58     new_cred.set_pubkey(object_gid.get_pubkey())
59     new_cred.set_privileges(rights)
60     new_cred.set_delegate(True)
61     auth_kind = "authority,ma,sa"
62     new_cred.set_parent(api.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind))
63     new_cred.encode()
64     new_cred.sign()
65
66     return new_cred.save_to_string(save_parents=True)
67
68 def resolve(api, xrns, type=None, origin_hrn=None):
69
70     # load all know registry names into a prefix tree and attempt to find
71     # the longest matching prefix
72     if not isinstance(xrns, types.ListType):
73         xrns = [xrns]
74     hrns = [urn_to_hrn(xrn)[0] for xrn in xrns] 
75     # create a dict whre key is an registry hrn and its value is a
76     # hrns at that registry (determined by the known prefix tree).  
77     xrn_dict = {}
78     registries = Registries(api)
79     tree = prefixTree()
80     registry_hrns = registries.keys()
81     tree.load(registry_hrns)
82     for xrn in xrns:
83         registry_hrn = tree.best_match(urn_to_hrn(xrn)[0])
84         if registry_hrn not in xrn_dict:
85             xrn_dict[registry_hrn] = []
86         xrn_dict[registry_hrn].append(xrn)
87         
88     records = [] 
89     for registry_hrn in xrn_dict:
90         # skip the hrn without a registry hrn
91         # XX should we let the user know the authority is unknown?       
92         if not registry_hrn:
93             continue
94
95         # if the best match (longest matching hrn) is not the local registry,
96         # forward the request
97         xrns = xrn_dict[registry_hrn]
98         if registry_hrn != api.hrn:
99             credential = api.getCredential()
100             peer_records = registries[registry_hrn].resolve(credential, xrns, origin_hrn)
101             records.extend([SfaRecord(dict=record).as_dict() for record in peer_records])
102
103     # try resolving the remaining unfound records at the local registry
104     remaining_hrns = set(hrns).difference([record['hrn'] for record in records])
105     # convert set to list
106     remaining_hrns = [hrn for hrn in remaining_hrns] 
107     table = SfaTable()
108     local_records = table.findObjects({'hrn': remaining_hrns})
109     for record in local_records:
110         try:
111             api.fill_record_info(record)
112             records.append(dict(record))
113         except PlanetLabRecordDoesNotExist:
114             # silently drop the ones that are missing in PL
115             print >> log, "ignoring SFA record ", record['hrn'], \
116                               " because pl record does not exist"    
117             table.remove(record)
118
119     if not records:
120         raise RecordNotFound(str(hrns))
121
122     if type:
123         records = filter(lambda rec: rec['type'] in [type], records)
124
125     return records
126
127 def list(api, xrn, origin_hrn=None):
128     hrn, type = urn_to_hrn(xrn)
129     # load all know registry names into a prefix tree and attempt to find
130     # the longest matching prefix
131     records = []
132     registries = Registries(api)
133     registry_hrns = registries.keys()
134     tree = prefixTree()
135     tree.load(registry_hrns)
136     registry_hrn = tree.best_match(hrn)
137     
138     #if there was no match then this record belongs to an unknow registry
139     if not registry_hrn:
140         raise MissingAuthority(xrn)
141     
142     # if the best match (longest matching hrn) is not the local registry,
143     # forward the request
144     records = []    
145     if registry_hrn != api.hrn:
146         credential = api.getCredential()
147         record_list = registries[registry_hrn].list(credential, xrn, origin_hrn)
148         records = [SfaRecord(dict=record).as_dict() for record in record_list]
149     
150     # if we still havnt found the record yet, try the local registry
151     if not records:
152         if not api.auth.hierarchy.auth_exists(hrn):
153             raise MissingAuthority(hrn)
154
155         table = SfaTable()
156         records = table.find({'authority': hrn})
157
158     return records
159
160
161 def register(api, record):
162
163     hrn, type = record['hrn'], record['type']
164
165     # validate the type
166     if type not in ['authority', 'slice', 'node', 'user']:
167         raise UnknownSfaType(type) 
168     
169     # check if record already exists
170     table = SfaTable()
171     existing_records = table.find({'type': type, 'hrn': hrn})
172     if existing_records:
173         raise ExistingRecord(hrn)
174        
175     record = SfaRecord(dict = record)
176     record['authority'] = get_authority(record['hrn'])
177     type = record['type']
178     hrn = record['hrn']
179     api.auth.verify_object_permission(hrn)
180     auth_info = api.auth.get_auth_info(record['authority'])
181     pub_key = None
182     # make sure record has a gid
183     if 'gid' not in record:
184         uuid = create_uuid()
185         pkey = Keypair(create=True)
186         if 'key' in record and record['key']:
187             if isinstance(record['key'], types.ListType):
188                 pub_key = record['key'][0]
189             else:
190                 pub_key = record['key']
191             pkey = convert_public_key(pub_key)
192
193         gid_object = api.auth.hierarchy.create_gid(hrn, uuid, pkey)
194         gid = gid_object.save_to_string(save_parents=True)
195         record['gid'] = gid
196         record.set_gid(gid)
197
198     if type in ["authority"]:
199         # update the tree
200         if not api.auth.hierarchy.auth_exists(hrn):
201             api.auth.hierarchy.create_auth(hrn)
202
203         # get the GID from the newly created authority
204         gid = auth_info.get_gid_object()
205         record.set_gid(gid.save_to_string(save_parents=True))
206         pl_record = api.sfa_fields_to_pl_fields(type, hrn, record)
207         sites = api.plshell.GetSites(api.plauth, [pl_record['login_base']])
208         if not sites:
209             pointer = api.plshell.AddSite(api.plauth, pl_record)
210         else:
211             pointer = sites[0]['site_id']
212
213         record.set_pointer(pointer)
214         record['pointer'] = pointer
215
216     elif (type == "slice"):
217         acceptable_fields=['url', 'instantiation', 'name', 'description']
218         pl_record = api.sfa_fields_to_pl_fields(type, hrn, record)
219         for key in pl_record.keys():
220             if key not in acceptable_fields:
221                 pl_record.pop(key)
222         slices = api.plshell.GetSlices(api.plauth, [pl_record['name']])
223         if not slices:
224              pointer = api.plshell.AddSlice(api.plauth, pl_record)
225         else:
226              pointer = slices[0]['slice_id']
227         record.set_pointer(pointer)
228         record['pointer'] = pointer
229
230     elif  (type == "user"):
231         persons = api.plshell.GetPersons(api.plauth, [record['email']])
232         if not persons:
233             pointer = api.plshell.AddPerson(api.plauth, dict(record))
234         else:
235             pointer = persons[0]['person_id']
236
237         if 'enabled' in record and record['enabled']:
238             api.plshell.UpdatePerson(api.plauth, pointer, {'enabled': record['enabled']})
239         # add this persons to the site only if he is being added for the first
240         # time by sfa and doesont already exist in plc
241         if not persons or not persons[0]['site_ids']:
242             login_base = get_leaf(record['authority'])
243             api.plshell.AddPersonToSite(api.plauth, pointer, login_base)
244
245         # What roles should this user have?
246         api.plshell.AddRoleToPerson(api.plauth, 'user', pointer)
247         # Add the user's key
248         if pub_key:
249             api.plshell.AddPersonKey(api.plauth, pointer, {'key_type' : 'ssh', 'key' : pub_key})
250
251     elif (type == "node"):
252         pl_record = api.sfa_fields_to_pl_fields(type, hrn, record)
253         login_base = hrn_to_pl_login_base(record['authority'])
254         nodes = api.plshell.GetNodes(api.plauth, [pl_record['hostname']])
255         if not nodes:
256             pointer = api.plshell.AddNode(api.plauth, login_base, pl_record)
257         else:
258             pointer = nodes[0]['node_id']
259
260     record['pointer'] = pointer
261     record.set_pointer(pointer)
262     record_id = table.insert(record)
263     record['record_id'] = record_id
264
265     # update membership for researchers, pis, owners, operators
266     api.update_membership(None, record)
267
268     return record.get_gid_object().save_to_string(save_parents=True)
269
270 def update(api, record_dict):
271     new_record = SfaRecord(dict = record_dict)
272     type = new_record['type']
273     hrn = new_record['hrn']
274     api.auth.verify_object_permission(hrn)
275     table = SfaTable()
276     # make sure the record exists
277     records = table.findObjects({'type': type, 'hrn': hrn})
278     if not records:
279         raise RecordNotFound(hrn)
280     record = records[0]
281     record['last_updated'] = time.gmtime()
282
283     # Update_membership needs the membership lists in the existing record
284     # filled in, so it can see if members were added or removed
285     api.fill_record_info(record)
286
287     # Use the pointer from the existing record, not the one that the user
288     # gave us. This prevents the user from inserting a forged pointer
289     pointer = record['pointer']
290     # update the PLC information that was specified with the record
291
292     if (type == "authority"):
293         api.plshell.UpdateSite(api.plauth, pointer, new_record)
294
295     elif type == "slice":
296         pl_record=api.sfa_fields_to_pl_fields(type, hrn, new_record)
297         if 'name' in pl_record:
298             pl_record.pop('name')
299             api.plshell.UpdateSlice(api.plauth, pointer, pl_record)
300
301     elif type == "user":
302         # SMBAKER: UpdatePerson only allows a limited set of fields to be
303         #    updated. Ideally we should have a more generic way of doing
304         #    this. I copied the field names from UpdatePerson.py...
305         update_fields = {}
306         all_fields = new_record
307         for key in all_fields.keys():
308             if key in ['first_name', 'last_name', 'title', 'email',
309                        'password', 'phone', 'url', 'bio', 'accepted_aup',
310                        'enabled']:
311                 update_fields[key] = all_fields[key]
312         api.plshell.UpdatePerson(api.plauth, pointer, update_fields)
313
314         if 'key' in new_record and new_record['key']:
315             # must check this key against the previous one if it exists
316             persons = api.plshell.GetPersons(api.plauth, [pointer], ['key_ids'])
317             person = persons[0]
318             keys = person['key_ids']
319             keys = api.plshell.GetKeys(api.plauth, person['key_ids'])
320             key_exists = False
321             if isinstance(new_record['key'], types.ListType):
322                 new_key = new_record['key'][0]
323             else:
324                 new_key = new_record['key']
325             
326             # Delete all stale keys
327             for key in keys:
328                 if new_record['key'] != key['key']:
329                     api.plshell.DeleteKey(api.plauth, key['key_id'])
330                 else:
331                     key_exists = True
332             if not key_exists:
333                 api.plshell.AddPersonKey(api.plauth, pointer, {'key_type': 'ssh', 'key': new_key})
334
335             # update the openssl key and gid
336             pkey = convert_public_key(new_key)
337             uuid = create_uuid()
338             gid_object = api.auth.hierarchy.create_gid(hrn, uuid, pkey)
339             gid = gid_object.save_to_string(save_parents=True)
340             record['gid'] = gid
341             record = SfaRecord(dict=record)
342             table.update(record)
343
344     elif type == "node":
345         api.plshell.UpdateNode(api.plauth, pointer, new_record)
346
347     else:
348         raise UnknownSfaType(type)
349
350     # update membership for researchers, pis, owners, operators
351     api.update_membership(record, new_record)
352     
353     return 1 
354
355 def remove(api, xrn, type, origin_hrn=None):
356     # convert xrn to hrn     
357     if type:
358         hrn = urn_to_hrn(xrn)[0]
359     else:
360         hrn, type = urn_to_hrn(xrn)    
361
362     table = SfaTable()
363     filter = {'hrn': hrn}
364     if type not in ['all', '*']:
365         filter['type'] = type
366     records = table.find(filter)
367     if not records:
368         raise RecordNotFound(hrn)
369     record = records[0]
370     type = record['type']
371
372     credential = api.getCredential()
373     registries = Registries(api)
374
375     # Try to remove the object from the PLCDB of federated agg.
376     # This is attempted before removing the object from the local agg's PLCDB and sfa table
377     if hrn.startswith(api.hrn) and type in ['user', 'slice', 'authority']:
378         for registry in registries:
379             if registry not in [api.hrn]:
380                 try:
381                     result=registries[registry].remove_peer_object(credential, record, origin_hrn)
382                 except:
383                     pass
384     if type == "user":
385         persons = api.plshell.GetPersons(api.plauth, record['pointer'])
386         # only delete this person if he has site ids. if he doesnt, it probably means
387         # he was just removed from a site, not actually deleted
388         if persons and persons[0]['site_ids']:
389             api.plshell.DeletePerson(api.plauth, record['pointer'])
390     elif type == "slice":
391         if api.plshell.GetSlices(api.plauth, record['pointer']):
392             api.plshell.DeleteSlice(api.plauth, record['pointer'])
393     elif type == "node":
394         if api.plshell.GetNodes(api.plauth, record['pointer']):
395             api.plshell.DeleteNode(api.plauth, record['pointer'])
396     elif type == "authority":
397         if api.plshell.GetSites(api.plauth, record['pointer']):
398             api.plshell.DeleteSite(api.plauth, record['pointer'])
399     else:
400         raise UnknownSfaType(type)
401
402     table.remove(record)
403
404     return 1
405
406 def remove_peer_object(api, record, origin_hrn=None):
407     pass
408
409 def register_peer_object(api, record, origin_hrn=None):
410     pass