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