sailesh1993 / rpms / cloud-init

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