Renew cluster and client certificates together
[ganeti-github.git] / test / py / ganeti.tools.common_unittest.py
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2015 Google Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are
9 # met:
10 #
11 # 1. Redistributions of source code must retain the above copyright notice,
12 # this list of conditions and the following disclaimer.
13 #
14 # 2. Redistributions in binary form must reproduce the above copyright
15 # notice, this list of conditions and the following disclaimer in the
16 # documentation and/or other materials provided with the distribution.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
19 # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30
31 """Script for testing ganeti.tools.ssl_update"""
32
33 import unittest
34 import shutil
35 import tempfile
36 import os.path
37 import OpenSSL
38 import time
39
40 from ganeti import constants
41 from ganeti import utils
42 from ganeti.tools import common
43
44 import testutils
45
46
47 class TestGenerateClientCert(unittest.TestCase):
48
49 def setUp(self):
50 self.tmpdir = tempfile.mkdtemp()
51
52 self.client_cert = os.path.join(self.tmpdir, "client.pem")
53
54 self.server_cert = os.path.join(self.tmpdir, "server.pem")
55 some_serial_no = int(time.time())
56 utils.GenerateSelfSignedSslCert(self.server_cert, some_serial_no)
57
58 def tearDown(self):
59 shutil.rmtree(self.tmpdir)
60
61 def testRegnerateClientCertificate(self):
62 my_node_name = "mynode.example.com"
63 data = {constants.NDS_CLUSTER_NAME: "winnie_poohs_cluster",
64 constants.NDS_NODE_DAEMON_CERTIFICATE: "some_cert",
65 constants.NDS_NODE_NAME: my_node_name}
66
67 common.GenerateClientCertificate(data, Exception,
68 client_cert=self.client_cert,
69 signing_cert=self.server_cert)
70
71 client_cert_pem = utils.ReadFile(self.client_cert)
72 server_cert_pem = utils.ReadFile(self.server_cert)
73 client_cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
74 client_cert_pem)
75 signing_cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,
76 server_cert_pem)
77 self.assertEqual(client_cert.get_issuer().CN, signing_cert.get_subject().CN)
78 self.assertEqual(client_cert.get_subject().CN, my_node_name)
79
80
81 if __name__ == "__main__":
82 testutils.GanetiTestProgram()