summaryrefslogtreecommitdiff
path: root/cloudinit
diff options
context:
space:
mode:
authorMarlin Cremers <marlin@true.nl>2019-01-15 23:22:05 +0000
committerServer Team CI Bot <josh.powers+server-team-bot@canonical.com>2019-01-15 23:22:05 +0000
commitad170db966492e845b9dc23346cc7297e8a99032 (patch)
tree7b7990c8d64f981f1422f84fb0c9b918409d6a0a /cloudinit
parentfdadcb5fae51f4e6799314ab98e3aec56c79b17c (diff)
downloadvyos-cloud-init-ad170db966492e845b9dc23346cc7297e8a99032.tar.gz
vyos-cloud-init-ad170db966492e845b9dc23346cc7297e8a99032.zip
cc_set_passwords: Fix regex when parsing hashed passwords
Correct invalid regex to match hashes starting with the following: - $1, $2a, $2y, $5 or $6 LP: #1811446
Diffstat (limited to 'cloudinit')
-rwxr-xr-xcloudinit/config/cc_set_passwords.py2
-rw-r--r--cloudinit/config/tests/test_set_passwords.py40
2 files changed, 41 insertions, 1 deletions
diff --git a/cloudinit/config/cc_set_passwords.py b/cloudinit/config/cc_set_passwords.py
index 5ef97376..4585e4d3 100755
--- a/cloudinit/config/cc_set_passwords.py
+++ b/cloudinit/config/cc_set_passwords.py
@@ -160,7 +160,7 @@ def handle(_name, cfg, cloud, log, args):
hashed_users = []
randlist = []
users = []
- prog = re.compile(r'\$[1,2a,2y,5,6](\$.+){2}')
+ prog = re.compile(r'\$(1|2a|2y|5|6)(\$.+){2}')
for line in plist:
u, p = line.split(':', 1)
if prog.match(p) is not None and ":" not in p:
diff --git a/cloudinit/config/tests/test_set_passwords.py b/cloudinit/config/tests/test_set_passwords.py
index b051ec82..a2ea5ec4 100644
--- a/cloudinit/config/tests/test_set_passwords.py
+++ b/cloudinit/config/tests/test_set_passwords.py
@@ -68,4 +68,44 @@ class TestHandleSshPwauth(CiTestCase):
m_update.assert_called_with({optname: optval})
m_subp.assert_not_called()
+
+class TestSetPasswordsHandle(CiTestCase):
+ """Test cc_set_passwords.handle"""
+
+ with_logs = True
+
+ def test_handle_on_empty_config(self):
+ """handle logs that no password has changed when config is empty."""
+ cloud = self.tmp_cloud(distro='ubuntu')
+ setpass.handle(
+ 'IGNORED', cfg={}, cloud=cloud, log=self.logger, args=[])
+ self.assertEqual(
+ "DEBUG: Leaving ssh config 'PasswordAuthentication' unchanged. "
+ 'ssh_pwauth=None\n',
+ self.logs.getvalue())
+
+ @mock.patch(MODPATH + "util.subp")
+ def test_handle_on_chpasswd_list_parses_common_hashes(self, m_subp):
+ """handle parses command password hashes."""
+ cloud = self.tmp_cloud(distro='ubuntu')
+ valid_hashed_pwds = [
+ 'root:$2y$10$8BQjxjVByHA/Ee.O1bCXtO8S7Y5WojbXWqnqYpUW.BrPx/'
+ 'Dlew1Va',
+ 'ubuntu:$6$5hOurLPO$naywm3Ce0UlmZg9gG2Fl9acWCVEoakMMC7dR52q'
+ 'SDexZbrN9z8yHxhUM2b.sxpguSwOlbOQSW/HpXazGGx3oo1']
+ cfg = {'chpasswd': {'list': valid_hashed_pwds}}
+ with mock.patch(MODPATH + 'util.subp') as m_subp:
+ setpass.handle(
+ 'IGNORED', cfg=cfg, cloud=cloud, log=self.logger, args=[])
+ self.assertIn(
+ 'DEBUG: Handling input for chpasswd as list.',
+ self.logs.getvalue())
+ self.assertIn(
+ "DEBUG: Setting hashed password for ['root', 'ubuntu']",
+ self.logs.getvalue())
+ self.assertEqual(
+ [mock.call(['chpasswd', '-e'],
+ '\n'.join(valid_hashed_pwds) + '\n')],
+ m_subp.call_args_list)
+
# vi: ts=4 expandtab