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