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
24 from sfa.trust.certificate import Keypair, Certificate
25 from sfa.trust.gid import GID, create_uuid
26 from sfa.trust.hierarchy import Hierarchy
27 from sfa.util.config import Config
28 from collections import defaultdict
33 parser = OptionParser(usage="%(script_name)s [options]" % locals())
34 parser.add_option("-d", "--display", dest="display", default=None,
35 help="print contents of specified gid")
36 parser.add_option("-s", "--sign", dest="sign", default=None,
38 parser.add_option("-k", "--key", dest="key", default=None,
39 help="keyfile to use for signing")
40 parser.add_option("-a", "--authority", dest="authority", default=None,
41 help="sign the gid using the specified authority ")
42 parser.add_option("-i", "--import", dest="importgid", default=None,
43 help="gid file to import into the registry")
44 parser.add_option("-e", "--export", dest="export",
45 help="name of gid to export from registry")
46 parser.add_option("-t", "--type", dest="type",
47 help="record type", default=None)
48 parser.add_option("-o", "--outfile", dest="outfile",
49 help="where to write the exprted gid")
50 parser.add_option("-v", "--verbose", dest="verbose", default=False,
51 action="store_true", help="be verbose")
53 (options, args) = parser.parse_args()
60 elif options.importgid:
71 Display the sepcified GID
73 gidfile = os.path.abspath(options.display)
74 if not gidfile or not os.path.isfile(gidfile):
75 print "No such gid: %s" % gidfile
77 gid = GID(filename=gidfile)
78 gid.dump(dump_parents=True)
82 Sign the specified gid
84 hierarchy = Hierarchy()
86 default_authority = config.SFA_INTERFACE_HRN
87 auth_info = hierarchy.get_auth_info(default_authority)
90 gidfile = os.path.abspath(options.sign)
91 if not os.path.isfile(gidfile):
92 print "no such gid: %s" % gidfile
94 gid = GID(filename=gidfile)
96 # extract pub_key and create new gid
97 pkey = gid.get_pubkey()
99 gid = hierarchy.create_gid(urn, create_uuid(), pkey)
102 outfile = options.outfile
104 outfile = os.path.abspath('./signed-%s.gid' % gid.get_hrn())
106 # save the signed gid
108 print "Writing signed gid %s" % outfile
109 gid.save_to_file(outfile, save_parents=True)
112 def export_gid(options):
113 from sfa.util.table import SfaTable
114 # lookup the record for the specified hrn
117 # check sfa table first
118 filter = {'hrn': hrn}
120 filter['type'] = type
122 records = table.find(filter)
124 # check the authorities hierarchy
125 hierarchy = Hierarchy()
127 auth_info = hierarchy.get_auth_info()
128 gid = auth_info.gid_object
130 print "Record: %s not found" % hrn
134 gid = GID(string=record['gid'])
137 outfile = options.outfile
139 outfile = os.path.abspath('./%s.gid' % gid.get_hrn())
143 print "Writing %s gid to %s" % (gid.get_hrn(), outfile)
144 gid.save_to_file(outfile, save_parents=True)
146 def import_gid(options):
148 Import the specified gid into the registry (db and authorities
149 hierarchy) overwriting any previous gid.
151 from sfa.util.table import SfaTable
152 from sfa.util.record import SfaRecord
154 gidfile = os.path.abspath(options.importgid)
155 if not gidfile or not os.path.isfile(gidfile):
156 print "No such gid: %s" % gidfile
158 gid = GID(filename=gidfile)
160 # check if it exists within the hierarchy
161 hierarchy = Hierarchy()
162 if not hierarchy.auth_exists(gid.get_hrn()):
163 print "%s not found in hierarchy" % gid.get_hrn()
166 # check if record exists in db
168 records = table.find({'hrn': gid.get_hrn(), 'type': 'authority'})
170 print "%s not found in record database" % get.get_hrn()
173 # update the database record
175 record['gid'] = gid.save_to_string(save_parents=True)
178 print "Imported %s gid into db" % record['hrn']
180 # update the hierarchy
181 auth_info = hierarchy.get_auth_info(gid.get_hrn())
182 filename = auth_info.gid_filename
183 gid.save_to_file(filename, save_parents=True)
185 print "Writing %s gid to %s" % (gid.get_hrn(), filename)
190 if __name__ == '__main__':