1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | import sys, os |
---|
4 | import re |
---|
5 | import subprocess |
---|
6 | |
---|
7 | import ABAC |
---|
8 | import Creddy |
---|
9 | |
---|
10 | from string import join, ascii_letters |
---|
11 | from random import choice |
---|
12 | |
---|
13 | from deter import topdl |
---|
14 | from deter import fedid, generate_fedid |
---|
15 | |
---|
16 | from federation.proof import proof |
---|
17 | from federation.remote_service import service_caller |
---|
18 | from federation.client_lib import client_opts, exit_with_fault, RPCException, \ |
---|
19 | wrangle_standard_options, do_rpc, get_experiment_names, save_certfile,\ |
---|
20 | get_abac_certs, log_authentication |
---|
21 | from federation.util import abac_split_cert, abac_context_to_creds |
---|
22 | |
---|
23 | from xml.parsers.expat import ExpatError |
---|
24 | |
---|
25 | class fedd_create_opts(client_opts): |
---|
26 | """ |
---|
27 | Extra options that create needs. Help entries should explain them. |
---|
28 | """ |
---|
29 | def __init__(self): |
---|
30 | client_opts.__init__(self) |
---|
31 | self.add_option("--experiment_cert", dest="out_certfile", |
---|
32 | action="callback", callback=self.expand_file, |
---|
33 | type="string", help="output certificate file") |
---|
34 | self.add_option("--experiment_name", dest="exp_name", |
---|
35 | type="string", help="Suggested experiment name") |
---|
36 | self.add_option("--file", dest="file", action="callback", |
---|
37 | callback=self.expand_file, type="str", |
---|
38 | help="experiment description file") |
---|
39 | self.add_option("--project", action="store", dest="project", |
---|
40 | type="string", |
---|
41 | help="Project to export from master") |
---|
42 | self.add_option("--master", dest="master", |
---|
43 | help="Master testbed in the federation (pseudo project export)") |
---|
44 | self.add_option("--service", dest="service", action="append", |
---|
45 | type="string", default=[], |
---|
46 | help="Service description name:exporters:importers:attrs") |
---|
47 | self.add_option("--map", dest="map", action="append", |
---|
48 | type="string", default=[], |
---|
49 | help="Explicit map from testbed label to URI - " + \ |
---|
50 | "deter:https://users.isi.deterlab/net:13232") |
---|
51 | self.add_option('--gen_cert', action='store_true', dest='gen_cert', |
---|
52 | default=False, |
---|
53 | help='generate a cert to which to delegate rights') |
---|
54 | self.add_option('--delegate', action='store_true', dest='generate', |
---|
55 | help='Delegate rights to a generated cert (default)') |
---|
56 | self.add_option('--no-delegate', action='store_true', dest='generate', |
---|
57 | help='Do not delegate rights to a generated cert') |
---|
58 | |
---|
59 | self.set_defaults(delegate=True) |
---|
60 | |
---|
61 | def parse_service(svc): |
---|
62 | """ |
---|
63 | Pasre a service entry into a hash representing a service entry in a |
---|
64 | message. The format is: |
---|
65 | svc_name:exporter(s):importer(s):attr=val,attr=val |
---|
66 | The svc_name is teh service name, exporter is the exporting testbeds |
---|
67 | (comma-separated) importer is the importing testbeds (if any) and the rest |
---|
68 | are attr=val pairs that will become attributes of the service. These |
---|
69 | include parameters and other such stuff. |
---|
70 | """ |
---|
71 | |
---|
72 | terms = svc.split(':') |
---|
73 | svcd = { } |
---|
74 | if len(terms) < 2 or len(terms[0]) == 0 or len(terms[1]) == 0: |
---|
75 | sys.exit("Bad service description '%s': Not enough terms" % svc) |
---|
76 | |
---|
77 | svcd['name'] = terms[0] |
---|
78 | svcd['export'] = terms[1].split(",") |
---|
79 | if len(terms) > 2 and len(terms[2]) > 0: |
---|
80 | svcd['import'] = terms[2].split(",") |
---|
81 | if len(terms) > 3 and len(terms[3]) > 0: |
---|
82 | svcd['fedAttr'] = [ ] |
---|
83 | for t in terms[3].split(";"): |
---|
84 | i = t.find("=") |
---|
85 | if i != -1 : |
---|
86 | svcd['fedAttr'].append( |
---|
87 | {'attribute': t[0:i], 'value': t[i+1:]}) |
---|
88 | else: |
---|
89 | sys.exit("Bad service attribute '%s': no equals sign" % t) |
---|
90 | return svcd |
---|
91 | |
---|
92 | def project_export_service(master, project): |
---|
93 | """ |
---|
94 | Output the dict representation of a project_export service for this project |
---|
95 | and master. |
---|
96 | """ |
---|
97 | return { |
---|
98 | 'name': 'project_export', |
---|
99 | 'export': [master], |
---|
100 | 'importall': True, |
---|
101 | 'fedAttr': [ |
---|
102 | { 'attribute': 'project', 'value': project }, |
---|
103 | ], |
---|
104 | } |
---|
105 | |
---|
106 | def delegate(fedid, cert, dir, name=None, debug=False): |
---|
107 | ''' |
---|
108 | Make the creddy call to create an attribute delegating rights to the new |
---|
109 | experiment. The cert parameter points to a conventional cert & key combo, |
---|
110 | which we split out into tempfiles, which we delete on return. The return |
---|
111 | value if the filename in which the certificate was stored. |
---|
112 | ''' |
---|
113 | certfile = keyfile = None |
---|
114 | expid = "%s" % fedid |
---|
115 | |
---|
116 | # Trim the "fedid:" |
---|
117 | if expid.startswith("fedid:"): expid = expid[6:] |
---|
118 | |
---|
119 | try: |
---|
120 | keyfile, certfile = abac_split_cert(cert) |
---|
121 | |
---|
122 | rv = 0 |
---|
123 | if name: |
---|
124 | fn ='%s/%s_attr.der' % (dir, name) |
---|
125 | id_fn = '%s/%s_id.pem' % (dir, name) |
---|
126 | else: |
---|
127 | fn = '%s/%s_attr.der' % (dir, expid) |
---|
128 | id_fn = '%s/%s_id.pem' % (dir, expid) |
---|
129 | |
---|
130 | try: |
---|
131 | cid = Creddy.ID(certfile) |
---|
132 | cid.load_privkey(keyfile) |
---|
133 | cattr = Creddy.Attribute(cid, 'acting_for', 3600 * 24 * 365 * 10) |
---|
134 | cattr.principal("%s" % expid) |
---|
135 | cattr.bake() |
---|
136 | cattr.write_name(fn) |
---|
137 | except RuntimeError: |
---|
138 | print >>sys.stderr, "Cannot create ABAC delegation. " + \ |
---|
139 | "Did you run cert_to_fedid.py on your X.509 cert?" |
---|
140 | return [] |
---|
141 | |
---|
142 | context = ABAC.Context() |
---|
143 | if context.load_id_file(certfile) != ABAC.ABAC_CERT_SUCCESS or \ |
---|
144 | context.load_attribute_file(fn) != ABAC.ABAC_CERT_SUCCESS: |
---|
145 | print >>sys.stderr, "Cannot load delegation into ABAC context. " + \ |
---|
146 | "Did you run cert_to_fedid.py on your X.509 cert?" |
---|
147 | return [] |
---|
148 | ids, attrs = abac_context_to_creds(context) |
---|
149 | |
---|
150 | return ids + attrs |
---|
151 | |
---|
152 | |
---|
153 | finally: |
---|
154 | if keyfile: os.unlink(keyfile) |
---|
155 | if certfile: os.unlink(certfile) |
---|
156 | |
---|
157 | |
---|
158 | # Main line |
---|
159 | service_re = re.compile('^\\s*#\\s*SERVICE:\\s*([\\S]+)') |
---|
160 | parser = fedd_create_opts() |
---|
161 | (opts, args) = parser.parse_args() |
---|
162 | |
---|
163 | svcs = [] |
---|
164 | # Option processing |
---|
165 | try: |
---|
166 | cert, fid, url = wrangle_standard_options(opts) |
---|
167 | except RuntimeError, e: |
---|
168 | sys.exit("%s" %e) |
---|
169 | |
---|
170 | if opts.file: |
---|
171 | |
---|
172 | top = None |
---|
173 | # Try the file as a topdl description |
---|
174 | try: |
---|
175 | top = topdl.topology_from_xml(filename=opts.file, top='experiment') |
---|
176 | except EnvironmentError: |
---|
177 | # Can't read the file, fail now |
---|
178 | sys.exit("Cannot read description file (%s)" %opts.file) |
---|
179 | except ExpatError: |
---|
180 | # The file is not topdl, fall and assume it's ns2 |
---|
181 | pass |
---|
182 | |
---|
183 | if top is None: |
---|
184 | try: |
---|
185 | lines = [ line for line in open(opts.file, 'r')] |
---|
186 | exp_desc = "".join(lines) |
---|
187 | # Parse all the strings that we can pull out of the file using the |
---|
188 | # service_re. |
---|
189 | svcs.extend([parse_service(service_re.match(l).group(1)) \ |
---|
190 | for l in lines if service_re.match(l)]) |
---|
191 | except EnvironmentError: |
---|
192 | sys.exit("Cannot read description file (%s)" %opts.file) |
---|
193 | else: |
---|
194 | sys.exit("Must specify an experiment description (--file)") |
---|
195 | |
---|
196 | out_certfile = opts.out_certfile |
---|
197 | |
---|
198 | # If there is no abac directory in which to store a delegated credential, don't |
---|
199 | # delegate. |
---|
200 | if not opts.abac_dir and opts.delegate: |
---|
201 | opts.delegate = False |
---|
202 | |
---|
203 | # Load ABAC certs |
---|
204 | if opts.abac_dir: |
---|
205 | try: |
---|
206 | acerts = get_abac_certs(opts.abac_dir) |
---|
207 | except EnvironmentError, e: |
---|
208 | sys.exit('%s: %s' % (e.filename, e.strerror)) |
---|
209 | else: |
---|
210 | acerts = None |
---|
211 | |
---|
212 | # Fill in services |
---|
213 | if opts.master and opts.project: |
---|
214 | svcs.append(project_export_service(opts.master, opts.project)) |
---|
215 | svcs.extend([ parse_service(s) for s in opts.service]) |
---|
216 | |
---|
217 | # Create a testbed map if one is specified |
---|
218 | tbmap = { } |
---|
219 | for m in opts.map: |
---|
220 | i = m.find(":") |
---|
221 | if i != -1: tbmap[m[0:i]] = m[i+1:] |
---|
222 | else: sys.exit("Bad mapping argument: %s" %m ) |
---|
223 | |
---|
224 | |
---|
225 | if not svcs: |
---|
226 | print >>sys.stderr, "Warning:Neither master/project nor services requested" |
---|
227 | |
---|
228 | # Construct the New experiment request |
---|
229 | msg = { } |
---|
230 | |
---|
231 | |
---|
232 | # Generate a certificate if requested and put it into the message |
---|
233 | if opts.gen_cert: |
---|
234 | expid, expcert = generate_fedid(opts.exp_name or 'dummy') |
---|
235 | msg['experimentAccess'] = { 'X509': expcert } |
---|
236 | else: |
---|
237 | expid = expcert = None |
---|
238 | |
---|
239 | if opts.exp_name: |
---|
240 | msg['experimentID'] = { 'localname': opts.exp_name } |
---|
241 | |
---|
242 | if acerts: |
---|
243 | msg['credential'] = acerts |
---|
244 | |
---|
245 | # ZSI will not properly construct an empty message. If nothing has been added |
---|
246 | # to msg, pick a random localname to ensure the message is created |
---|
247 | if not msg: |
---|
248 | msg['experimentID'] = { |
---|
249 | 'localname': join([choice(ascii_letters) for i in range(0,8)],""), |
---|
250 | } |
---|
251 | |
---|
252 | |
---|
253 | if opts.debug > 1: print >>sys.stderr, msg |
---|
254 | |
---|
255 | # The New call |
---|
256 | try: |
---|
257 | resp_dict = do_rpc(msg, |
---|
258 | url, opts.transport, cert, opts.trusted, |
---|
259 | serialize_only=opts.serialize_only, |
---|
260 | tracefile=opts.tracefile, |
---|
261 | caller=service_caller('New'), responseBody="NewResponseBody") |
---|
262 | except RPCException, e: |
---|
263 | exit_with_fault(e, 'New (create)', opts) |
---|
264 | except RuntimeError, e: |
---|
265 | sys.exit("Error processing RPC: %s" % e) |
---|
266 | |
---|
267 | if opts.debug > 1: print >>sys.stderr, resp_dict |
---|
268 | |
---|
269 | proof = proof.from_dict(resp_dict.get('proof', {})) |
---|
270 | if proof and opts.auth_log: |
---|
271 | log_authentication(opts.auth_log, 'New (create)', 'succeeded', proof) |
---|
272 | # Save the experiment ID certificate if we need it |
---|
273 | try: |
---|
274 | save_certfile(opts.out_certfile, resp_dict.get('experimentAccess', None)) |
---|
275 | except EnvironmentError, e: |
---|
276 | sys.exit('Could not write to %s: %s' % (opts.out_certfile, e)) |
---|
277 | |
---|
278 | e_fedid, e_local = get_experiment_names(resp_dict.get('experimentID', None)) |
---|
279 | if not e_fedid and not e_local and opts.serialize_only: |
---|
280 | e_local = "serialize" |
---|
281 | |
---|
282 | # If delegation is requested and we have a target, make the delegation, and add |
---|
283 | # the credential to acerts. |
---|
284 | if e_fedid and opts.delegate: |
---|
285 | try: |
---|
286 | creds = delegate(e_fedid, cert, opts.abac_dir, name=opts.exp_name) |
---|
287 | if creds: |
---|
288 | acerts.extend(creds) |
---|
289 | except EnvironmentError, e: |
---|
290 | sys.exit("Cannot delegate rights %s: %s" % (e.filename, e.strerror)); |
---|
291 | |
---|
292 | # Construct the Create message |
---|
293 | if top: |
---|
294 | msg = { 'experimentdescription' : { 'topdldescription': top.to_dict() }, } |
---|
295 | else: |
---|
296 | msg = { 'experimentdescription': { 'ns2description': exp_desc }, } |
---|
297 | |
---|
298 | if svcs: |
---|
299 | msg['service'] = svcs |
---|
300 | |
---|
301 | if e_fedid: |
---|
302 | msg['experimentID'] = { 'fedid': e_fedid } |
---|
303 | elif e_local: |
---|
304 | msg['experimentID'] = { 'localname': e_local } |
---|
305 | else: |
---|
306 | sys.exit("New did not return an experiment ID??") |
---|
307 | |
---|
308 | if acerts: |
---|
309 | msg['credential'] = acerts |
---|
310 | |
---|
311 | if tbmap: |
---|
312 | msg['testbedmap'] = [ { 'testbed': t, 'uri': u } for t, u in tbmap.items() ] |
---|
313 | |
---|
314 | if opts.debug > 1: print >>sys.stderr, msg |
---|
315 | |
---|
316 | # make the call |
---|
317 | try: |
---|
318 | resp_dict = do_rpc(msg, |
---|
319 | url, opts.transport, cert, opts.trusted, |
---|
320 | serialize_only=opts.serialize_only, |
---|
321 | tracefile=opts.tracefile, |
---|
322 | caller=service_caller('Create', max_retries=1), responseBody="CreateResponseBody") |
---|
323 | except RPCException, e: |
---|
324 | exit_with_fault(e, 'Create', opts) |
---|
325 | except RuntimeError, e: |
---|
326 | sys.exit("Error processing RPC: %s" % e) |
---|
327 | |
---|
328 | if opts.debug > 1: print >>sys.stderr, resp_dict |
---|
329 | |
---|
330 | # output |
---|
331 | e_fedid, e_local = get_experiment_names(resp_dict.get('experimentID', None)) |
---|
332 | st = resp_dict.get('experimentStatus', None) |
---|
333 | |
---|
334 | if e_local: print "localname: %s" % e_local |
---|
335 | if e_fedid: print "fedid: %s" % e_fedid |
---|
336 | if st: print "status: %s" % st |
---|
337 | proof = proof.from_dict(resp_dict.get('proof', {})) |
---|
338 | if proof and opts.auth_log: |
---|
339 | log_authentication(opts.auth_log, 'Create', 'succeeded', proof) |
---|