2 # Import PLC records into the Geni database. It is indended that this tool be
3 # run once to create Geni records that reflect the current state of the
6 # The import tool assumes that the existing PLC hierarchy should all be part
7 # of "planetlab.us" (see the root_auth and level1_auth variables below).
9 # Public keys are extracted from the users' SSH keys automatically and used to
10 # create GIDs. This is relatively experimental as a custom tool had to be
11 # written to perform conversion from SSH to OpenSSL format. It only supports
12 # RSA keys at this time, not DSA keys.
19 from geni.util.cert import *
20 from geni.util.trustedroot import *
21 from geni.util.hierarchy import *
22 from geni.util.record import *
23 from geni.util.genitable import *
24 from geni.util.misc import *
25 from geni.util.config import *
27 # get PL account settings from config module
28 pl_auth = get_pl_auth()
30 # connect to planetlab
32 from geni.util import remoteshell
33 shell = remoteshell.RemoteShell()
36 shell = PLC.Shell.Shell(globals = globals())
39 # Two authorities are specified: the root authority and the level1 authority.
44 #root_auth = "planetlab"
45 #level1_auth = "planetlab.us"
48 root_auth = config.GENI_REGISTRY_ROOT_AUTH
49 level1_auth = config.GENI_REGISTRY_LEVEL1_AUTH
50 if not level1_auth or level1_auth in ['']:
52 keyconvert_fn = config.GENI_BASE_DIR + os.sep + "keyconvert/keyconvert"
56 if isinstance(str, unicode):
57 return str.encode("ascii", "ignore")
61 def cleanup_string(str):
62 # pgsql has a fit with strings that have high ascii in them, so filter it
63 # out when generating the hrns.
71 str = str.replace(" ", "_")
72 str = str.replace(".", "_")
73 str = str.replace("(", "_")
74 str = str.replace("'", "_")
75 str = str.replace(")", "_")
76 str = str.replace('"', "_")
79 def process_options():
82 (options, args) = getopt.getopt(sys.argv[1:], '', [])
90 # get PL account settings from config module
91 pl_auth = get_pl_auth()
93 # connect to planetlab
95 from geni.util import remoteshell
96 shell = remoteshell.RemoteShell()
99 shell = PLC.Shell.Shell(globals = globals())
103 def get_auth_table(auth_name):
104 AuthHierarchy = Hierarchy()
105 auth_info = AuthHierarchy.get_auth_info(auth_name)
107 table = GeniTable(hrn=auth_name,
108 cninfo=auth_info.get_dbinfo())
110 # if the table doesn't exist, then it means we haven't put any records
111 # into this authority yet.
113 if not table.exists():
114 report.trace("Import: creating table for authority " + auth_name)
119 def get_pl_pubkey(key_id):
120 keys = shell.GetKeys(pl_auth, [key_id])
122 key_str = keys[0]['key']
124 if "ssh-dss" in key_str:
125 print "XXX: DSA key encountered, ignoring"
128 # generate temporary files to hold the keys
129 (ssh_f, ssh_fn) = tempfile.mkstemp()
130 ssl_fn = tempfile.mktemp()
132 os.write(ssh_f, key_str)
135 if not os.path.exists(keyconvert_fn):
136 report.trace(" keyconvert utility " + str(keyconvert_fn) + " does not exist");
139 cmd = keyconvert_fn + " " + ssh_fn + " " + ssl_fn
143 # this check leaves the temporary file containing the public key so
144 # that it can be expected to see why it failed.
145 # TODO: for production, cleanup the temporary files
146 if not os.path.exists(ssl_fn):
147 report.trace(" failed to convert key from " + ssh_fn + " to " + ssl_fn)
152 k.load_pubkey_from_file(ssl_fn)
154 print "XXX: Error while converting key: ", key_str
157 # remove the temporary files
165 def person_to_hrn(parent_hrn, person):
166 # the old way - Lastname_Firstname
167 #personname = person['last_name'] + "_" + person['first_name']
169 # the new way - use email address up to the "@"
170 personname = person['email'].split("@")[0]
172 personname = cleanup_string(personname)
174 hrn = parent_hrn + "." + personname
177 def import_person(parent_hrn, person):
178 AuthHierarchy = Hierarchy()
179 hrn = person_to_hrn(parent_hrn, person)
181 # ASN.1 will have problems with hrn's longer than 64 characters
185 report.trace("Import: importing person " + hrn)
187 table = get_auth_table(parent_hrn)
189 person_record = table.resolve("user", hrn)
190 if not person_record:
191 key_ids = person["key_ids"]
194 # get the user's private key from the SSH keys they have uploaded
196 pkey = get_pl_pubkey(key_ids[0])
198 # the user has no keys
199 report.trace(" person " + hrn + " does not have a PL public key")
202 # if a key is unavailable, then we still need to put something in the
203 # user's GID. So make one up.
205 pkey = Keypair(create=True)
207 person_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
208 person_record = GeniRecord(name=hrn, gid=person_gid, type="user", pointer=person['person_id'])
209 report.trace(" inserting user record for " + hrn)
210 table.insert(person_record)
212 key_ids = person["key_ids"]
214 pkey = get_pl_pubkey(key_ids[0])
215 person_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
216 person_record = GeniRecord(name=hrn, gid=person_gid, type="user", pointer=person['person_id'])
217 report.trace(" updating user record for " + hrn)
218 table.update(person_record)
220 def import_slice(parent_hrn, slice):
221 AuthHierarchy = Hierarchy()
222 slicename = slice['name'].split("_",1)[-1]
223 slicename = cleanup_string(slicename)
226 report.error("Import_Slice: failed to parse slice name " + slice['name'])
229 hrn = parent_hrn + "." + slicename
230 report.trace("Import: importing slice " + hrn)
232 table = get_auth_table(parent_hrn)
234 slice_record = table.resolve("slice", hrn)
236 pkey = Keypair(create=True)
237 slice_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
238 slice_record = GeniRecord(name=hrn, gid=slice_gid, type="slice", pointer=slice['slice_id'])
239 report.trace(" inserting slice record for " + hrn)
240 table.insert(slice_record)
242 def import_node(parent_hrn, node):
243 AuthHierarchy = Hierarchy()
244 nodename = node['hostname'].split(".")[0]
245 nodename = cleanup_string(nodename)
248 report.error("Import_node: failed to parse node name " + node['hostname'])
251 hrn = parent_hrn + "." + nodename
253 # ASN.1 will have problems with hrn's longer than 64 characters
257 report.trace("Import: importing node " + hrn)
259 table = get_auth_table(parent_hrn)
261 node_record = table.resolve("node", hrn)
263 pkey = Keypair(create=True)
264 node_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
265 node_record = GeniRecord(name=hrn, gid=node_gid, type="node", pointer=node['node_id'])
266 report.trace(" inserting node record for " + hrn)
267 table.insert(node_record)
269 def import_site(parent_hrn, site):
270 AuthHierarchy = Hierarchy()
271 sitename = site['login_base']
272 sitename = cleanup_string(sitename)
274 hrn = parent_hrn + "." + sitename
276 report.trace("Import_Site: importing site " + hrn)
278 # create the authority
279 if not AuthHierarchy.auth_exists(hrn):
280 AuthHierarchy.create_auth(hrn)
282 auth_info = AuthHierarchy.get_auth_info(hrn)
284 table = get_auth_table(parent_hrn)
286 sa_record = table.resolve("sa", hrn)
288 sa_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="sa", pointer=site['site_id'])
289 report.trace(" inserting sa record for " + hrn)
290 table.insert(sa_record)
292 ma_record = table.resolve("ma", hrn)
294 ma_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="ma", pointer=site['site_id'])
295 report.trace(" inserting ma record for " + hrn)
296 table.insert(ma_record)
298 for person_id in site['person_ids']:
299 persons = shell.GetPersons(pl_auth, [person_id])
302 import_person(hrn, persons[0])
304 report.trace("Failed to import: %s" % persons[0])
305 for slice_id in site['slice_ids']:
306 slices = shell.GetSlices(pl_auth, [slice_id])
309 import_slice(hrn, slices[0])
311 report.trace("Failed to import: %s" % slices[0])
312 for node_id in site['node_ids']:
313 nodes = shell.GetNodes(pl_auth, [node_id])
316 import_node(hrn, nodes[0])
318 report.trace("Failed to import: %s" % nodes[0])
320 def create_top_level_auth_records(hrn):
321 parent_hrn = get_authority(hrn)
322 print hrn, ":", parent_hrn
325 auth_info = AuthHierarchy.get_auth_info(parent_hrn)
326 table = get_auth_table(parent_hrn)
328 sa_record = table.resolve("sa", hrn)
330 sa_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="sa", pointer=-1)
331 report.trace(" inserting sa record for " + hrn)
332 table.insert(sa_record)
334 ma_record = table.resolve("ma", hrn)
336 ma_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="ma", pointer=-1)
337 report.trace(" inserting ma record for " + hrn)
338 table.insert(ma_record)
346 print "Base Directory: ", config.GENI_BASE_DIR
348 AuthHierarchy = Hierarchy()
349 TrustedRoots = TrustedRootList()
351 print "Import: creating top level authorities"
353 if not AuthHierarchy.auth_exists(root_auth):
354 AuthHierarchy.create_auth(root_auth)
356 create_top_level_auth_records(root_auth)
358 if not AuthHierarchy.auth_exists(level1_auth):
359 AuthHierarchy.create_auth(level1_auth)
360 create_top_level_auth_records(level1_auth)
361 import_auth = level1_auth
363 import_auth = root_auth
365 print "Import: adding", root_auth, "to trusted list"
366 root = AuthHierarchy.get_auth_info(root_auth)
367 TrustedRoots.add_gid(root.get_gid_object())
371 sites = shell.GetSites(pl_auth)
373 import_site(import_auth, site)
375 if __name__ == "__main__":