Blame cloudinit/config/tests/test_set_passwords.py

Packit Service a04d08
# This file is part of cloud-init. See LICENSE file for license information.
Packit Service a04d08
Packit Service 751c4a
from unittest import mock
Packit Service a04d08
Packit Service a04d08
from cloudinit.config import cc_set_passwords as setpass
Packit Service a04d08
from cloudinit.tests.helpers import CiTestCase
Packit Service a04d08
from cloudinit import util
Packit Service a04d08
Packit Service a04d08
MODPATH = "cloudinit.config.cc_set_passwords."
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
class TestHandleSshPwauth(CiTestCase):
Packit Service a04d08
    """Test cc_set_passwords handling of ssh_pwauth in handle_ssh_pwauth."""
Packit Service a04d08
Packit Service a04d08
    with_logs = True
Packit Service a04d08
Packit Service 751c4a
    @mock.patch(MODPATH + "subp.subp")
Packit Service a04d08
    def test_unknown_value_logs_warning(self, m_subp):
Packit Service a04d08
        setpass.handle_ssh_pwauth("floo")
Packit Service a04d08
        self.assertIn("Unrecognized value: ssh_pwauth=floo",
Packit Service a04d08
                      self.logs.getvalue())
Packit Service a04d08
        m_subp.assert_not_called()
Packit Service a04d08
Packit Service a04d08
    @mock.patch(MODPATH + "update_ssh_config", return_value=True)
Packit Service 751c4a
    @mock.patch(MODPATH + "subp.subp")
Packit Service a04d08
    def test_systemctl_as_service_cmd(self, m_subp, m_update_ssh_config):
Packit Service a04d08
        """If systemctl in service cmd: systemctl restart name."""
Packit Service a04d08
        setpass.handle_ssh_pwauth(
Packit Service a04d08
            True, service_cmd=["systemctl"], service_name="myssh")
Packit Service a04d08
        self.assertEqual(mock.call(["systemctl", "restart", "myssh"]),
Packit Service a04d08
                         m_subp.call_args)
Packit Service a04d08
Packit Service a04d08
    @mock.patch(MODPATH + "update_ssh_config", return_value=True)
Packit Service 751c4a
    @mock.patch(MODPATH + "subp.subp")
Packit Service a04d08
    def test_service_as_service_cmd(self, m_subp, m_update_ssh_config):
Packit Service a04d08
        """If systemctl in service cmd: systemctl restart name."""
Packit Service a04d08
        setpass.handle_ssh_pwauth(
Packit Service a04d08
            True, service_cmd=["service"], service_name="myssh")
Packit Service a04d08
        self.assertEqual(mock.call(["service", "myssh", "restart"]),
Packit Service a04d08
                         m_subp.call_args)
Packit Service a04d08
Packit Service a04d08
    @mock.patch(MODPATH + "update_ssh_config", return_value=False)
Packit Service 751c4a
    @mock.patch(MODPATH + "subp.subp")
Packit Service a04d08
    def test_not_restarted_if_not_updated(self, m_subp, m_update_ssh_config):
Packit Service a04d08
        """If config is not updated, then no system restart should be done."""
Packit Service a04d08
        setpass.handle_ssh_pwauth(True)
Packit Service a04d08
        m_subp.assert_not_called()
Packit Service 751c4a
        self.assertIn("No need to restart SSH", self.logs.getvalue())
Packit Service a04d08
Packit Service a04d08
    @mock.patch(MODPATH + "update_ssh_config", return_value=True)
Packit Service 751c4a
    @mock.patch(MODPATH + "subp.subp")
Packit Service a04d08
    def test_unchanged_does_nothing(self, m_subp, m_update_ssh_config):
Packit Service a04d08
        """If 'unchanged', then no updates to config and no restart."""
Packit Service a04d08
        setpass.handle_ssh_pwauth(
Packit Service a04d08
            "unchanged", service_cmd=["systemctl"], service_name="myssh")
Packit Service a04d08
        m_update_ssh_config.assert_not_called()
Packit Service a04d08
        m_subp.assert_not_called()
Packit Service a04d08
Packit Service 751c4a
    @mock.patch(MODPATH + "subp.subp")
Packit Service a04d08
    def test_valid_change_values(self, m_subp):
Packit Service a04d08
        """If value is a valid changen value, then update should be called."""
Packit Service a04d08
        upname = MODPATH + "update_ssh_config"
Packit Service a04d08
        optname = "PasswordAuthentication"
Packit Service a04d08
        for value in util.FALSE_STRINGS + util.TRUE_STRINGS:
Packit Service a04d08
            optval = "yes" if value in util.TRUE_STRINGS else "no"
Packit Service a04d08
            with mock.patch(upname, return_value=False) as m_update:
Packit Service a04d08
                setpass.handle_ssh_pwauth(value)
Packit Service a04d08
                m_update.assert_called_with({optname: optval})
Packit Service a04d08
        m_subp.assert_not_called()
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
class TestSetPasswordsHandle(CiTestCase):
Packit Service a04d08
    """Test cc_set_passwords.handle"""
Packit Service a04d08
Packit Service a04d08
    with_logs = True
Packit Service a04d08
Packit Service 751c4a
    def setUp(self):
Packit Service 751c4a
        super(TestSetPasswordsHandle, self).setUp()
Packit Service 751c4a
        self.add_patch('cloudinit.config.cc_set_passwords.sys.stderr', 'm_err')
Packit Service 751c4a
Packit Service a04d08
    def test_handle_on_empty_config(self, *args):
Packit Service a04d08
        """handle logs that no password has changed when config is empty."""
