ce43c44257b6c8939223877b4f0d9b25dab0ba78
[sfa.git] / plc / import.py
1 ##
2 # Import PLC records into the Geni database. It is indended that this tool be
3 # run once to create Geni records that reflect the current state of the
4 # planetlab database.
5 #
6 # The import tool assumes that the existing PLC hierarchy should all be part
7 # of "planetlab.us" (see the root_auth and level1_auth variables below).
8 #
9 # Public keys are extracted from the users' SSH keys automatically and used to
10 # create GIDs. This is relatively experimental as a custom tool had to be
11 # written to perform conversion from SSH to OpenSSL format. It only supports
12 # RSA keys at this time, not DSA keys.
13 ##
14
15 import getopt
16 import sys
17 import tempfile
18
19 from cert import *
20 from trustedroot import *
21 from hierarchy import *
22 from record import *
23 from genitable import *
24 from misc import *
25
26 shell = None
27
28 ##
29 # Two authorities are specified: the root authority and the level1 authority.
30
31 root_auth = "planetlab"
32 level1_auth = "planetlab.us"
33
34 def process_options():
35    global hrn
36
37    (options, args) = getopt.getopt(sys.argv[1:], '', [])
38    for opt in options:
39        name = opt[0]
40        val = opt[1]
41
42 def connect_shell():
43     global pl_auth, shell
44
45     # get PL account settings from config module
46     pl_auth = get_pl_auth()
47
48     # connect to planetlab
49     if "Url" in pl_auth:
50         import remoteshell
51         shell = remoteshell.RemoteShell()
52     else:
53         import PLC.Shell
54         shell = PLC.Shell.Shell(globals = globals())
55
56 def get_auth_table(auth_name):
57     auth_info = AuthHierarchy.get_auth_info(auth_name)
58
59     table = GeniTable(hrn=auth_name,
60                       cninfo=auth_info.get_dbinfo())
61
62     # if the table doesn't exist, then it means we haven't put any records
63     # into this authority yet.
64
65     if not table.exists():
66         report.trace("Import: creating table for authority " + auth_name)
67         table.create()
68
69     return table
70
71 def get_pl_pubkey(key_id):
72     keys = shell.GetKeys(pl_auth, [key_id])
73     if keys:
74         key_str = keys[0]['key']
75
76         # generate temporary files to hold the keys
77         (ssh_f, ssh_fn) = tempfile.mkstemp()
78         ssl_fn = tempfile.mktemp()
79
80         os.write(ssh_f, key_str)
81         os.close(ssh_f)
82
83         cmd = "../keyconvert/keyconvert " + ssh_fn + " " + ssl_fn
84         print cmd
85         os.system(cmd)
86
87         # this check leaves the temporary file containing the public key so
88         # that it can be expected to see why it failed.
89         # TODO: for production, cleanup the temporary files
90         if not os.path.exists(ssl_fn):
91             report.trace("  failed to convert key from " + ssh_fn + " to " + ssl_fn)
92             return None
93
94         k = Keypair()
95         k.load_pubkey_from_file(ssl_fn)
96
97         #sys.exit(-1)
98
99         # remove the temporary files
100         os.remove(ssh_fn)
101         os.remove(ssl_fn)
102
103         return k
104     else:
105         return None
106
107 def person_to_hrn(parent_hrn, person):
108     personname = person['last_name'] + "_" + person['first_name']
109     personname = personname.replace(" ", "_")
110     personname = personname.replace(".", "_")
111     hrn = parent_hrn + "." + personname
112     return hrn
113
114 def import_person(parent_hrn, person):
115     hrn = person_to_hrn(parent_hrn, person)
116
117     report.trace("Import: importing person " + hrn)
118
119     table = get_auth_table(parent_hrn)
120
121     person_record = table.resolve("slice", hrn)
122     if not person_record:
123         #pkey = Keypair(create=True)
124         key_ids = person["key_ids"]
125         if key_ids:
126             pkey = get_pl_pubkey(key_ids[0])
127         else:
128             report.trace("   person " + hrn + " does not have a PL public key")
129             pkey = Keypair(create=True)
130         person_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
131         person_record = GeniRecord(name=hrn, gid=person_gid, type="user", pointer=person['person_id'])
132         report.trace("  inserting user record for " + hrn)
133         table.insert(person_record)
134
135 def import_slice(parent_hrn, slice):
136     slicename = slice['name'].split("_",1)[-1]
137
138     if not slicename:
139         report.error("Import_Slice: failed to parse slice name " + slice['name'])
140         return
141
142     hrn = parent_hrn + "." + slicename
143     report.trace("Import: importing slice " + hrn)
144
145     table = get_auth_table(parent_hrn)
146
147     slice_record = table.resolve("slice", hrn)
148     if not slice_record:
149         pkey = Keypair(create=True)
150         slice_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
151         slice_record = GeniRecord(name=hrn, gid=slice_gid, type="slice", pointer=slice['slice_id'])
152         report.trace("  inserting slice record for " + hrn)
153         table.insert(slice_record)
154
155 def import_site(parent_hrn, site):
156     hrn = parent_hrn + "." + site['login_base']
157
158     report.trace("Import_Site: importing site " + hrn)
159
160     # create the authority
161     if not AuthHierarchy.auth_exists(hrn):
162         AuthHierarchy.create_auth(hrn)
163
164     auth_info = AuthHierarchy.get_auth_info(hrn)
165
166     table = get_auth_table(parent_hrn)
167
168     sa_record = table.resolve("sa", hrn)
169     if not sa_record:
170         sa_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="sa", pointer=site['site_id'])
171         report.trace("  inserting sa record for " + hrn)
172         table.insert(sa_record)
173
174     ma_record = table.resolve("ma", hrn)
175     if not ma_record:
176         ma_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="ma", pointer=site['site_id'])
177         report.trace("  inserting ma record for " + hrn)
178         table.insert(ma_record)
179
180     for person_id in site['person_ids']:
181         persons = shell.GetPersons(pl_auth, [person_id])
182         if persons:
183             import_person(hrn, persons[0])
184
185     for slice_id in site['slice_ids']:
186         slices = shell.GetSlices(pl_auth, [slice_id])
187         if slices:
188             import_slice(hrn, slices[0])
189
190 def create_top_level_auth_records(hrn):
191     parent_hrn = get_authority(hrn)
192
193     auth_info = AuthHierarchy.get_auth_info(parent_hrn)
194     table = get_auth_table(parent_hrn)
195
196     sa_record = table.resolve("sa", hrn)
197     if not sa_record:
198         sa_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="sa", pointer=-1)
199         report.trace("  inserting sa record for " + hrn)
200         table.insert(sa_record)
201
202     ma_record = table.resolve("ma", hrn)
203     if not ma_record:
204         ma_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="ma", pointer=-1)
205         report.trace("  inserting ma record for " + hrn)
206         table.insert(ma_record)
207
208 def main():
209     global AuthHierarchy
210     global TrustedRoots
211
212     process_options()
213
214     AuthHierarchy = Hierarchy()
215     TrustedRoots = TrustedRootList()
216
217     print "Import: creating top level authorities"
218
219     if not AuthHierarchy.auth_exists(root_auth):
220         AuthHierarchy.create_auth(root_auth)
221     #create_top_level_auth_records(root_auth)
222     if not AuthHierarchy.auth_exists(level1_auth):
223         AuthHierarchy.create_auth(level1_auth)
224     create_top_level_auth_records(level1_auth)
225
226     print "Import: adding", root_auth, "to trusted list"
227     root = AuthHierarchy.get_auth_info(root_auth)
228     TrustedRoots.add_gid(root.get_gid_object())
229
230     connect_shell()
231
232     sites = shell.GetSites(pl_auth)
233     for site in sites:
234         import_site(level1_auth, site)
235
236 if __name__ == "__main__":
237     main()