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