sailesh1993 / rpms / cloud-init

Forked from rpms/cloud-init a year ago
Clone
088c30
From c0df7233fa99d4191b5d4142e209e7465d8db5f6 Mon Sep 17 00:00:00 2001
088c30
From: Anh Vo <anhvo@microsoft.com>
088c30
Date: Tue, 27 Apr 2021 13:40:59 -0400
088c30
Subject: [PATCH 7/7] Azure: adding support for consuming userdata from IMDS
088c30
 (#884)
088c30
088c30
RH-Author: Eduardo Otubo <otubo@redhat.com>
088c30
RH-MergeRequest: 45: Add support for userdata on Azure from IMDS
088c30
RH-Commit: [7/7] 32f840412da1a0f49b9ab5ba1d6f1bcb1bfacc16
088c30
RH-Bugzilla: 2023940
088c30
RH-Acked-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
088c30
RH-Acked-by: Mohamed Gamal Morsy <mmorsy@redhat.com>
088c30
---
088c30
 cloudinit/sources/DataSourceAzure.py          | 23 ++++++++-
088c30
 tests/unittests/test_datasource/test_azure.py | 50 +++++++++++++++++++
088c30
 2 files changed, 72 insertions(+), 1 deletion(-)
088c30
088c30
diff --git a/cloudinit/sources/DataSourceAzure.py b/cloudinit/sources/DataSourceAzure.py
088c30
index d0be6d84..a66f023d 100755
088c30
--- a/cloudinit/sources/DataSourceAzure.py
088c30
+++ b/cloudinit/sources/DataSourceAzure.py
088c30
@@ -83,7 +83,7 @@ AGENT_SEED_DIR = '/var/lib/waagent'
088c30
 IMDS_TIMEOUT_IN_SECONDS = 2
088c30
 IMDS_URL = "http://169.254.169.254/metadata"
088c30
 IMDS_VER_MIN = "2019-06-01"
088c30
-IMDS_VER_WANT = "2020-10-01"
088c30
+IMDS_VER_WANT = "2021-01-01"
088c30
 
088c30
 
088c30
 # This holds SSH key data including if the source was
088c30
@@ -539,6 +539,20 @@ class DataSourceAzure(sources.DataSource):
088c30
                     imds_disable_password
088c30
                 )
088c30
                 crawled_data['metadata']['disable_password'] = imds_disable_password  # noqa: E501
088c30
+
088c30
+            # only use userdata from imds if OVF did not provide custom data
088c30
+            # userdata provided by IMDS is always base64 encoded
088c30
+            if not userdata_raw:
088c30
+                imds_userdata = _userdata_from_imds(imds_md)
088c30
+                if imds_userdata:
088c30
+                    LOG.debug("Retrieved userdata from IMDS")
088c30
+                    try:
088c30
+                        crawled_data['userdata_raw'] = base64.b64decode(
088c30
+                            ''.join(imds_userdata.split()))
088c30
+                    except Exception:
088c30
+                        report_diagnostic_event(
088c30
+                            "Bad userdata in IMDS",
088c30
+                            logger_func=LOG.warning)
088c30
             found = cdev
088c30
 
088c30
             report_diagnostic_event(
088c30
@@ -1512,6 +1526,13 @@ def _username_from_imds(imds_data):
088c30
         return None
088c30
 
088c30
 
088c30
+def _userdata_from_imds(imds_data):
088c30
+    try:
088c30
+        return imds_data['compute']['userData']
088c30
+    except KeyError:
088c30
+        return None
088c30
+
088c30
+
088c30
 def _hostname_from_imds(imds_data):
088c30
     try:
088c30
         return imds_data['compute']['osProfile']['computerName']
088c30
diff --git a/tests/unittests/test_datasource/test_azure.py b/tests/unittests/test_datasource/test_azure.py
088c30
index c4a8e08d..f8433690 100644
088c30
--- a/tests/unittests/test_datasource/test_azure.py
088c30
+++ b/tests/unittests/test_datasource/test_azure.py
088c30
@@ -1899,6 +1899,56 @@ scbus-1 on xpt0 bus 0
088c30
         dsrc.get_data()
088c30
         self.assertTrue(dsrc.metadata["disable_password"])
088c30
 
088c30
+    @mock.patch(MOCKPATH + 'get_metadata_from_imds')
088c30
+    def test_userdata_from_imds(self, m_get_metadata_from_imds):
088c30
+        sys_cfg = {'datasource': {'Azure': {'apply_network_config': True}}}
088c30
+        odata = {'HostName': "myhost", 'UserName': "myuser"}
088c30
+        data = {
088c30
+            'ovfcontent': construct_valid_ovf_env(data=odata),
088c30
+            'sys_cfg': sys_cfg
088c30
+        }
088c30
+        userdata = "userdataImds"
088c30
+        imds_data = copy.deepcopy(NETWORK_METADATA)
088c30
+        imds_data["compute"]["osProfile"] = dict(
088c30
+            adminUsername="username1",
088c30
+            computerName="hostname1",
088c30
+            disablePasswordAuthentication="true",
088c30
+        )
088c30
+        imds_data["compute"]["userData"] = b64e(userdata)
088c30
+        m_get_metadata_from_imds.return_value = imds_data
088c30
+        dsrc = self._get_ds(data)
088c30
+        ret = dsrc.get_data()
088c30
+        self.assertTrue(ret)
088c30
+        self.assertEqual(dsrc.userdata_raw, userdata.encode('utf-8'))
088c30
+
088c30
+    @mock.patch(MOCKPATH + 'get_metadata_from_imds')
088c30
+    def test_userdata_from_imds_with_customdata_from_OVF(
088c30
+            self, m_get_metadata_from_imds):
088c30
+        userdataOVF = "userdataOVF"
088c30
+        odata = {
088c30
+            'HostName': "myhost", 'UserName': "myuser",
088c30
+            'UserData': {'text': b64e(userdataOVF), 'encoding': 'base64'}
088c30
+        }
088c30
+        sys_cfg = {'datasource': {'Azure': {'apply_network_config': True}}}
088c30
+        data = {
088c30
+            'ovfcontent': construct_valid_ovf_env(data=odata),
088c30
+            'sys_cfg': sys_cfg
088c30
+        }
088c30
+
088c30
+        userdataImds = "userdataImds"
088c30
+        imds_data = copy.deepcopy(NETWORK_METADATA)
088c30
+        imds_data["compute"]["osProfile"] = dict(
088c30
+            adminUsername="username1",
088c30
+            computerName="hostname1",
088c30
+            disablePasswordAuthentication="true",
088c30
+        )
088c30
+        imds_data["compute"]["userData"] = b64e(userdataImds)
088c30
+        m_get_metadata_from_imds.return_value = imds_data
088c30
+        dsrc = self._get_ds(data)
088c30
+        ret = dsrc.get_data()
088c30
+        self.assertTrue(ret)
088c30
+        self.assertEqual(dsrc.userdata_raw, userdataOVF.encode('utf-8'))
088c30
+
088c30
 
088c30
 class TestAzureBounce(CiTestCase):
088c30
 
088c30
-- 
088c30
2.27.0
088c30