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