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"
36 if isinstance(str, unicode):
37 return str.encode("ascii", "ignore")
41 def cleanup_string(str):
42 # pgsql has a fit with strings that have high ascii in them, so filter it
43 # out when generating the hrns.
51 str = str.replace(" ", "_")
52 str = str.replace(".", "_")
53 str = str.replace("(", "_")
54 str = str.replace("'", "_")
55 str = str.replace(")", "_")
56 str = str.replace('"', "_")
59 def process_options():
62 (options, args) = getopt.getopt(sys.argv[1:], '', [])
70 # get PL account settings from config module
71 pl_auth = get_pl_auth()
73 # connect to planetlab
76 shell = remoteshell.RemoteShell()
79 shell = PLC.Shell.Shell(globals = globals())
81 def get_auth_table(auth_name):
82 AuthHierarchy = Hierarchy()
83 auth_info = AuthHierarchy.get_auth_info(auth_name)
85 table = GeniTable(hrn=auth_name,
86 cninfo=auth_info.get_dbinfo())
88 # if the table doesn't exist, then it means we haven't put any records
89 # into this authority yet.
91 if not table.exists():
92 report.trace("Import: creating table for authority " + auth_name)
97 def get_pl_pubkey(key_id):
98 keys = shell.GetKeys(pl_auth, [key_id])
100 key_str = keys[0]['key']
102 if "ssh-dss" in key_str:
103 print "XXX: DSA key encountered, ignoring"
106 # generate temporary files to hold the keys
107 (ssh_f, ssh_fn) = tempfile.mkstemp()
108 ssl_fn = tempfile.mktemp()
110 os.write(ssh_f, key_str)
113 cmd = "../keyconvert/keyconvert " + ssh_fn + " " + ssl_fn
117 # this check leaves the temporary file containing the public key so
118 # that it can be expected to see why it failed.
119 # TODO: for production, cleanup the temporary files
120 if not os.path.exists(ssl_fn):
121 report.trace(" failed to convert key from " + ssh_fn + " to " + ssl_fn)
126 k.load_pubkey_from_file(ssl_fn)
128 print "XXX: Error while converting key: ", key_str
131 # remove the temporary files
139 def person_to_hrn(parent_hrn, person):
140 personname = person['last_name'] + "_" + person['first_name']
142 personname = cleanup_string(personname)
144 hrn = parent_hrn + "." + personname
147 def import_person(parent_hrn, person):
148 AuthHierarchy = Hierarchy()
149 hrn = person_to_hrn(parent_hrn, person)
151 # ASN.1 will have problems with hrn's longer than 64 characters
155 report.trace("Import: importing person " + hrn)
157 table = get_auth_table(parent_hrn)
159 person_record = table.resolve("user", hrn)
160 if not person_record:
161 key_ids = person["key_ids"]
164 # get the user's private key from the SSH keys they have uploaded
166 pkey = get_pl_pubkey(key_ids[0])
168 # the user has no keys
169 report.trace(" person " + hrn + " does not have a PL public key")
172 # if a key is unavailable, then we still need to put something in the
173 # user's GID. So make one up.
175 pkey = Keypair(create=True)
177 person_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
178 person_record = GeniRecord(name=hrn, gid=person_gid, type="user", pointer=person['person_id'])
179 report.trace(" inserting user record for " + hrn)
180 table.insert(person_record)
182 key_ids = person["key_ids"]
184 pkey = get_pl_pubkey(key_ids[0])
185 person_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
186 person_record = GeniRecord(name=hrn, gid=person_gid, type="user", pointer=person['person_id'])
187 report.trace(" updating user record for " + hrn)
188 table.update(person_record)
190 def import_slice(parent_hrn, slice):
191 AuthHierarchy = Hierarchy()
192 slicename = slice['name'].split("_",1)[-1]
193 slicename = cleanup_string(slicename)
196 report.error("Import_Slice: failed to parse slice name " + slice['name'])
199 hrn = parent_hrn + "." + slicename
200 report.trace("Import: importing slice " + hrn)
202 table = get_auth_table(parent_hrn)
204 slice_record = table.resolve("slice", hrn)
206 pkey = Keypair(create=True)
207 slice_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
208 slice_record = GeniRecord(name=hrn, gid=slice_gid, type="slice", pointer=slice['slice_id'])
209 report.trace(" inserting slice record for " + hrn)
210 table.insert(slice_record)
212 def import_node(parent_hrn, node):
213 AuthHierarchy = Hierarchy()
214 nodename = node['hostname']
215 nodename = cleanup_string(nodename)
218 report.error("Import_node: failed to parse node name " + node['hostname'])
221 hrn = parent_hrn + "." + nodename
223 # ASN.1 will have problems with hrn's longer than 64 characters
227 report.trace("Import: importing node " + hrn)
229 table = get_auth_table(parent_hrn)
231 node_record = table.resolve("node", hrn)
233 pkey = Keypair(create=True)
234 node_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
235 node_record = GeniRecord(name=hrn, gid=node_gid, type="node", pointer=node['node_id'])
236 report.trace(" inserting node record for " + hrn)
237 table.insert(node_record)
239 def import_site(parent_hrn, site):
240 AuthHierarchy = Hierarchy()
241 sitename = site['login_base']
242 sitename = cleanup_string(sitename)
244 hrn = parent_hrn + "." + sitename
246 report.trace("Import_Site: importing site " + hrn)
248 # create the authority
249 if not AuthHierarchy.auth_exists(hrn):
250 AuthHierarchy.create_auth(hrn)
252 auth_info = AuthHierarchy.get_auth_info(hrn)
254 table = get_auth_table(parent_hrn)
256 sa_record = table.resolve("sa", hrn)
258 sa_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="sa", pointer=site['site_id'])
259 report.trace(" inserting sa record for " + hrn)
260 table.insert(sa_record)
262 ma_record = table.resolve("ma", hrn)
264 ma_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="ma", pointer=site['site_id'])
265 report.trace(" inserting ma record for " + hrn)
266 table.insert(ma_record)
268 for person_id in site['person_ids']:
269 persons = shell.GetPersons(pl_auth, [person_id])
271 import_person(hrn, persons[0])
273 for slice_id in site['slice_ids']:
274 slices = shell.GetSlices(pl_auth, [slice_id])
276 import_slice(hrn, slices[0])
278 for node_id in site['node_ids']:
279 nodes = shell.GetNodes(pl_auth, [node_id])
281 import_node(hrn, nodes[0])
283 def create_top_level_auth_records(hrn):
284 parent_hrn = get_authority(hrn)
285 print hrn, ":", parent_hrn
286 auth_info = AuthHierarchy.get_auth_info(parent_hrn)
287 table = get_auth_table(parent_hrn)
289 sa_record = table.resolve("sa", hrn)
291 sa_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="sa", pointer=-1)
292 report.trace(" inserting sa record for " + hrn)
293 table.insert(sa_record)
295 ma_record = table.resolve("ma", hrn)
297 ma_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="ma", pointer=-1)
298 report.trace(" inserting ma record for " + hrn)
299 table.insert(ma_record)
307 AuthHierarchy = Hierarchy()
308 TrustedRoots = TrustedRootList()
310 print "Import: creating top level authorities"
312 if not AuthHierarchy.auth_exists(root_auth):
313 AuthHierarchy.create_auth(root_auth)
314 #create_top_level_auth_records(root_auth)
315 if not AuthHierarchy.auth_exists(level1_auth):
316 AuthHierarchy.create_auth(level1_auth)
317 create_top_level_auth_records(level1_auth)
319 print "Import: adding", root_auth, "to trusted list"
320 root = AuthHierarchy.get_auth_info(root_auth)
321 TrustedRoots.add_gid(root.get_gid_object())
325 sites = shell.GetSites(pl_auth)
327 import_site(level1_auth, site)
329 if __name__ == "__main__":