69c4fd
From f73d2460e5ad205a1cd2d74a73c2d1308265d9f9 Mon Sep 17 00:00:00 2001
69c4fd
From: Miroslav Rezanina <mrezanin@redhat.com>
69c4fd
Date: Wed, 18 May 2022 05:23:48 -0400
69c4fd
Subject: [PATCH] Add \r\n check for SSH keys in Azure (#889)
69c4fd
69c4fd
RH-Author: Miroslav Rezanina <mrezanin@redhat.com>
69c4fd
RH-MergeRequest: 64: Properly handle \r\n in SSH keys in Azure
69c4fd
RH-Commit: [1/1] c0868258fd63f6c531acd8da81e0494a8412d5ea (mrezanin/src_rhel_cloud-init)
69c4fd
RH-Bugzilla: 2088028
69c4fd
RH-Acked-by: xiachen <xiachen@redhat.com>
69c4fd
RH-Acked-by: Eduardo Otubo <otubo@redhat.com>
69c4fd
RH-Acked-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
69c4fd
69c4fd
See https://bugs.launchpad.net/cloud-init/+bug/1910835
69c4fd
69c4fd
(cherry picked from commit f17f78fa9d28e62793a5f2c7109fc29eeffb0c89)
69c4fd
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
69c4fd
---
69c4fd
 cloudinit/sources/DataSourceAzure.py          |  3 +++
69c4fd
 tests/unittests/test_datasource/test_azure.py | 12 ++++++++++++
69c4fd
 2 files changed, 15 insertions(+)
69c4fd
69c4fd
diff --git a/cloudinit/sources/DataSourceAzure.py b/cloudinit/sources/DataSourceAzure.py
69c4fd
index a66f023d..247284ad 100755
69c4fd
--- a/cloudinit/sources/DataSourceAzure.py
69c4fd
+++ b/cloudinit/sources/DataSourceAzure.py
69c4fd
@@ -1551,6 +1551,9 @@ def _key_is_openssh_formatted(key):
69c4fd
     """
69c4fd
     Validate whether or not the key is OpenSSH-formatted.
69c4fd
     """
69c4fd
+    # See https://bugs.launchpad.net/cloud-init/+bug/1910835
69c4fd
+    if '\r\n' in key.strip():
69c4fd
+        return False
69c4fd
 
69c4fd
     parser = ssh_util.AuthKeyLineParser()
69c4fd
     try:
69c4fd
diff --git a/tests/unittests/test_datasource/test_azure.py b/tests/unittests/test_datasource/test_azure.py
69c4fd
index f8433690..742d1faa 100644
69c4fd
--- a/tests/unittests/test_datasource/test_azure.py
69c4fd
+++ b/tests/unittests/test_datasource/test_azure.py
69c4fd
@@ -1764,6 +1764,18 @@ scbus-1 on xpt0 bus 0
69c4fd
         self.assertEqual(ssh_keys, ["ssh-rsa key1"])
69c4fd
         self.assertEqual(m_parse_certificates.call_count, 0)
69c4fd
 
69c4fd
+    def test_key_without_crlf_valid(self):
69c4fd
+        test_key = 'ssh-rsa somerandomkeystuff some comment'
69c4fd
+        assert True is dsaz._key_is_openssh_formatted(test_key)
69c4fd
+
69c4fd
+    def test_key_with_crlf_invalid(self):
69c4fd
+        test_key = 'ssh-rsa someran\r\ndomkeystuff some comment'
69c4fd
+        assert False is dsaz._key_is_openssh_formatted(test_key)
69c4fd
+
69c4fd
+    def test_key_endswith_crlf_valid(self):
69c4fd
+        test_key = 'ssh-rsa somerandomkeystuff some comment\r\n'
69c4fd
+        assert True is dsaz._key_is_openssh_formatted(test_key)
69c4fd
+
69c4fd
     @mock.patch(
69c4fd
         'cloudinit.sources.helpers.azure.OpenSSLManager.parse_certificates')
69c4fd
     @mock.patch(MOCKPATH + 'get_metadata_from_imds')
69c4fd
-- 
69c4fd
2.31.1
69c4fd