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