3 # for get_key_from_incoming_ip
8 from sfa.util.faults import RecordNotFound, AccountNotEnabled, PermissionError, MissingAuthority, \
9 UnknownSfaType, ExistingRecord, NonExistingRecord
10 from sfa.util.sfatime import utcparse, datetime_to_epoch
11 from sfa.util.prefixTree import prefixTree
12 from sfa.util.xrn import Xrn, get_authority, hrn_to_urn, urn_to_hrn
13 from sfa.util.plxrn import hrn_to_pl_login_base
14 from sfa.util.version import version_core
15 from sfa.util.sfalogging import logger
16 from sfa.trust.gid import GID
17 from sfa.trust.credential import Credential
18 from sfa.trust.certificate import Certificate, Keypair, convert_public_key
19 from sfa.trust.gid import create_uuid
20 from sfa.storage.record import SfaRecord
21 from sfa.storage.table import SfaTable
22 from sfa.managers import registry_manager
24 class RegistryManager(registry_manager.RegistryManager):
26 def GetCredential(self, api, xrn, type, is_self=False):
29 hrn = urn_to_hrn(xrn)[0]
31 hrn, type = urn_to_hrn(xrn)
33 # Is this a root or sub authority
34 auth_hrn = api.auth.get_authority(hrn)
35 if not auth_hrn or hrn == api.config.SFA_INTERFACE_HRN:
38 auth_info = api.auth.get_auth_info(auth_hrn)
40 records = table.findObjects({'type': type, 'hrn': hrn})
42 raise RecordNotFound(hrn)
45 # verify_cancreate_credential requires that the member lists
46 # (researchers, pis, etc) be filled in
47 self.driver.augment_records_with_testbed_info (record)
48 if not self.driver.is_enabled (record):
49 raise AccountNotEnabled(": PlanetLab account %s is not enabled. Please contact your site PI" %(record['email']))
52 # if this is a self cred the record's gid is the caller's gid
55 caller_gid = record.get_gid_object()
57 caller_gid = api.auth.client_cred.get_gid_caller()
58 caller_hrn = caller_gid.get_hrn()
60 object_hrn = record.get_gid_object().get_hrn()
61 rights = api.auth.determine_user_rights(caller_hrn, record)
62 # make sure caller has rights to this object
64 raise PermissionError(caller_hrn + " has no rights to " + record['name'])
66 object_gid = GID(string=record['gid'])
67 new_cred = Credential(subject = object_gid.get_subject())
68 new_cred.set_gid_caller(caller_gid)
69 new_cred.set_gid_object(object_gid)
70 new_cred.set_issuer_keys(auth_info.get_privkey_filename(), auth_info.get_gid_filename())
71 #new_cred.set_pubkey(object_gid.get_pubkey())
72 new_cred.set_privileges(rights)
73 new_cred.get_privileges().delegate_all_privileges(True)
74 if 'expires' in record:
75 date = utcparse(record['expires'])
76 expires = datetime_to_epoch(date)
77 new_cred.set_expiration(int(expires))
78 auth_kind = "authority,ma,sa"
79 # Parent not necessary, verify with certs
80 #new_cred.set_parent(api.auth.hierarchy.get_auth_cred(auth_hrn, kind=auth_kind))
84 return new_cred.save_to_string(save_parents=True)
87 # subject_record describes the subject of the relationships
88 # ref_record contains the target values for the various relationships we need to manage
89 # (to begin with, this is just the slice x person relationship)
90 def update_relations (self, subject_record, ref_record):
91 type=subject_record['type']
93 self.update_relation(subject_record, 'researcher', ref_record.get('researcher'), 'user')
95 # field_key is the name of one field in the record, typically 'researcher' for a 'slice' record
96 # hrns is the list of hrns that should be linked to the subject from now on
97 # target_type would be e.g. 'user' in the 'slice' x 'researcher' example
98 def update_relation (self, sfa_record, field_key, hrns, target_type):
99 # locate the linked objects in our db
100 subject_type=sfa_record['type']
101 subject_id=sfa_record['pointer']
103 link_sfa_records = table.find ({'type':target_type, 'hrn': hrns})
104 link_ids = [ rec.get('pointer') for rec in link_sfa_records ]
105 self.driver.update_relation (subject_type, target_type, subject_id, link_ids)