moved plc specific code out of sfa.importer.sfaImporter. Refactored sfa.importer...
[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.table import SfaTable
20 from sfa.storage.record import SfaRecord
21 from sfa.storage.table import SfaTable
22
23
24 def _un_unicode(str):
25    if isinstance(str, unicode):
26        return str.encode("ascii", "ignore")
27    else:
28        return str
29
30 def _cleanup_string(str):
31     # pgsql has a fit with strings that have high ascii in them, so filter it
32     # out when generating the hrns.
33     tmp = ""
34     for c in str:
35         if ord(c) < 128:
36             tmp = tmp + c
37     str = tmp
38
39     str = _un_unicode(str)
40     str = str.replace(" ", "_")
41     str = str.replace(".", "_")
42     str = str.replace("(", "_")
43     str = str.replace("'", "_")
44     str = str.replace(")", "_")
45     str = str.replace('"', "_")
46     return str
47
48 class sfaImport:
49
50     def __init__(self):
51        self.logger = _SfaLogger(logfile='/var/log/sfa_import.log', loggername='importlog')
52        self.AuthHierarchy = Hierarchy()
53        self.table = SfaTable()     
54        self.config = Config()
55        self.TrustedRoots = TrustedRoots(Config.get_trustedroots_dir(self.config))
56        self.root_auth = self.config.SFA_REGISTRY_ROOT_AUTH
57        # should use a driver instead...
58        from sfa.plc.plshell import PlShell
59        # how to connect to planetlab 
60        self.shell = PlShell (self.config)
61
62     def create_top_level_records(self):
63         """
64         Create top level and interface records
65         """
66         # create root authority
67         interface_hrn = self.config.SFA_INTERFACE_HRN
68         self.create_top_level_auth_records(interface_hrn)
69
70         # create s user record for the slice manager
71         self.create_sm_client_record()
72
73         # create interface records
74         self.logger.info("Import: creating interface records")
75         self.create_interface_records()
76
77         # add local root authority's cert  to trusted list
78         self.logger.info("Import: adding " + interface_hrn + " to trusted list")
79         authority = self.AuthHierarchy.get_auth_info(interface_hrn)
80         self.TrustedRoots.add_gid(authority.get_gid_object())
81
82     def create_top_level_auth_records(self, hrn):
83         """
84         Create top level db records (includes root and sub authorities (local/remote)
85         """
86         # make sure parent exists
87         parent_hrn = get_authority(hrn)
88         if not parent_hrn:
89             parent_hrn = hrn
90         if not parent_hrn == hrn:
91             self.create_top_level_auth_records(parent_hrn)
92
93         # enxure key and cert exists:
94         self.AuthHierarchy.create_top_level_auth(hrn)    
95         # create the db record if it doesnt already exist    
96         auth_info = self.AuthHierarchy.get_auth_info(hrn)
97         auth_record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=-1, authority=get_authority(hrn))
98         auth_record.sync(verbose=True)
99
100     def create_sm_client_record(self):
101         """
102         Create a user record for the Slicemanager service.
103         """
104         hrn = self.config.SFA_INTERFACE_HRN + '.slicemanager'
105         urn = hrn_to_urn(hrn, 'user')
106         if not self.AuthHierarchy.auth_exists(urn):
107             self.logger.info("Import: creating Slice Manager user")
108             self.AuthHierarchy.create_auth(urn)
109
110         auth_info = self.AuthHierarchy.get_auth_info(hrn)
111         record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), \
112                            type="user", pointer=-1, authority=get_authority(hrn))
113         record.sync(verbose=True)
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         table = SfaTable()
124         auth_info = self.AuthHierarchy.get_auth_info(hrn)
125         pkey = auth_info.get_pkey_object()
126         for interface in interfaces:
127             urn = hrn_to_urn(hrn, interface)
128             gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
129             interface_record = SfaRecord(hrn=hrn, type=interface, pointer=-1,
130                                          gid = gid, authority=get_authority(hrn))
131             interface_record.sync(verbose=True)
132              
133     def delete_record(self, hrn, type):
134         # delete the record
135         table = SfaTable()
136         record_list = table.find({'type': type, 'hrn': hrn})
137         for record in record_list:
138             self.logger.info("Import: removing record %s %s" % (type, hrn))
139             table.remove(record)