[2e46f35] | 1 | #!/usr/bin/env python |
---|
[71461a4] | 2 | |
---|
[353db8c] | 3 | import sys |
---|
[c573278] | 4 | import os, os.path |
---|
[353db8c] | 5 | |
---|
[f4f036f] | 6 | from federation.util import file_expanding_opts |
---|
| 7 | from federation.authorizer import abac_authorizer |
---|
[71461a4] | 8 | |
---|
[f4f036f] | 9 | class Parser(file_expanding_opts): |
---|
[71461a4] | 10 | def __init__(self): |
---|
[f4f036f] | 11 | file_expanding_opts.__init__(self, usage='%prog [options]') |
---|
[62f3dd9] | 12 | self.add_option('--cert', dest='cert', |
---|
| 13 | action='callback', callback=self.expand_file, type='str', |
---|
| 14 | help='Identity certificate') |
---|
| 15 | self.add_option('--key', dest='key', |
---|
| 16 | action='callback', callback=self.expand_file, type='str', |
---|
| 17 | help='Identity key') |
---|
[71461a4] | 18 | self.add_option('--policy', dest='policy', |
---|
[62f3dd9] | 19 | action='callback', callback=self.expand_file, type='str', |
---|
[71461a4] | 20 | help='ABAC policy certificates') |
---|
[62f3dd9] | 21 | self.add_option('--dir', dest='out_dir', |
---|
| 22 | action='callback', callback=self.expand_file, type='str', |
---|
| 23 | help='directory to save into') |
---|
[71461a4] | 24 | |
---|
| 25 | parser = Parser() |
---|
| 26 | opts, args = parser.parse_args() |
---|
| 27 | |
---|
[353db8c] | 28 | if any([ not x for x in (opts.cert, opts.policy, opts.out_dir)]): |
---|
[71461a4] | 29 | parser.print_help() |
---|
| 30 | sys.exit(1) |
---|
[c573278] | 31 | |
---|
| 32 | try: |
---|
| 33 | for path, dirs, files in os.walk(opts.out_dir, topdown=False): |
---|
| 34 | for f in files: os.unlink(os.path.join(path, f)) |
---|
| 35 | for d in dirs: os.rmdir(os.path.join(path, d)) |
---|
| 36 | except EnvironmentError, e: |
---|
| 37 | sys.exit("Can't remove %s: %s" % ( e.filename, e.strerror)) |
---|
| 38 | |
---|
[353db8c] | 39 | try: |
---|
| 40 | a = abac_authorizer(key=opts.key, me=opts.cert, certs=opts.policy, |
---|
| 41 | save=opts.out_dir) |
---|
| 42 | a.save(opts.out_dir) |
---|
| 43 | except EnvironmentError, e: |
---|
| 44 | sys.exit("Can't create or write %s: %s" % (e.filename, e.strerror)) |
---|
[6e63513] | 45 | except abac_authorizer.bad_cert_error, e: |
---|
[353db8c] | 46 | sys.exit("Error creating authorizer: %s" % e) |
---|