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