46a734
From 7d4e16bfc1cefbdd4d1477480b02b1d6c1399e4d Mon Sep 17 00:00:00 2001
46a734
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
46a734
Date: Mon, 20 Sep 2021 12:16:36 +0200
46a734
Subject: [PATCH] ssh_utils.py: ignore when sshd_config options are not
46a734
 key/value pairs (#1007)
46a734
46a734
RH-Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
46a734
RH-MergeRequest: 31: ssh_utils.py: ignore when sshd_config options are not key/value pairs (#1007)
46a734
RH-Commit: [1/1] 9007fb8a116e98036ff17df0168a76e9a5843671 (eesposit/cloud-init)
46a734
RH-Bugzilla: 1862933
46a734
RH-Acked-by: Mohamed Gamal Morsy <mmorsy@redhat.com>
46a734
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
46a734
46a734
TESTED: by me
46a734
BREW: 39832462
46a734
46a734
commit 2ce857248162957a785af61c135ca8433fdbbcde
46a734
Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
46a734
Date:   Wed Sep 8 02:08:36 2021 +0200
46a734
46a734
    ssh_utils.py: ignore when sshd_config options are not key/value pairs (#1007)
46a734
46a734
    As specified in #LP 1845552,
46a734
    In cloudinit/ssh_util.py, in parse_ssh_config_lines(), we attempt to
46a734
    parse each line of sshd_config. This function expects each line to
46a734
    be one of the following forms:
46a734
46a734
        \# comment
46a734
        key value
46a734
        key=value
46a734
46a734
    However, options like DenyGroups and DenyUsers are specified to
46a734
    *optionally* accepts values in sshd_config.
46a734
    Cloud-init should comply to this and skip the option if a value
46a734
    is not provided.
46a734
46a734
    Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
46a734
46a734
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
46a734
---
46a734
 cloudinit/ssh_util.py           | 8 +++++++-
46a734
 tests/unittests/test_sshutil.py | 8 ++++++++
46a734
 2 files changed, 15 insertions(+), 1 deletion(-)
46a734
46a734
diff --git a/cloudinit/ssh_util.py b/cloudinit/ssh_util.py
46a734
index 9ccadf09..33679dcc 100644
46a734
--- a/cloudinit/ssh_util.py
46a734
+++ b/cloudinit/ssh_util.py
46a734
@@ -484,7 +484,13 @@ def parse_ssh_config_lines(lines):
46a734
         try:
46a734
             key, val = line.split(None, 1)
46a734
         except ValueError:
46a734
-            key, val = line.split('=', 1)
46a734
+            try:
46a734
+                key, val = line.split('=', 1)
46a734
+            except ValueError:
46a734
+                LOG.debug(
46a734
+                    "sshd_config: option \"%s\" has no key/value pair,"
46a734
+                    " skipping it", line)
46a734
+                continue
46a734
         ret.append(SshdConfigLine(line, key, val))
46a734
     return ret
46a734
 
46a734
diff --git a/tests/unittests/test_sshutil.py b/tests/unittests/test_sshutil.py
46a734
index a66788bf..08e20050 100644
46a734
--- a/tests/unittests/test_sshutil.py
46a734
+++ b/tests/unittests/test_sshutil.py
46a734
@@ -525,6 +525,14 @@ class TestUpdateSshConfigLines(test_helpers.CiTestCase):
46a734
         self.assertEqual([self.pwauth], result)
46a734
         self.check_line(lines[-1], self.pwauth, "no")
46a734
 
46a734
+    def test_option_without_value(self):
46a734
+        """Implementation only accepts key-value pairs."""
46a734
+        extended_exlines = self.exlines.copy()
46a734
+        denyusers_opt = "DenyUsers"
46a734
+        extended_exlines.append(denyusers_opt)
46a734
+        lines = ssh_util.parse_ssh_config_lines(list(extended_exlines))
46a734
+        self.assertNotIn(denyusers_opt, str(lines))
46a734
+
46a734
     def test_single_option_updated(self):
46a734
         """A single update should have change made and line updated."""
46a734
         opt, val = ("UsePAM", "no")
46a734
-- 
46a734
2.27.0
46a734