1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import os
17 import sys
18 from optparse import Option, OptionParser
19
20 from spacewalk.common import rhnTB
21 from spacewalk.server import rhnSQL
22
23 import satCerts
24
25 DEFAULT_TRUSTED_CERT = 'RHN-ORG-TRUSTED-SSL-CERT'
26
27
29
30 options = [
31 Option('--ca-cert', action='store', default=DEFAULT_TRUSTED_CERT, type="string",
32 help='public CA certificate, default is %s' % DEFAULT_TRUSTED_CERT),
33 Option('--label', action='store', default='RHN-ORG-TRUSTED-SSL-CERT', type="string",
34 help='FOR TESTING ONLY - alternative database label for this CA certificate, '
35 + 'default is "RHN-ORG-TRUSTED-SSL-CERT"'),
36 Option('-v', '--verbose', action='count',
37 help='be verbose (accumulable: -vvv means "be *really* verbose").'),
38 ]
39
40 values, args = OptionParser(option_list=options).parse_args()
41
42
43 if args:
44 msg = ("ERROR: these arguments make no sense in this context (try "
45 "--help): %s\n" % repr(args))
46 raise ValueError(msg)
47
48 if not os.path.exists(values.ca_cert):
49 sys.stderr.write("ERROR: can't find CA certificate at this location: "
50 "%s\n" % values.ca_cert)
51 sys.exit(10)
52
53
54 try:
55 rhnSQL.initDB()
56 except Exception:
57 sys.stderr.write("""\
58 ERROR: there was a problem trying to initialize the database:
59
60 %s\n""" % rhnTB.fetchTraceback())
61 sys.exit(11)
62
63 if values.verbose:
64 print('Public CA SSL certificate: %s' % values.ca_cert)
65
66 return values
67
68
69
70
72 """ main routine
73 10 CA certificate not found
74 11 DB initialization failure
75 13 Couldn't insert the certificate for whatever reason.
76 """
77
78 values = processCommandline()
79
80 def writeError(e):
81 sys.stderr.write('\nERROR: %s\n' % e)
82
83 try:
84 satCerts.store_CaCert(values.label, values.ca_cert, verbosity=values.verbose)
85 except satCerts.CaCertInsertionError:
86 writeError("Cannot insert certificate into DB!\n\n%s\n" % rhnTB.fetchTraceback())
87 sys.exit(13)
88 return 0
89
90
91 if __name__ == "__main__":
92 sys.stderr.write('\nWARNING: intended to be wrapped by another executable\n'
93 ' calling program.\n')
94 sys.exit(main() or 0)
95
96