235877c936ab79bdaaf98c8c0e28244e196e4a59
[sfa.git] / sfa / plc / sfa-import-plc.py
1 #!/usr/bin/python
2 #
3 ### $Id$
4 ### $URL$
5 #
6 ##
7 # Import PLC records into the SFA database. It is indended that this tool be
8 # run once to create SFA records that reflect the current state of the
9 # planetlab database.
10 #
11 # The import tool assumes that the existing PLC hierarchy should all be part
12 # of "planetlab.us" (see the root_auth and level1_auth variables below).
13 #
14 # Public keys are extracted from the users' SSH keys automatically and used to
15 # create GIDs. This is relatively experimental as a custom tool had to be
16 # written to perform conversion from SSH to OpenSSL format. It only supports
17 # RSA keys at this time, not DSA keys.
18 ##
19
20 import getopt
21 import sys
22 import tempfile
23 import logging.handlers
24 import logging
25 from sfa.util.record import *
26 from sfa.util.table import SfaTable
27 from sfa.util.namespace import *
28 from sfa.util.config import Config
29 from sfa.trust.certificate import convert_public_key, Keypair
30 from sfa.trust.trustedroot import *
31 from sfa.trust.hierarchy import *
32 from sfa.plc.api import *
33 from sfa.trust.gid import create_uuid
34 from sfa.plc.sfaImport import *
35 from sfa.util.report import trace, error
36
37 def process_options():
38    global hrn
39
40    (options, args) = getopt.getopt(sys.argv[1:], '', [])
41    for opt in options:
42        name = opt[0]
43        val = opt[1]
44
45
46 def load_keys(filename):
47     keys = {}
48     tmp_dict = {}
49     try:
50         execfile(filename, tmp_dict)
51         if 'keys' in tmp_dict:
52             keys = tmp_dict['keys']
53         return keys
54     except:
55         return keys
56
57 def save_keys(filename, keys):
58     f = open(filename, 'w')
59     f.write("keys = %s" % str(keys))
60     f.close()
61
62 def main():
63     # setup the logger
64     LOGFILE='/var/log/sfa_import_plc.log'
65     logging.basicConfig(level=logging.INFO,
66                         format='%(asctime)s - %(message)s',
67                         filename=LOGFILE)
68     rotate_handler = logging.handlers.RotatingFileHandler(LOGFILE, maxBytes=1000000, backupCount=5) 
69     logger = logging.getLogger()
70     logger.addHandler(rotate_handler)
71     
72     process_options()
73     config = Config()
74     if not config.SFA_REGISTRY_ENABLED:
75         sys.exit(0)
76     root_auth = config.SFA_REGISTRY_ROOT_AUTH
77     level1_auth = config.SFA_REGISTRY_LEVEL1_AUTH
78     keys_filename = config.config_path + os.sep + 'person_keys.py' 
79     sfaImporter = sfaImport(logger)
80     shell = sfaImporter.shell
81     plc_auth = sfaImporter.plc_auth 
82     AuthHierarchy = sfaImporter.AuthHierarchy
83     TrustedRoots = sfaImporter.TrustedRoots
84     table = SfaTable()
85
86     if not table.exists():
87        table.create()
88
89     if not level1_auth or level1_auth in ['']:
90         level1_auth = None
91     
92     if not level1_auth:
93         sfaImporter.create_top_level_auth_records(root_auth)
94         import_auth = root_auth
95     else:
96         if not AuthHierarchy.auth_exists(level1_auth):
97             AuthHierarchy.create_auth(level1_auth)
98         sfaImporter.create_top_level_auth_records(level1_auth)
99         import_auth = level1_auth
100
101     trace("Import: adding " + import_auth + " to trusted list", logger)
102     authority = AuthHierarchy.get_auth_info(import_auth)
103     TrustedRoots.add_gid(authority.get_gid_object())
104
105     if ".vini" in import_auth and import_auth.endswith('vini'):
106         # create a fake internet2 site first
107         i2site = {'name': 'Internet2', 'abbreviated_name': 'I2',
108                     'login_base': 'internet2', 'site_id': -1}
109         sfaImporter.import_site(import_auth, i2site)
110    
111     # create dict of all existing sfa records
112     existing_records = {}
113     existing_hrns = []
114     key_ids = []
115     person_keys = {} 
116     results = table.find()
117     for result in results:
118         existing_records[(result['hrn'], result['type'])] = result
119         existing_hrns.append(result['hrn']) 
120             
121     # Get all plc sites
122     sites = shell.GetSites(plc_auth, {'peer_id': None})
123     sites_dict = {}
124     for site in sites:
125         sites_dict[site['login_base']] = site 
126     
127     # Get all plc users
128     persons = shell.GetPersons(plc_auth, {'peer_id': None}, ['person_id', 'email', 'key_ids', 'site_ids'])
129     persons_dict = {}
130     for person in persons:
131         persons_dict[person['person_id']] = person
132         key_ids.extend(person['key_ids'])
133
134     # Get all public keys
135     keys = shell.GetKeys(plc_auth, {'peer_id': None, 'key_id': key_ids})
136     keys_dict = {}
137     for key in keys:
138         keys_dict[key['key_id']] = key['key']
139
140     # create a dict of person keys keyed on key_id 
141     old_person_keys = load_keys(keys_filename)
142     for person in persons:
143         pubkeys = []
144         for key_id in person['key_ids']:
145             pubkeys.append(keys_dict[key_id])
146         person_keys[person['person_id']] = pubkeys
147
148     # Get all plc nodes  
149     nodes = shell.GetNodes(plc_auth, {'peer_id': None}, ['node_id', 'hostname', 'site_id'])
150     nodes_dict = {}
151     for node in nodes:
152         nodes_dict[node['node_id']] = node
153
154     # Get all plc slices
155     slices = shell.GetSlices(plc_auth, {'peer_id': None}, ['slice_id', 'name'])
156     slices_dict = {}
157     for slice in slices:
158         slices_dict[slice['slice_id']] = slice
159     # start importing 
160     for site in sites:
161         site_hrn = import_auth + "." + site['login_base']
162         print "Importing site: %s" % site_hrn
163
164         # import if hrn is not in list of existing hrns or if the hrn exists
165         # but its not a site record
166         if site_hrn not in existing_hrns or \
167            (site_hrn, 'authority') not in existing_records:
168             site_hrn = sfaImporter.import_site(import_auth, site)
169              
170         # import node records
171         for node_id in site['node_ids']:
172             if node_id not in nodes_dict:
173                 continue 
174             node = nodes_dict[node_id]
175             hrn =  hostname_to_hrn(import_auth, site['login_base'], node['hostname'])
176             if hrn not in existing_hrns or \
177                (hrn, 'node') not in existing_records:
178                 sfaImporter.import_node(site_hrn, node)
179
180         # import slices
181         for slice_id in site['slice_ids']:
182             if slice_id not in slices_dict:
183                 continue 
184             slice = slices_dict[slice_id]
185             hrn = slicename_to_hrn(import_auth, slice['name'])
186             if hrn not in existing_hrns or \
187                (hrn, 'slice') not in existing_records:
188                 sfaImporter.import_slice(site_hrn, slice)      
189
190         # import persons
191         for person_id in site['person_ids']:
192             if person_id not in persons_dict:
193                 continue 
194             person = persons_dict[person_id]
195             hrn = email_to_hrn(site_hrn, person['email'])
196             old_keys = []
197             new_keys = []
198             if person_id in old_person_keys:
199                 old_keys = old_person_keys[person_id]
200             if person_id in person_keys:
201                 new_keys = person_keys[person_id]
202             update_record = False
203             for key in new_keys:
204                 if key not in old_keys:
205                     update_record = True 
206
207             if hrn not in existing_hrns or \
208                (hrn, 'user') not in existing_records or update_record:
209                 sfaImporter.import_person(site_hrn, person)
210
211     # remove stale records    
212     for (record_hrn, type) in existing_records.keys():
213         record = existing_records[(record_hrn, type)]
214         # if this is the interface name dont do anything
215         if record_hrn == import_auth or record['peer_authority']:
216             continue
217         # dont delete vini's internet2 placeholdder record
218         # normally this would be deleted becuase it does not have a plc record 
219         if ".vini" in import_auth and import_auth.endswith('vini') and \
220            record_hrn.endswith("internet2"):     
221             continue
222
223         found = False
224         
225         if type == 'authority':    
226             for site in sites:
227                 site_hrn = import_auth + "." + site['login_base']
228                 if site_hrn == record_hrn and site['site_id'] == record['pointer']:
229                     found = True
230                     break
231
232         elif type == 'user':
233             login_base = get_leaf(get_authority(record_hrn))
234             username = get_leaf(record_hrn)
235             if login_base in sites_dict:
236                 site = sites_dict[login_base]
237                 for person in persons:
238                     tmp_username = person['email'].split("@")[0]
239                     alt_username = person['email'].split("@")[0].replace(".", "_")
240                     if username in [tmp_username, alt_username] and \
241                        site['site_id'] in person['site_ids'] and \
242                        person['person_id'] == record['pointer']:
243                         found = True
244                         break
245         
246         elif type == 'slice':
247             slicename = hrn_to_pl_slicename(record_hrn)
248             for slice in slices:
249                 if slicename == slice['name'] and \
250                    slice['slice_id'] == record['pointer']:
251                     found = True
252                     break    
253  
254         elif type == 'node':
255             login_base = get_leaf(get_authority(record_hrn))
256             nodename = get_leaf(record_hrn)
257             if login_base in sites_dict:
258                 site = sites_dict[login_base]
259                 for node in nodes:
260                     tmp_nodename = node['hostname'].split(".")[0]
261                     if tmp_nodename == nodename and \
262                        node['site_id'] == site['site_id'] and \
263                        node['node_id'] == record['pointer']:
264                         found = True
265                         break  
266         else:
267             continue 
268         
269         if not found:
270             record_object = existing_records[(record_hrn, type)]
271             sfaImporter.delete_record(record_hrn, type) 
272                                    
273     # save pub keys
274     trace('Import: saving current pub keys', logger)
275     save_keys(keys_filename, person_keys)                
276         
277 if __name__ == "__main__":
278     main()