5d6d67
From ba515fc24d309a40f5552b514b59381777d8ad0e Mon Sep 17 00:00:00 2001
5d6d67
From: Eduardo Otubo <otubo@redhat.com>
5d6d67
Date: Tue, 10 Mar 2020 16:04:18 +0100
5d6d67
Subject: [PATCH] azure: avoid
5d6d67
5d6d67
Message-id: <20200310160418.887-1-otubo@redhat.com>
5d6d67
Patchwork-id: 94221
5d6d67
O-Subject: [RHEL-8.1.0/RHEL-7.8.z/RHEL-7.7.z cloud-init PATCH] azure: avoid re-running cloud-init when instance-id is byte-swapped (#84)
5d6d67
Bugzilla: 1810112
5d6d67
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
5d6d67
RH-Acked-by: Mohammed Gamal <mgamal@redhat.com>
5d6d67
5d6d67
commit 129b1c4ea250619bd7caed7aaffacc796b0139f2
5d6d67
Author: AOhassan <37305877+AOhassan@users.noreply.github.com>
5d6d67
Date:   Thu Dec 12 13:51:42 2019 -0800
5d6d67
5d6d67
    azure: avoid re-running cloud-init when instance-id is byte-swapped (#84)
5d6d67
5d6d67
    Azure stores the instance ID with an incorrect byte ordering for the
5d6d67
    first three hyphen delimited parts. This results in invalid
5d6d67
    is_new_instance checks forcing Azure datasource to recrawl the metadata
5d6d67
    service.
5d6d67
5d6d67
    When persisting instance-id from the metadata service, swap the
5d6d67
    instance-id string byte order such that it is consistent with
5d6d67
    that returned by dmi information. Check whether the instance-id
5d6d67
    string is a byte-swapped match when determining correctly whether
5d6d67
    the Azure platform instance-id has actually changed.
5d6d67
5d6d67
Signed-off-by: Eduardo Otubo <otubo@redhat.com>
5d6d67
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
5d6d67
---
5d6d67
 cloudinit/sources/DataSourceAzure.py               | 16 ++++++++++---
5d6d67
 cloudinit/sources/helpers/azure.py                 | 27 ++++++++++++++++++++++
5d6d67
 tests/unittests/test_datasource/test_azure.py      | 24 ++++++++++++++++---
5d6d67
 .../unittests/test_datasource/test_azure_helper.py | 19 +++++++++++++++
5d6d67
 4 files changed, 80 insertions(+), 6 deletions(-)
5d6d67
5d6d67
diff --git a/cloudinit/sources/DataSourceAzure.py b/cloudinit/sources/DataSourceAzure.py
5d6d67
index 5baf8da..66bbe5e 100755
5d6d67
--- a/cloudinit/sources/DataSourceAzure.py
5d6d67
+++ b/cloudinit/sources/DataSourceAzure.py
5d6d67
@@ -28,7 +28,8 @@ from cloudinit.reporting import events
5d6d67
 
5d6d67
 from cloudinit.sources.helpers.azure import (azure_ds_reporter,
5d6d67
                                              azure_ds_telemetry_reporter,
5d6d67
-                                             get_metadata_from_fabric)
5d6d67
+                                             get_metadata_from_fabric,
5d6d67
+                                             is_byte_swapped)
5d6d67
 
5d6d67
 LOG = logging.getLogger(__name__)
5d6d67
 
5d6d67
@@ -458,8 +459,7 @@ class DataSourceAzure(sources.DataSource):
5d6d67
         seed = _get_random_seed()
5d6d67
         if seed:
5d6d67
             crawled_data['metadata']['random_seed'] = seed
5d6d67
-        crawled_data['metadata']['instance-id'] = util.read_dmi_data(
5d6d67
-            'system-uuid')
5d6d67
+        crawled_data['metadata']['instance-id'] = self._iid()
5d6d67
 
5d6d67
         if perform_reprovision:
5d6d67
             LOG.info("Reporting ready to Azure after getting ReprovisionData")
5d6d67
@@ -530,6 +530,16 @@ class DataSourceAzure(sources.DataSource):
5d6d67
         # quickly (local check only) if self.instance_id is still valid
5d6d67
         return sources.instance_id_matches_system_uuid(self.get_instance_id())
5d6d67
 
5d6d67
+    def _iid(self, previous=None):
5d6d67
+        prev_iid_path = os.path.join(
5d6d67
+            self.paths.get_cpath('data'), 'instance-id')
5d6d67
+        iid = util.read_dmi_data('system-uuid')
5d6d67
+        if os.path.exists(prev_iid_path):
5d6d67
+            previous = util.load_file(prev_iid_path).strip()
5d6d67
+            if is_byte_swapped(previous, iid):
5d6d67
+                return previous
5d6d67
+        return iid
5d6d67
+
5d6d67
     @azure_ds_telemetry_reporter
5d6d67
     def setup(self, is_new_instance):
5d6d67
         if self._negotiated is False:
5d6d67
diff --git a/cloudinit/sources/helpers/azure.py b/cloudinit/sources/helpers/azure.py
5d6d67
index 82c4c8c..c2a57cc 100755
5d6d67
--- a/cloudinit/sources/helpers/azure.py
5d6d67
+++ b/cloudinit/sources/helpers/azure.py
5d6d67
@@ -7,6 +7,7 @@ import re
5d6d67
 import socket
5d6d67
 import struct
5d6d67
 import time
5d6d67
+import textwrap
5d6d67
 
5d6d67
 from cloudinit.net import dhcp
5d6d67
 from cloudinit import stages
5d6d67
@@ -40,6 +41,32 @@ def azure_ds_telemetry_reporter(func):
5d6d67
     return impl
5d6d67
 
5d6d67
 
5d6d67
+def is_byte_swapped(previous_id, current_id):
5d6d67
+    """
5d6d67
+    Azure stores the instance ID with an incorrect byte ordering for the
5d6d67
+    first parts. This corrects the byte order such that it is consistent with
5d6d67
+    that returned by the metadata service.
5d6d67
+    """
5d6d67
+    if previous_id == current_id:
5d6d67
+        return False
5d6d67
+
5d6d67
+    def swap_bytestring(s, width=2):
5d6d67
+        dd = [byte for byte in textwrap.wrap(s, 2)]
5d6d67
+        dd.reverse()
5d6d67
+        return ''.join(dd)
5d6d67
+
5d6d67
+    parts = current_id.split('-')
5d6d67
+    swapped_id = '-'.join([
5d6d67
+            swap_bytestring(parts[0]),
5d6d67
+            swap_bytestring(parts[1]),
5d6d67
+            swap_bytestring(parts[2]),
5d6d67
+            parts[3],
5d6d67
+            parts[4]
5d6d67
+        ])
5d6d67
+
5d6d67
+    return previous_id == swapped_id
5d6d67
+
5d6d67
+
5d6d67
 @contextmanager
5d6d67
 def cd(newdir):
5d6d67
     prevdir = os.getcwd()
5d6d67
diff --git a/tests/unittests/test_datasource/test_azure.py b/tests/unittests/test_datasource/test_azure.py
5d6d67
index bc8b42c..1fb0565 100644
5d6d67
--- a/tests/unittests/test_datasource/test_azure.py
5d6d67
+++ b/tests/unittests/test_datasource/test_azure.py
5d6d67
@@ -314,7 +314,7 @@ scbus-1 on xpt0 bus 0
5d6d67
             'public-keys': [],
5d6d67
         })
