Ignore:
Timestamp:
Sep 15, 2010 1:45:58 AM (14 years ago)
Author:
Ted Faber <faber@…>
Branches:
axis_example, compt_changes, info-ops, master
Children:
af25848
Parents:
9cce15a
Message:

Basic function pending creddy updates.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • fedd/access_to_abac.py

    r9cce15a r5a721ed  
    6262                [attribute(p, x) for x in (gp, gu) if x is not None])
    6363        creds.add(c)
    64         if a in to_id: to_id[a].append(c)
    65         else: to_id[a] = [ c ]
     64        if (project, user) in to_id: to_id[(project,user)].append(c)
     65        else: to_id[(project,user)] = [ c ]
    6666    else:
    6767        raise parse_error("Badly formatted local mapping: %s" % l)
    6868
     69
     70def parse_protogeni(l, creds, me, to_id, p, gp, gu):
     71    right_side_str = '\s*,\s*\(\s*(%s)\s*,\s*(%s)\s*,\s*(%s)\s*,\s*(%s)\s*\)' \
     72            % (path_str, id_str, path_str, id_str)
     73
     74    m = re.match(right_side_str, l)
     75    if m:
     76        cert, user, key, pw = m.group(1,2,3,4)
     77        acert = re.sub('/', '_', cert)
     78
     79        a = "cert_%s_user_%s" % (acert, user)
     80        c = credential(me, a,
     81                [attribute(p, x) for x in (gp, gu) if x is not None])
     82        creds.add(c)
     83        if (cert, user, key, pw) in to_id:
     84            to_id[(cert, user, key, pw)].append(c)
     85        else:
     86            to_id[(cert, user, key, pw)] = [ c ]
     87    else:
     88        raise parse_error("Badly formatted local mapping: %s" % l)
    6989
    7090def parse_dragon(l, creds, me, to_id, p, gp, gu):
     
    94114            'internal': parse_internal,
    95115            'skel': parse_skel,
     116            'protogeni': parse_protogeni,
    96117            }
    97118
     
    110131        self.add_option('--key', dest='key', default=None,
    111132                help='key for the certificate')
     133        self.add_option('--dir', dest='dir', default=None,
     134                help='Output directory for credentials')
    112135        self.add_option('--type', action='callback', nargs=1, type='str',
    113136                callback=access_opts.parse_mapper,
     
    116139                        ", ".join(access_opts.mappers.keys()) + \
    117140                        'Omit for generic parsing.')
     141        self.add_option('--quiet', dest='quiet', action='store_true',
     142                default=False,
     143                help='Do not print credential to local attribute map')
     144        self.add_option('--create-creds', action='store_true',
     145                dest='create_creds', default=False,
     146                help='create credentials for rules.  Requires ' + \
     147                        '--cert, --key, and --dir to be given.')
    118148        self.set_defaults(mapper=None)
    119149
    120 
     150def create_creds(creds, cert, key, dir, creddy='/usr/local/bin/creddy'):
     151    def attrs(r):
     152        if r.principal and r.attr:
     153            return ['--subject-id=%s' % r.principal,
     154                    '--subject-role=%s' %r.attr]
     155        elif r.principal:
     156            return ['--subject-id=%s' % r.prinicpal]
     157        else:
     158            raise parse_error('Attribute without a principal?')
     159
     160    for i, c in enumerate(creds):
     161        cmd = [creddy, '--attribute', '--issuer=%s' % cert, '--key=%s' % key,
     162                '--role=%s' % c.attr, '--out=%s/cred%d' % (dir, i)]
     163        for r in c.req:
     164            cmd.extend(attrs(r))
     165        print " ".join(cmd)
    121166
    122167comment_re = re.compile('^\s*#|^$')
    123168fedid_str = 'fedid:([0-9a-fA-F]{40})'
    124169id_str = '[a-zA-Z][\w_-]*'
     170path_str = '[a-zA-Z_/\.-]+'
    125171id_any_str = '(%s|<any>)' % id_str
    126172id_same_str = '(%s|<same>)' % id_str
     
    144190    print >>sys.stderr, 'No --cert, using dummy fedid'
    145191    me = fedid(hexstr='0123456789012345678901234567890123456789')
     192
     193if opts.key and not os.access(opts.key, os.R_OK):
     194    sys.exit('Cannot read key (%s)' % opts.key)
     195
     196if opts.dir:
     197    if not os.path.isdir(opts.dir):
     198        sys.exit('%s is not a directory' % opts.dir)
     199    elif not os.access(opts.dir, os.W_OK):
     200        sys.exit('%s is not writable' % opts.dir)
    146201
    147202mapper = opts.mapper
     
    185240        continue
    186241
    187     for c in creds:
    188         print "%s" % c
    189 
    190     for k, c in to_id.items():
    191         print "%s: %s" % ( k , ", ".join(set(["%s.%s" % (x.principal, x.attr) \
    192                 for x in c])))
     242    if opts.create_creds:
     243        if all([opts.cert, opts.key, opts.dir]):
     244            create_creds([c for c in creds if c.principal == me],
     245                    opts.cert, opts.key, opts.dir)
     246        else:
     247            print >>sys.stderr, 'Cannot create credentials.  Missing parameter'
     248
     249    if not opts.quiet:
     250        for k, c in to_id.items():
     251            for a in set(["%s.%s" % (x.principal, x.attr) for x in c]):
     252                print "%s -> (%s)" % ( a, ", ".join(k))
Note: See TracChangeset for help on using the changeset viewer.