source: fedd/fedd_create.py @ 7ec0dc2

compt_changes
Last change on this file since 7ec0dc2 was 7ec0dc2, checked in by Ted Faber <faber@…>, 12 years ago

Deal with list of proofs correctly

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