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.sfalogging import sfa_logger_goes_to_import,sfa_logger
17 from sfa.util.record import *
18 from sfa.util.table import SfaTable
19 from sfa.util.namespace import *
20 from sfa.util.config import Config
21 from sfa.trust.certificate import convert_public_key, Keypair
22 from sfa.trust.trustedroot import *
23 from sfa.trust.hierarchy import *
24 from sfa.trust.gid import create_uuid
28 if isinstance(str, unicode):
29 return str.encode("ascii", "ignore")
33 def _cleanup_string(str):
34 # pgsql has a fit with strings that have high ascii in them, so filter it
35 # out when generating the hrns.
42 str = _un_unicode(str)
43 str = str.replace(" ", "_")
44 str = str.replace(".", "_")
45 str = str.replace("(", "_")
46 str = str.replace("'", "_")
47 str = str.replace(")", "_")
48 str = str.replace('"', "_")
54 sfa_logger_goes_to_import()
55 self.logger = sfa_logger()
56 self.AuthHierarchy = Hierarchy()
57 self.config = Config()
58 self.TrustedRoots = TrustedRootList(Config.get_trustedroots_dir(self.config))
59 self.plc_auth = self.config.get_plc_auth()
60 self.root_auth = self.config.SFA_REGISTRY_ROOT_AUTH
62 # connect to planetlab
64 if "Url" in self.plc_auth:
65 from sfa.plc.remoteshell import RemoteShell
66 self.shell = RemoteShell(self.logger)
69 self.shell = PLC.Shell.Shell(globals = globals())
72 def create_top_level_auth_records(self, hrn):
73 urn = hrn_to_urn(hrn, 'authority')
74 # make sure parent exists
75 parent_hrn = get_authority(hrn)
78 if not parent_hrn == hrn:
79 self.create_top_level_auth_records(parent_hrn)
81 # create the authority if it doesnt already exist
82 if not self.AuthHierarchy.auth_exists(urn):
83 self.logger.info("Import: creating top level authorities")
84 self.AuthHierarchy.create_auth(urn)
86 # create the db record if it doesnt already exist
87 auth_info = self.AuthHierarchy.get_auth_info(hrn)
89 auth_record = table.find({'type': 'authority', 'hrn': hrn})
92 auth_record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=-1)
93 auth_record['authority'] = get_authority(auth_record['hrn'])
94 self.logger.info("Import: inserting authority record for " + hrn)
95 table.insert(auth_record)
98 def import_person(self, parent_hrn, person):
99 hrn = email_to_hrn(parent_hrn, person['email'])
101 # ASN.1 will have problems with hrn's longer than 64 characters
105 self.logger.info("Import: person " + hrn)
107 if 'key_ids' in person and person['key_ids']:
108 key_ids = person["key_ids"]
109 # get the user's private key from the SSH keys they have uploaded
111 keys = self.shell.GetKeys(self.plc_auth, key_ids)
113 pkey = convert_public_key(key)
115 pkey = Keypair(create=True)
117 # the user has no keys
118 self.logger.warning("Import: person %s does not have a PL public key"%hrn)
119 # if a key is unavailable, then we still need to put something in the
120 # user's GID. So make one up.
121 pkey = Keypair(create=True)
124 urn = hrn_to_urn(hrn, 'user')
125 person_gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
127 person_record = SfaRecord(hrn=hrn, gid=person_gid, type="user", pointer=person['person_id'])
128 person_record['authority'] = get_authority(person_record['hrn'])
129 existing_records = table.find({'hrn': hrn, 'type': 'user', 'pointer': person['person_id']})
130 if not existing_records:
131 table.insert(person_record)
133 self.logger.info("Import: %s exists, updating " % hrn)
134 existing_record = existing_records[0]
135 person_record['record_id'] = existing_record['record_id']
136 table.update(person_record)
138 def import_slice(self, parent_hrn, slice):
139 slicename = slice['name'].split("_",1)[-1]
140 slicename = _cleanup_string(slicename)
143 self.logger.error("Import: failed to parse slice name " + slice['name'])
146 hrn = parent_hrn + "." + slicename
147 self.logger.info("Import: slice " + hrn)
149 pkey = Keypair(create=True)
150 urn = hrn_to_urn(hrn, 'slice')
151 slice_gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
152 slice_record = SfaRecord(hrn=hrn, gid=slice_gid, type="slice", pointer=slice['slice_id'])
153 slice_record['authority'] = get_authority(slice_record['hrn'])
155 existing_records = table.find({'hrn': hrn, 'type': 'slice', 'pointer': slice['slice_id']})
156 if not existing_records:
157 table.insert(slice_record)
159 self.logger.info("Import: %s exists, updating " % hrn)
160 existing_record = existing_records[0]
161 slice_record['record_id'] = existing_record['record_id']
162 table.update(slice_record)
164 def import_node(self, parent_hrn, node):
165 nodename = node['hostname'].split(".")[0]
166 nodename = _cleanup_string(nodename)
169 self.logger.error("Import: failed to parse node name " + node['hostname'])
172 hrn = parent_hrn + "." + nodename
173 self.logger.info("Import: node %s" % hrn)
174 # ASN.1 will have problems with hrn's longer than 64 characters
179 node_record = table.find({'type': 'node', 'hrn': hrn})
180 pkey = Keypair(create=True)
181 urn = hrn_to_urn(hrn, 'node')
182 node_gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
183 node_record = SfaRecord(hrn=hrn, gid=node_gid, type="node", pointer=node['node_id'])
184 node_record['authority'] = get_authority(node_record['hrn'])
185 existing_records = table.find({'hrn': hrn, 'type': 'node', 'pointer': node['node_id']})
186 if not existing_records:
187 table.insert(node_record)
189 self.logger.info("Import: %s exists, updating " % hrn)
190 existing_record = existing_records[0]
191 node_record['record_id'] = existing_record['record_id']
192 table.update(node_record)
195 def import_site(self, parent_hrn, site):
197 plc_auth = self.plc_auth
198 sitename = site['login_base']
199 sitename = _cleanup_string(sitename)
200 hrn = parent_hrn + "." + sitename
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 urn = hrn_to_urn(hrn, 'authority')
213 self.logger.info("Import: site " + hrn)
215 # create the authority
216 if not self.AuthHierarchy.auth_exists(urn):
217 self.AuthHierarchy.create_auth(urn)
219 auth_info = self.AuthHierarchy.get_auth_info(urn)
222 auth_record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=site['site_id'])
223 auth_record['authority'] = get_authority(auth_record['hrn'])
224 existing_records = table.find({'hrn': hrn, 'type': 'authority', 'pointer': site['site_id']})
225 if not existing_records:
226 table.insert(auth_record)
228 self.logger.info("Import: %s exists, updating " % hrn)
229 existing_record = existing_records[0]
230 auth_record['record_id'] = existing_record['record_id']
231 table.update(auth_record)
236 def delete_record(self, hrn, type):
239 record_list = table.find({'type': type, 'hrn': hrn})
240 for record in record_list:
241 self.logger.info("Import: removing record %s %s" % (type, hrn))