[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 |
---|
[65f6442] | 8 | import Creddy |
---|
[c573278] | 9 | |
---|
[990b746] | 10 | from string import join, ascii_letters |
---|
| 11 | from random import choice |
---|
[6e63513] | 12 | |
---|
[6bedbdba] | 13 | from deter import topdl |
---|
| 14 | from deter import fedid, generate_fedid |
---|
| 15 | |
---|
[e83f2f2] | 16 | from federation.proof import proof |
---|
[d743d60] | 17 | from federation.remote_service import service_caller |
---|
| 18 | from 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] | 21 | from federation.util import abac_split_cert, abac_context_to_creds |
---|
[a7c0bcb] | 22 | |
---|
| 23 | from xml.parsers.expat import ExpatError |
---|
[d743d60] | 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", |
---|
[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") |
---|
[fd07c48] | 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") |
---|
[353db8c] | 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 | |
---|
[6e63513] | 59 | self.set_defaults(delegate=True) |
---|
[d743d60] | 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'] = [ ] |
---|
[0de1b94] | 83 | for t in terms[3].split(";"): |
---|
[d743d60] | 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 | |
---|
[65f6442] | 106 | def delegate(fedid, cert, dir, name=None, debug=False): |
---|
[353db8c] | 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: |
---|
[6e63513] | 120 | keyfile, certfile = abac_split_cert(cert) |
---|
[353db8c] | 121 | |
---|
| 122 | rv = 0 |
---|
[c573278] | 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) |
---|
[353db8c] | 129 | |
---|
[65f6442] | 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?" |
---|
[c573278] | 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: |
---|
[65f6442] | 145 | print >>sys.stderr, "Cannot load delegation into ABAC context. " + \ |
---|
[66879a0] | 146 | "Did you run cert_to_fedid.py on your X.509 cert?" |
---|
[c573278] | 147 | return [] |
---|
| 148 | ids, attrs = abac_context_to_creds(context) |
---|
| 149 | |
---|
| 150 | return ids + attrs |
---|
[6e63513] | 151 | |
---|
| 152 | |
---|
[353db8c] | 153 | finally: |
---|
| 154 | if keyfile: os.unlink(keyfile) |
---|
| 155 | if certfile: os.unlink(certfile) |
---|
| 156 | |
---|
[6e63513] | 157 | |
---|
[d743d60] | 158 | # Main line |
---|
[b10375f] | 159 | service_re = re.compile('^\\s*#\\s*SERVICE:\\s*([\\S]+)') |
---|
[d743d60] | 160 | parser = fedd_create_opts() |
---|
| 161 | (opts, args) = parser.parse_args() |
---|
| 162 | |
---|
[cd60510] | 163 | svcs = [] |
---|
[353db8c] | 164 | # Option processing |
---|
[a0c2866] | 165 | try: |
---|
| 166 | cert, fid, url = wrangle_standard_options(opts) |
---|
| 167 | except RuntimeError, e: |
---|
| 168 | sys.exit("%s" %e) |
---|
[d743d60] | 169 | |
---|
| 170 | if opts.file: |
---|
[a7c0bcb] | 171 | |
---|
| 172 | top = None |
---|
| 173 | # Try the file as a topdl description |
---|
[d743d60] | 174 | try: |
---|
[a7c0bcb] | 175 | top = topdl.topology_from_xml(filename=opts.file, top='experiment') |
---|
[d743d60] | 176 | except EnvironmentError: |
---|
[a7c0bcb] | 177 | # Can't read the file, fail now |
---|
[d743d60] | 178 | sys.exit("Cannot read description file (%s)" %opts.file) |
---|
[a7c0bcb] | 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) |
---|
[d743d60] | 193 | else: |
---|
| 194 | sys.exit("Must specify an experiment description (--file)") |
---|
| 195 | |
---|
| 196 | out_certfile = opts.out_certfile |
---|
| 197 | |
---|
[353db8c] | 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)) |
---|
[725c55d] | 209 | else: |
---|
| 210 | acerts = None |
---|
[353db8c] | 211 | |
---|
[d743d60] | 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]) |
---|
[353db8c] | 216 | |
---|
[fd07c48] | 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 | |
---|
[d743d60] | 225 | if not svcs: |
---|
| 226 | print >>sys.stderr, "Warning:Neither master/project nor services requested" |
---|
| 227 | |
---|
[353db8c] | 228 | # Construct the New experiment request |
---|
[d743d60] | 229 | msg = { } |
---|
| 230 | |
---|
[353db8c] | 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 | |
---|
[d743d60] | 239 | if opts.exp_name: |
---|
| 240 | msg['experimentID'] = { 'localname': opts.exp_name } |
---|
| 241 | |
---|
[353db8c] | 242 | if acerts: |
---|
| 243 | msg['credential'] = acerts |
---|
| 244 | |
---|
[990b746] | 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 | |
---|
[d743d60] | 253 | if opts.debug > 1: print >>sys.stderr, msg |
---|
| 254 | |
---|
[353db8c] | 255 | # The New call |
---|
[d743d60] | 256 | try: |
---|
| 257 | resp_dict = do_rpc(msg, |
---|
[5d854e1] | 258 | url, opts.transport, cert, opts.trusted, |
---|
[d743d60] | 259 | serialize_only=opts.serialize_only, |
---|
| 260 | tracefile=opts.tracefile, |
---|
| 261 | caller=service_caller('New'), responseBody="NewResponseBody") |
---|
| 262 | except RPCException, e: |
---|
[e83f2f2] | 263 | exit_with_fault(e, 'New (create)', opts) |
---|
[d743d60] | 264 | except RuntimeError, e: |
---|
| 265 | sys.exit("Error processing RPC: %s" % e) |
---|
| 266 | |
---|
| 267 | if opts.debug > 1: print >>sys.stderr, resp_dict |
---|
| 268 | |
---|
[7ec0dc2] | 269 | p = proof.from_dict(resp_dict.get('proof', {})) |
---|
| 270 | if p and opts.auth_log: |
---|
| 271 | log_authentication(opts.auth_log, 'New (create)', 'succeeded', p) |
---|
[353db8c] | 272 | # Save the experiment ID certificate if we need it |
---|
[d743d60] | 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 | |
---|
[353db8c] | 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: |
---|
[6e63513] | 285 | try: |
---|
[c573278] | 286 | creds = delegate(e_fedid, cert, opts.abac_dir, name=opts.exp_name) |
---|
| 287 | if creds: |
---|
| 288 | acerts.extend(creds) |
---|
[6e63513] | 289 | except EnvironmentError, e: |
---|
| 290 | sys.exit("Cannot delegate rights %s: %s" % (e.filename, e.strerror)); |
---|
[353db8c] | 291 | |
---|
| 292 | # Construct the Create message |
---|
[a7c0bcb] | 293 | if top: |
---|
| 294 | msg = { 'experimentdescription' : { 'topdldescription': top.to_dict() }, } |
---|
| 295 | else: |
---|
| 296 | msg = { 'experimentdescription': { 'ns2description': exp_desc }, } |
---|
[d743d60] | 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 | |
---|
[353db8c] | 308 | if acerts: |
---|
| 309 | msg['credential'] = acerts |
---|
| 310 | |
---|
[fd07c48] | 311 | if tbmap: |
---|
[a11eda5] | 312 | msg['testbedmap'] = [ |
---|
| 313 | { |
---|
| 314 | 'testbed': t, |
---|
| 315 | 'uri': u, |
---|
| 316 | } for t, u in tbmap.items() |
---|
| 317 | ] |
---|
[fd07c48] | 318 | |
---|
[d743d60] | 319 | if opts.debug > 1: print >>sys.stderr, msg |
---|
| 320 | |
---|
[353db8c] | 321 | # make the call |
---|
[d743d60] | 322 | try: |
---|
| 323 | resp_dict = do_rpc(msg, |
---|
[5d854e1] | 324 | url, opts.transport, cert, opts.trusted, |
---|
[d743d60] | 325 | serialize_only=opts.serialize_only, |
---|
| 326 | tracefile=opts.tracefile, |
---|
[e83f2f2] | 327 | caller=service_caller('Create', max_retries=1), responseBody="CreateResponseBody") |
---|
[d743d60] | 328 | except RPCException, e: |
---|
[e83f2f2] | 329 | exit_with_fault(e, 'Create', opts) |
---|
[d743d60] | 330 | except RuntimeError, e: |
---|
| 331 | sys.exit("Error processing RPC: %s" % e) |
---|
| 332 | |
---|
| 333 | if opts.debug > 1: print >>sys.stderr, resp_dict |
---|
| 334 | |
---|
[353db8c] | 335 | # output |
---|
[d743d60] | 336 | e_fedid, e_local = get_experiment_names(resp_dict.get('experimentID', None)) |
---|
| 337 | st = resp_dict.get('experimentStatus', None) |
---|
| 338 | |
---|
| 339 | if e_local: print "localname: %s" % e_local |
---|
| 340 | if e_fedid: print "fedid: %s" % e_fedid |
---|
| 341 | if st: print "status: %s" % st |
---|
[7ec0dc2] | 342 | #proof = proof.from_dict(resp_dict.get('proof', {})) |
---|
| 343 | p_list = resp_dict.get('proof', {}) |
---|
| 344 | if p_list and opts.auth_log: |
---|
| 345 | for p in p_list: |
---|
| 346 | log_authentication(opts.auth_log, 'Create', 'succeeded', |
---|
| 347 | proof.from_dict(p)) |
---|