5 from sfa.util.xrn import get_authority, hrn_to_urn
6 from sfa.generic import Generic
7 from sfa.util.config import Config
8 from sfa.util.sfalogging import _SfaLogger
9 from sfa.trust.hierarchy import Hierarchy
10 from sfa.trust.trustedroots import TrustedRoots
11 from sfa.trust.gid import create_uuid
12 from sfa.storage.alchemy import dbsession
13 from sfa.storage.model import RegRecord, RegAuthority, RegUser
14 from sfa.trust.certificate import convert_public_key, Keypair
19 self.config = Config()
20 self.logger = _SfaLogger(logfile='/var/log/sfa_import.log', loggername='importlog')
21 self.logger.setLevelFromOptVerbose(self.config.SFA_API_LOGLEVEL)
22 self.auth_hierarchy = Hierarchy ()
23 self.TrustedRoots = TrustedRoots(self.config.get_trustedroots_dir())
25 # check before creating a RegRecord entry as we run this over and over
26 def record_exists (self, type, hrn):
27 return dbsession.query(RegRecord).filter_by(hrn=hrn,type=type).count()!=0
29 def create_top_level_auth_records(self, hrn):
31 Create top level db records (includes root and sub authorities (local/remote)
33 # make sure parent exists
34 parent_hrn = get_authority(hrn)
37 if not parent_hrn == hrn:
38 self.create_top_level_auth_records(parent_hrn)
40 # ensure key and cert exists:
41 self.auth_hierarchy.create_top_level_auth(hrn)
42 # create the db record if it doesnt already exist
43 if not self.record_exists ('authority',hrn):
44 auth_info = self.auth_hierarchy.get_auth_info(hrn)
45 auth_record = RegAuthority(hrn=hrn, gid=auth_info.get_gid_object(),
46 authority=get_authority(hrn))
47 auth_record.just_created()
48 dbsession.add (auth_record)
50 self.logger.info("SfaImporter: imported authority (parent) %s " % auth_record)
53 def create_sm_client_record(self):
55 Create a user record for the Slicemanager service.
57 hrn = self.interface_hrn + '.slicemanager'
58 urn = hrn_to_urn(hrn, 'user')
59 if not self.auth_hierarchy.auth_exists(urn):
60 self.logger.info("SfaImporter: creating Slice Manager user")
61 self.auth_hierarchy.create_auth(urn)
63 if self.record_exists ('user',hrn): return
64 auth_info = self.auth_hierarchy.get_auth_info(hrn)
65 user_record = RegUser(hrn=hrn, gid=auth_info.get_gid_object(),
66 authority=get_authority(hrn))
67 user_record.just_created()
68 dbsession.add (user_record)
70 self.logger.info("SfaImporter: importing user (slicemanager) %s " % user_record)
73 def create_interface_records(self):
75 Create a record for each SFA interface
77 # just create certs for all sfa interfaces even if they
79 auth_info = self.auth_hierarchy.get_auth_info(self.config.SFA_INTERFACE_HRN)
80 pkey = auth_info.get_pkey_object()
81 hrn=self.config.SFA_INTERFACE_HRN
82 for type in [ 'authority+sa', 'authority+am', 'authority+sm', ]:
83 urn = hrn_to_urn(hrn, type)
84 gid = self.auth_hierarchy.create_gid(urn, create_uuid(), pkey)
85 # for now we have to preserve the authority+<> stuff
86 if self.record_exists (type,hrn): continue
87 interface_record = RegAuthority(type=type, hrn=hrn, gid=gid,
88 authority=get_authority(hrn))
89 interface_record.just_created()
90 dbsession.add (interface_record)
92 self.logger.info("SfaImporter: imported authority (%s) %s " % (type,interface_record))
94 def run(self, options=None):
95 if not self.config.SFA_REGISTRY_ENABLED:
96 self.logger.critical("Importer: need SFA_REGISTRY_ENABLED to run import")
98 # testbed-neutral : create local certificates and the like
99 auth_hierarchy = Hierarchy ()
100 self.create_top_level_auth_records(self.config.SFA_INTERFACE_HRN)
101 self.create_interface_records()
104 testbed_importer = None
105 generic=Generic.the_flavour()
106 importer_class = generic.importer_class()
108 self.logger.info ("Using flavour %s for importing (class %s)"%\
109 (generic.flavour,importer_class.__name__))
110 testbed_importer = importer_class (auth_hierarchy, self.logger)
112 testbed_importer.add_options(options)
113 testbed_importer.run (options)