diff options
author | Gonéri Le Bouder <goneri@lebouder.net> | 2020-03-25 13:44:10 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-25 13:44:10 -0400 |
commit | 993f3e3e76e56266a83776a8f54dbb3ba59cfce7 (patch) | |
tree | a7226ed8f85b6a8b09c14d923bc08aadf5a1c11b /cloudinit | |
parent | 42f69f410ab8850c02b1f53dd67c132aa8ef64f5 (diff) | |
download | vyos-cloud-init-993f3e3e76e56266a83776a8f54dbb3ba59cfce7.tar.gz vyos-cloud-init-993f3e3e76e56266a83776a8f54dbb3ba59cfce7.zip |
set_passwords: avoid chpasswd on BSD (#268)
Avoid chpasswd on all the BSD variants.
Diffstat (limited to 'cloudinit')
-rwxr-xr-x | cloudinit/config/cc_set_passwords.py | 2 | ||||
-rw-r--r-- | cloudinit/config/tests/test_set_passwords.py | 16 | ||||
-rw-r--r-- | cloudinit/tests/test_util.py | 6 | ||||
-rw-r--r-- | cloudinit/util.py | 7 |
4 files changed, 19 insertions, 12 deletions
diff --git a/cloudinit/config/cc_set_passwords.py b/cloudinit/config/cc_set_passwords.py index 4943d545..7b7aa885 100755 --- a/cloudinit/config/cc_set_passwords.py +++ b/cloudinit/config/cc_set_passwords.py @@ -241,7 +241,7 @@ def rand_user_password(pwlen=20): def chpasswd(distro, plist_in, hashed=False): - if util.is_FreeBSD(): + if util.is_BSD(): for pentry in plist_in.splitlines(): u, p = pentry.split(":") distro.set_passwd(u, p, hashed=hashed) diff --git a/cloudinit/config/tests/test_set_passwords.py b/cloudinit/config/tests/test_set_passwords.py index 8247c388..2732bd60 100644 --- a/cloudinit/config/tests/test_set_passwords.py +++ b/cloudinit/config/tests/test_set_passwords.py @@ -112,12 +112,12 @@ class TestSetPasswordsHandle(CiTestCase): '\n'.join(valid_hashed_pwds) + '\n')], m_subp.call_args_list) - @mock.patch(MODPATH + "util.is_FreeBSD") + @mock.patch(MODPATH + "util.is_BSD") @mock.patch(MODPATH + "util.subp") - def test_freebsd_calls_custom_pw_cmds_to_set_and_expire_passwords( - self, m_subp, m_is_freebsd): - """FreeBSD calls custom pw commands instead of chpasswd and passwd""" - m_is_freebsd.return_value = True + def test_bsd_calls_custom_pw_cmds_to_set_and_expire_passwords( + self, m_subp, m_is_bsd): + """BSD don't use chpasswd""" + m_is_bsd.return_value = True cloud = self.tmp_cloud(distro='freebsd') valid_pwds = ['ubuntu:passw0rd'] cfg = {'chpasswd': {'list': valid_pwds}} @@ -129,12 +129,12 @@ class TestSetPasswordsHandle(CiTestCase): mock.call(['pw', 'usermod', 'ubuntu', '-p', '01-Jan-1970'])], m_subp.call_args_list) - @mock.patch(MODPATH + "util.is_FreeBSD") + @mock.patch(MODPATH + "util.is_BSD") @mock.patch(MODPATH + "util.subp") def test_handle_on_chpasswd_list_creates_random_passwords(self, m_subp, - m_is_freebsd): + m_is_bsd): """handle parses command set random passwords.""" - m_is_freebsd.return_value = False + m_is_bsd.return_value = False cloud = self.tmp_cloud(distro='ubuntu') valid_random_pwds = [ 'root:R', diff --git a/cloudinit/tests/test_util.py b/cloudinit/tests/test_util.py index 877ab5c5..bfccfe1e 100644 --- a/cloudinit/tests/test_util.py +++ b/cloudinit/tests/test_util.py @@ -441,13 +441,15 @@ class TestGetLinuxDistro(CiTestCase): @mock.patch('platform.system') @mock.patch('platform.release') @mock.patch('cloudinit.util._parse_redhat_release') - def test_get_linux_freebsd(self, m_path_exists, m_platform_release, - m_platform_system, m_parse_redhat_release): + def test_get_linux_freebsd(self, m_parse_redhat_release, + m_platform_release, + m_platform_system, m_path_exists): """Verify we get the correct name and release name on FreeBSD.""" m_path_exists.return_value = False m_platform_release.return_value = '12.0-RELEASE-p10' m_platform_system.return_value = 'FreeBSD' m_parse_redhat_release.return_value = {} + util.is_BSD.cache_clear() dist = util.get_linux_distro() self.assertEqual(('freebsd', '12.0-RELEASE-p10', ''), dist) diff --git a/cloudinit/util.py b/cloudinit/util.py index 89889459..db60b9d2 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -543,6 +543,11 @@ def is_ipv4(instr): @lru_cache() +def is_BSD(): + return 'BSD' in platform.system() + + +@lru_cache() def is_FreeBSD(): return system_info()['variant'] == "freebsd" @@ -625,7 +630,7 @@ def get_linux_distro(): flavor = match.groupdict()['codename'] if distro_name == 'rhel': distro_name = 'redhat' - elif 'BSD' in platform.system(): + elif is_BSD(): distro_name = platform.system().lower() distro_version = platform.release() else: |