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