summaryrefslogtreecommitdiff
path: root/tests/unittests/test_handler_ca_certs.py
blob: d6513b5bb4a58468e16db9e9fc004b9414ccf452 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from mocker import MockerTestCase

from cloudinit.util import write_file, delete_dir_contents
from cloudinit.CloudConfig.cc_ca_certs import (
    handle, update_ca_certs, add_ca_certs, remove_default_ca_certs)
from logging import getLogger


class TestNoConfig(MockerTestCase):
    def setUp(self):
        super(TestNoConfig, self).setUp()
        self.name = "ca-certs"
        self.cloud_init = None
        self.log = getLogger("TestNoConfig")
        self.args = []

    def test_no_config(self):
        """
        Test that nothing is done if no ca-certs configuration is provided.
        """
        config = {"unknown-key": "value"}

        self.mocker.replace(write_file, passthrough=False)
        self.mocker.replace(update_ca_certs, passthrough=False)
        self.mocker.replay()

        handle(self.name, config, self.cloud_init, self.log, self.args)


class TestConfig(MockerTestCase):
    def setUp(self):
        super(TestConfig, self).setUp()
        self.name = "ca-certs"
        self.cloud_init = None
        self.log = getLogger("TestNoConfig")
        self.args = []

        # Mock out the functions that actually modify the system
        self.mock_add = self.mocker.replace(add_ca_certs, passthrough=False)
        self.mock_update = self.mocker.replace(update_ca_certs,
                                               passthrough=False)
        self.mock_remove = self.mocker.replace(remove_default_ca_certs,
                                               passthrough=False)
        # Order must be correct
        self.mocker.order()

    def test_no_trusted_list(self):
        """
        Test that no certificates are written if the 'trusted' key is not
        present.
        """
        config = {"ca-certs": {}}

        # No functions should be called
        self.mock_update()
        self.mocker.replay()

        handle(self.name, config, self.cloud_init, self.log, self.args)

    def test_empty_trusted_list(self):
        """Test that no certificate are written if 'trusted' list is empty"""
        config = {"ca-certs": {"trusted": []}}

        # No functions should be called
        self.mock_update()
        self.mocker.replay()

        handle(self.name, config, self.cloud_init, self.log, self.args)

    def test_single_trusted(self):
        """Test that a single cert gets passed to add_ca_certs"""
        config = {"ca-certs": {"trusted": ["CERT1"]}}

        self.mock_add(["CERT1"])
        self.mock_update()
        self.mocker.replay()

        handle(self.name, config, self.cloud_init, self.log, self.args)

    def test_multiple_trusted(self):
        """Test that multiple certs get passed to add_ca_certs"""
        config = {"ca-certs": {"trusted": ["CERT1", "CERT2"]}}

        self.mock_add(["CERT1", "CERT2"])
        self.mock_update()
        self.mocker.replay()

        handle(self.name, config, self.cloud_init, self.log, self.args)

    def test_remove_default_ca_certs(self):
        """Test remove_defaults works as expected"""
        config = {"ca-certs": {"remove-defaults": True}}

        self.mock_remove()
        self.mock_update()
        self.mocker.replay()

        handle(self.name, config, self.cloud_init, self.log, self.args)

    def test_no_remove_defaults_if_false(self):
        """Test remove_defaults is not called when config value is False"""
        config = {"ca-certs": {"remove-defaults": False}}

        self.mock_update()
        self.mocker.replay()

        handle(self.name, config, self.cloud_init, self.log, self.args)

    def test_correct_order_for_remove_then_add(self):
        """Test remove_defaults is not called when config value is False"""
        config = {"ca-certs": {"remove-defaults": True, "trusted": ["CERT1"]}}

        self.mock_remove()
        self.mock_add(["CERT1"])
        self.mock_update()
        self.mocker.replay()

        handle(self.name, config, self.cloud_init, self.log, self.args)


class TestAddCaCerts(MockerTestCase):
    def test_no_certs_in_list(self):
        """Test that no certificate are written if not provided."""
        self.mocker.replace(write_file, passthrough=False)
        self.mocker.replay()

        add_ca_certs([])

    def test_single_cert(self):
        """Test adding a single certificate to the trusted CAs"""
        cert = "CERT1\nLINE2\nLINE3"

        mock_write = self.mocker.replace(write_file, passthrough=False)
        mock_write("/usr/share/ca-certificates/cloud-init-ca-certs.crt",
                   cert, mode=0644)
        mock_write("/etc/ca-certificates.conf",
                   "\ncloud-init-ca-certs.crt", omode="a")
        self.mocker.replay()

        add_ca_certs([cert])

    def test_multiple_certs(self):
        """Test adding multiple certificates to the trusted CAs"""
        certs = ["CERT1\nLINE2\nLINE3", "CERT2\nLINE2\nLINE3"]
        expected_cert_file = "\n".join(certs)

        mock_write = self.mocker.replace(write_file, passthrough=False)
        mock_write("/usr/share/ca-certificates/cloud-init-ca-certs.crt",
                   expected_cert_file, mode=0644)
        mock_write("/etc/ca-certificates.conf",
                   "\ncloud-init-ca-certs.crt", omode="a")
        self.mocker.replay()

        add_ca_certs(certs)


class TestUpdateCaCerts(MockerTestCase):
    def test_commands(self):
        mock_check_call = self.mocker.replace("subprocess.check_call",
                                              passthrough=False)
        mock_check_call(["update-ca-certificates"])
        self.mocker.replay()

        update_ca_certs()


class TestRemoveDefaultCaCerts(MockerTestCase):
    def test_commands(self):
        mock_delete_dir_contents = self.mocker.replace(delete_dir_contents,
                                                       passthrough=False)
        mock_write = self.mocker.replace(write_file, passthrough=False)

        mock_delete_dir_contents("/usr/share/ca-certificates/")
        mock_delete_dir_contents("/etc/ssl/certs/")
        mock_write("/etc/ca-certificates.conf", "", mode=0644)
        self.mocker.replay()

        remove_default_ca_certs()