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