fix creation and update times
[sfa.git] / sfa / importer / sfaImport.py
1 #
2 # The import tool assumes that the existing PLC hierarchy should all be part
3 # of "planetlab.us" (see the root_auth and level1_auth variables below).
4 #
5 # Public keys are extracted from the users' SSH keys automatically and used to
6 # create GIDs. This is relatively experimental as a custom tool had to be
7 # written to perform conversion from SSH to OpenSSL format. It only supports
8 # RSA keys at this time, not DSA keys.
9 ##
10
11 from sfa.util.sfalogging import _SfaLogger
12 from sfa.util.xrn import get_authority, hrn_to_urn
13 from sfa.util.plxrn import email_to_hrn
14 from sfa.util.config import Config
15 from sfa.trust.certificate import convert_public_key, Keypair
16 from sfa.trust.trustedroots import TrustedRoots
17 from sfa.trust.hierarchy import Hierarchy
18 from sfa.trust.gid import create_uuid
19 from sfa.storage.persistentobjs import RegRecord
20 from sfa.storage.alchemy import dbsession
21
22 def _un_unicode(str):
23    if isinstance(str, unicode):
24        return str.encode("ascii", "ignore")
25    else:
26        return str
27
28 def _cleanup_string(str):
29     # pgsql has a fit with strings that have high ascii in them, so filter it
30     # out when generating the hrns.
31     tmp = ""
32     for c in str:
33         if ord(c) < 128:
34             tmp = tmp + c
35     str = tmp
36
37     str = _un_unicode(str)
38     str = str.replace(" ", "_")
39     str = str.replace(".", "_")
40     str = str.replace("(", "_")
41     str = str.replace("'", "_")
42     str = str.replace(")", "_")
43     str = str.replace('"', "_")
44     return str
45
46 class sfaImport:
47
48     def __init__(self):
49        self.logger = _SfaLogger(logfile='/var/log/sfa_import.log', loggername='importlog')
50        self.AuthHierarchy = Hierarchy()
51        self.config = Config()
52        self.TrustedRoots = TrustedRoots(Config.get_trustedroots_dir(self.config))
53        self.root_auth = self.config.SFA_REGISTRY_ROOT_AUTH
54
55     def create_top_level_records(self):
56         """
57         Create top level and interface records
58         """
59         # create root authority
60         interface_hrn = self.config.SFA_INTERFACE_HRN
61         self.create_top_level_auth_records(interface_hrn)
62
63         # create s user record for the slice manager
64         self.create_sm_client_record()
65
66         # create interface records
67         self.logger.info("Import: creating interface records")
68         self.create_interface_records()
69
70         # add local root authority's cert  to trusted list
71         self.logger.info("Import: adding " + interface_hrn + " to trusted list")
72         authority = self.AuthHierarchy.get_auth_info(interface_hrn)
73         self.TrustedRoots.add_gid(authority.get_gid_object())
74
75     def create_top_level_auth_records(self, hrn):
76         """
77         Create top level db records (includes root and sub authorities (local/remote)
78         """
79         # make sure parent exists
80         parent_hrn = get_authority(hrn)
81         if not parent_hrn:
82             parent_hrn = hrn
83         if not parent_hrn == hrn:
84             self.create_top_level_auth_records(parent_hrn)
85
86         # ensure key and cert exists:
87         self.AuthHierarchy.create_top_level_auth(hrn)    
88         # create the db record if it doesnt already exist    
89         auth_info = self.AuthHierarchy.get_auth_info(hrn)
90         auth_record = RegRecord("authority", hrn=hrn, gid=auth_info.get_gid_object(), 
91                                 authority=get_authority(hrn))
92         auth_record.just_created()
93         self.logger.info("Import: importing auth %s " % auth_record)
94         dbsession.add (auth_record)
95         dbsession.commit()
96
97     def create_sm_client_record(self):
98         """
99         Create a user record for the Slicemanager service.
100         """
101         hrn = self.config.SFA_INTERFACE_HRN + '.slicemanager'
102         urn = hrn_to_urn(hrn, 'user')
103         if not self.AuthHierarchy.auth_exists(urn):
104             self.logger.info("Import: creating Slice Manager user")
105             self.AuthHierarchy.create_auth(urn)
106
107         auth_info = self.AuthHierarchy.get_auth_info(hrn)
108         user_record = RegRecord("user", hrn=hrn, gid=auth_info.get_gid_object(), \
109                                    authority=get_authority(hrn))
110         user_record.just_created()
111         self.logger.info("Import: importing user %s " % user_record)
112         dbsession.add (user_record)
113         dbsession.commit()
114
115     def create_interface_records(self):
116         """
117         Create a record for each SFA interface
118         """
119         # just create certs for all sfa interfaces even if they
120         # arent enabled
121         hrn = self.config.SFA_INTERFACE_HRN
122         interfaces = ['authority+sa', 'authority+am', 'authority+sm']
123         auth_info = self.AuthHierarchy.get_auth_info(hrn)
124         pkey = auth_info.get_pkey_object()
125         for interface in interfaces:
126             urn = hrn_to_urn(hrn, interface)
127             gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
128             interface_record = RegRecord(interface, hrn=hrn, gid = gid, 
129                                          authority=get_authority(hrn))
130             interface_record.just_created()
131             self.logger.info("Import: importing %s " % interface_record)
132             dbsession.add (interface_record)
133             dbsession.commit()
134              
135     def delete_record(self, hrn, type):
136         # delete the record
137         for rec in dbsession.query(RegRecord).filter_by(type=type,hrn=hrn):
138            del rec
139         dbsession.commit()