From fb0ff769bdce25497949770d392f43b2888a732b Mon Sep 17 00:00:00 2001 From: Mike Milner Date: Thu, 12 Jan 2012 18:51:48 +0100 Subject: Add tests for ca-certs handler. --- tests/unittests/test_handler_ca_certs.py | 64 ++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/unittests/test_handler_ca_certs.py (limited to 'tests/unittests/test_handler_ca_certs.py') diff --git a/tests/unittests/test_handler_ca_certs.py b/tests/unittests/test_handler_ca_certs.py new file mode 100644 index 00000000..21eddf18 --- /dev/null +++ b/tests/unittests/test_handler_ca_certs.py @@ -0,0 +1,64 @@ +from unittest import TestCase +from mocker import MockerTestCase + +from cloudinit.CloudConfig.cc_ca_certs import handle, write_file + +class TestAddCaCerts(MockerTestCase): + def setUp(self): + super(TestAddCaCerts, self).setUp() + self.name = "ca-certs" + self.cloud_init = None + self.log = None + self.args = [] + + def test_no_config(self): + """Test that no certificate are written if not provided.""" + config = {"unknown-key": "value"} + + mock = self.mocker.replace(write_file, passthrough=False) + self.mocker.replay() + + handle(self.name, config, self.cloud_init, self.log, self.args) + + def test_no_trusted_list(self): + """Test that no certificate are written if not provided.""" + config = {"ca-certs": {}} + + mock = self.mocker.replace(write_file, passthrough=False) + self.mocker.replay() + + handle(self.name, config, self.cloud_init, self.log, self.args) + + def test_no_certs_in_list(self): + """Test that no certificate are written if not provided.""" + config = {"ca-certs": {"trusted": []}} + + mock = self.mocker.replace(write_file, passthrough=False) + self.mocker.replay() + + handle(self.name, config, self.cloud_init, self.log, self.args) + + def test_single_cert(self): + """Test adding a single certificate to the trusted CAs""" + cert = "CERT1\nLINE2\nLINE3" + config = {"ca-certs": {"trusted": cert}} + + mock = self.mocker.replace(write_file, passthrough=False) + mock("/usr/share/ca-certificates/cloud-init-provided.crt", + cert, "root", "root", "644") + self.mocker.replay() + + handle(self.name, config, self.cloud_init, self.log, self.args) + + def test_multiple_certs(self): + """Test adding multiple certificate to the trusted CAs""" + certs = ["CERT1\nLINE2\nLINE3", "CERT2\nLINE2\nLINE3"] + cert_file = "\n".join(certs) + config = {"ca-certs": {"trusted": certs}} + + mock = self.mocker.replace(write_file, passthrough=False) + mock("/usr/share/ca-certificates/cloud-init-provided.crt", + cert_file, "root", "root", "644") + self.mocker.replay() + + handle(self.name, config, self.cloud_init, self.log, self.args) -- cgit v1.2.3 From a717e4f8b9210374edcc9053ca6ff980cb0cefff Mon Sep 17 00:00:00 2001 From: Mike Milner Date: Thu, 12 Jan 2012 19:28:09 +0100 Subject: Add ability to rebuild CA certificate file. --- cloudinit/CloudConfig/cc_ca_certs.py | 25 +++++++++++++++++++-- tests/unittests/test_handler_ca_certs.py | 37 +++++++++++++++++++++++++++----- 2 files changed, 55 insertions(+), 7 deletions(-) (limited to 'tests/unittests/test_handler_ca_certs.py') diff --git a/cloudinit/CloudConfig/cc_ca_certs.py b/cloudinit/CloudConfig/cc_ca_certs.py index e2110890..81ed7237 100644 --- a/cloudinit/CloudConfig/cc_ca_certs.py +++ b/cloudinit/CloudConfig/cc_ca_certs.py @@ -16,7 +16,7 @@ import os import pwd import socket -import subprocess +from subprocess import check_call import json import StringIO import ConfigParser @@ -26,10 +26,29 @@ import cloudinit.util as util CERT_FILENAME = "/usr/share/ca-certificates/cloud-init-provided.crt" def write_file(filename, contents, owner, group, mode): - raise Exception() + """ + Write a file to disk with specified owner, group, and mode. If the file + exists already it will be overwritten. + + @param filename: Full path to the new file. + @param contents: The contents of the newly created file. + @param owner: The username who should own the file. + @param group: The group for the new file. + @param mode: The octal mode (as string) for the new file. + """ + raise NotImplementedError() + +def update_ca_certs(): + """ + Updates the CA certificate cache on the current machine. + """ + check_call(["dpkg-reconfigure", "ca-certificates"]) + check_call(["update-ca-certificates"]) def handle(name, cfg, cloud, log, args): """ + Call to handle ca-cert sections in cloud-config file. + @param name: The module name "ca-cert" from cloud.cfg @param cfg: A nested dict containing the entire cloud config contents. @param cloud: The L{CloudInit} object in use @@ -49,3 +68,5 @@ def handle(name, cfg, cloud, log, args): if trusted_certs: cert_file_contents = "\n".join(trusted_certs) write_file(CERT_FILENAME, cert_file_contents, "root", "root", "644") + + update_ca_certs() diff --git a/tests/unittests/test_handler_ca_certs.py b/tests/unittests/test_handler_ca_certs.py index 21eddf18..254c8727 100644 --- a/tests/unittests/test_handler_ca_certs.py +++ b/tests/unittests/test_handler_ca_certs.py @@ -1,25 +1,42 @@ from unittest import TestCase from mocker import MockerTestCase -from cloudinit.CloudConfig.cc_ca_certs import handle, write_file +from cloudinit.CloudConfig.cc_ca_certs import handle, write_file, update_ca_certs -class TestAddCaCerts(MockerTestCase): +class TestNoConfig(MockerTestCase): def setUp(self): - super(TestAddCaCerts, self).setUp() + super(TestNoConfig, self).setUp() self.name = "ca-certs" self.cloud_init = None self.log = None self.args = [] def test_no_config(self): - """Test that no certificate are written if not provided.""" + """ + Test that nothing is done if no ca-certs configuration is provided. + """ config = {"unknown-key": "value"} - mock = self.mocker.replace(write_file, passthrough=False) + 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 TestAddCaCerts(MockerTestCase): + def setUp(self): + super(TestAddCaCerts, self).setUp() + self.name = "ca-certs" + self.cloud_init = None + self.log = None + self.args = [] + + # The config option is present for all these tests so + # update_ca_certs should always be called. + mock = self.mocker.replace(update_ca_certs, passthrough=False) + mock() + def test_no_trusted_list(self): """Test that no certificate are written if not provided.""" config = {"ca-certs": {}} @@ -62,3 +79,13 @@ class TestAddCaCerts(MockerTestCase): self.mocker.replay() handle(self.name, config, self.cloud_init, self.log, self.args) + +class TestUpdateCaCerts(MockerTestCase): + def test_commands(self): + mock_check_call = self.mocker.replace("subprocess.check_call", + passthrough=False) + mock_check_call(["dpkg-reconfigure", "ca-certificates"]) + mock_check_call(["update-ca-certificates"]) + self.mocker.replay() + + update_ca_certs() -- cgit v1.2.3 From 094e915e91186401ebc7c97564917334faade150 Mon Sep 17 00:00:00 2001 From: Mike Milner Date: Sat, 14 Jan 2012 10:49:09 +0000 Subject: Factor out writing of certificates. --- cloudinit/CloudConfig/cc_ca_certs.py | 15 ++++++++++++--- tests/unittests/test_handler_ca_certs.py | 22 ++++++++++------------ 2 files changed, 22 insertions(+), 15 deletions(-) (limited to 'tests/unittests/test_handler_ca_certs.py') diff --git a/cloudinit/CloudConfig/cc_ca_certs.py b/cloudinit/CloudConfig/cc_ca_certs.py index 81ed7237..07074e2f 100644 --- a/cloudinit/CloudConfig/cc_ca_certs.py +++ b/cloudinit/CloudConfig/cc_ca_certs.py @@ -45,6 +45,17 @@ def update_ca_certs(): check_call(["dpkg-reconfigure", "ca-certificates"]) check_call(["update-ca-certificates"]) +def add_ca_certs(certs): + """ + Adds certificates to the system. To actually apply the new certificates + you must also call L{update_ca_certs}. + + @param certs: A list of certificate strings. + """ + if certs: + cert_file_contents = "\n".join(certs) + write_file(CERT_FILENAME, cert_file_contents, "root", "root", "644") + def handle(name, cfg, cloud, log, args): """ Call to handle ca-cert sections in cloud-config file. @@ -66,7 +77,5 @@ def handle(name, cfg, cloud, log, args): if ca_cert_cfg.has_key('trusted'): trusted_certs = util.get_cfg_option_list_or_str(ca_cert_cfg, 'trusted') if trusted_certs: - cert_file_contents = "\n".join(trusted_certs) - write_file(CERT_FILENAME, cert_file_contents, "root", "root", "644") - + add_ca_certs(trusted_certs) update_ca_certs() diff --git a/tests/unittests/test_handler_ca_certs.py b/tests/unittests/test_handler_ca_certs.py index 254c8727..7c0197ed 100644 --- a/tests/unittests/test_handler_ca_certs.py +++ b/tests/unittests/test_handler_ca_certs.py @@ -1,7 +1,7 @@ from unittest import TestCase from mocker import MockerTestCase -from cloudinit.CloudConfig.cc_ca_certs import handle, write_file, update_ca_certs +from cloudinit.CloudConfig.cc_ca_certs import handle, write_file, update_ca_certs, add_ca_certs class TestNoConfig(MockerTestCase): def setUp(self): @@ -24,9 +24,9 @@ class TestNoConfig(MockerTestCase): handle(self.name, config, self.cloud_init, self.log, self.args) -class TestAddCaCerts(MockerTestCase): +class TestConfig(MockerTestCase): def setUp(self): - super(TestAddCaCerts, self).setUp() + super(TestConfig, self).setUp() self.name = "ca-certs" self.cloud_init = None self.log = None @@ -46,39 +46,37 @@ class TestAddCaCerts(MockerTestCase): 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.""" - config = {"ca-certs": {"trusted": []}} - mock = self.mocker.replace(write_file, passthrough=False) self.mocker.replay() - handle(self.name, config, self.cloud_init, self.log, self.args) + add_ca_certs([]) def test_single_cert(self): """Test adding a single certificate to the trusted CAs""" cert = "CERT1\nLINE2\nLINE3" - config = {"ca-certs": {"trusted": cert}} mock = self.mocker.replace(write_file, passthrough=False) mock("/usr/share/ca-certificates/cloud-init-provided.crt", cert, "root", "root", "644") self.mocker.replay() - handle(self.name, config, self.cloud_init, self.log, self.args) + add_ca_certs([cert]) def test_multiple_certs(self): """Test adding multiple certificate to the trusted CAs""" certs = ["CERT1\nLINE2\nLINE3", "CERT2\nLINE2\nLINE3"] - cert_file = "\n".join(certs) - config = {"ca-certs": {"trusted": certs}} + expected_cert_file = "\n".join(certs) mock = self.mocker.replace(write_file, passthrough=False) mock("/usr/share/ca-certificates/cloud-init-provided.crt", - cert_file, "root", "root", "644") + expected_cert_file, "root", "root", "644") self.mocker.replay() - handle(self.name, config, self.cloud_init, self.log, self.args) + add_ca_certs(certs) class TestUpdateCaCerts(MockerTestCase): def test_commands(self): -- cgit v1.2.3 From 667a3da2be1c6351496d3584ee658d58f479f4b0 Mon Sep 17 00:00:00 2001 From: Mike Milner Date: Sat, 14 Jan 2012 12:22:27 -0400 Subject: Handle config flag for removing default trusted CAs. --- cloudinit/CloudConfig/cc_ca_certs.py | 9 ++++ tests/unittests/test_handler_ca_certs.py | 93 +++++++++++++++++++++++++++++--- 2 files changed, 95 insertions(+), 7 deletions(-) (limited to 'tests/unittests/test_handler_ca_certs.py') diff --git a/cloudinit/CloudConfig/cc_ca_certs.py b/cloudinit/CloudConfig/cc_ca_certs.py index 07074e2f..a51dbe9f 100644 --- a/cloudinit/CloudConfig/cc_ca_certs.py +++ b/cloudinit/CloudConfig/cc_ca_certs.py @@ -56,6 +56,12 @@ def add_ca_certs(certs): cert_file_contents = "\n".join(certs) write_file(CERT_FILENAME, cert_file_contents, "root", "root", "644") +def remove_default_ca_certs(): + """ + Removes all default trusted CA certificates from the system. + """ + raise NotImplementedError() + def handle(name, cfg, cloud, log, args): """ Call to handle ca-cert sections in cloud-config file. @@ -71,6 +77,9 @@ def handle(name, cfg, cloud, log, args): return ca_cert_cfg = cfg['ca-certs'] + if ca_cert_cfg.get("remove-defaults", False): + remove_default_ca_certs() + # set the validation key based on the presence of either 'validation_key' # or 'validation_cert'. In the case where both exist, 'validation_key' # takes precedence diff --git a/tests/unittests/test_handler_ca_certs.py b/tests/unittests/test_handler_ca_certs.py index 7c0197ed..08126d19 100644 --- a/tests/unittests/test_handler_ca_certs.py +++ b/tests/unittests/test_handler_ca_certs.py @@ -1,7 +1,8 @@ from unittest import TestCase from mocker import MockerTestCase -from cloudinit.CloudConfig.cc_ca_certs import handle, write_file, update_ca_certs, add_ca_certs +from cloudinit.CloudConfig.cc_ca_certs import handle, write_file, update_ca_certs, add_ca_certs, remove_default_ca_certs + class TestNoConfig(MockerTestCase): def setUp(self): @@ -32,16 +33,82 @@ class TestConfig(MockerTestCase): self.log = None self.args = [] - # The config option is present for all these tests so - # update_ca_certs should always be called. - mock = self.mocker.replace(update_ca_certs, passthrough=False) - mock() + # 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 certificate are written if not provided.""" + """ + Test that no certificates are written if the 'trusted' key is not + present. + """ config = {"ca-certs": {}} - mock = self.mocker.replace(write_file, passthrough=False) + # 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) @@ -78,6 +145,7 @@ class TestAddCaCerts(MockerTestCase): add_ca_certs(certs) + class TestUpdateCaCerts(MockerTestCase): def test_commands(self): mock_check_call = self.mocker.replace("subprocess.check_call", @@ -87,3 +155,14 @@ class TestUpdateCaCerts(MockerTestCase): self.mocker.replay() update_ca_certs() + + +#class TestRemoveDefaultCaCerts(MockerTestCase): +# def test_commands(self): +# mock_check_call = self.mocker.replace("subprocess.check_call", +# passthrough=False) +# mock_check_call(["dpkg-reconfigure", "ca-certificates"]) +# mock_check_call(["update-ca-certificates"]) +# self.mocker.replay() +# +# update_ca_certs() -- cgit v1.2.3 From db55fc96f62258598cfdf98ee806151aa0fb2d6d Mon Sep 17 00:00:00 2001 From: Mike Milner Date: Sun, 15 Jan 2012 22:17:25 -0400 Subject: Added function for deleting default trusted CA certs. --- cloudinit/CloudConfig/cc_ca_certs.py | 33 ++++++++++++++++++++++---- tests/unittests/test_handler_ca_certs.py | 40 ++++++++++++++++++-------------- 2 files changed, 52 insertions(+), 21 deletions(-) (limited to 'tests/unittests/test_handler_ca_certs.py') diff --git a/cloudinit/CloudConfig/cc_ca_certs.py b/cloudinit/CloudConfig/cc_ca_certs.py index a51dbe9f..b2ac7d60 100644 --- a/cloudinit/CloudConfig/cc_ca_certs.py +++ b/cloudinit/CloudConfig/cc_ca_certs.py @@ -23,7 +23,10 @@ import ConfigParser import cloudinit.CloudConfig as cc import cloudinit.util as util -CERT_FILENAME = "/usr/share/ca-certificates/cloud-init-provided.crt" +CA_CERT_PATH = "/usr/share/ca-certificates/" +CA_CERT_FILENAME = "cloud-init-ca-certs.crt" +CA_CERT_CONFIG = "/etc/ca-certificates.conf" +CA_CERT_SYSTEM_PATH = "/etc/ssl/certs/" def write_file(filename, contents, owner, group, mode): """ @@ -38,11 +41,29 @@ def write_file(filename, contents, owner, group, mode): """ raise NotImplementedError() +def append_to_file(filename, contents): + """ + Append C{contents} to an existing file on the filesystem. If the file + doesn't exist it will be created with the default owner and permissions. + + @param filename: Full path to the new file. + @param contents: The contents to append to the file. + """ + raise NotImplementedError() + +def delete_dir_contents(dirname): + """ + Delete all the contents of the directory specified by C{dirname} without + deleting the directory itself. + + @param dirname: The directory whose contents should be deleted. + """ + raise NotImplementedError() + def update_ca_certs(): """ Updates the CA certificate cache on the current machine. """ - check_call(["dpkg-reconfigure", "ca-certificates"]) check_call(["update-ca-certificates"]) def add_ca_certs(certs): @@ -54,13 +75,17 @@ def add_ca_certs(certs): """ if certs: cert_file_contents = "\n".join(certs) - write_file(CERT_FILENAME, cert_file_contents, "root", "root", "644") + cert_file_fullpath = os.path.join(CA_CERT_PATH, CA_CERT_FILENAME) + write_file(cert_file_fullpath, cert_file_contents, "root", "root", "644") + append_to_file(CA_CERT_CONFIG, CA_CERT_FILENAME) def remove_default_ca_certs(): """ Removes all default trusted CA certificates from the system. """ - raise NotImplementedError() + delete_dir_contents(CA_CERT_PATH) + delete_dir_contents(CA_CERT_SYSTEM_PATH) + write_file(CA_CERT_CONFIG, "", "root", "root", "644") def handle(name, cfg, cloud, log, args): """ diff --git a/tests/unittests/test_handler_ca_certs.py b/tests/unittests/test_handler_ca_certs.py index 08126d19..7c6dc873 100644 --- a/tests/unittests/test_handler_ca_certs.py +++ b/tests/unittests/test_handler_ca_certs.py @@ -1,7 +1,7 @@ from unittest import TestCase from mocker import MockerTestCase -from cloudinit.CloudConfig.cc_ca_certs import handle, write_file, update_ca_certs, add_ca_certs, remove_default_ca_certs +from cloudinit.CloudConfig.cc_ca_certs import handle, write_file, update_ca_certs, add_ca_certs, remove_default_ca_certs, append_to_file, delete_dir_contents class TestNoConfig(MockerTestCase): @@ -126,9 +126,11 @@ class TestAddCaCerts(MockerTestCase): """Test adding a single certificate to the trusted CAs""" cert = "CERT1\nLINE2\nLINE3" - mock = self.mocker.replace(write_file, passthrough=False) - mock("/usr/share/ca-certificates/cloud-init-provided.crt", - cert, "root", "root", "644") + mock_write = self.mocker.replace(write_file, passthrough=False) + mock_append = self.mocker.replace(append_to_file, passthrough=False) + mock_write("/usr/share/ca-certificates/cloud-init-ca-certs.crt", + cert, "root", "root", "644") + mock_append("/etc/ca-certificates.conf", "cloud-init-ca-certs.crt") self.mocker.replay() add_ca_certs([cert]) @@ -138,9 +140,11 @@ class TestAddCaCerts(MockerTestCase): certs = ["CERT1\nLINE2\nLINE3", "CERT2\nLINE2\nLINE3"] expected_cert_file = "\n".join(certs) - mock = self.mocker.replace(write_file, passthrough=False) - mock("/usr/share/ca-certificates/cloud-init-provided.crt", - expected_cert_file, "root", "root", "644") + mock_write = self.mocker.replace(write_file, passthrough=False) + mock_append = self.mocker.replace(append_to_file, passthrough=False) + mock_write("/usr/share/ca-certificates/cloud-init-ca-certs.crt", + expected_cert_file, "root", "root", "644") + mock_append("/etc/ca-certificates.conf", "cloud-init-ca-certs.crt") self.mocker.replay() add_ca_certs(certs) @@ -150,19 +154,21 @@ class TestUpdateCaCerts(MockerTestCase): def test_commands(self): mock_check_call = self.mocker.replace("subprocess.check_call", passthrough=False) - mock_check_call(["dpkg-reconfigure", "ca-certificates"]) mock_check_call(["update-ca-certificates"]) self.mocker.replay() update_ca_certs() -#class TestRemoveDefaultCaCerts(MockerTestCase): -# def test_commands(self): -# mock_check_call = self.mocker.replace("subprocess.check_call", -# passthrough=False) -# mock_check_call(["dpkg-reconfigure", "ca-certificates"]) -# 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", "", "root", "root", "644") + + self.mocker.replay() + + remove_default_ca_certs() -- cgit v1.2.3 From dfadfb05c1d8fcb50f97952a6ba7ca662babeba4 Mon Sep 17 00:00:00 2001 From: Mike Milner Date: Tue, 17 Jan 2012 10:22:29 -0400 Subject: Convert code to use the write_file function from cloudinit.util. --- cloudinit/CloudConfig/cc_ca_certs.py | 34 ++++++-------------------------- tests/unittests/test_handler_ca_certs.py | 17 ++++++++-------- 2 files changed, 14 insertions(+), 37 deletions(-) (limited to 'tests/unittests/test_handler_ca_certs.py') diff --git a/cloudinit/CloudConfig/cc_ca_certs.py b/cloudinit/CloudConfig/cc_ca_certs.py index cec70e5c..9d7dcf7f 100644 --- a/cloudinit/CloudConfig/cc_ca_certs.py +++ b/cloudinit/CloudConfig/cc_ca_certs.py @@ -21,36 +21,13 @@ import json import StringIO import ConfigParser import cloudinit.CloudConfig as cc -import cloudinit.util as util +from cloudinit.util import write_file, get_cfg_option_list_or_str CA_CERT_PATH = "/usr/share/ca-certificates/" CA_CERT_FILENAME = "cloud-init-ca-certs.crt" CA_CERT_CONFIG = "/etc/ca-certificates.conf" CA_CERT_SYSTEM_PATH = "/etc/ssl/certs/" -def write_file(filename, contents, owner, group, mode): - """ - Write a file to disk with specified owner, group, and mode. If the file - exists already it will be overwritten. - - @param filename: Full path to the new file. - @param contents: The contents of the newly created file. - @param owner: The username who should own the file. - @param group: The group for the new file. - @param mode: The octal mode (as string) for the new file. - """ - raise NotImplementedError() - -def append_to_file(filename, contents): - """ - Append C{contents} to an existing file on the filesystem. If the file - doesn't exist it will be created with the default owner and permissions. - - @param filename: Full path to the new file. - @param contents: The contents to append to the file. - """ - raise NotImplementedError() - def delete_dir_contents(dirname): """ Delete all the contents of the directory specified by C{dirname} without @@ -76,8 +53,9 @@ def add_ca_certs(certs): if certs: cert_file_contents = "\n".join(certs) cert_file_fullpath = os.path.join(CA_CERT_PATH, CA_CERT_FILENAME) - write_file(cert_file_fullpath, cert_file_contents, "root", "root", "644") - append_to_file(CA_CERT_CONFIG, CA_CERT_FILENAME) + write_file(cert_file_fullpath, cert_file_contents, mode=0644) + # Append cert filename to CA_CERT_CONFIG file. + write_file(CA_CERT_CONFIG, "\n%s" % CA_CERT_FILENAME, omode="a") def remove_default_ca_certs(): """ @@ -86,7 +64,7 @@ def remove_default_ca_certs(): """ delete_dir_contents(CA_CERT_PATH) delete_dir_contents(CA_CERT_SYSTEM_PATH) - write_file(CA_CERT_CONFIG, "", "root", "root", "644") + write_file(CA_CERT_CONFIG, "", mode=0644) def handle(name, cfg, cloud, log, args): """ @@ -110,7 +88,7 @@ def handle(name, cfg, cloud, log, args): # If we are given any new trusted CA certs to add, add them. if ca_cert_cfg.has_key('trusted'): - trusted_certs = util.get_cfg_option_list_or_str(ca_cert_cfg, 'trusted') + trusted_certs = get_cfg_option_list_or_str(ca_cert_cfg, 'trusted') if trusted_certs: add_ca_certs(trusted_certs) diff --git a/tests/unittests/test_handler_ca_certs.py b/tests/unittests/test_handler_ca_certs.py index 7c6dc873..d8b98a6b 100644 --- a/tests/unittests/test_handler_ca_certs.py +++ b/tests/unittests/test_handler_ca_certs.py @@ -1,7 +1,8 @@ from unittest import TestCase from mocker import MockerTestCase -from cloudinit.CloudConfig.cc_ca_certs import handle, write_file, update_ca_certs, add_ca_certs, remove_default_ca_certs, append_to_file, delete_dir_contents +from cloudinit.util import write_file +from cloudinit.CloudConfig.cc_ca_certs import handle, update_ca_certs, add_ca_certs, remove_default_ca_certs, delete_dir_contents class TestNoConfig(MockerTestCase): @@ -127,24 +128,22 @@ class TestAddCaCerts(MockerTestCase): cert = "CERT1\nLINE2\nLINE3" mock_write = self.mocker.replace(write_file, passthrough=False) - mock_append = self.mocker.replace(append_to_file, passthrough=False) mock_write("/usr/share/ca-certificates/cloud-init-ca-certs.crt", - cert, "root", "root", "644") - mock_append("/etc/ca-certificates.conf", "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 certificate to the trusted CAs""" + """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_append = self.mocker.replace(append_to_file, passthrough=False) mock_write("/usr/share/ca-certificates/cloud-init-ca-certs.crt", - expected_cert_file, "root", "root", "644") - mock_append("/etc/ca-certificates.conf", "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) @@ -167,7 +166,7 @@ class TestRemoveDefaultCaCerts(MockerTestCase): mock_delete_dir_contents("/usr/share/ca-certificates/") mock_delete_dir_contents("/etc/ssl/certs/") - mock_write("/etc/ca-certificates.conf", "", "root", "root", "644") + mock_write("/etc/ca-certificates.conf", "", mode=0644) self.mocker.replay() -- cgit v1.2.3 From 19da04df35c1e6b22379c7b8e0457b16af299593 Mon Sep 17 00:00:00 2001 From: Mike Milner Date: Tue, 17 Jan 2012 11:49:32 -0400 Subject: Use delete_dir_contents from cloudinit.util. --- cloudinit/CloudConfig/cc_ca_certs.py | 11 +---------- tests/unittests/test_handler_ca_certs.py | 4 ++-- 2 files changed, 3 insertions(+), 12 deletions(-) (limited to 'tests/unittests/test_handler_ca_certs.py') diff --git a/cloudinit/CloudConfig/cc_ca_certs.py b/cloudinit/CloudConfig/cc_ca_certs.py index 9d7dcf7f..ef651f8b 100644 --- a/cloudinit/CloudConfig/cc_ca_certs.py +++ b/cloudinit/CloudConfig/cc_ca_certs.py @@ -21,22 +21,13 @@ import json import StringIO import ConfigParser import cloudinit.CloudConfig as cc -from cloudinit.util import write_file, get_cfg_option_list_or_str +from cloudinit.util import write_file, get_cfg_option_list_or_str, delete_dir_contents CA_CERT_PATH = "/usr/share/ca-certificates/" CA_CERT_FILENAME = "cloud-init-ca-certs.crt" CA_CERT_CONFIG = "/etc/ca-certificates.conf" CA_CERT_SYSTEM_PATH = "/etc/ssl/certs/" -def delete_dir_contents(dirname): - """ - Delete all the contents of the directory specified by C{dirname} without - deleting the directory itself. - - @param dirname: The directory whose contents should be deleted. - """ - raise NotImplementedError() - def update_ca_certs(): """ Updates the CA certificate cache on the current machine. diff --git a/tests/unittests/test_handler_ca_certs.py b/tests/unittests/test_handler_ca_certs.py index d8b98a6b..92460088 100644 --- a/tests/unittests/test_handler_ca_certs.py +++ b/tests/unittests/test_handler_ca_certs.py @@ -1,8 +1,8 @@ from unittest import TestCase from mocker import MockerTestCase -from cloudinit.util import write_file -from cloudinit.CloudConfig.cc_ca_certs import handle, update_ca_certs, add_ca_certs, remove_default_ca_certs, delete_dir_contents +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 class TestNoConfig(MockerTestCase): -- cgit v1.2.3 From f52ffe2dda01dd0314523fcac559e6f3fbb3578e Mon Sep 17 00:00:00 2001 From: Mike Milner Date: Tue, 17 Jan 2012 13:35:31 -0400 Subject: Lint fixes. --- cloudinit/CloudConfig/cc_ca_certs.py | 19 +++++++++---------- tests/unittests/test_handler_ca_certs.py | 22 +++++++++++++--------- tests/unittests/test_util.py | 7 ++++++- 3 files changed, 28 insertions(+), 20 deletions(-) (limited to 'tests/unittests/test_handler_ca_certs.py') diff --git a/cloudinit/CloudConfig/cc_ca_certs.py b/cloudinit/CloudConfig/cc_ca_certs.py index ef651f8b..e6cdc3f5 100644 --- a/cloudinit/CloudConfig/cc_ca_certs.py +++ b/cloudinit/CloudConfig/cc_ca_certs.py @@ -14,26 +14,23 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . import os -import pwd -import socket from subprocess import check_call -import json -import StringIO -import ConfigParser -import cloudinit.CloudConfig as cc -from cloudinit.util import write_file, get_cfg_option_list_or_str, delete_dir_contents +from cloudinit.util import (write_file, get_cfg_option_list_or_str, + delete_dir_contents) CA_CERT_PATH = "/usr/share/ca-certificates/" CA_CERT_FILENAME = "cloud-init-ca-certs.crt" CA_CERT_CONFIG = "/etc/ca-certificates.conf" CA_CERT_SYSTEM_PATH = "/etc/ssl/certs/" + def update_ca_certs(): """ Updates the CA certificate cache on the current machine. """ check_call(["update-ca-certificates"]) + def add_ca_certs(certs): """ Adds certificates to the system. To actually apply the new certificates @@ -48,6 +45,7 @@ def add_ca_certs(certs): # Append cert filename to CA_CERT_CONFIG file. write_file(CA_CERT_CONFIG, "\n%s" % CA_CERT_FILENAME, omode="a") + def remove_default_ca_certs(): """ Removes all default trusted CA certificates from the system. To actually @@ -57,6 +55,7 @@ def remove_default_ca_certs(): delete_dir_contents(CA_CERT_SYSTEM_PATH) write_file(CA_CERT_CONFIG, "", mode=0644) + def handle(name, cfg, cloud, log, args): """ Call to handle ca-cert sections in cloud-config file. @@ -68,7 +67,7 @@ def handle(name, cfg, cloud, log, args): @param args: Any module arguments from cloud.cfg """ # If there isn't a ca-certs section in the configuration don't do anything - if not cfg.has_key('ca-certs'): + if "ca-certs" not in cfg: return ca_cert_cfg = cfg['ca-certs'] @@ -78,8 +77,8 @@ def handle(name, cfg, cloud, log, args): remove_default_ca_certs() # If we are given any new trusted CA certs to add, add them. - if ca_cert_cfg.has_key('trusted'): - trusted_certs = get_cfg_option_list_or_str(ca_cert_cfg, 'trusted') + if "trusted" in ca_cert_cfg: + trusted_certs = get_cfg_option_list_or_str(ca_cert_cfg, "trusted") if trusted_certs: add_ca_certs(trusted_certs) diff --git a/tests/unittests/test_handler_ca_certs.py b/tests/unittests/test_handler_ca_certs.py index 92460088..c289a4f6 100644 --- a/tests/unittests/test_handler_ca_certs.py +++ b/tests/unittests/test_handler_ca_certs.py @@ -1,8 +1,8 @@ -from unittest import TestCase 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 cloudinit.CloudConfig.cc_ca_certs import ( + handle, update_ca_certs, add_ca_certs, remove_default_ca_certs) class TestNoConfig(MockerTestCase): @@ -36,8 +36,10 @@ class TestConfig(MockerTestCase): # 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) + 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() @@ -118,7 +120,7 @@ class TestConfig(MockerTestCase): class TestAddCaCerts(MockerTestCase): def test_no_certs_in_list(self): """Test that no certificate are written if not provided.""" - mock = self.mocker.replace(write_file, passthrough=False) + self.mocker.replace(write_file, passthrough=False) self.mocker.replay() add_ca_certs([]) @@ -130,7 +132,8 @@ class TestAddCaCerts(MockerTestCase): 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") + mock_write("/etc/ca-certificates.conf", + "\ncloud-init-ca-certs.crt", omode="a") self.mocker.replay() add_ca_certs([cert]) @@ -143,7 +146,8 @@ class TestAddCaCerts(MockerTestCase): 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") + mock_write("/etc/ca-certificates.conf", + "\ncloud-init-ca-certs.crt", omode="a") self.mocker.replay() add_ca_certs(certs) @@ -161,13 +165,13 @@ class TestUpdateCaCerts(MockerTestCase): class TestRemoveDefaultCaCerts(MockerTestCase): def test_commands(self): - mock_delete_dir_contents = self.mocker.replace(delete_dir_contents, passthrough=False) + 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() diff --git a/tests/unittests/test_util.py b/tests/unittests/test_util.py index f2b2ee3d..d8da8bc9 100644 --- a/tests/unittests/test_util.py +++ b/tests/unittests/test_util.py @@ -5,7 +5,9 @@ from shutil import rmtree import os import stat -from cloudinit.util import mergedict, get_cfg_option_list_or_str, write_file, delete_dir_contents +from cloudinit.util import (mergedict, get_cfg_option_list_or_str, write_file, + delete_dir_contents) + class TestMergeDict(TestCase): def test_simple_merge(self): @@ -65,6 +67,7 @@ class TestMergeDict(TestCase): result = mergedict(source, candidate) self.assertEqual(source, result) + class TestGetCfgOptionListOrStr(TestCase): def test_not_found_no_default(self): """None is returned if key is not found and no default given.""" @@ -96,6 +99,7 @@ class TestGetCfgOptionListOrStr(TestCase): result = get_cfg_option_list_or_str(config, "key") self.assertEqual([], result) + class TestWriteFile(MockerTestCase): def setUp(self): super(TestWriteFile, self).setUp() @@ -174,6 +178,7 @@ class TestWriteFile(MockerTestCase): write_file(path, contents) + class TestDeleteDirContents(TestCase): def setUp(self): super(TestDeleteDirContents, self).setUp() -- cgit v1.2.3