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()
21 assert args[0] in ['add-ca', 'request', 'remove-ca', 'stop-tracking',
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'))
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)
36 if args[0] == 'add-ca':
38 assert opts.c not in cas.keys()
40 elif args[0] == 'remove-ca':
41 # Remove a fake CA entry
42 assert opts.c in cas.keys()
44 elif args[0] == 'list-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),
57 # Create dummy key and cert (empty files)
58 with open(opts.k, 'w') as w:
60 with open(opts.f, 'w') as w:
62 elif args[0] == 'stop-tracking':
63 # Remove the fake cert request
64 assert opts.i in certs.keys()
66 elif args[0] == 'list':
67 # List the fake cert requests
68 print('Number of certificates and requests being tracked: %d.' % \
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):
83 with open(dump_file, 'wb') as w:
84 pickle.dump((cas, certs), w)