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