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.
20 from trustedroot import *
21 from hierarchy import *
23 from genitable import *
29 # Two authorities are specified: the root authority and the level1 authority.
31 root_auth = "planetlab"
32 level1_auth = "planetlab.us"
35 if isinstance(str, unicode):
36 return str.encode("ascii", "ignore")
40 def cleanup_string(str):
41 # pgsql has a fit with strings that have high ascii in them, so filter it
42 # out when generating the hrns.
50 str = str.replace(" ", "_")
51 str = str.replace(".", "_")
52 str = str.replace("(", "_")
53 str = str.replace("'", "_")
54 str = str.replace(")", "_")
55 str = str.replace('"', "_")
58 def process_options():
61 (options, args) = getopt.getopt(sys.argv[1:], '', [])
69 # get PL account settings from config module
70 pl_auth = get_pl_auth()
72 # connect to planetlab
75 shell = remoteshell.RemoteShell()
78 shell = PLC.Shell.Shell(globals = globals())
80 def get_auth_table(auth_name):
81 auth_info = AuthHierarchy.get_auth_info(auth_name)
83 table = GeniTable(hrn=auth_name,
84 cninfo=auth_info.get_dbinfo())
86 # if the table doesn't exist, then it means we haven't put any records
87 # into this authority yet.
89 if not table.exists():
90 report.trace("Import: creating table for authority " + auth_name)
95 def get_pl_pubkey(key_id):
96 keys = shell.GetKeys(pl_auth, [key_id])
98 key_str = keys[0]['key']
100 if "ssh-dss" in key_str:
101 print "XXX: DSA key encountered, ignoring"
104 # generate temporary files to hold the keys
105 (ssh_f, ssh_fn) = tempfile.mkstemp()
106 ssl_fn = tempfile.mktemp()
108 os.write(ssh_f, key_str)
111 cmd = "../keyconvert/keyconvert " + ssh_fn + " " + ssl_fn
115 # this check leaves the temporary file containing the public key so
116 # that it can be expected to see why it failed.
117 # TODO: for production, cleanup the temporary files
118 if not os.path.exists(ssl_fn):
119 report.trace(" failed to convert key from " + ssh_fn + " to " + ssl_fn)
124 k.load_pubkey_from_file(ssl_fn)
126 print "XXX: Error while converting key: ", key_str
129 # remove the temporary files
137 def person_to_hrn(parent_hrn, person):
138 personname = person['last_name'] + "_" + person['first_name']
140 personname = cleanup_string(personname)
142 hrn = parent_hrn + "." + personname
145 def import_person(parent_hrn, person):
146 hrn = person_to_hrn(parent_hrn, person)
148 # ASN.1 will have problems with hrn's longer than 64 characters
152 report.trace("Import: importing person " + hrn)
154 table = get_auth_table(parent_hrn)
156 person_record = table.resolve("user", hrn)
157 if not person_record:
158 key_ids = person["key_ids"]
161 # get the user's private key from the SSH keys they have uploaded
163 pkey = get_pl_pubkey(key_ids[0])
165 # the user has no keys
166 report.trace(" person " + hrn + " does not have a PL public key")
169 # if a key is unavailable, then we still need to put something in the
170 # user's GID. So make one up.
172 pkey = Keypair(create=True)
174 person_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
175 person_record = GeniRecord(name=hrn, gid=person_gid, type="user", pointer=person['person_id'])
176 report.trace(" inserting user record for " + hrn)
177 table.insert(person_record)
179 def import_slice(parent_hrn, slice):
180 slicename = slice['name'].split("_",1)[-1]
181 slicename = cleanup_string(slicename)
184 report.error("Import_Slice: failed to parse slice name " + slice['name'])
187 hrn = parent_hrn + "." + slicename
188 report.trace("Import: importing slice " + hrn)
190 table = get_auth_table(parent_hrn)
192 slice_record = table.resolve("slice", hrn)
194 pkey = Keypair(create=True)
195 slice_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
196 slice_record = GeniRecord(name=hrn, gid=slice_gid, type="slice", pointer=slice['slice_id'])
197 report.trace(" inserting slice record for " + hrn)
198 table.insert(slice_record)
200 def import_node(parent_hrn, node):
201 nodename = node['hostname']
202 nodename = cleanup_string(nodename)
205 report.error("Import_node: failed to parse node name " + node['hostname'])
208 hrn = parent_hrn + "." + nodename
210 # ASN.1 will have problems with hrn's longer than 64 characters
214 report.trace("Import: importing node " + hrn)
216 table = get_auth_table(parent_hrn)
218 node_record = table.resolve("node", hrn)
220 pkey = Keypair(create=True)
221 node_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
222 node_record = GeniRecord(name=hrn, gid=node_gid, type="node", pointer=node['node_id'])
223 report.trace(" inserting node record for " + hrn)
224 table.insert(node_record)
226 def import_site(parent_hrn, site):
227 sitename = site['login_base']
228 sitename = cleanup_string(sitename)
230 hrn = parent_hrn + "." + sitename
232 report.trace("Import_Site: importing site " + hrn)
234 # create the authority
235 if not AuthHierarchy.auth_exists(hrn):
236 AuthHierarchy.create_auth(hrn)
238 auth_info = AuthHierarchy.get_auth_info(hrn)
240 table = get_auth_table(parent_hrn)
242 sa_record = table.resolve("sa", hrn)
244 sa_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="sa", pointer=site['site_id'])
245 report.trace(" inserting sa record for " + hrn)
246 table.insert(sa_record)
248 ma_record = table.resolve("ma", hrn)
250 ma_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="ma", pointer=site['site_id'])
251 report.trace(" inserting ma record for " + hrn)
252 table.insert(ma_record)
254 for person_id in site['person_ids']:
255 persons = shell.GetPersons(pl_auth, [person_id])
257 import_person(hrn, persons[0])
259 for slice_id in site['slice_ids']:
260 slices = shell.GetSlices(pl_auth, [slice_id])
262 import_slice(hrn, slices[0])
264 for node_id in site['node_ids']:
265 nodes = shell.GetNodes(pl_auth, [node_id])
267 import_node(hrn, nodes[0])
269 def create_top_level_auth_records(hrn):
270 parent_hrn = get_authority(hrn)
272 auth_info = AuthHierarchy.get_auth_info(parent_hrn)
273 table = get_auth_table(parent_hrn)
275 sa_record = table.resolve("sa", hrn)
277 sa_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="sa", pointer=-1)
278 report.trace(" inserting sa record for " + hrn)
279 table.insert(sa_record)
281 ma_record = table.resolve("ma", hrn)
283 ma_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="ma", pointer=-1)
284 report.trace(" inserting ma record for " + hrn)
285 table.insert(ma_record)
293 AuthHierarchy = Hierarchy()
294 TrustedRoots = TrustedRootList()
296 print "Import: creating top level authorities"
298 if not AuthHierarchy.auth_exists(root_auth):
299 AuthHierarchy.create_auth(root_auth)
300 #create_top_level_auth_records(root_auth)
301 if not AuthHierarchy.auth_exists(level1_auth):
302 AuthHierarchy.create_auth(level1_auth)
303 create_top_level_auth_records(level1_auth)
305 print "Import: adding", root_auth, "to trusted list"
306 root = AuthHierarchy.get_auth_info(root_auth)
307 TrustedRoots.add_gid(root.get_gid_object())
311 sites = shell.GetSites(pl_auth)
313 import_site(level1_auth, site)
315 if __name__ == "__main__":