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