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