0fbe140f7c44c6ee98f1b96e0ca4875df1a01543
[sfa.git] / sfa / server / sfa-ca.py
1 #!/usr/bin/python
2
3 #
4 # SFA Certificate Signing and management. Root authorities can use this script 
5 # to sign  the certificate of another authority and become its parent. Sub 
6 # authorities (authorities that have had their cert signed by another authority) 
7 # can use this script to update their registry hierarchy with the new cert    
8
9 # Example usage: 
10 #
11 ## sign a peer cert
12 # sfa-ca.py --sign PEER_CERT_FILENAME -o OUTPUT_FILENAME 
13 #
14 ## import a cert and update the registry hierarchy
15 # sfa-ca.py --import CERT_FILENAME   
16 #
17 ## display a cert
18 # sfa-ca.py --display CERT_FILENAME
19
20
21 import os
22 import sys
23 from optparse import OptionParser
24
25 from sfa.util.config import Config
26 from sfa.util.table import SfaTable
27
28 from sfa.trust.gid import GID, create_uuid
29 from sfa.trust.hierarchy import Hierarchy
30
31 def main():
32     args = sys.argv
33     script_name = args[0]
34     parser = OptionParser(usage="%(script_name)s [options]" % locals())
35     parser.add_option("-d", "--display", dest="display", default=None,
36                       help="print contents of specified gid")           
37     parser.add_option("-s", "--sign", dest="sign", default=None, 
38                       help="gid to sign" )
39     parser.add_option("-k", "--key", dest="key", default=None, 
40                       help="keyfile to use for signing")
41     parser.add_option("-a", "--authority", dest="authority", default=None, 
42                       help="sign the gid using the specified authority ")
43     parser.add_option("-i", "--import", dest="importgid", default=None,
44                       help="gid file to import into the registry")
45     parser.add_option("-e", "--export", dest="export", 
46                       help="name of gid to export from registry")
47     parser.add_option("-t", "--type", dest="type",
48                       help="record type", default=None)
49     parser.add_option("-o", "--outfile", dest="outfile",
50                       help="where to write the exprted gid") 
51     parser.add_option("-v", "--verbose", dest="verbose", default=False, 
52                       action="store_true", help="be verbose")           
53                 
54     (options, args) = parser.parse_args()
55
56
57     if options.display:
58         display(options)
59     elif options.sign:
60         sign(options)
61     elif options.importgid:
62         import_gid(options) 
63     elif options.export:
64         export_gid(options)  
65     else:
66         parser.print_help()
67         sys.exit(1)        
68
69
70 def display(options):
71     """
72     Display the sepcified GID
73     """
74     gidfile = os.path.abspath(options.display)
75     if not gidfile or not os.path.isfile(gidfile):
76         print "No such gid: %s" % gidfile
77         sys.exit(1)
78     gid = GID(filename=gidfile)
79     gid.dump(dump_parents=True)
80
81 def sign(options):
82     """
83     Sign the specified gid
84     """
85     hierarchy = Hierarchy()
86     config = Config()
87     default_authority = config.SFA_INTERFACE_HRN
88     auth_info = hierarchy.get_auth_info(default_authority)
89
90     # load the gid
91     gidfile = os.path.abspath(options.sign)
92     if not os.path.isfile(gidfile):
93         print "no such gid: %s" % gidfile
94         sys.exit(1)
95     gid = GID(filename=gidfile)
96
97     # extract pub_key and create new gid
98     pkey = gid.get_pubkey()
99     urn = gid.get_urn()
100     gid = hierarchy.create_gid(urn, create_uuid(), pkey)
101
102     # get the outfile
103     outfile = options.outfile
104     if not outfile:
105         outfile = os.path.abspath('./signed-%s.gid' % gid.get_hrn())
106    
107     # save the signed gid
108     if options.verbose:
109         print "Writing signed gid %s" % outfile  
110     gid.save_to_file(outfile, save_parents=True)
111     
112
113 def export_gid(options):
114     # lookup the record for the specified hrn 
115     hrn = options.export
116     type = options.type
117     # check sfa table first
118     filter = {'hrn': hrn}
119     if type:
120         filter['type'] = type                    
121     table = SfaTable()
122     records = table.find(filter)
123     if not records:
124         # check the authorities hierarchy 
125         hierarchy = Hierarchy()
126         try:
127             auth_info = hierarchy.get_auth_info(hrn)
128             gid = auth_info.gid_object 
129         except:
130             print "Record: %s not found" % hrn
131             sys.exit(1)
132     else:
133         record = records[0]
134         gid = GID(string=record['gid'])
135         
136     # get the outfile
137     outfile = options.outfile
138     if not outfile:
139         outfile = os.path.abspath('./%s.gid' % gid.get_hrn())
140
141     # save it
142     if options.verbose:
143         print "Writing %s gid to %s" % (gid.get_hrn(), outfile)
144     gid.save_to_file(outfile, save_parents=True)
145
146 def import_gid(options):
147     """
148     Import the specified gid into the registry (db and authorities 
149     hierarchy) overwriting any previous gid.
150     """
151     # load the gid
152     gidfile = os.path.abspath(options.importgid)
153     if not gidfile or not os.path.isfile(gidfile):
154         print "No such gid: %s" % gidfile
155         sys.exit(1)
156     gid = GID(filename=gidfile)
157     
158     # check if it exists within the hierarchy
159     hierarchy = Hierarchy()
160     if not hierarchy.auth_exists(gid.get_hrn()):
161         print "%s not found in hierarchy" % gid.get_hrn()
162         sys.exit(1)
163
164     # check if record exists in db
165     table = SfaTable()
166     records = table.find({'hrn': gid.get_hrn(), 'type': 'authority'})
167     if not records:
168         print "%s not found in record database" % gid.get_hrn()  
169         sys.exit(1)
170
171     # update the database record
172     record = records[0]
173     record['gid'] = gid.save_to_string(save_parents=True)
174     table.update(record)
175     if options.verbose:
176         print "Imported %s gid into db" % record['hrn']
177
178     # update the hierarchy
179     auth_info = hierarchy.get_auth_info(gid.get_hrn())  
180     filename = auth_info.gid_filename
181     gid.save_to_file(filename, save_parents=True)
182     if options.verbose:
183         print "Writing %s gid to %s" % (gid.get_hrn(), filename)
184
185     # ending here
186     return
187
188 if __name__ == '__main__':
189     main()