Setting tag sfa-2.0-10
[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
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.table = SfaTable()     
53        self.config = Config()
54        self.TrustedRoots = TrustedRoots(Config.get_trustedroots_dir(self.config))
55        self.root_auth = self.config.SFA_REGISTRY_ROOT_AUTH
56
57     def create_top_level_records(self):
58         """
59         Create top level and interface records
60         """
61         # create root authority
62         interface_hrn = self.config.SFA_INTERFACE_HRN
63         self.create_top_level_auth_records(interface_hrn)
64
65         # create s user record for the slice manager
66         self.create_sm_client_record()
67
68         # create interface records
69         self.logger.info("Import: creating interface records")
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         # enxure 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 = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=-1, authority=get_authority(hrn))
93         self.logger.info("Import: importing %s " % auth_record.summary_string())
94         auth_record.sync()
95
96     def create_sm_client_record(self):
97         """
98         Create a user record for the Slicemanager service.
99         """
100         hrn = self.config.SFA_INTERFACE_HRN + '.slicemanager'
101         urn = hrn_to_urn(hrn, 'user')
102         if not self.AuthHierarchy.auth_exists(urn):
103             self.logger.info("Import: creating Slice Manager user")
104             self.AuthHierarchy.create_auth(urn)
105
106         auth_info = self.AuthHierarchy.get_auth_info(hrn)
107         record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), \
108                            type="user", pointer=-1, authority=get_authority(hrn))
109         self.logger.info("Import: importing %s " % record.summary_string())
110         record.sync()
111
112     def create_interface_records(self):
113         """
114         Create a record for each SFA interface
115         """
116         # just create certs for all sfa interfaces even if they
117         # arent enabled
118         hrn = self.config.SFA_INTERFACE_HRN
119         interfaces = ['authority+sa', 'authority+am', 'authority+sm']
120         table = SfaTable()
121         auth_info = self.AuthHierarchy.get_auth_info(hrn)
122         pkey = auth_info.get_pkey_object()
123         for interface in interfaces:
124             urn = hrn_to_urn(hrn, interface)
125             gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
126             interface_record = SfaRecord(hrn=hrn, type=interface, pointer=-1,
127                                          gid = gid, authority=get_authority(hrn))
128             self.logger.info("Import: importing %s " % interface_record.summary_string())
129             interface_record.sync()
130              
131     def delete_record(self, hrn, type):
132         # delete the record
133         table = SfaTable()
134         record_list = table.find({'type': type, 'hrn': hrn})
135         for record in record_list:
136             self.logger.info("Import: removing record %s %s" % (type, hrn))
137             table.remove(record)