2 # The import tool assumes that the existing PLC hierarchy should all be part
3 # of "planetlab.us" (see the root_auth and level1_auth variables below).
5 # Public keys are extracted from the users' SSH keys automatically and used to
6 # create GIDs. This is relatively experimental as a custom tool had to be
7 # written to perform conversion from SSH to OpenSSL format. It only supports
8 # RSA keys at this time, not DSA keys.
15 from sfa.util.record import *
16 from sfa.util.table import SfaTable
17 from sfa.util.namespace import *
18 from sfa.util.config import Config
19 from sfa.util.report import trace, error
20 from sfa.trust.certificate import convert_public_key, Keypair
21 from sfa.trust.trustedroot import *
22 from sfa.trust.hierarchy import *
23 from sfa.trust.gid import create_uuid
27 if isinstance(str, unicode):
28 return str.encode("ascii", "ignore")
32 def cleanup_string(str):
33 # pgsql has a fit with strings that have high ascii in them, so filter it
34 # out when generating the hrns.
42 str = str.replace(" ", "_")
43 str = str.replace(".", "_")
44 str = str.replace("(", "_")
45 str = str.replace("'", "_")
46 str = str.replace(")", "_")
47 str = str.replace('"', "_")
52 def __init__(self, logger=None):
54 self.AuthHierarchy = Hierarchy()
55 self.config = Config()
56 self.TrustedRoots = TrustedRootList(Config.get_trustedroots_dir(self.config))
57 self.plc_auth = self.config.get_plc_auth()
58 self.root_auth = self.config.SFA_REGISTRY_ROOT_AUTH
60 # connect to planetlab
62 if "Url" in self.plc_auth:
63 from sfa.plc.remoteshell import RemoteShell
64 self.shell = RemoteShell()
67 self.shell = PLC.Shell.Shell(globals = globals())
70 def create_top_level_auth_records(self, hrn):
71 # create the authority if it doesnt already exist
72 AuthHierarchy = self.AuthHierarchy
73 urn = hrn_to_urn(hrn, 'authority')
74 if not AuthHierarchy.auth_exists(urn):
75 trace("Import: creating top level authorites", self.logger)
76 AuthHierarchy.create_auth(urn)
77 parent_hrn = get_authority(hrn)
80 auth_info = AuthHierarchy.get_auth_info(parent_hrn)
82 # create the db record if it doesnt already exist
84 auth_record = table.find({'type': 'authority', 'hrn': hrn})
87 auth_record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=-1)
88 auth_record['authority'] = get_authority(auth_record['hrn'])
89 trace("Import: inserting authority record for " + hrn, self.logger)
90 table.insert(auth_record)
93 def import_person(self, parent_hrn, person):
94 AuthHierarchy = self.AuthHierarchy
95 hrn = email_to_hrn(parent_hrn, person['email'])
97 # ASN.1 will have problems with hrn's longer than 64 characters
101 trace("Import: importing person " + hrn, self.logger)
103 if 'key_ids' in person and person['key_ids']:
104 key_ids = person["key_ids"]
105 # get the user's private key from the SSH keys they have uploaded
107 keys = self.shell.GetKeys(self.plc_auth, key_ids)
109 pkey = convert_public_key(key)
111 pkey = Keypair(create=True)
113 # the user has no keys
114 trace(" person " + hrn + " does not have a PL public key", self.logger)
115 # if a key is unavailable, then we still need to put something in the
116 # user's GID. So make one up.
117 pkey = Keypair(create=True)
120 urn = hrn_to_urn(hrn, 'user')
121 person_gid = AuthHierarchy.create_gid(urn, create_uuid(), pkey)
123 person_record = SfaRecord(hrn=hrn, gid=person_gid, type="user", pointer=person['person_id'])
124 person_record['authority'] = get_authority(person_record['hrn'])
125 existing_records = table.find({'hrn': hrn, 'type': 'user', 'pointer': person['person_id']})
126 if not existing_records:
127 table.insert(person_record)
129 trace("Import: %s exists, updating " % hrn, self.logger)
130 existing_record = existing_records[0]
131 person_record['record_id'] = existing_record['record_id']
132 table.update(person_record)
134 def import_slice(self, parent_hrn, slice):
135 AuthHierarchy = self.AuthHierarchy
136 slicename = slice['name'].split("_",1)[-1]
137 slicename = cleanup_string(slicename)
140 error("Import_Slice: failed to parse slice name " + slice['name'], self.logger)
143 hrn = parent_hrn + "." + slicename
144 trace("Import: importing slice " + hrn, self.logger)
146 pkey = Keypair(create=True)
147 urn = hrn_to_urn(hrn, 'slice')
148 slice_gid = AuthHierarchy.create_gid(urn, create_uuid(), pkey)
149 slice_record = SfaRecord(hrn=hrn, gid=slice_gid, type="slice", pointer=slice['slice_id'])
150 slice_record['authority'] = get_authority(slice_record['hrn'])
152 existing_records = table.find({'hrn': hrn, 'type': 'slice', 'pointer': slice['slice_id']})
153 if not existing_records:
154 table.insert(slice_record)
156 trace("Import: %s exists, updating " % hrn, self.logger)
157 existing_record = existing_records[0]
158 slice_record['record_id'] = existing_record['record_id']
159 table.update(slice_record)
161 def import_node(self, parent_hrn, node):
162 AuthHierarchy = self.AuthHierarchy
163 nodename = node['hostname'].split(".")[0]
164 nodename = cleanup_string(nodename)
167 error("Import_node: failed to parse node name " + node['hostname'], self.logger)
170 hrn = parent_hrn + "." + nodename
171 trace("Import: importing node " + hrn, self.logger)
172 # ASN.1 will have problems with hrn's longer than 64 characters
177 node_record = table.find({'type': 'node', 'hrn': hrn})
178 pkey = Keypair(create=True)
179 urn = hrn_to_urn(hrn, 'node')
180 node_gid = AuthHierarchy.create_gid(urn, create_uuid(), pkey)
181 node_record = SfaRecord(hrn=hrn, gid=node_gid, type="node", pointer=node['node_id'])
182 node_record['authority'] = get_authority(node_record['hrn'])
183 existing_records = table.find({'hrn': hrn, 'type': 'node', 'pointer': node['node_id']})
184 if not existing_records:
185 table.insert(node_record)
187 trace("Import: %s exists, updating " % hrn, self.logger)
188 existing_record = existing_records[0]
189 node_record['record_id'] = existing_record['record_id']
190 table.update(node_record)
193 def import_site(self, parent_hrn, site):
194 AuthHierarchy = self.AuthHierarchy
196 plc_auth = self.plc_auth
197 sitename = site['login_base']
198 sitename = cleanup_string(sitename)
199 hrn = parent_hrn + "." + sitename
200 urn = hrn_to_urn(hrn, 'authority')
201 # Hardcode 'internet2' into the hrn for sites hosting
202 # internet2 nodes. This is a special operation for some vini
204 if ".vini" in parent_hrn and parent_hrn.endswith('vini'):
205 if sitename.startswith("i2"):
206 #sitename = sitename.replace("ii", "")
207 hrn = ".".join([parent_hrn, "internet2", sitename])
208 elif sitename.startswith("nlr"):
209 #sitename = sitename.replace("nlr", "")
210 hrn = ".".join([parent_hrn, "internet2", sitename])
212 trace("Import: importing site " + hrn, self.logger)
214 # create the authority
215 if not AuthHierarchy.auth_exists(urn):
216 AuthHierarchy.create_auth(urn)
218 auth_info = AuthHierarchy.get_auth_info(urn)
221 auth_record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=site['site_id'])
222 auth_record['authority'] = get_authority(auth_record['hrn'])
223 existing_records = table.find({'hrn': hrn, 'type': 'authority', 'pointer': site['site_id']})
224 if not existing_records:
225 table.insert(auth_record)
227 trace("Import: %s exists, updating " % hrn, self.logger)
228 existing_record = existing_records[0]
229 auth_record['record_id'] = existing_record['record_id']
230 table.update(auth_record)
235 def delete_record(self, hrn, type):
238 record_list = table.find({'type': type, 'hrn': hrn})
239 for record in record_list:
240 trace("Import: Removing record %s %s" % (type, hrn), self.logger)