1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 """Common functions for tool scripts.
31
32 """
33
34 import logging
35 import OpenSSL
36 import os
37 import time
38 from cStringIO import StringIO
39
40 from ganeti import constants
41 from ganeti import pathutils
42 from ganeti import utils
43 from ganeti import serializer
44 from ganeti import ssconf
45
46
48 """Verifies options and arguments for correctness.
49
50 """
51 if args:
52 parser.error("No arguments are expected")
53
54 return opts
55
56
59 """Verifies a certificate against the local node daemon certificate.
60
61 @type cert_pem: string
62 @param cert_pem: Certificate and key in PEM format
63 @type error_fn: callable
64 @param error_fn: function to call in case of an error
65 @rtype: string
66 @return: Formatted key and certificate
67
68 """
69 try:
70 cert = \
71 OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert_pem)
72 except Exception, err:
73 raise error_fn("(stdin) Unable to load certificate: %s" % err)
74
75 try:
76 key = OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM, cert_pem)
77 except OpenSSL.crypto.Error, err:
78 raise error_fn("(stdin) Unable to load private key: %s" % err)
79
80
81
82 try:
83 utils.X509CertKeyCheck(cert, key)
84 except OpenSSL.SSL.Error:
85 raise error_fn("(stdin) Certificate is not signed with given key")
86
87
88
89 _check_fn(cert)
90
91 key_encoded = OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, key)
92 cert_encoded = OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM,
93 cert)
94 complete_cert_encoded = key_encoded + cert_encoded
95 if not cert_pem == complete_cert_encoded:
96 logging.error("The certificate differs after being reencoded. Please"
97 " renew the certificates cluster-wide to prevent future"
98 " inconsistencies.")
99
100
101 buf = StringIO()
102 buf.write(cert_pem)
103 return buf.getvalue()
104
105
107 """Verifies cluster certificate.
108
109 @type data: dict
110 @type error_fn: callable
111 @param error_fn: function to call in case of an error
112 @rtype: string
113 @return: Formatted key and certificate
114
115 """
116 cert = data.get(constants.NDS_NODE_DAEMON_CERTIFICATE)
117 if not cert:
118 raise error_fn("Node daemon certificate must be specified")
119
120 return _verify_fn(cert, error_fn)
121
122
125 """Verifies cluster name.
126
127 @type data: dict
128
129 """
130 name = data.get(constants.SSHS_CLUSTER_NAME)
131 if name:
132 _verify_fn(name)
133 else:
134 raise error_fn("Cluster name must be specified")
135
136
144
145
149 """Regenerates the client certificate of the node.
150
151 @type data: string
152 @param data: the JSON-formated input data
153
154 """
155 if not os.path.exists(signing_cert):
156 raise error_fn("The signing certificate '%s' cannot be found."
157 % signing_cert)
158
159
160
161
162
163
164 serial_no = int(time.time())
165
166
167 hostname = data.get(constants.NDS_NODE_NAME)
168 if not hostname:
169 raise error_fn("No hostname found.")
170
171 utils.GenerateSignedSslCert(client_cert, serial_no, signing_cert,
172 common_name=hostname)
173