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
26 from sfa.util.table import SfaTable
28 from sfa.trust.gid import GID, create_uuid
29 from sfa.trust.hierarchy import Hierarchy
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,
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")
54 (options, args) = parser.parse_args()
61 elif options.importgid:
72 Display the sepcified GID
74 gidfile = os.path.abspath(options.display)
75 if not gidfile or not os.path.isfile(gidfile):
76 print "No such gid: %s" % gidfile
78 gid = GID(filename=gidfile)
79 gid.dump(dump_parents=True)
83 Sign the specified gid
85 hierarchy = Hierarchy()
87 default_authority = config.SFA_INTERFACE_HRN
88 auth_info = hierarchy.get_auth_info(default_authority)
91 gidfile = os.path.abspath(options.sign)
92 if not os.path.isfile(gidfile):
93 print "no such gid: %s" % gidfile
95 gid = GID(filename=gidfile)
97 # extract pub_key and create new gid
98 pkey = gid.get_pubkey()
100 gid = hierarchy.create_gid(urn, create_uuid(), pkey)
103 outfile = options.outfile
105 outfile = os.path.abspath('./signed-%s.gid' % gid.get_hrn())
107 # save the signed gid
109 print "Writing signed gid %s" % outfile
110 gid.save_to_file(outfile, save_parents=True)
113 def export_gid(options):
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(hrn)
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.
152 gidfile = os.path.abspath(options.importgid)
153 if not gidfile or not os.path.isfile(gidfile):
154 print "No such gid: %s" % gidfile
156 gid = GID(filename=gidfile)
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()
164 # check if record exists in db
166 records = table.find({'hrn': gid.get_hrn(), 'type': 'authority'})
168 print "%s not found in record database" % gid.get_hrn()
171 # update the database record
173 record['gid'] = gid.save_to_string(save_parents=True)
176 print "Imported %s gid into db" % record['hrn']
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)
183 print "Writing %s gid to %s" % (gid.get_hrn(), filename)
188 if __name__ == '__main__':