3 # pylint: disable=c0111, w1201, w0622
5 from datetime import datetime
7 from sfa.util.xrn import get_authority, hrn_to_urn
8 from sfa.generic import Generic
9 from sfa.util.config import Config
10 from sfa.util.sfalogging import init_logger, logger as default_logger
11 from sfa.trust.hierarchy import Hierarchy
12 # from sfa.trust.trustedroots import TrustedRoots
13 from sfa.trust.gid import create_uuid
14 # using global alchemy.session() here is fine
15 # as importer is on standalone one-shot process
16 from sfa.storage.alchemy import global_dbsession
17 from sfa.storage.model import RegRecord, RegAuthority, RegUser
20 # it is doubtful that anyone ever used the ability to
21 # pass a logger to this class, and that can probably be
23 # However a quick attempt showed that it seems to
24 # also require changes in the Generic layer
28 def __init__(self, auth_hierarchy=None, logger=None):
29 self.config = Config()
30 if auth_hierarchy is not None:
31 self.auth_hierarchy = auth_hierarchy
33 self.auth_hierarchy = Hierarchy()
35 # redirect in sfa-import.log
36 self.logger = default_logger
40 self.logger.setLevelFromOptVerbose(self.config.SFA_API_LOGLEVEL)
41 # ugly side effect so that other modules get it right
42 import sfa.util.sfalogging
43 sfa.util.sfalogging.logger = logger
44 # self.TrustedRoots = TrustedRoots(self.config.get_trustedroots_dir())
46 # check before creating a RegRecord entry as we run this over and over
48 def record_exists(type, hrn):
49 return (global_dbsession.query(RegRecord)
50 .filter_by(hrn=hrn, type=type).count() != 0)
52 def create_top_level_auth_records(self, hrn):
54 Create top level db records
55 includes root and sub authorities (local/remote)
57 # make sure parent exists
58 parent_hrn = get_authority(hrn)
61 if not parent_hrn == hrn:
62 self.create_top_level_auth_records(parent_hrn)
64 # ensure key and cert exists:
65 self.auth_hierarchy.create_top_level_auth(hrn)
66 # create the db record if it doesnt already exist
67 if not self.record_exists('authority', hrn):
68 auth_info = self.auth_hierarchy.get_auth_info(hrn)
69 auth_record = RegAuthority(hrn=hrn, gid=auth_info.get_gid_object(),
70 authority=get_authority(hrn))
71 auth_record.just_created()
72 global_dbsession.add(auth_record)
73 global_dbsession.commit()
75 "SfaImporter: imported authority (parent) %s " % auth_record)
77 def create_interface_records(self):
79 Create a record for each SFA interface
81 # just create certs for all sfa interfaces even if they
83 auth_info = self.auth_hierarchy.get_auth_info(
84 self.config.SFA_INTERFACE_HRN)
85 pkey = auth_info.get_pkey_object()
86 hrn = self.config.SFA_INTERFACE_HRN
87 for type in ['authority+sa', 'authority+am', 'authority+sm', ]:
88 urn = hrn_to_urn(hrn, type)
89 gid = self.auth_hierarchy.create_gid(urn, create_uuid(), pkey)
90 # for now we have to preserve the authority+<> stuff
91 if self.record_exists(type, hrn):
93 interface_record = RegAuthority(type=type, hrn=hrn, gid=gid,
94 authority=get_authority(hrn))
95 interface_record.just_created()
96 global_dbsession.add(interface_record)
97 global_dbsession.commit()
98 self.logger.info("SfaImporter: imported authority (%s) %s " % (
99 type, interface_record))
101 def run(self, options=None):
102 if not self.config.SFA_REGISTRY_ENABLED:
103 self.logger.critical(
104 "Importer: need SFA_REGISTRY_ENABLED to run import")
106 # testbed-neutral : create local certificates and the like
107 auth_hierarchy = Hierarchy()
108 self.create_top_level_auth_records(self.config.SFA_INTERFACE_HRN)
109 self.create_interface_records()
112 testbed_importer = None
113 generic = Generic.the_flavour()
114 importer_class = generic.importer_class()
116 begin_time = datetime.utcnow()
117 self.logger.info(30 * '=')
119 "Starting import on %s, using class %s from flavour %s" %
120 (begin_time, importer_class.__name__, generic.flavour))
121 testbed_importer = importer_class(auth_hierarchy, self.logger)
123 testbed_importer.add_options(options)
124 testbed_importer.run(options)
125 end_time = datetime.utcnow()
126 duration = end_time - begin_time
127 self.logger.info("Import took %s" % duration)
128 self.logger.info(30 * '=')