initial checkin of tool used manage (display, sign, import, export) registry gids
[sfa.git] / sfa / server / sfa-ca.py
1 #!/usr/bin/python
2
3 #
4 # SFA Certificate Signing and management 
5 #   
6
7 import os
8 import sys
9 from optparse import OptionParser
10 from sfa.trust.certificate import Keypair, Certificate
11 from sfa.trust.gid import GID, create_uuid
12 from sfa.trust.hierarchy import Hierarchy
13 from sfa.util.config import Config
14
15 def main():
16     args = sys.argv
17     script_name = args[0]
18     parser = OptionParser(usage="%(script_name)s [options]" % locals())
19     parser.add_option("-d", "--display", dest="display", default=None,
20                       help="print contents of specified gid")           
21     parser.add_option("-s", "--sign", dest="sign", default=None, 
22                       help="gid to sign" )
23     parser.add_option("-k", "--key", dest="key", default=None, 
24                       help="keyfile to use for signing")
25     parser.add_option("-i", "--import", dest="importgid", default=None,
26                       help="gid file to import into the registry")
27     parser.add_option("-e", "--export", dest="export", 
28                       help="name of gid to export from registry")
29     parser.add_option("-o", "--outfile", dest="outfile",
30                       help="where to write the exprted gid") 
31     parser.add_option("-v", "--verbose", dest="verobse", 
32                       help="be verbose")           
33                 
34     (options, args) = parser.parse_args()
35
36
37     if options.display:
38         display(options)
39     elif options.sign:
40         sign(options)
41     elif options.importgid:
42         import_gid(options) 
43     elif options.export:
44         export_gid(options)  
45     else:
46         parser.print_help()
47         sys.exit(1)        
48
49
50 def display(options):
51     gidfile = os.path.abspath(options.display)
52     print gidfile
53     if not gidfile or not os.path.isfile(gidfile):
54         print "No such gid: %s" % gidfile
55         sys.exit(1) 
56     gid = GID(filename=gidfile)
57     gid.dump(dump_parents=True)
58
59 def sign(options):
60     from sfa.util.table import SfaTable
61     hierarchy = Hierarchy()
62     config = Config()
63     parent_hrn = config.SFA_INTERFACE_HRN
64     auth_info = hierarchy.get_auth_info(parent_hrn)
65
66     # load the gid
67     gidfile = os.path.abspath(options.sign)
68     if not os.path.isfile(gidfile):
69         print "no such gid: %s" % gidfile
70         sys.exit(1)
71     gid = GID(filename=gidfile)
72
73     # load the parent private key
74     pkeyfile = options.key
75     # if no pkey was specified, then use the this authority's key
76     if not pkeyfile:
77         pkeyfile = auth_info.privkey_filename
78     if not os.path.isfile(pkeyfile):
79         print "no such pkey: %s.\nPlease specify a valid private key" % pkeyfile
80         sys.exit(1)
81     parent_key = Keypair(filename=pkeyfile)
82
83     # load the parent gid
84     parent_gid = auth_info.gid_object
85
86     # get the outfile
87     outfile = options.outfile
88     if not outfile:
89         outfile = os.path.abspath('./signed-%s.gid' % gid.get_hrn())
90     
91     # sign the gid
92     gid.set_issuer(parent_key, parent_hrn)
93     gid.set_parent(parent_gid)
94     gid.save_to_file(outfile, save_parents=True)            
95     
96
97 def export(options):
98     from sfa.util.table import SfaTable
99     pass
100
101 def import_gid(options):
102     from sfa.util.table import SfaTable
103     pass
104
105 if __name__ == '__main__':
106     main()