7 # Import PLC records into the Geni database. It is indended that this tool be
8 # run once to create Geni records that reflect the current state of the
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).
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.
24 from sfa.util.record import *
25 from sfa.util.genitable import GeniTable
26 from sfa.util.misc import *
27 from sfa.util.config import Config
28 from sfa.util.report import trace, error
30 from sfa.trust.certificate import convert_public_key, Keypair
31 from sfa.trust.trustedroot import *
32 from sfa.trust.hierarchy import *
33 from sfa.trust.gid import create_uuid
37 # get PL account settings from config module
38 plc_auth = config.get_plc_auth()
44 # connect to planetlab
46 from sfa.plc.remoteshell import RemoteShell
50 shell = PLC.Shell.Shell(globals = globals())
53 # connect to planetlab
54 shell = connect_shell()
57 # Two authorities are specified: the root authority and the level1 authority.
62 #root_auth = "planetlab"
63 #level1_auth = "planetlab.us"
65 root_auth = config.SFA_REGISTRY_ROOT_AUTH
66 level1_auth = config.SFA_REGISTRY_LEVEL1_AUTH
67 if not level1_auth or level1_auth in ['']:
71 if isinstance(str, unicode):
72 return str.encode("ascii", "ignore")
76 def cleanup_string(str):
77 # pgsql has a fit with strings that have high ascii in them, so filter it
78 # out when generating the hrns.
86 str = str.replace(" ", "_")
87 str = str.replace(".", "_")
88 str = str.replace("(", "_")
89 str = str.replace("'", "_")
90 str = str.replace(")", "_")
91 str = str.replace('"', "_")
94 def process_options():
97 (options, args) = getopt.getopt(sys.argv[1:], '', [])
102 def get_auth_table(auth_name):
103 AuthHierarchy = Hierarchy()
104 auth_info = AuthHierarchy.get_auth_info(auth_name)
106 table = GeniTable(hrn=auth_name,
107 cninfo=auth_info.get_dbinfo())
109 # if the table doesn't exist, then it means we haven't put any records
110 # into this authority yet.
112 if not table.exists():
113 trace("Import: creating table for authority " + auth_name)
118 def person_to_hrn(parent_hrn, person):
119 # the old way - Lastname_Firstname
120 #personname = person['last_name'] + "_" + person['first_name']
122 # the new way - use email address up to the "@"
123 personname = person['email'].split("@")[0]
125 personname = cleanup_string(personname)
127 hrn = parent_hrn + "." + personname
130 def import_person(parent_hrn, person):
131 AuthHierarchy = Hierarchy()
132 hrn = person_to_hrn(parent_hrn, person)
134 # ASN.1 will have problems with hrn's longer than 64 characters
138 trace("Import: importing person " + hrn)
140 table = get_auth_table(parent_hrn)
143 if 'key_ids' in person:
144 key_ids = person["key_ids"]
146 # get the user's private key from the SSH keys they have uploaded
148 keys = shell.GetKeys(plc_auth, key_ids)
150 pkey =convert_public_key(key)
152 # the user has no keys
153 trace(" person " + hrn + " does not have a PL public key")
155 # if a key is unavailable, then we still need to put something in the
156 # user's GID. So make one up.
157 pkey = Keypair(create=True)
160 person_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
161 person_record = table.resolve("user", hrn)
162 if not person_record:
163 trace(" inserting user record for " + hrn)
164 person_record = GeniRecord(hrn=hrn, gid=person_gid, type="user", pointer=person['person_id'])
165 table.insert(person_record)
167 trace(" updating user record for " + hrn)
168 person_record = GeniRecord(hrn=hrn, gid=person_gid, type="user", pointer=person['person_id'])
169 table.update(person_record)
171 def import_slice(parent_hrn, slice):
172 AuthHierarchy = Hierarchy()
173 slicename = slice['name'].split("_",1)[-1]
174 slicename = cleanup_string(slicename)
177 error("Import_Slice: failed to parse slice name " + slice['name'])
180 hrn = parent_hrn + "." + slicename
181 trace("Import: importing slice " + hrn)
183 table = get_auth_table(parent_hrn)
185 slice_record = table.resolve("slice", hrn)
187 pkey = Keypair(create=True)
188 slice_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
189 slice_record = GeniRecord(hrn=hrn, gid=slice_gid, type="slice", pointer=slice['slice_id'])
190 trace(" inserting slice record for " + hrn)
191 table.insert(slice_record)
193 def import_node(parent_hrn, node):
194 AuthHierarchy = Hierarchy()
195 nodename = node['hostname'].split(".")[0]
196 nodename = cleanup_string(nodename)
199 error("Import_node: failed to parse node name " + node['hostname'])
202 hrn = parent_hrn + "." + nodename
204 # ASN.1 will have problems with hrn's longer than 64 characters
208 trace("Import: importing node " + hrn)
210 table = get_auth_table(parent_hrn)
212 node_record = table.resolve("node", hrn)
214 pkey = Keypair(create=True)
215 node_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
216 node_record = GeniRecord(hrn=hrn, gid=node_gid, type="node", pointer=node['node_id'])
217 trace(" inserting node record for " + hrn)
218 table.insert(node_record)
220 def import_site(parent_hrn, site):
221 AuthHierarchy = Hierarchy()
222 sitename = site['login_base']
223 sitename = cleanup_string(sitename)
225 hrn = parent_hrn + "." + sitename
227 # Hardcode 'internet2' into the hrn for sites hosting
228 # internet2 nodes. This is a special operation for some vini
230 if ".vini" in parent_hrn and parent_hrn.endswith('vini'):
231 if sitename.startswith("ii"):
232 sitename = sitename.replace("ii", "")
233 hrn = ".".join([parent_hrn, "internet2", sitename])
234 elif sitename.startswith("nlr"):
235 hrn = ".".join([parent_hrn, "internet2", sitename])
236 sitename = sitename.replace("nlr", "")
238 trace("Import_Site: importing site " + hrn)
240 # create the authority
241 if not AuthHierarchy.auth_exists(hrn):
242 AuthHierarchy.create_auth(hrn)
244 auth_info = AuthHierarchy.get_auth_info(hrn)
246 table = get_auth_table(parent_hrn)
248 auth_record = table.resolve("authority", hrn)
250 auth_record = GeniRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=site['site_id'])
251 trace(" inserting authority record for " + hrn)
252 table.insert(auth_record)
254 if 'person_ids' in site:
255 for person_id in site['person_ids']:
256 persons = shell.GetPersons(plc_auth, [person_id])
259 import_person(hrn, persons[0])
261 trace("Failed to import: %s (%s)" % (persons[0], e))
262 if 'slice_ids' in site:
263 for slice_id in site['slice_ids']:
264 slices = shell.GetSlices(plc_auth, [slice_id])
267 import_slice(hrn, slices[0])
269 trace("Failed to import: %s (%s)" % (slices[0], e))
270 if 'node_ids' in site:
271 for node_id in site['node_ids']:
272 nodes = shell.GetNodes(plc_auth, [node_id])
275 import_node(hrn, nodes[0])
277 trace("Failed to import: %s (%s)" % (nodes[0], e))
279 def create_top_level_auth_records(hrn):
280 parent_hrn = get_authority(hrn)
281 print hrn, ":", parent_hrn
284 auth_info = AuthHierarchy.get_auth_info(parent_hrn)
285 table = get_auth_table(parent_hrn)
287 auth_record = table.resolve("authority", hrn)
289 auth_record = GeniRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=-1)
290 trace(" inserting authority record for " + hrn)
291 table.insert(auth_record)
299 AuthHierarchy = Hierarchy()
300 TrustedRoots = TrustedRootList()
302 print "Import: creating top level authorities"
304 if not AuthHierarchy.auth_exists(root_auth):
305 AuthHierarchy.create_auth(root_auth)
307 create_top_level_auth_records(root_auth)
309 if not AuthHierarchy.auth_exists(level1_auth):
310 AuthHierarchy.create_auth(level1_auth)
311 create_top_level_auth_records(level1_auth)
312 import_auth = level1_auth
314 import_auth = root_auth
316 print "Import: adding", root_auth, "to trusted list"
317 root = AuthHierarchy.get_auth_info(root_auth)
318 TrustedRoots.add_gid(root.get_gid_object())
320 sites = shell.GetSites(plc_auth, {'peer_id': None})
321 # create a fake internet2 site first
322 i2site = {'name': 'Internet2', 'abbreviated_name': 'I2',
323 'login_base': 'internet2', 'site_id': -1}
324 import_site(import_auth, i2site)
327 import_site(import_auth, site)
329 if __name__ == "__main__":