use inheritance to model the various kinds of RegRecords
[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, RegAuthority, RegUser
20 from sfa.storage.persistentobjs import RegTmpAuthSa, RegTmpAuthAm, RegTmpAuthSm
21 from sfa.storage.alchemy import dbsession
22
23 def _un_unicode(str):
24    if isinstance(str, unicode):
25        return str.encode("ascii", "ignore")
26    else:
27        return str
28
29 def _cleanup_string(str):
30     # pgsql has a fit with strings that have high ascii in them, so filter it
31     # out when generating the hrns.
32     tmp = ""
33     for c in str:
34         if ord(c) < 128:
35             tmp = tmp + c
36     str = tmp
37
38     str = _un_unicode(str)
39     str = str.replace(" ", "_")
40     str = str.replace(".", "_")
41     str = str.replace("(", "_")
42     str = str.replace("'", "_")
43     str = str.replace(")", "_")
44     str = str.replace('"', "_")
45     return str
46
47 class sfaImport:
48
49     def __init__(self):
50        self.logger = _SfaLogger(logfile='/var/log/sfa_import.log', loggername='importlog')
51        self.AuthHierarchy = Hierarchy()
52        self.config = Config()
53        self.TrustedRoots = TrustedRoots(Config.get_trustedroots_dir(self.config))
54        self.root_auth = self.config.SFA_REGISTRY_ROOT_AUTH
55
56     def create_top_level_records(self):
57         """
58         Create top level and interface records
59         """
60         # create root authority
61         interface_hrn = self.config.SFA_INTERFACE_HRN
62         self.create_top_level_auth_records(interface_hrn)
63
64         # create s user record for the slice manager
65         self.create_sm_client_record()
66
67         # create interface records
68         self.logger.info("Import: creating interface records")
69 # xxx authority+ turning off the creation of authority+*
70 #        self.create_interface_records()
71
72         # add local root authority's cert  to trusted list
73         self.logger.info("Import: adding " + interface_hrn + " to trusted list")
74         authority = self.AuthHierarchy.get_auth_info(interface_hrn)
75         self.TrustedRoots.add_gid(authority.get_gid_object())
76
77     def create_top_level_auth_records(self, hrn):
78         """
79         Create top level db records (includes root and sub authorities (local/remote)
80         """
81         # make sure parent exists
82         parent_hrn = get_authority(hrn)
83         if not parent_hrn:
84             parent_hrn = hrn
85         if not parent_hrn == hrn:
86             self.create_top_level_auth_records(parent_hrn)
87
88         # ensure key and cert exists:
89         self.AuthHierarchy.create_top_level_auth(hrn)    
90         # create the db record if it doesnt already exist    
91         auth_info = self.AuthHierarchy.get_auth_info(hrn)
92         auth_record = RegAuthority()
93         auth_record.hrn=hrn
94         auth_record.gid=auth_info.get_gid_object()
95         auth_record.authority=get_authority(hrn)
96         auth_record.just_created()
97         dbsession.add (auth_record)
98         dbsession.commit()
99         self.logger.info("Import: imported authority (parent) %s " % auth_record)
100
101     def create_sm_client_record(self):
102         """
103         Create a user record for the Slicemanager service.
104         """
105         hrn = self.config.SFA_INTERFACE_HRN + '.slicemanager'
106         urn = hrn_to_urn(hrn, 'user')
107         if not self.AuthHierarchy.auth_exists(urn):
108             self.logger.info("Import: creating Slice Manager user")
109             self.AuthHierarchy.create_auth(urn)
110
111         auth_info = self.AuthHierarchy.get_auth_info(hrn)
112         user_record = RegUser()
113         user_record.hrn=hrn
114         user_record.gid=auth_info.get_gid_object()
115         user_record.authority=get_authority(hrn)
116         user_record.just_created()
117         dbsession.add (user_record)
118         dbsession.commit()
119         self.logger.info("Import: importing user (slicemanager) %s " % user_record)
120
121 # xxx authority+ - this is currently turned off 
122     def create_interface_records(self):
123         """
124         Create a record for each SFA interface
125         """
126         # just create certs for all sfa interfaces even if they
127         # arent enabled
128         hrn = self.config.SFA_INTERFACE_HRN
129         reg_classes_info = [ (RegTmpAuthSa, 'authority+sa'),
130                           (RegTmpAuthAm, 'authority+am'),
131                           (RegTmpAuthSm, 'authority+sm'), ]
132         # interfaces = ['authority+sa', 'authority+am', 'authority+sm']
133         auth_info = self.AuthHierarchy.get_auth_info(hrn)
134         pkey = auth_info.get_pkey_object()
135         for (reg_class, interface) in reg_classes_info:
136             urn = hrn_to_urn(hrn, interface)
137             gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
138             # xxx this should probably use a RegAuthority, or a to-be-defined RegPeer object
139             # but for now we have to preserve the authority+<> stuff
140             interface_record = reg_class()
141             #interface_record = RegAuthority()
142             interface_record.hrn=hrn
143             interface_record.gid= gid
144             interface_record.authority=get_authority(hrn)
145             interface_record.just_created()
146             dbsession.add (interface_record)
147             dbsession.commit()
148             self.logger.info("Import: imported authority (%s) %s " % (interface,interface_record))
149              
150     def delete_record(self, hrn, type):
151         # delete the record
152         for rec in dbsession.query(RegRecord).filter_by(type=type,hrn=hrn):
153            dbsession.delete(rec)
154         dbsession.commit()