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