Packit Service a04d08
        cloud = self.tmp_cloud(distro='ubuntu')
Packit Service a04d08
        setpass.handle(
Packit Service a04d08
            'IGNORED', cfg={}, cloud=cloud, log=self.logger, args=[])
Packit Service a04d08
        self.assertEqual(
Packit Service 751c4a
            "DEBUG: Leaving SSH config 'PasswordAuthentication' unchanged. "
Packit Service a04d08
            'ssh_pwauth=None\n',
Packit Service a04d08
            self.logs.getvalue())
Packit Service a04d08
Packit Service 751c4a
    @mock.patch(MODPATH + "subp.subp")
Packit Service a04d08
    def test_handle_on_chpasswd_list_parses_common_hashes(self, m_subp):
Packit Service a04d08
        """handle parses command password hashes."""
Packit Service a04d08
        cloud = self.tmp_cloud(distro='ubuntu')
Packit Service a04d08
        valid_hashed_pwds = [
Packit Service a04d08
            'root:$2y$10$8BQjxjVByHA/Ee.O1bCXtO8S7Y5WojbXWqnqYpUW.BrPx/'
Packit Service a04d08
            'Dlew1Va',
Packit Service a04d08
            'ubuntu:$6$5hOurLPO$naywm3Ce0UlmZg9gG2Fl9acWCVEoakMMC7dR52q'
Packit Service a04d08
            'SDexZbrN9z8yHxhUM2b.sxpguSwOlbOQSW/HpXazGGx3oo1']
Packit Service a04d08
        cfg = {'chpasswd': {'list': valid_hashed_pwds}}
Packit Service 751c4a
        with mock.patch(MODPATH + 'subp.subp') as m_subp:
Packit Service a04d08
            setpass.handle(
Packit Service a04d08
                'IGNORED', cfg=cfg, cloud=cloud, log=self.logger, args=[])
Packit Service a04d08
        self.assertIn(
Packit Service a04d08
            'DEBUG: Handling input for chpasswd as list.',
Packit Service a04d08
            self.logs.getvalue())
Packit Service a04d08
        self.assertIn(
Packit Service a04d08
            "DEBUG: Setting hashed password for ['root', 'ubuntu']",
Packit Service a04d08
            self.logs.getvalue())
Packit Service a04d08
        self.assertEqual(
Packit Service a04d08
            [mock.call(['chpasswd', '-e'],
Packit Service a04d08
             '\n'.join(valid_hashed_pwds) + '\n')],
Packit Service a04d08
            m_subp.call_args_list)
Packit Service a04d08
Packit Service 751c4a
    @mock.patch(MODPATH + "util.is_BSD")
Packit Service 751c4a
    @mock.patch(MODPATH + "subp.subp")
Packit Service 751c4a
    def test_bsd_calls_custom_pw_cmds_to_set_and_expire_passwords(
Packit Service 751c4a
            self, m_subp, m_is_bsd):
Packit Service 751c4a
        """BSD don't use chpasswd"""
Packit Service 751c4a
        m_is_bsd.return_value = True
Packit Service a04d08
        cloud = self.tmp_cloud(distro='freebsd')
Packit Service a04d08
        valid_pwds = ['ubuntu:passw0rd']
Packit Service a04d08
        cfg = {'chpasswd': {'list': valid_pwds}}
Packit Service a04d08
        setpass.handle(
Packit Service a04d08
            'IGNORED', cfg=cfg, cloud=cloud, log=self.logger, args=[])
Packit Service a04d08
        self.assertEqual([
Packit Service a04d08
            mock.call(['pw', 'usermod', 'ubuntu', '-h', '0'], data='passw0rd',
Packit Service a04d08
                      logstring="chpasswd for ubuntu"),
Packit Service a04d08
            mock.call(['pw', 'usermod', 'ubuntu', '-p', '01-Jan-1970'])],
Packit Service a04d08
            m_subp.call_args_list)
Packit Service a04d08
Packit Service 751c4a
    @mock.patch(MODPATH + "util.is_BSD")
Packit Service 751c4a
    @mock.patch(MODPATH + "subp.subp")
Packit Service a04d08
    def test_handle_on_chpasswd_list_creates_random_passwords(self, m_subp,
Packit Service 751c4a
                                                              m_is_bsd):
Packit Service a04d08
        """handle parses command set random passwords."""
Packit Service 751c4a
        m_is_bsd.return_value = False
Packit Service a04d08
        cloud = self.tmp_cloud(distro='ubuntu')
Packit Service a04d08
        valid_random_pwds = [
Packit Service a04d08
            'root:R',
Packit Service a04d08
            'ubuntu:RANDOM']
Packit Service a04d08
        cfg = {'chpasswd': {'expire': 'false', 'list': valid_random_pwds}}
Packit Service 751c4a
        with mock.patch(MODPATH + 'subp.subp') as m_subp:
Packit Service a04d08
            setpass.handle(
Packit Service a04d08
                'IGNORED', cfg=cfg, cloud=cloud, log=self.logger, args=[])
Packit Service a04d08
        self.assertIn(
Packit Service a04d08
            'DEBUG: Handling input for chpasswd as list.',
Packit Service a04d08
            self.logs.getvalue())
Packit Service a04d08
        self.assertNotEqual(
Packit Service a04d08
            [mock.call(['chpasswd'],
Packit Service a04d08
             '\n'.join(valid_random_pwds) + '\n')],
Packit Service a04d08
            m_subp.call_args_list)
Packit Service a04d08
Packit Service a04d08
Packit Service a04d08
# vi: ts=4 expandtab