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.genitable import GeniTable
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
59 self.level1_auth = self.config.SFA_REGISTRY_LEVEL1_AUTH
60 if not self.level1_auth or self.level1_auth in ['']:
61 self.level1_auth = None
63 # connect to planetlab
65 if "Url" in self.plc_auth:
66 from sfa.plc.remoteshell import RemoteShell
67 self.shell = RemoteShell()
70 self.shell = PLC.Shell.Shell(globals = globals())
73 def create_top_level_auth_records(self, hrn):
74 AuthHierarchy = self.AuthHierarchy
76 # if auth records for this hrn dont exist, create it
77 if not AuthHierarchy.auth_exists(hrn):
78 trace("Import: creating top level authorites", self.logger)
79 AuthHierarchy.create_auth(hrn)
82 # get the auth info of the newly created root auth (parent)
83 # or level1_auth if it exists
85 auth_info = AuthHierarchy.get_auth_info(hrn)
88 parent_hrn = get_authority(hrn)
91 auth_info = AuthHierarchy.get_auth_info(parent_hrn)
94 auth_record = table.find({'type': 'authority', 'hrn': hrn})
97 auth_record = GeniRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=-1)
98 auth_record['authority'] = get_authority(auth_record['hrn'])
99 trace("Import: inserting authority record for " + hrn, self.logger)
100 table.insert(auth_record)
103 def import_person(self, parent_hrn, person):
104 AuthHierarchy = self.AuthHierarchy
105 hrn = email_to_hrn(parent_hrn, person['email'])
107 # ASN.1 will have problems with hrn's longer than 64 characters
111 trace("Import: importing person " + hrn, self.logger)
113 if 'key_ids' in person and person['key_ids']:
114 key_ids = person["key_ids"]
115 # get the user's private key from the SSH keys they have uploaded
117 keys = self.shell.GetKeys(self.plc_auth, key_ids)
119 pkey = convert_public_key(key)
121 pkey = Keypair(create=True)
123 # the user has no keys
124 trace(" person " + hrn + " does not have a PL public key", self.logger)
125 # if a key is unavailable, then we still need to put something in the
126 # user's GID. So make one up.
127 pkey = Keypair(create=True)
130 person_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
132 person_record = GeniRecord(hrn=hrn, gid=person_gid, type="user", pointer=person['person_id'])
133 person_record['authority'] = get_authority(person_record['hrn'])
134 existing_records = table.find({'hrn': hrn, 'type': 'user', 'pointer': person['person_id']})
135 if not existing_records:
136 table.insert(person_record)
138 trace("Import: %s exists, updating " % hrn, self.logger)
139 existing_record = existing_records[0]
140 person_record['record_id'] = existing_record['record_id']
141 table.update(person_record)
143 def import_slice(self, parent_hrn, slice):
144 AuthHierarchy = self.AuthHierarchy
145 slicename = slice['name'].split("_",1)[-1]
146 slicename = cleanup_string(slicename)
149 error("Import_Slice: failed to parse slice name " + slice['name'], self.logger)
152 hrn = parent_hrn + "." + slicename
153 trace("Import: importing slice " + hrn, self.logger)
155 pkey = Keypair(create=True)
156 slice_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
157 slice_record = GeniRecord(hrn=hrn, gid=slice_gid, type="slice", pointer=slice['slice_id'])
158 slice_record['authority'] = get_authority(slice_record['hrn'])
160 existing_records = table.find({'hrn': hrn, 'type': 'slice', 'pointer': slice['slice_id']})
161 if not existing_records:
162 table.insert(slice_record)
164 trace("Import: %s exists, updating " % hrn, self.logger)
165 existing_record = existing_records[0]
166 slice_record['record_id'] = existing_record['record_id']
167 table.update(slice_record)
169 def import_node(self, parent_hrn, node):
170 AuthHierarchy = self.AuthHierarchy
171 nodename = node['hostname'].split(".")[0]
172 nodename = cleanup_string(nodename)
175 error("Import_node: failed to parse node name " + node['hostname'], self.logger)
178 hrn = parent_hrn + "." + nodename
179 trace("Import: importing node " + hrn, self.logger)
180 # ASN.1 will have problems with hrn's longer than 64 characters
185 node_record = table.find({'type': 'node', 'hrn': hrn})
186 pkey = Keypair(create=True)
187 node_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
188 node_record = GeniRecord(hrn=hrn, gid=node_gid, type="node", pointer=node['node_id'])
189 node_record['authority'] = get_authority(node_record['hrn'])
190 existing_records = table.find({'hrn': hrn, 'type': 'node', 'pointer': node['node_id']})
191 if not existing_records:
192 table.insert(node_record)
194 trace("Import: %s exists, updating " % hrn, self.logger)
195 existing_record = existing_records[0]
196 node_record['record_id'] = existing_record['record_id']
197 table.update(node_record)
200 def import_site(self, parent_hrn, site):
201 AuthHierarchy = self.AuthHierarchy
203 plc_auth = self.plc_auth
204 sitename = site['login_base']
205 sitename = cleanup_string(sitename)
207 hrn = parent_hrn + "." + sitename
209 # Hardcode 'internet2' into the hrn for sites hosting
210 # internet2 nodes. This is a special operation for some vini
212 if ".vini" in parent_hrn and parent_hrn.endswith('vini'):
213 if sitename.startswith("i2"):
214 #sitename = sitename.replace("ii", "")
215 hrn = ".".join([parent_hrn, "internet2", sitename])
216 elif sitename.startswith("nlr"):
217 #sitename = sitename.replace("nlr", "")
218 hrn = ".".join([parent_hrn, "internet2", sitename])
220 trace("Import: importing site " + hrn, self.logger)
222 # create the authority
223 if not AuthHierarchy.auth_exists(hrn):
224 AuthHierarchy.create_auth(hrn)
226 auth_info = AuthHierarchy.get_auth_info(hrn)
229 auth_record = GeniRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=site['site_id'])
230 auth_record['authority'] = get_authority(auth_record['hrn'])
231 existing_records = table.find({'hrn': hrn, 'type': 'authority', 'pointer': site['site_id']})
232 if not existing_records:
233 table.insert(auth_record)
235 trace("Import: %s exists, updating " % hrn, self.logger)
236 existing_record = existing_records[0]
237 auth_record['record_id'] = existing_record['record_id']
238 table.update(auth_record)
243 def delete_record(self, hrn, type):
246 record_list = table.find({'type': type, 'hrn': hrn})
247 for record in record_list:
248 trace("Import: Removing record %s %s" % (type, hrn), self.logger)