Initial checkin
[sfa.git] / sfa / managers / registry_manager_openstack.py
1 import types
2 import time 
3 # for get_key_from_incoming_ip
4 import tempfile
5 import os
6 import commands
7
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
23
24 class RegistryManager(registry_manager.RegistryManager):
25
26     def GetCredential(self, api, xrn, type, is_self=False):
27         # convert xrn to hrn     
28         if type:
29             hrn = urn_to_hrn(xrn)[0]
30         else:
31             hrn, type = urn_to_hrn(xrn)
32             
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:
36             auth_hrn = hrn
37         # get record info
38         auth_info = api.auth.get_auth_info(auth_hrn)
39         table = SfaTable()
40         records = table.findObjects({'type': type, 'hrn': hrn})
41         if not records:
42             raise RecordNotFound(hrn)
43         record = records[0]
44     
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']))
50     
51         # get the callers gid
52         # if this is a self cred the record's gid is the caller's gid
53         if is_self:
54             caller_hrn = hrn
55             caller_gid = record.get_gid_object()
56         else:
57             caller_gid = api.auth.client_cred.get_gid_caller() 
58             caller_hrn = caller_gid.get_hrn()
59         
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
63         if rights.is_empty():
64             raise PermissionError(caller_hrn + " has no rights to " + record['name'])
65     
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))
81         new_cred.encode()
82         new_cred.sign()
83     
84         return new_cred.save_to_string(save_parents=True)
85     
86     
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']
92         if type=='slice':
93             self.update_relation(subject_record, 'researcher', ref_record.get('researcher'), 'user')
94         
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']
102         table = SfaTable()
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)
106         
107