rename directory
[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 import_person(parent_hrn, person):
108     personname = person['last_name'] + "_" + person['first_name']
109
110     hrn = parent_hrn + "." + personname
111     report.trace("Import: importing person " + hrn)
112
113     table = get_auth_table(parent_hrn)
114
115     person_record = table.resolve("slice", hrn)
116     if not person_record:
117         #pkey = Keypair(create=True)
118         key_ids = person["key_ids"]
119         if key_ids:
120             pkey = get_pl_pubkey(key_ids[0])
121         else:
122             report.trace("   person " + hrn + " does not have a PL public key")
123             pkey = Keypair(create=True)
124         person_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
125         person_record = GeniRecord(name=hrn, gid=person_gid, type="user", pointer=person['person_id'])
126         report.trace("  inserting user record for " + hrn)
127         table.insert(person_record)
128
129 def import_slice(parent_hrn, slice):
130     slicename = slice['name'].split("_",1)[-1]
131
132     if not slicename:
133         report.error("Import_Slice: failed to parse slice name " + slice['name'])
134         return
135
136     hrn = parent_hrn + "." + slicename
137     report.trace("Import: importing slice " + hrn)
138
139     table = get_auth_table(parent_hrn)
140
141     slice_record = table.resolve("slice", hrn)
142     if not slice_record:
143         pkey = Keypair(create=True)
144         slice_gid = AuthHierarchy.create_gid(hrn, create_uuid(), pkey)
145         slice_record = GeniRecord(name=hrn, gid=slice_gid, type="slice", pointer=slice['slice_id'])
146         report.trace("  inserting slice record for " + hrn)
147         table.insert(slice_record)
148
149 def import_site(parent_hrn, site):
150     hrn = parent_hrn + "." + site['login_base']
151
152     report.trace("Import_Site: importing site " + hrn)
153
154     # create the authority
155     if not AuthHierarchy.auth_exists(hrn):
156         AuthHierarchy.create_auth(hrn)
157
158     auth_info = AuthHierarchy.get_auth_info(hrn)
159
160     table = get_auth_table(parent_hrn)
161
162     sa_record = table.resolve("sa", hrn)
163     if not sa_record:
164         sa_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="sa", pointer=site['site_id'])
165         report.trace("  inserting sa record for " + hrn)
166         table.insert(sa_record)
167
168     ma_record = table.resolve("ma", hrn)
169     if not ma_record:
170         ma_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="ma", pointer=site['site_id'])
171         report.trace("  inserting ma record for " + hrn)
172         table.insert(ma_record)
173
174     for person_id in site['person_ids']:
175         persons = shell.GetPersons(pl_auth, [person_id])
176         if persons:
177             import_person(hrn, persons[0])
178
179     for slice_id in site['slice_ids']:
180         slices = shell.GetSlices(pl_auth, [slice_id])
181         if slices:
182             import_slice(hrn, slices[0])
183
184 def create_top_level_auth_records(hrn):
185     parent_hrn = get_authority(hrn)
186
187     auth_info = AuthHierarchy.get_auth_info(parent_hrn)
188     table = get_auth_table(parent_hrn)
189
190     sa_record = table.resolve("sa", hrn)
191     if not sa_record:
192         sa_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="sa", pointer=-1)
193         report.trace("  inserting sa record for " + hrn)
194         table.insert(sa_record)
195
196     ma_record = table.resolve("ma", hrn)
197     if not ma_record:
198         ma_record = GeniRecord(name=hrn, gid=auth_info.get_gid_object(), type="ma", pointer=-1)
199         report.trace("  inserting ma record for " + hrn)
200         table.insert(ma_record)
201
202 def main():
203     global AuthHierarchy
204     global TrustedRoots
205
206     process_options()
207
208     AuthHierarchy = Hierarchy()
209     TrustedRoots = TrustedRootList()
210
211     print "Import: creating top level authorities"
212
213     if not AuthHierarchy.auth_exists(root_auth):
214         AuthHierarchy.create_auth(root_auth)
215     #create_top_level_auth_records(root_auth)
216     if not AuthHierarchy.auth_exists(level1_auth):
217         AuthHierarchy.create_auth(level1_auth)
218     create_top_level_auth_records(level1_auth)
219
220     print "Import: adding", root_auth, "to trusted list"
221     root = AuthHierarchy.get_auth_info(root_auth)
222     TrustedRoots.add_gid(root.get_gid_object())
223
224     connect_shell()
225
226     sites = shell.GetSites(pl_auth)
227     for site in sites:
228         import_site(level1_auth, site)
229
230 if __name__ == "__main__":
231     main()