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