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.xrn import get_authority, hrn_to_urn
20 from sfa.util.plxrn import email_to_hrn
21 from sfa.util.config import Config
22 from sfa.trust.certificate import convert_public_key, Keypair
23 from sfa.trust.trustedroot import *
24 from sfa.trust.hierarchy import *
25 from sfa.trust.gid import create_uuid
29 if isinstance(str, unicode):
30 return str.encode("ascii", "ignore")
34 def _cleanup_string(str):
35 # pgsql has a fit with strings that have high ascii in them, so filter it
36 # out when generating the hrns.
43 str = _un_unicode(str)
44 str = str.replace(" ", "_")
45 str = str.replace(".", "_")
46 str = str.replace("(", "_")
47 str = str.replace("'", "_")
48 str = str.replace(")", "_")
49 str = str.replace('"', "_")
55 sfa_logger_goes_to_import()
56 self.logger = sfa_logger()
57 self.AuthHierarchy = Hierarchy()
58 self.config = Config()
59 self.TrustedRoots = TrustedRootList(Config.get_trustedroots_dir(self.config))
60 self.plc_auth = self.config.get_plc_auth()
61 self.root_auth = self.config.SFA_REGISTRY_ROOT_AUTH
63 # connect to planetlab
65 if "Url" in self.plc_auth:
66 from sfa.plc.remoteshell import RemoteShell
67 self.shell = RemoteShell(self.logger)
70 self.shell = PLC.Shell.Shell(globals = globals())
73 def create_top_level_auth_records(self, hrn):
74 urn = hrn_to_urn(hrn, 'authority')
75 # make sure parent exists
76 parent_hrn = get_authority(hrn)
79 if not parent_hrn == hrn:
80 self.create_top_level_auth_records(parent_hrn)
82 # create the authority if it doesnt already exist
83 if not self.AuthHierarchy.auth_exists(urn):
84 self.logger.info("Import: creating top level authorities")
85 self.AuthHierarchy.create_auth(urn)
87 # create the db record if it doesnt already exist
88 auth_info = self.AuthHierarchy.get_auth_info(hrn)
90 auth_record = table.find({'type': 'authority', 'hrn': hrn})
93 auth_record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=-1)
94 auth_record['authority'] = get_authority(auth_record['hrn'])
95 self.logger.info("Import: inserting authority record for " + hrn)
96 table.insert(auth_record)
99 def import_person(self, parent_hrn, person):
100 hrn = email_to_hrn(parent_hrn, person['email'])
102 # ASN.1 will have problems with hrn's longer than 64 characters
106 self.logger.info("Import: person " + hrn)
108 if 'key_ids' in person and person['key_ids']:
109 key_ids = person["key_ids"]
110 # get the user's private key from the SSH keys they have uploaded
112 keys = self.shell.GetKeys(self.plc_auth, key_ids)
114 pkey = convert_public_key(key)
116 pkey = Keypair(create=True)
118 # the user has no keys
119 self.logger.warning("Import: person %s does not have a PL public key"%hrn)
120 # if a key is unavailable, then we still need to put something in the
121 # user's GID. So make one up.
122 pkey = Keypair(create=True)
125 urn = hrn_to_urn(hrn, 'user')
126 person_gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
128 person_record = SfaRecord(hrn=hrn, gid=person_gid, type="user", pointer=person['person_id'])
129 person_record['authority'] = get_authority(person_record['hrn'])
130 existing_records = table.find({'hrn': hrn, 'type': 'user', 'pointer': person['person_id']})
131 if not existing_records:
132 table.insert(person_record)
134 self.logger.info("Import: %s exists, updating " % hrn)
135 existing_record = existing_records[0]
136 person_record['record_id'] = existing_record['record_id']
137 table.update(person_record)
139 def import_slice(self, parent_hrn, slice):
140 slicename = slice['name'].split("_",1)[-1]
141 slicename = _cleanup_string(slicename)
144 self.logger.error("Import: failed to parse slice name " + slice['name'])
147 hrn = parent_hrn + "." + slicename
148 self.logger.info("Import: slice " + hrn)
150 pkey = Keypair(create=True)
151 urn = hrn_to_urn(hrn, 'slice')
152 slice_gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
153 slice_record = SfaRecord(hrn=hrn, gid=slice_gid, type="slice", pointer=slice['slice_id'])
154 slice_record['authority'] = get_authority(slice_record['hrn'])
156 existing_records = table.find({'hrn': hrn, 'type': 'slice', 'pointer': slice['slice_id']})
157 if not existing_records:
158 table.insert(slice_record)
160 self.logger.info("Import: %s exists, updating " % hrn)
161 existing_record = existing_records[0]
162 slice_record['record_id'] = existing_record['record_id']
163 table.update(slice_record)
165 def import_node(self, parent_hrn, node):
166 nodename = node['hostname'].split(".")[0]
167 nodename = _cleanup_string(nodename)
170 self.logger.error("Import: failed to parse node name " + node['hostname'])
173 hrn = parent_hrn + "." + nodename
174 self.logger.info("Import: node %s" % hrn)
175 # ASN.1 will have problems with hrn's longer than 64 characters
180 node_record = table.find({'type': 'node', 'hrn': hrn})
181 pkey = Keypair(create=True)
182 urn = hrn_to_urn(hrn, 'node')
183 node_gid = self.AuthHierarchy.create_gid(urn, create_uuid(), pkey)
184 node_record = SfaRecord(hrn=hrn, gid=node_gid, type="node", pointer=node['node_id'])
185 node_record['authority'] = get_authority(node_record['hrn'])
186 existing_records = table.find({'hrn': hrn, 'type': 'node', 'pointer': node['node_id']})
187 if not existing_records:
188 table.insert(node_record)
190 self.logger.info("Import: %s exists, updating " % hrn)
191 existing_record = existing_records[0]
192 node_record['record_id'] = existing_record['record_id']
193 table.update(node_record)
196 def import_site(self, parent_hrn, site):
198 plc_auth = self.plc_auth
199 sitename = site['login_base']
200 sitename = _cleanup_string(sitename)
201 hrn = parent_hrn + "." + sitename
202 # Hardcode 'internet2' into the hrn for sites hosting
203 # internet2 nodes. This is a special operation for some vini
205 if ".vini" in parent_hrn and parent_hrn.endswith('vini'):
206 if sitename.startswith("i2"):
207 #sitename = sitename.replace("ii", "")
208 hrn = ".".join([parent_hrn, "internet2", sitename])
209 elif sitename.startswith("nlr"):
210 #sitename = sitename.replace("nlr", "")
211 hrn = ".".join([parent_hrn, "internet2", sitename])
213 urn = hrn_to_urn(hrn, 'authority')
214 self.logger.info("Import: site " + hrn)
216 # create the authority
217 if not self.AuthHierarchy.auth_exists(urn):
218 self.AuthHierarchy.create_auth(urn)
220 auth_info = self.AuthHierarchy.get_auth_info(urn)
223 auth_record = SfaRecord(hrn=hrn, gid=auth_info.get_gid_object(), type="authority", pointer=site['site_id'])
224 auth_record['authority'] = get_authority(auth_record['hrn'])
225 existing_records = table.find({'hrn': hrn, 'type': 'authority', 'pointer': site['site_id']})
226 if not existing_records:
227 table.insert(auth_record)
229 self.logger.info("Import: %s exists, updating " % hrn)
230 existing_record = existing_records[0]
231 auth_record['record_id'] = existing_record['record_id']
232 table.update(auth_record)
237 def delete_record(self, hrn, type):
240 record_list = table.find({'type': type, 'hrn': hrn})
241 for record in record_list:
242 self.logger.info("Import: removing record %s %s" % (type, hrn))