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 # using global alchemy.session() here is fine
14 # as importer is on standalone one-shot process
15 from sfa.storage.alchemy import global_dbsession
16 from sfa.storage.model import RegRecord, RegAuthority, RegUser
17 from sfa.trust.certificate import convert_public_key, Keypair
22 def __init__(self, auth_hierarchy=None, logger=None):
23 self.config = Config()
24 if auth_hierarchy is not None:
25 self.auth_hierarchy = auth_hierarchy
27 self.auth_hierarchy = Hierarchy()
28 if logger is not None:
31 self.logger = _SfaLogger(
32 logfile='/var/log/sfa_import.log', loggername='importlog')
33 self.logger.setLevelFromOptVerbose(self.config.SFA_API_LOGLEVEL)
34 # ugly side effect so that other modules get it right
35 import sfa.util.sfalogging
36 sfa.util.sfalogging.logger = logger
37 # self.TrustedRoots = TrustedRoots(self.config.get_trustedroots_dir())
39 # check before creating a RegRecord entry as we run this over and over
40 def record_exists(self, type, hrn):
41 return global_dbsession.query(RegRecord).filter_by(hrn=hrn, type=type).count() != 0
43 def create_top_level_auth_records(self, hrn):
45 Create top level db records (includes root and sub authorities (local/remote)
47 # make sure parent exists
48 parent_hrn = get_authority(hrn)
51 if not parent_hrn == hrn:
52 self.create_top_level_auth_records(parent_hrn)
54 # ensure key and cert exists:
55 self.auth_hierarchy.create_top_level_auth(hrn)
56 # create the db record if it doesnt already exist
57 if not self.record_exists('authority', hrn):
58 auth_info = self.auth_hierarchy.get_auth_info(hrn)
59 auth_record = RegAuthority(hrn=hrn, gid=auth_info.get_gid_object(),
60 authority=get_authority(hrn))
61 auth_record.just_created()
62 global_dbsession.add(auth_record)
63 global_dbsession.commit()
65 "SfaImporter: imported authority (parent) %s " % auth_record)
67 def create_sm_client_record(self):
69 Create a user record for the Slicemanager service.
71 hrn = self.interface_hrn + '.slicemanager'
72 urn = hrn_to_urn(hrn, 'user')
73 if not self.auth_hierarchy.auth_exists(urn):
74 self.logger.info("SfaImporter: creating Slice Manager user")
75 self.auth_hierarchy.create_auth(urn)
77 if self.record_exists('user', hrn):
79 auth_info = self.auth_hierarchy.get_auth_info(hrn)
80 user_record = RegUser(hrn=hrn, gid=auth_info.get_gid_object(),
81 authority=get_authority(hrn))
82 user_record.just_created()
83 global_dbsession.add(user_record)
84 global_dbsession.commit()
86 "SfaImporter: importing user (slicemanager) %s " % user_record)
88 def create_interface_records(self):
90 Create a record for each SFA interface
92 # just create certs for all sfa interfaces even if they
94 auth_info = self.auth_hierarchy.get_auth_info(
95 self.config.SFA_INTERFACE_HRN)
96 pkey = auth_info.get_pkey_object()
97 hrn = self.config.SFA_INTERFACE_HRN
98 for type in ['authority+sa', 'authority+am', 'authority+sm', ]:
99 urn = hrn_to_urn(hrn, type)
100 gid = self.auth_hierarchy.create_gid(urn, create_uuid(), pkey)
101 # for now we have to preserve the authority+<> stuff
102 if self.record_exists(type, hrn):
104 interface_record = RegAuthority(type=type, hrn=hrn, gid=gid,
105 authority=get_authority(hrn))
106 interface_record.just_created()
107 global_dbsession.add(interface_record)
108 global_dbsession.commit()
109 self.logger.info("SfaImporter: imported authority (%s) %s " % (
110 type, interface_record))
112 def run(self, options=None):
113 if not self.config.SFA_REGISTRY_ENABLED:
114 self.logger.critical(
115 "Importer: need SFA_REGISTRY_ENABLED to run import")
117 # testbed-neutral : create local certificates and the like
118 auth_hierarchy = Hierarchy()
119 self.create_top_level_auth_records(self.config.SFA_INTERFACE_HRN)
120 self.create_interface_records()
123 testbed_importer = None
124 generic = Generic.the_flavour()
125 importer_class = generic.importer_class()
127 begin_time = datetime.utcnow()
128 self.logger.info(30 * '=')
129 self.logger.info("Starting import on %s, using class %s from flavour %s" %
130 (begin_time, importer_class.__name__, generic.flavour))
131 testbed_importer = importer_class(auth_hierarchy, self.logger)
133 testbed_importer.add_options(options)
134 testbed_importer.run(options)
135 end_time = datetime.utcnow()
136 duration = end_time - begin_time
137 self.logger.info("Import took %s" % duration)
138 self.logger.info(30 * '=')