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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
|
# This file is part of cloud-init. See LICENSE file for license information.
import logging
import re
import shutil
import tempfile
import unittest
from contextlib import ExitStack
from unittest import mock
import pytest
from cloudinit import distros, helpers, subp, util
from cloudinit.config import cc_ca_certs
from cloudinit.config.schema import (
SchemaValidationError,
get_schema,
validate_cloudconfig_schema,
)
from tests.unittests.helpers import TestCase, skipUnlessJsonSchema
from tests.unittests.util import get_cloud
class TestNoConfig(unittest.TestCase):
def setUp(self):
super(TestNoConfig, self).setUp()
self.name = "ca-certs"
self.cloud_init = None
self.log = logging.getLogger("TestNoConfig")
self.args = []
def test_no_config(self):
"""
Test that nothing is done if no ca-certs configuration is provided.
"""
config = util.get_builtin_cfg()
with ExitStack() as mocks:
util_mock = mocks.enter_context(
mock.patch.object(util, "write_file")
)
certs_mock = mocks.enter_context(
mock.patch.object(cc_ca_certs, "update_ca_certs")
)
cc_ca_certs.handle(
self.name, config, self.cloud_init, self.log, self.args
)
self.assertEqual(util_mock.call_count, 0)
self.assertEqual(certs_mock.call_count, 0)
class TestConfig(TestCase):
def setUp(self):
super(TestConfig, self).setUp()
self.name = "ca-certs"
self.paths = None
self.log = logging.getLogger("TestNoConfig")
self.args = []
def _fetch_distro(self, kind):
cls = distros.fetch(kind)
paths = helpers.Paths({})
return cls(kind, {}, paths)
def _mock_init(self):
self.mocks = ExitStack()
self.addCleanup(self.mocks.close)
# Mock out the functions that actually modify the system
self.mock_add = self.mocks.enter_context(
mock.patch.object(cc_ca_certs, "add_ca_certs")
)
self.mock_update = self.mocks.enter_context(
mock.patch.object(cc_ca_certs, "update_ca_certs")
)
self.mock_remove = self.mocks.enter_context(
mock.patch.object(cc_ca_certs, "remove_default_ca_certs")
)
def test_no_trusted_list(self):
"""
Test that no certificates are written if the 'trusted' key is not
present.
"""
config = {"ca-certs": {}}
for distro_name in cc_ca_certs.distros:
self._mock_init()
cloud = get_cloud(distro_name)
cc_ca_certs.handle(self.name, config, cloud, self.log, self.args)
self.assertEqual(self.mock_add.call_count, 0)
self.assertEqual(self.mock_update.call_count, 1)
self.assertEqual(self.mock_remove.call_count, 0)
def test_empty_trusted_list(self):
"""Test that no certificate are written if 'trusted' list is empty."""
config = {"ca-certs": {"trusted": []}}
for distro_name in cc_ca_certs.distros:
self._mock_init()
cloud = get_cloud(distro_name)
cc_ca_certs.handle(self.name, config, cloud, self.log, self.args)
self.assertEqual(self.mock_add.call_count, 0)
self.assertEqual(self.mock_update.call_count, 1)
self.assertEqual(self.mock_remove.call_count, 0)
def test_single_trusted(self):
"""Test that a single cert gets passed to add_ca_certs."""
config = {"ca-certs": {"trusted": ["CERT1"]}}
for distro_name in cc_ca_certs.distros:
self._mock_init()
cloud = get_cloud(distro_name)
conf = cc_ca_certs._distro_ca_certs_configs(distro_name)
cc_ca_certs.handle(self.name, config, cloud, self.log, self.args)
self.mock_add.assert_called_once_with(conf, ["CERT1"])
self.assertEqual(self.mock_update.call_count, 1)
self.assertEqual(self.mock_remove.call_count, 0)
def test_multiple_trusted(self):
"""Test that multiple certs get passed to add_ca_certs."""
config = {"ca-certs": {"trusted": ["CERT1", "CERT2"]}}
for distro_name in cc_ca_certs.distros:
self._mock_init()
cloud = get_cloud(distro_name)
conf = cc_ca_certs._distro_ca_certs_configs(distro_name)
cc_ca_certs.handle(self.name, config, cloud, self.log, self.args)
self.mock_add.assert_called_once_with(conf, ["CERT1", "CERT2"])
self.assertEqual(self.mock_update.call_count, 1)
self.assertEqual(self.mock_remove.call_count, 0)
def test_remove_default_ca_certs(self):
"""Test remove_defaults works as expected."""
config = {"ca_certs": {"remove_defaults": True}}
for distro_name in cc_ca_certs.distros:
self._mock_init()
cloud = get_cloud(distro_name)
cc_ca_certs.handle(self.name, config, cloud, self.log, self.args)
self.assertEqual(self.mock_add.call_count, 0)
self.assertEqual(self.mock_update.call_count, 1)
self.assertEqual(self.mock_remove.call_count, 1)
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}}
for distro_name in cc_ca_certs.distros:
self._mock_init()
cloud = get_cloud(distro_name)
cc_ca_certs.handle(self.name, config, cloud, self.log, self.args)
self.assertEqual(self.mock_add.call_count, 0)
self.assertEqual(self.mock_update.call_count, 1)
self.assertEqual(self.mock_remove.call_count, 0)
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"]}}
for distro_name in cc_ca_certs.distros:
self._mock_init()
cloud = get_cloud(distro_name)
conf = cc_ca_certs._distro_ca_certs_configs(distro_name)
cc_ca_certs.handle(self.name, config, cloud, self.log, self.args)
self.mock_add.assert_called_once_with(conf, ["CERT1"])
self.assertEqual(self.mock_update.call_count, 1)
self.assertEqual(self.mock_remove.call_count, 1)
class TestAddCaCerts(TestCase):
def setUp(self):
super(TestAddCaCerts, self).setUp()
tmpdir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, tmpdir)
self.paths = helpers.Paths(
{
"cloud_dir": tmpdir,
}
)
self.add_patch("cloudinit.config.cc_ca_certs.os.stat", "m_stat")
def _fetch_distro(self, kind):
cls = distros.fetch(kind)
paths = helpers.Paths({})
return cls(kind, {}, paths)
def test_no_certs_in_list(self):
"""Test that no certificate are written if not provided."""
for distro_name in cc_ca_certs.distros:
conf = cc_ca_certs._distro_ca_certs_configs(distro_name)
with mock.patch.object(util, "write_file") as mockobj:
cc_ca_certs.add_ca_certs(conf, [])
self.assertEqual(mockobj.call_count, 0)
def test_single_cert_trailing_cr(self):
"""Test adding a single certificate to the trusted CAs
when existing ca-certificates has trailing newline"""
cert = "CERT1\nLINE2\nLINE3"
ca_certs_content = "line1\nline2\ncloud-init-ca-certs.crt\nline3\n"
expected = "line1\nline2\nline3\ncloud-init-ca-certs.crt\n"
self.m_stat.return_value.st_size = 1
for distro_name in cc_ca_certs.distros:
conf = cc_ca_certs._distro_ca_certs_configs(distro_name)
with ExitStack() as mocks:
mock_write = mocks.enter_context(
mock.patch.object(util, "write_file")
)
mock_load = mocks.enter_context(
mock.patch.object(
util, "load_file", return_value=ca_certs_content
)
)
cc_ca_certs.add_ca_certs(conf, [cert])
mock_write.assert_has_calls(
[mock.call(conf["ca_cert_full_path"], cert, mode=0o644)]
)
if conf["ca_cert_config"] is not None:
mock_write.assert_has_calls(
[
mock.call(
conf["ca_cert_config"], expected, omode="wb"
)
]
)
mock_load.assert_called_once_with(conf["ca_cert_config"])
def test_single_cert_no_trailing_cr(self):
"""Test adding a single certificate to the trusted CAs
when existing ca-certificates has no trailing newline"""
cert = "CERT1\nLINE2\nLINE3"
ca_certs_content = "line1\nline2\nline3"
self.m_stat.return_value.st_size = 1
for distro_name in cc_ca_certs.distros:
conf = cc_ca_certs._distro_ca_certs_configs(distro_name)
with ExitStack() as mocks:
mock_write = mocks.enter_context(
mock.patch.object(util, "write_file")
)
mock_load = mocks.enter_context(
mock.patch.object(
util, "load_file", return_value=ca_certs_content
)
)
cc_ca_certs.add_ca_certs(conf, [cert])
mock_write.assert_has_calls(
[mock.call(conf["ca_cert_full_path"], cert, mode=0o644)]
)
if conf["ca_cert_config"] is not None:
mock_write.assert_has_calls(
[
mock.call(
conf["ca_cert_config"],
"%s\n%s\n"
% (ca_certs_content, conf["ca_cert_filename"]),
omode="wb",
)
]
)
mock_load.assert_called_once_with(conf["ca_cert_config"])
def test_single_cert_to_empty_existing_ca_file(self):
"""Test adding a single certificate to the trusted CAs
when existing ca-certificates.conf is empty"""
cert = "CERT1\nLINE2\nLINE3"
expected = "cloud-init-ca-certs.crt\n"
self.m_stat.return_value.st_size = 0
for distro_name in cc_ca_certs.distros:
conf = cc_ca_certs._distro_ca_certs_configs(distro_name)
with mock.patch.object(
util, "write_file", autospec=True
) as m_write:
cc_ca_certs.add_ca_certs(conf, [cert])
m_write.assert_has_calls(
[mock.call(conf["ca_cert_full_path"], cert, mode=0o644)]
)
if conf["ca_cert_config"] is not None:
m_write.assert_has_calls(
[
mock.call(
conf["ca_cert_config"], expected, omode="wb"
)
]
)
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)
ca_certs_content = "line1\nline2\nline3"
self.m_stat.return_value.st_size = 1
for distro_name in cc_ca_certs.distros:
conf = cc_ca_certs._distro_ca_certs_configs(distro_name)
with ExitStack() as mocks:
mock_write = mocks.enter_context(
mock.patch.object(util, "write_file")
)
mock_load = mocks.enter_context(
mock.patch.object(
util, "load_file", return_value=ca_certs_content
)
)
cc_ca_certs.add_ca_certs(conf, certs)
mock_write.assert_has_calls(
[
mock.call(
conf["ca_cert_full_path"],
expected_cert_file,
mode=0o644,
)
]
)
if conf["ca_cert_config"] is not None:
mock_write.assert_has_calls(
[
mock.call(
conf["ca_cert_config"],
"%s\n%s\n"
% (ca_certs_content, conf["ca_cert_filename"]),
omode="wb",
)
]
)
mock_load.assert_called_once_with(conf["ca_cert_config"])
class TestUpdateCaCerts(unittest.TestCase):
def test_commands(self):
for distro_name in cc_ca_certs.distros:
conf = cc_ca_certs._distro_ca_certs_configs(distro_name)
with mock.patch.object(subp, "subp") as mockobj:
cc_ca_certs.update_ca_certs(conf)
mockobj.assert_called_once_with(
conf["ca_cert_update_cmd"], capture=False
)
class TestRemoveDefaultCaCerts(TestCase):
def setUp(self):
super(TestRemoveDefaultCaCerts, self).setUp()
tmpdir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, tmpdir)
self.paths = helpers.Paths(
{
"cloud_dir": tmpdir,
}
)
def test_commands(self):
for distro_name in cc_ca_certs.distros:
conf = cc_ca_certs._distro_ca_certs_configs(distro_name)
with ExitStack() as mocks:
mock_delete = mocks.enter_context(
mock.patch.object(util, "delete_dir_contents")
)
mock_write = mocks.enter_context(
mock.patch.object(util, "write_file")
)
mock_subp = mocks.enter_context(
mock.patch.object(subp, "subp")
)
cc_ca_certs.remove_default_ca_certs(distro_name, conf)
mock_delete.assert_has_calls(
[
mock.call(conf["ca_cert_path"]),
mock.call(conf["ca_cert_system_path"]),
]
)
if conf["ca_cert_config"] is not None:
mock_write.assert_called_once_with(
conf["ca_cert_config"], "", mode=0o644
)
if distro_name in ["debian", "ubuntu"]:
mock_subp.assert_called_once_with(
("debconf-set-selections", "-"),
"ca-certificates ca-certificates/trust_new_crts"
" select no",
)
class TestCACertsSchema:
"""Directly test schema rather than through handle."""
@pytest.mark.parametrize(
"config, error_msg",
(
# Valid, yet deprecated schemas
({"ca-certs": {"remove-defaults": True}}, None),
# Invalid schemas
(
{"ca_certs": 1},
"ca_certs: 1 is not of type 'object'",
),
(
{"ca_certs": {}},
re.escape("ca_certs: {} does not have enough properties"),
),
(
{"ca_certs": {"boguskey": 1}},
re.escape(
"ca_certs: Additional properties are not allowed"
" ('boguskey' was unexpected)"
),
),
(
{"ca_certs": {"remove_defaults": 1}},
"ca_certs.remove_defaults: 1 is not of type 'boolean'",
),
(
{"ca_certs": {"trusted": [1]}},
"ca_certs.trusted.0: 1 is not of type 'string'",
),
(
{"ca_certs": {"trusted": []}},
re.escape("ca_certs.trusted: [] is too short"),
),
),
)
@skipUnlessJsonSchema()
def test_schema_validation(self, config, error_msg):
"""Assert expected schema validation and error messages."""
# New-style schema $defs exist in config/cloud-init-schema*.json
schema = get_schema()
if error_msg is None:
validate_cloudconfig_schema(config, schema, strict=True)
else:
with pytest.raises(SchemaValidationError, match=error_msg):
validate_cloudconfig_schema(config, schema, strict=True)
@mock.patch.object(cc_ca_certs, "update_ca_certs")
def test_deprecate_key_warnings(self, update_ca_certs, caplog):
"""Assert warnings are logged for deprecated keys."""
log = logging.getLogger("CALogTest")
cloud = get_cloud("ubuntu")
cc_ca_certs.handle(
"IGNORE", {"ca-certs": {"remove-defaults": False}}, cloud, log, []
)
expected_warnings = [
"DEPRECATION: key 'ca-certs' is now deprecated. Use 'ca_certs'"
" instead.",
"DEPRECATION: key 'ca-certs.remove-defaults' is now deprecated."
" Use 'ca_certs.remove_defaults' instead.",
]
for warning in expected_warnings:
assert warning in caplog.text
assert 1 == update_ca_certs.call_count
@mock.patch.object(cc_ca_certs, "update_ca_certs")
def test_duplicate_keys(self, update_ca_certs, caplog):
"""Assert warnings are logged for deprecated keys."""
log = logging.getLogger("CALogTest")
cloud = get_cloud("ubuntu")
cc_ca_certs.handle(
"IGNORE",
{
"ca-certs": {"remove-defaults": True},
"ca_certs": {"remove_defaults": False},
},
cloud,
log,
[],
)
expected_warning = (
"Found both ca-certs (deprecated) and ca_certs config keys."
" Ignoring ca-certs."
)
assert expected_warning in caplog.text
assert 1 == update_ca_certs.call_count
# vi: ts=4 expandtab
|