sailesh1993 / rpms / cloud-init

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