sailesh1993 / rpms / cloud-init

Forked from rpms/cloud-init a year ago
Clone
c36ff1
From f844e9c263e59a623ca8c647bd87bf4f91374d54 Mon Sep 17 00:00:00 2001
c36ff1
From: Thomas Stringer <thstring@microsoft.com>
c36ff1
Date: Wed, 3 Mar 2021 11:07:43 -0500
c36ff1
Subject: [PATCH 1/7] Add flexibility to IMDS api-version (#793)
c36ff1
c36ff1
RH-Author: Eduardo Otubo <otubo@redhat.com>
c36ff1
RH-MergeRequest: 18: Add support for userdata on Azure from IMDS
c36ff1
RH-Commit: [1/7] 99a3db20e3f277a2f12ea21e937e06939434a2ca (otubo/cloud-init-src)
c36ff1
RH-Bugzilla: 2042351
c36ff1
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
c36ff1
RH-Acked-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
c36ff1
c36ff1
Add flexibility to IMDS api-version by having both a desired IMDS
c36ff1
api-version and a minimum api-version. The desired api-version will
c36ff1
be used first, and if that fails it will fall back to the minimum
c36ff1
api-version.
c36ff1
---
c36ff1
 cloudinit/sources/DataSourceAzure.py          | 113 ++++++++++++++----
c36ff1
 tests/unittests/test_datasource/test_azure.py |  42 ++++++-
c36ff1
 2 files changed, 129 insertions(+), 26 deletions(-)
c36ff1
c36ff1
diff --git a/cloudinit/sources/DataSourceAzure.py b/cloudinit/sources/DataSourceAzure.py
c36ff1
index 553b5a7e..de1452ce 100755
c36ff1
--- a/cloudinit/sources/DataSourceAzure.py
c36ff1
+++ b/cloudinit/sources/DataSourceAzure.py
c36ff1
@@ -78,17 +78,15 @@ AGENT_SEED_DIR = '/var/lib/waagent'
c36ff1
 # In the event where the IMDS primary server is not
c36ff1
 # available, it takes 1s to fallback to the secondary one
c36ff1
 IMDS_TIMEOUT_IN_SECONDS = 2
c36ff1
-IMDS_URL = "http://169.254.169.254/metadata/"
c36ff1
-IMDS_VER = "2019-06-01"
c36ff1
-IMDS_VER_PARAM = "api-version={}".format(IMDS_VER)
c36ff1
+IMDS_URL = "http://169.254.169.254/metadata"
c36ff1
+IMDS_VER_MIN = "2019-06-01"
c36ff1
+IMDS_VER_WANT = "2020-09-01"
c36ff1
 
c36ff1
 
c36ff1
 class metadata_type(Enum):
c36ff1
-    compute = "{}instance?{}".format(IMDS_URL, IMDS_VER_PARAM)
c36ff1
-    network = "{}instance/network?{}".format(IMDS_URL,
c36ff1
-                                             IMDS_VER_PARAM)
c36ff1
-    reprovisiondata = "{}reprovisiondata?{}".format(IMDS_URL,
c36ff1
-                                                    IMDS_VER_PARAM)
c36ff1
+    compute = "{}/instance".format(IMDS_URL)
c36ff1
+    network = "{}/instance/network".format(IMDS_URL)
c36ff1
+    reprovisiondata = "{}/reprovisiondata".format(IMDS_URL)
c36ff1
 
c36ff1
 
c36ff1
 PLATFORM_ENTROPY_SOURCE = "/sys/firmware/acpi/tables/OEM0"
c36ff1
@@ -349,6 +347,8 @@ class DataSourceAzure(sources.DataSource):
c36ff1
         self.update_events['network'].add(EventType.BOOT)
c36ff1
         self._ephemeral_dhcp_ctx = None
c36ff1
 
c36ff1
+        self.failed_desired_api_version = False
c36ff1
+
c36ff1
     def __str__(self):
c36ff1
         root = sources.DataSource.__str__(self)
c36ff1
         return "%s [seed=%s]" % (root, self.seed)
c36ff1
@@ -520,8 +520,10 @@ class DataSourceAzure(sources.DataSource):
c36ff1
                     self._wait_for_all_nics_ready()
c36ff1
                 ret = self._reprovision()
c36ff1
 
c36ff1
-            imds_md = get_metadata_from_imds(
c36ff1
-                self.fallback_interface, retries=10)
c36ff1
+            imds_md = self.get_imds_data_with_api_fallback(
c36ff1
+                self.fallback_interface,
c36ff1
+                retries=10
c36ff1
+            )
c36ff1
             (md, userdata_raw, cfg, files) = ret
c36ff1
             self.seed = cdev
c36ff1
             crawled_data.update({
c36ff1
@@ -652,6 +654,57 @@ class DataSourceAzure(sources.DataSource):
c36ff1
             self.ds_cfg['data_dir'], crawled_data['files'], dirmode=0o700)
c36ff1
         return True
c36ff1
 
c36ff1
+    @azure_ds_telemetry_reporter
c36ff1
+    def get_imds_data_with_api_fallback(
c36ff1
+            self,
c36ff1
+            fallback_nic,
c36ff1
+            retries,
c36ff1
+            md_type=metadata_type.compute):
c36ff1
+        """
c36ff1
+        Wrapper for get_metadata_from_imds so that we can have flexibility
c36ff1
+        in which IMDS api-version we use. If a particular instance of IMDS
c36ff1
+        does not have the api version that is desired, we want to make
c36ff1
+        this fault tolerant and fall back to a good known minimum api
c36ff1
+        version.
c36ff1
+        """
c36ff1
+
c36ff1
+        if not self.failed_desired_api_version:
c36ff1
+            for _ in range(retries):
c36ff1
+                try:
c36ff1
+                    LOG.info(
c36ff1
+                        "Attempting IMDS api-version: %s",
c36ff1
+                        IMDS_VER_WANT
c36ff1
+                    )
c36ff1
+                    return get_metadata_from_imds(
c36ff1
+                        fallback_nic=fallback_nic,
c36ff1
+                        retries=0,
c36ff1
+                        md_type=md_type,
c36ff1
+                        api_version=IMDS_VER_WANT
c36ff1
+                    )
c36ff1
+                except UrlError as err:
c36ff1
+                    LOG.info(
c36ff1
+                        "UrlError with IMDS api-version: %s",
c36ff1
+                        IMDS_VER_WANT
c36ff1
+                    )
c36ff1
+                    if err.code == 400:
c36ff1
+                        log_msg = "Fall back to IMDS api-version: {}".format(
c36ff1
+                            IMDS_VER_MIN
c36ff1
+                        )
c36ff1
+                        report_diagnostic_event(
c36ff1
+                            log_msg,
c36ff1
+                            logger_func=LOG.info
c36ff1
+                        )
c36ff1
+                        self.failed_desired_api_version = True
c36ff1
+                        break
c36ff1
+
c36ff1
+        LOG.info("Using IMDS api-version: %s", IMDS_VER_MIN)
c36ff1
+        return get_metadata_from_imds(
c36ff1
+            fallback_nic=fallback_nic,
c36ff1
+            retries=retries,
c36ff1
+            md_type=md_type,
c36ff1
+            api_version=IMDS_VER_MIN
c36ff1
+        )
c36ff1
+
c36ff1
     def device_name_to_device(self, name):
c36ff1
         return self.ds_cfg['disk_aliases'].get(name)
c36ff1
 
c36ff1
@@ -880,10 +933,11 @@ class DataSourceAzure(sources.DataSource):
c36ff1
         # primary nic is being attached first helps here. Otherwise each nic
c36ff1
         # could add several seconds of delay.
c36ff1
         try:
c36ff1
-            imds_md = get_metadata_from_imds(
c36ff1
+            imds_md = self.get_imds_data_with_api_fallback(
c36ff1
                 ifname,
c36ff1
                 5,
c36ff1
-                metadata_type.network)
c36ff1
+                metadata_type.network
c36ff1
+            )
c36ff1
         except Exception as e:
c36ff1
             LOG.warning(
c36ff1
                 "Failed to get network metadata using nic %s. Attempt to "
c36ff1
@@ -1017,7 +1071,10 @@ class DataSourceAzure(sources.DataSource):
c36ff1
     def _poll_imds(self):
c36ff1
         """Poll IMDS for the new provisioning data until we get a valid
c36ff1
         response. Then return the returned JSON object."""
c36ff1
-        url = metadata_type.reprovisiondata.value
c36ff1
+        url = "{}?api-version={}".format(
c36ff1
+            metadata_type.reprovisiondata.value,
c36ff1
+            IMDS_VER_MIN
c36ff1
+        )
c36ff1
         headers = {"Metadata": "true"}
c36ff1
         nl_sock = None
c36ff1
         report_ready = bool(not os.path.isfile(REPORTED_READY_MARKER_FILE))
c36ff1
@@ -2059,7 +2116,8 @@ def _generate_network_config_from_fallback_config() -> dict:
c36ff1
 @azure_ds_telemetry_reporter
c36ff1
 def get_metadata_from_imds(fallback_nic,
c36ff1
                            retries,
c36ff1
-                           md_type=metadata_type.compute):
c36ff1
+                           md_type=metadata_type.compute,
c36ff1
+                           api_version=IMDS_VER_MIN):
c36ff1
     """Query Azure's instance metadata service, returning a dictionary.
c36ff1
 
c36ff1
     If network is not up, setup ephemeral dhcp on fallback_nic to talk to the
c36ff1
@@ -2069,13 +2127,16 @@ def get_metadata_from_imds(fallback_nic,
c36ff1
     @param fallback_nic: String. The name of the nic which requires active
c36ff1
         network in order to query IMDS.
c36ff1
     @param retries: The number of retries of the IMDS_URL.
c36ff1
+    @param md_type: Metadata type for IMDS request.
c36ff1
+    @param api_version: IMDS api-version to use in the request.
c36ff1
 
c36ff1
     @return: A dict of instance metadata containing compute and network
c36ff1
         info.
c36ff1
     """
c36ff1
     kwargs = {'logfunc': LOG.debug,
c36ff1
               'msg': 'Crawl of Azure Instance Metadata Service (IMDS)',
c36ff1
-              'func': _get_metadata_from_imds, 'args': (retries, md_type,)}
c36ff1
+              'func': _get_metadata_from_imds,
c36ff1
+              'args': (retries, md_type, api_version,)}
c36ff1
     if net.is_up(fallback_nic):
c36ff1
         return util.log_time(**kwargs)
c36ff1
     else:
c36ff1
@@ -2091,20 +2152,26 @@ def get_metadata_from_imds(fallback_nic,
c36ff1
 
c36ff1
 
c36ff1
 @azure_ds_telemetry_reporter
c36ff1
-def _get_metadata_from_imds(retries, md_type=metadata_type.compute):
c36ff1
-
c36ff1
-    url = md_type.value
c36ff1
+def _get_metadata_from_imds(
c36ff1
+        retries,
c36ff1
+        md_type=metadata_type.compute,
c36ff1
+        api_version=IMDS_VER_MIN):
c36ff1
+    url = "{}?api-version={}".format(md_type.value, api_version)
c36ff1
     headers = {"Metadata": "true"}
c36ff1
     try:
c36ff1
         response = readurl(
c36ff1
             url, timeout=IMDS_TIMEOUT_IN_SECONDS, headers=headers,
c36ff1
             retries=retries, exception_cb=retry_on_url_exc)
c36ff1
     except Exception as e:
c36ff1
-        report_diagnostic_event(
c36ff1
-            'Ignoring IMDS instance metadata. '
c36ff1
-            'Get metadata from IMDS failed: %s' % e,
c36ff1
-            logger_func=LOG.warning)
c36ff1
-        return {}
c36ff1
+        # pylint:disable=no-member
c36ff1
+        if isinstance(e, UrlError) and e.code == 400:
c36ff1
+            raise
c36ff1
+        else:
c36ff1
+            report_diagnostic_event(
c36ff1
+                'Ignoring IMDS instance metadata. '
c36ff1
+                'Get metadata from IMDS failed: %s' % e,
c36ff1
+                logger_func=LOG.warning)
c36ff1
+            return {}
c36ff1
     try:
c36ff1
         from json.decoder import JSONDecodeError
c36ff1
         json_decode_error = JSONDecodeError
c36ff1
diff --git a/tests/unittests/test_datasource/test_azure.py b/tests/unittests/test_datasource/test_azure.py
c36ff1
index f597c723..dedebeb1 100644
c36ff1
--- a/tests/unittests/test_datasource/test_azure.py
c36ff1
+++ b/tests/unittests/test_datasource/test_azure.py
c36ff1
@@ -408,7 +408,9 @@ class TestGetMetadataFromIMDS(HttprettyTestCase):
c36ff1
 
c36ff1
     def setUp(self):
c36ff1
         super(TestGetMetadataFromIMDS, self).setUp()
c36ff1
-        self.network_md_url = dsaz.IMDS_URL + "instance?api-version=2019-06-01"
c36ff1
+        self.network_md_url = "{}/instance?api-version=2019-06-01".format(
c36ff1
+            dsaz.IMDS_URL
c36ff1
+        )
c36ff1
 
c36ff1
     @mock.patch(MOCKPATH + 'readurl')
c36ff1
     @mock.patch(MOCKPATH + 'EphemeralDHCPv4', autospec=True)
c36ff1
@@ -518,7 +520,7 @@ class TestGetMetadataFromIMDS(HttprettyTestCase):
c36ff1
         """Return empty dict when IMDS network metadata is absent."""
c36ff1
         httpretty.register_uri(
c36ff1
             httpretty.GET,
c36ff1
-            dsaz.IMDS_URL + 'instance?api-version=2017-12-01',
c36ff1
+            dsaz.IMDS_URL + '/instance?api-version=2017-12-01',
c36ff1
             body={}, status=404)
c36ff1
 
c36ff1
         m_net_is_up.return_value = True  # skips dhcp
c36ff1
@@ -1877,6 +1879,40 @@ scbus-1 on xpt0 bus 0
c36ff1
         ssh_keys = dsrc.get_public_ssh_keys()
c36ff1
         self.assertEqual(ssh_keys, ['key2'])
c36ff1
 
c36ff1
+    @mock.patch(MOCKPATH + 'get_metadata_from_imds')
c36ff1
+    def test_imds_api_version_wanted_nonexistent(
c36ff1
+            self,
c36ff1
+            m_get_metadata_from_imds):
c36ff1
+        def get_metadata_from_imds_side_eff(*args, **kwargs):
c36ff1
+            if kwargs['api_version'] == dsaz.IMDS_VER_WANT:
c36ff1
+                raise url_helper.UrlError("No IMDS version", code=400)
c36ff1
+            return NETWORK_METADATA
c36ff1
+        m_get_metadata_from_imds.side_effect = get_metadata_from_imds_side_eff
c36ff1
+        sys_cfg = {'datasource': {'Azure': {'apply_network_config': True}}}
c36ff1
+        odata = {'HostName': "myhost", 'UserName': "myuser"}
c36ff1
+        data = {
c36ff1
+            'ovfcontent': construct_valid_ovf_env(data=odata),
c36ff1
+            'sys_cfg': sys_cfg
c36ff1
+        }
c36ff1
+        dsrc = self._get_ds(data)
c36ff1
+        dsrc.get_data()
c36ff1
+        self.assertIsNotNone(dsrc.metadata)
c36ff1
+        self.assertTrue(dsrc.failed_desired_api_version)
c36ff1
+
c36ff1
+    @mock.patch(
c36ff1
+        MOCKPATH + 'get_metadata_from_imds', return_value=NETWORK_METADATA)
c36ff1
+    def test_imds_api_version_wanted_exists(self, m_get_metadata_from_imds):
c36ff1
+        sys_cfg = {'datasource': {'Azure': {'apply_network_config': True}}}
c36ff1
+        odata = {'HostName': "myhost", 'UserName': "myuser"}
c36ff1
+        data = {
c36ff1
+            'ovfcontent': construct_valid_ovf_env(data=odata),
c36ff1
+            'sys_cfg': sys_cfg
c36ff1
+        }
c36ff1
+        dsrc = self._get_ds(data)
c36ff1
+        dsrc.get_data()
c36ff1
+        self.assertIsNotNone(dsrc.metadata)
c36ff1
+        self.assertFalse(dsrc.failed_desired_api_version)
c36ff1
+
c36ff1
 
c36ff1
 class TestAzureBounce(CiTestCase):
c36ff1
 
c36ff1
@@ -2657,7 +2693,7 @@ class TestPreprovisioningHotAttachNics(CiTestCase):
c36ff1
     @mock.patch(MOCKPATH + 'DataSourceAzure.wait_for_link_up')
c36ff1
     @mock.patch('cloudinit.sources.helpers.netlink.wait_for_nic_attach_event')
c36ff1
     @mock.patch('cloudinit.sources.net.find_fallback_nic')
c36ff1
-    @mock.patch(MOCKPATH + 'get_metadata_from_imds')
c36ff1
+    @mock.patch(MOCKPATH + 'DataSourceAzure.get_imds_data_with_api_fallback')
c36ff1
     @mock.patch(MOCKPATH + 'EphemeralDHCPv4')
c36ff1
     @mock.patch(MOCKPATH + 'DataSourceAzure._wait_for_nic_detach')
c36ff1
     @mock.patch('os.path.isfile')
c36ff1
-- 
c36ff1
2.27.0
c36ff1