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