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
12 # sfa-ca.py --sign PEER_CERT_FILENAME -o OUTPUT_FILENAME
14 ## import a cert and update the registry hierarchy
15 # sfa-ca.py --import CERT_FILENAME
18 # sfa-ca.py --display CERT_FILENAME
23 from optparse import OptionParser
25 from sfa.util.config import Config
27 from sfa.trust.gid import GID, create_uuid
28 from sfa.trust.hierarchy import Hierarchy
30 from sfa.storage.table import SfaTable
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,
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")
55 (options, args) = parser.parse_args()
62 elif options.importgid:
73 Display the sepcified GID
75 gidfile = os.path.abspath(options.display)
76 if not gidfile or not os.path.isfile(gidfile):
77 print "No such gid: %s" % gidfile
79 gid = GID(filename=gidfile)
80 gid.dump(dump_parents=True)
84 Sign the specified gid
86 hierarchy = Hierarchy()
88 default_authority = config.SFA_INTERFACE_HRN
89 auth_info = hierarchy.get_auth_info(default_authority)
92 gidfile = os.path.abspath(options.sign)
93 if not os.path.isfile(gidfile):
94 print "no such gid: %s" % gidfile
96 gid = GID(filename=gidfile)
98 # extract pub_key and create new gid
99 pkey = gid.get_pubkey()
101 gid = hierarchy.create_gid(urn, create_uuid(), pkey)
104 outfile = options.outfile
106 outfile = os.path.abspath('./signed-%s.gid' % gid.get_hrn())
108 # save the signed gid
110 print "Writing signed gid %s" % outfile
111 gid.save_to_file(outfile, save_parents=True)
114 def export_gid(options):
115 # lookup the record for the specified hrn
118 # check sfa table first
119 filter = {'hrn': hrn}
121 filter['type'] = type
123 records = table.find(filter)
125 # check the authorities hierarchy
126 hierarchy = Hierarchy()
128 auth_info = hierarchy.get_auth_info(hrn)
129 gid = auth_info.gid_object
131 print "Record: %s not found" % hrn
135 gid = GID(string=record['gid'])
138 outfile = options.outfile
140 outfile = os.path.abspath('./%s.gid' % gid.get_hrn())
144 print "Writing %s gid to %s" % (gid.get_hrn(), outfile)
145 gid.save_to_file(outfile, save_parents=True)
147 def import_gid(options):
149 Import the specified gid into the registry (db and authorities
150 hierarchy) overwriting any previous 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
157 gid = GID(filename=gidfile)
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()
165 # check if record exists in db
167 records = table.find({'hrn': gid.get_hrn(), 'type': 'authority'})
169 print "%s not found in record database" % gid.get_hrn()
172 # update the database record
174 record['gid'] = gid.save_to_string(save_parents=True)
177 print "Imported %s gid into db" % record['hrn']
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)
184 print "Writing %s gid to %s" % (gid.get_hrn(), filename)
189 if __name__ == '__main__':