18322d
From ae0f85f867714009af7d4ec6c58e30c552bab556 Mon Sep 17 00:00:00 2001
18322d
From: Eduardo Otubo <otubo@redhat.com>
18322d
Date: Wed, 3 Jul 2019 13:06:49 +0200
18322d
Subject: [PATCH 2/2] Azure: Return static fallback address as if failed to
18322d
 find endpoint
18322d
18322d
RH-Author: Eduardo Otubo <otubo@redhat.com>
18322d
Message-id: <20190703130649.14511-1-otubo@redhat.com>
18322d
Patchwork-id: 89353
18322d
O-Subject: [RHEL-8.0.1/RHEL-8.1.0 cloud-init PATCH] Azure: Return static fallback address as if failed to find endpoint
18322d
Bugzilla: 1691986
18322d
RH-Acked-by: Bandan Das <bsd@redhat.com>
18322d
RH-Acked-by: Mohammed Gamal <mgamal@redhat.com>
18322d
18322d
commit ade77012c8bbcd215b7e26065981194ce1b6a157
18322d
Author: Jason Zions (MSFT) <jasonzio@microsoft.com>
18322d
Date:   Fri May 10 18:38:55 2019 +0000
18322d
18322d
    Azure: Return static fallback address as if failed to find endpoint
18322d
18322d
    The Azure data source helper attempts to use information in the dhcp
18322d
    lease to find the Wireserver endpoint (IP address). Under some unusual
18322d
    circumstances, those attempts will fail. This change uses a static
18322d
    address, known to be always correct in the Azure public and sovereign
18322d
    clouds, when the helper fails to locate a valid dhcp lease. This
18322d
    address is not guaranteed to be correct in Azure Stack environments;
18322d
    it's still best to use the information from the lease whenever possible.
18322d
18322d
Signed-off-by: Eduardo Otubo <otubo@redhat.com>
18322d
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
18322d
---
18322d
 cloudinit/sources/helpers/azure.py                   | 14 +++++++++++---
18322d
 tests/unittests/test_datasource/test_azure_helper.py |  9 +++++++--
18322d
 2 files changed, 18 insertions(+), 5 deletions(-)
18322d
18322d
diff --git a/cloudinit/sources/helpers/azure.py b/cloudinit/sources/helpers/azure.py
18322d
index d3af05e..82c4c8c 100755
18322d
--- a/cloudinit/sources/helpers/azure.py
18322d
+++ b/cloudinit/sources/helpers/azure.py
18322d
@@ -20,6 +20,9 @@ from cloudinit.reporting import events
18322d
 
18322d
 LOG = logging.getLogger(__name__)
18322d
 
18322d
+# This endpoint matches the format as found in dhcp lease files, since this
18322d
+# value is applied if the endpoint can't be found within a lease file
18322d
+DEFAULT_WIRESERVER_ENDPOINT = "a8:3f:81:10"
18322d
 
18322d
 azure_ds_reporter = events.ReportEventStack(
18322d
     name="azure-ds",
18322d
@@ -297,7 +300,12 @@ class WALinuxAgentShim(object):
18322d
     @azure_ds_telemetry_reporter
18322d
     def _get_value_from_leases_file(fallback_lease_file):
18322d
         leases = []
18322d
-        content = util.load_file(fallback_lease_file)
18322d
+        try:
18322d
+            content = util.load_file(fallback_lease_file)
18322d
+        except IOError as ex:
18322d
+            LOG.error("Failed to read %s: %s", fallback_lease_file, ex)
18322d
+            return None
18322d
+
18322d
         LOG.debug("content is %s", content)
18322d
         option_name = _get_dhcp_endpoint_option_name()
18322d
         for line in content.splitlines():
18322d
@@ -372,9 +380,9 @@ class WALinuxAgentShim(object):
18322d
                           fallback_lease_file)
18322d
                 value = WALinuxAgentShim._get_value_from_leases_file(
18322d
                     fallback_lease_file)
18322d
-
18322d
         if value is None:
18322d
-            raise ValueError('No endpoint found.')
18322d
+            LOG.warning("No lease found; using default endpoint")
18322d
+            value = DEFAULT_WIRESERVER_ENDPOINT
18322d
 
18322d
         endpoint_ip_address = WALinuxAgentShim.get_ip_from_lease_value(value)
18322d
         LOG.debug('Azure endpoint found at %s', endpoint_ip_address)
18322d
diff --git a/tests/unittests/test_datasource/test_azure_helper.py b/tests/unittests/test_datasource/test_azure_helper.py
18322d
index 0255616..bd006ab 100644
18322d
--- a/tests/unittests/test_datasource/test_azure_helper.py
18322d
+++ b/tests/unittests/test_datasource/test_azure_helper.py
18322d
@@ -67,12 +67,17 @@ class TestFindEndpoint(CiTestCase):
18322d
         self.networkd_leases.return_value = None
18322d
 
18322d
     def test_missing_file(self):
18322d
-        self.assertRaises(ValueError, wa_shim.find_endpoint)
18322d
+        """wa_shim find_endpoint uses default endpoint if leasefile not found
18322d
+        """
18322d
+        self.assertEqual(wa_shim.find_endpoint(), "168.63.129.16")
18322d
 
18322d
     def test_missing_special_azure_line(self):
18322d
+        """wa_shim find_endpoint uses default endpoint if leasefile is found
18322d
+        but does not contain DHCP Option 245 (whose value is the endpoint)
18322d
+        """
18322d
         self.load_file.return_value = ''
18322d
         self.dhcp_options.return_value = {'eth0': {'key': 'value'}}
18322d
-        self.assertRaises(ValueError, wa_shim.find_endpoint)
18322d
+        self.assertEqual(wa_shim.find_endpoint(), "168.63.129.16")
18322d
 
18322d
     @staticmethod
18322d
     def _build_lease_content(encoded_address):
18322d
-- 
18322d
1.8.3.1
18322d