5d6d67
 
5d6d67
-        self.instance_id = 'test-instance-id'
5d6d67
+        self.instance_id = 'D0DF4C54-4ECB-4A4B-9954-5BDF3ED5C3B8'
5d6d67
 
5d6d67
         def _dmi_mocks(key):
5d6d67
             if key == 'system-uuid':
5d6d67
@@ -511,7 +511,7 @@ fdescfs            /dev/fd          fdescfs rw              0 0
5d6d67
                       'subnet': [{'address': '10.0.0.0', 'prefix': '24'}]},
5d6d67
                 'ipv6': {'ipAddress': []},
5d6d67
                 'macAddress': '000D3A047598'}]}},
5d6d67
-            'instance-id': 'test-instance-id',
5d6d67
+            'instance-id': 'D0DF4C54-4ECB-4A4B-9954-5BDF3ED5C3B8',
5d6d67
             'local-hostname': u'myhost',
5d6d67
             'random_seed': 'wild'}
5d6d67
 
5d6d67
@@ -881,6 +881,24 @@ fdescfs            /dev/fd          fdescfs rw              0 0
5d6d67
         self.assertTrue(ret)
5d6d67
         self.assertEqual('value', dsrc.metadata['test'])
5d6d67
 
5d6d67
+    def test_instance_id_endianness(self):
5d6d67
+        """Return the previous iid when dmi uuid is the byteswapped iid."""
5d6d67
+        ds = self._get_ds({'ovfcontent': construct_valid_ovf_env()})
5d6d67
+        # byte-swapped previous
5d6d67
+        write_file(
5d6d67
+            os.path.join(self.paths.cloud_dir, 'data', 'instance-id'),
5d6d67
+            '544CDFD0-CB4E-4B4A-9954-5BDF3ED5C3B8')
5d6d67
+        ds.get_data()
5d6d67
+        self.assertEqual(
5d6d67
+            '544CDFD0-CB4E-4B4A-9954-5BDF3ED5C3B8', ds.metadata['instance-id'])
5d6d67
+        # not byte-swapped previous
5d6d67
+        write_file(
5d6d67
+            os.path.join(self.paths.cloud_dir, 'data', 'instance-id'),
5d6d67
+            '644CDFD0-CB4E-4B4A-9954-5BDF3ED5C3B8')
5d6d67
+        ds.get_data()
5d6d67
+        self.assertEqual(
5d6d67
+            'D0DF4C54-4ECB-4A4B-9954-5BDF3ED5C3B8', ds.metadata['instance-id'])
5d6d67
+
5d6d67
     def test_instance_id_from_dmidecode_used(self):
