4 from datetime import datetime
6 from sfa.util.xrn import get_authority, hrn_to_urn
7 from sfa.generic import Generic
8 from sfa.util.config import Config
9 from sfa.util.sfalogging import _SfaLogger
10 from sfa.trust.hierarchy import Hierarchy
11 #from sfa.trust.trustedroots import TrustedRoots
12 from sfa.trust.gid import create_uuid
13 from sfa.storage.alchemy import dbsession
14 from sfa.storage.model import RegRecord, RegAuthority, RegUser
15 from sfa.trust.certificate import convert_public_key, Keypair
20 def __init__(self,auth_hierarchy=None,logger=None):
21 self.config = Config()
22 if auth_hierarchy is not None:
23 self.auth_hierarchy=auth_hierarchy
25 self.auth_hierarchy = Hierarchy ()
26 if logger is not None:
29 self.logger = _SfaLogger(logfile='/var/log/sfa_import.log', loggername='importlog')
30 self.logger.setLevelFromOptVerbose(self.config.SFA_API_LOGLEVEL)
31 # ugly side effect so that other modules get it right
32 import sfa.util.sfalogging
33 sfa.util.sfalogging.logger=logger
34 # self.TrustedRoots = TrustedRoots(self.config.get_trustedroots_dir())
36 # check before creating a RegRecord entry as we run this over and over
37 def record_exists (self, type, hrn):
38 return dbsession.query(RegRecord).filter_by(hrn=hrn,type=type).count()!=0
40 def create_top_level_auth_records(self, hrn):
42 Create top level db records (includes root and sub authorities (local/remote)
44 # make sure parent exists
45 parent_hrn = get_authority(hrn)
48 if not parent_hrn == hrn:
49 self.create_top_level_auth_records(parent_hrn)
51 # ensure key and cert exists:
52 self.auth_hierarchy.create_top_level_auth(hrn)
53 # create the db record if it doesnt already exist
54 if not self.record_exists ('authority',hrn):
55 auth_info = self.auth_hierarchy.get_auth_info(hrn)
56 auth_record = RegAuthority(hrn=hrn, gid=auth_info.get_gid_object(),
57 authority=get_authority(hrn))
58 auth_record.just_created()
59 dbsession.add (auth_record)
61 self.logger.info("SfaImporter: imported authority (parent) %s " % auth_record)
64 def create_sm_client_record(self):
66 Create a user record for the Slicemanager service.
68 hrn = self.interface_hrn + '.slicemanager'
69 urn = hrn_to_urn(hrn, 'user')
70 if not self.auth_hierarchy.auth_exists(urn):
71 self.logger.info("SfaImporter: creating Slice Manager user")
72 self.auth_hierarchy.create_auth(urn)
74 if self.record_exists ('user',hrn): return
75 auth_info = self.auth_hierarchy.get_auth_info(hrn)
76 user_record = RegUser(hrn=hrn, gid=auth_info.get_gid_object(),
77 authority=get_authority(hrn))
78 user_record.just_created()
79 dbsession.add (user_record)
81 self.logger.info("SfaImporter: importing user (slicemanager) %s " % user_record)
84 def create_interface_records(self):
86 Create a record for each SFA interface
88 # just create certs for all sfa interfaces even if they
90 auth_info = self.auth_hierarchy.get_auth_info(self.config.SFA_INTERFACE_HRN)
91 pkey = auth_info.get_pkey_object()
92 hrn=self.config.SFA_INTERFACE_HRN
93 for type in [ 'authority+sa', 'authority+am', 'authority+sm', ]:
94 urn = hrn_to_urn(hrn, type)
95 gid = self.auth_hierarchy.create_gid(urn, create_uuid(), pkey)
96 # for now we have to preserve the authority+<> stuff
97 if self.record_exists (type,hrn): continue
98 interface_record = RegAuthority(type=type, hrn=hrn, gid=gid,
99 authority=get_authority(hrn))
100 interface_record.just_created()
101 dbsession.add (interface_record)
103 self.logger.info("SfaImporter: imported authority (%s) %s " % (type,interface_record))
105 def run(self, options=None):
106 if not self.config.SFA_REGISTRY_ENABLED:
107 self.logger.critical("Importer: need SFA_REGISTRY_ENABLED to run import")
109 # testbed-neutral : create local certificates and the like
110 auth_hierarchy = Hierarchy ()
111 self.create_top_level_auth_records(self.config.SFA_INTERFACE_HRN)
112 self.create_interface_records()
115 testbed_importer = None
116 generic=Generic.the_flavour()
117 importer_class = generic.importer_class()
119 begin_time=datetime.now()
120 self.logger.info ("Starting import on %s, using class %s from flavour %s"%\
121 (begin_time,importer_class.__name__,generic.flavour))
122 testbed_importer = importer_class (auth_hierarchy, self.logger)
124 testbed_importer.add_options(options)
125 testbed_importer.run (options)
126 end_time=datetime.now()
127 duration=end_time-begin_time
128 self.logger.info("Import took %s"%duration)