ctdb-scripts: Improve update and listing code
[samba4-gss.git] / python / samba / tests / bin / getcert
blob93895ebe1327d7e81b96b9399935562a955da004
1 #!/usr/bin/python3
2 import optparse
3 import os, sys, re
4 import pickle
6 sys.path.insert(0, "bin/python")
8 if __name__ == "__main__":
9     parser = optparse.OptionParser('getcert <cmd> [options]')
10     parser.add_option('-i')
11     parser.add_option('-c')
12     parser.add_option('-T')
13     parser.add_option('-I')
14     parser.add_option('-k')
15     parser.add_option('-f')
16     parser.add_option('-e')
17     parser.add_option('-g')
19     (opts, args) = parser.parse_args()
20     assert len(args) == 1
21     assert args[0] in ['add-ca', 'request', 'remove-ca', 'stop-tracking',
22                        'list', 'list-cas']
24     # Use a dir we can write to in the testenv
25     if 'LOCAL_PATH' in os.environ:
26         data_dir = os.path.realpath(os.environ.get('LOCAL_PATH'))
27     else:
28         data_dir = os.path.dirname(os.path.realpath(__file__))
29     dump_file = os.path.join(data_dir, 'getcert.dump')
30     if os.path.exists(dump_file):
31         with open(dump_file, 'rb') as r:
32             cas, certs = pickle.load(r)
33     else:
34         cas = {}
35         certs = {}
36     if args[0] == 'add-ca':
37         # Add a fake CA entry
38         assert opts.c not in cas.keys()
39         cas[opts.c] = opts.e
40     elif args[0] == 'remove-ca':
41         # Remove a fake CA entry
42         assert opts.c in cas.keys()
43         del cas[opts.c]
44     elif args[0] == 'list-cas':
45         # List the fake CAs
46         for ca, helper_location in cas.items():
47             print('CA \'%s\':\n\tis-default: no\n\tca-type: EXTERNAL\n' % ca +
48                   '\thelper-location: %s' % helper_location)
49     elif args[0] == 'request':
50         # Add a fake cert request
51         assert opts.c in cas.keys()
52         assert opts.I not in certs.keys()
53         certs[opts.I] = { 'ca': opts.c, 'template': opts.T,
54                           'keyfile': os.path.abspath(opts.k),
55                           'certfile': os.path.abspath(opts.f),
56                           'keysize': opts.g }
57         # Create dummy key and cert (empty files)
58         with open(opts.k, 'w') as w:
59             pass
60         with open(opts.f, 'w') as w:
61             pass
62     elif args[0] == 'stop-tracking':
63         # Remove the fake cert request
64         assert opts.i in certs.keys()
65         del certs[opts.i]
66     elif args[0] == 'list':
67         # List the fake cert requests
68         print('Number of certificates and requests being tracked: %d.' % \
69               len(certs))
70         for rid, data in certs.items():
71             print('Request ID \'%s\':\n\tstatus: MONITORING\n' % rid +
72                   '\tstuck: no\n\tkey pair storage: type=FILE,' +
73                   'location=\'%s\'' % data['keyfile'] + '\n\t' +
74                   'certificate: type=FILE,location=\'%s\'' % data['certfile'] +
75                   '\n\tCA: %s\n\t' % data['ca'] +
76                   'certificate template/profile: %s\n\t' % data['template'] +
77                   'track: yes\n\tauto-renew: yes')
79     if len(cas.items()) == 0 and len(certs.items()) == 0:
80         if os.path.exists(dump_file):
81             os.unlink(dump_file)
82     else:
83         with open(dump_file, 'wb') as w:
84             pickle.dump((cas, certs), w)