5d6d67
         ds = self._get_ds({'ovfcontent': construct_valid_ovf_env()})
5d6d67
         ds.get_data()
5d6d67
@@ -1080,7 +1098,7 @@ class TestAzureBounce(CiTestCase):
5d6d67
 
5d6d67
         def _dmi_mocks(key):
5d6d67
             if key == 'system-uuid':
5d6d67
-                return 'test-instance-id'
5d6d67
+                return 'D0DF4C54-4ECB-4A4B-9954-5BDF3ED5C3B8'
5d6d67
             elif key == 'chassis-asset-tag':
5d6d67
                 return '7783-7084-3265-9085-8269-3286-77'
5d6d67
             raise RuntimeError('should not get here')
5d6d67
diff --git a/tests/unittests/test_datasource/test_azure_helper.py b/tests/unittests/test_datasource/test_azure_helper.py
5d6d67
index bd006ab..7ad5cc1 100644
5d6d67
--- a/tests/unittests/test_datasource/test_azure_helper.py
5d6d67
+++ b/tests/unittests/test_datasource/test_azure_helper.py
5d6d67
@@ -170,6 +170,25 @@ class TestGoalStateParsing(CiTestCase):
5d6d67
         goal_state = self._get_goal_state(instance_id=instance_id)
5d6d67
         self.assertEqual(instance_id, goal_state.instance_id)
5d6d67
 
5d6d67
+    def test_instance_id_byte_swap(self):
5d6d67
+        """Return true when previous_iid is byteswapped current_iid"""
5d6d67
+        previous_iid = "D0DF4C54-4ECB-4A4B-9954-5BDF3ED5C3B8"
5d6d67
+        current_iid = "544CDFD0-CB4E-4B4A-9954-5BDF3ED5C3B8"
5d6d67
+        self.assertTrue(
5d6d67
+            azure_helper.is_byte_swapped(previous_iid, current_iid))
5d6d67
+
5d6d67
+    def test_instance_id_no_byte_swap_same_instance_id(self):
5d6d67
+        previous_iid = "D0DF4C54-4ECB-4A4B-9954-5BDF3ED5C3B8"
5d6d67
+        current_iid = "D0DF4C54-4ECB-4A4B-9954-5BDF3ED5C3B8"
5d6d67
+        self.assertFalse(
5d6d67
+            azure_helper.is_byte_swapped(previous_iid, current_iid))
5d6d67
+
5d6d67
+    def test_instance_id_no_byte_swap_diff_instance_id(self):
5d6d67
+        previous_iid = "D0DF4C54-4ECB-4A4B-9954-5BDF3ED5C3B8"
5d6d67
+        current_iid = "G0DF4C54-4ECB-4A4B-9954-5BDF3ED5C3B8"
5d6d67
+        self.assertFalse(
5d6d67
+            azure_helper.is_byte_swapped(previous_iid, current_iid))
5d6d67
+
5d6d67
     def test_certificates_xml_parsed_and_fetched_correctly(self):
5d6d67
         http_client = mock.MagicMock()
5d6d67
         certificates_url = 'TestCertificatesUrl'
5d6d67
-- 
5d6d67
1.8.3.1
5d6d67