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