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, file_expanding_opts |
---|
8 | |
---|
9 | class Parser(OptionParser): |
---|
10 | def __init__(self): |
---|
11 | OptionParser.__init__(self, usage='%prog [options]') |
---|
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') |
---|
18 | self.add_option('--policy', dest='policy', |
---|
19 | action='callback', callback=self.expand_file, type='str', |
---|
20 | help='ABAC policy certificates') |
---|
21 | self.add_option('--dir', dest='out_dir', |
---|
22 | action='callback', callback=self.expand_file, type='str', |
---|
23 | help='directory to save into') |
---|
24 | |
---|
25 | parser = Parser() |
---|
26 | opts, args = parser.parse_args() |
---|
27 | |
---|
28 | if any([ not x for x in (opts.cert, opts.policy, opts.out_dir)]): |
---|
29 | parser.print_help() |
---|
30 | sys.exit(1) |
---|
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 | |
---|
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)) |
---|
45 | except abac_authorizer.bad_cert_error, e: |
---|
46 | sys.exit("Error creating authorizer: %s" % e) |
---|