42024e
From 68f058e8d20a499f74bc78af8e0c6a90ca57ae20 Mon Sep 17 00:00:00 2001
42024e
From: Thomas Stringer <thstring@microsoft.com>
42024e
Date: Mon, 26 Apr 2021 09:41:38 -0400
42024e
Subject: [PATCH 5/7] Azure: Retrieve username and hostname from IMDS (#865)
42024e
42024e
RH-Author: Eduardo Otubo <otubo@redhat.com>
42024e
RH-MergeRequest: 18: Add support for userdata on Azure from IMDS
42024e
RH-Commit: [5/7] 6a768d31e63e5f00dae0fad2712a7618d62b0879 (otubo/cloud-init-src)
42024e
RH-Bugzilla: 2042351
42024e
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
42024e
RH-Acked-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
42024e
42024e
This change allows us to retrieve the username and hostname from
42024e
IMDS instead of having to rely on the mounted OVF.
42024e
---
42024e
 cloudinit/sources/DataSourceAzure.py          | 149 ++++++++++++++----
42024e
 tests/unittests/test_datasource/test_azure.py |  87 +++++++++-
42024e
 2 files changed, 205 insertions(+), 31 deletions(-)
42024e
42024e
diff --git a/cloudinit/sources/DataSourceAzure.py b/cloudinit/sources/DataSourceAzure.py
42024e
index 39e67c4f..6d7954ee 100755
42024e
--- a/cloudinit/sources/DataSourceAzure.py
42024e
+++ b/cloudinit/sources/DataSourceAzure.py
42024e
@@ -5,6 +5,7 @@
42024e
 # This file is part of cloud-init. See LICENSE file for license information.
42024e
 
42024e
 import base64
42024e
+from collections import namedtuple
42024e
 import contextlib
42024e
 import crypt
42024e
 from functools import partial
42024e
@@ -25,6 +26,7 @@ from cloudinit.net import device_driver
42024e
 from cloudinit.net.dhcp import EphemeralDHCPv4
42024e
 from cloudinit import sources
42024e
 from cloudinit.sources.helpers import netlink
42024e
+from cloudinit import ssh_util
42024e
 from cloudinit import subp
42024e
 from cloudinit.url_helper import UrlError, readurl, retry_on_url_exc
42024e
 from cloudinit import util
42024e
@@ -80,7 +82,12 @@ AGENT_SEED_DIR = '/var/lib/waagent'
42024e
 IMDS_TIMEOUT_IN_SECONDS = 2
42024e
 IMDS_URL = "http://169.254.169.254/metadata"
42024e
 IMDS_VER_MIN = "2019-06-01"
42024e
-IMDS_VER_WANT = "2020-09-01"
42024e
+IMDS_VER_WANT = "2020-10-01"
42024e
+
42024e
+
42024e
+# This holds SSH key data including if the source was
42024e
+# from IMDS, as well as the SSH key data itself.
42024e
+SSHKeys = namedtuple("SSHKeys", ("keys_from_imds", "ssh_keys"))
42024e
 
42024e
 
42024e
 class metadata_type(Enum):
42024e
@@ -391,6 +398,8 @@ class DataSourceAzure(sources.DataSource):
42024e
         """Return the subplatform metadata source details."""
42024e
         if self.seed.startswith('/dev'):
42024e
             subplatform_type = 'config-disk'
42024e
+        elif self.seed.lower() == 'imds':
42024e
+            subplatform_type = 'imds'
42024e
         else:
42024e
             subplatform_type = 'seed-dir'
42024e
         return '%s (%s)' % (subplatform_type, self.seed)
42024e
@@ -433,9 +442,11 @@ class DataSourceAzure(sources.DataSource):
42024e
 
42024e
         found = None
42024e
         reprovision = False
42024e
+        ovf_is_accessible = True
42024e
         reprovision_after_nic_attach = False
42024e
         for cdev in candidates:
42024e
             try:
42024e
+                LOG.debug("cdev: %s", cdev)
42024e
                 if cdev == "IMDS":
42024e
                     ret = None
42024e
                     reprovision = True
42024e
@@ -462,8 +473,18 @@ class DataSourceAzure(sources.DataSource):
42024e
                 raise sources.InvalidMetaDataException(msg)
42024e
             except util.MountFailedError:
42024e
                 report_diagnostic_event(
42024e
-                    '%s was not mountable' % cdev, logger_func=LOG.warning)
42024e
-                continue
42024e
+                    '%s was not mountable' % cdev, logger_func=LOG.debug)
42024e
+                cdev = 'IMDS'
42024e
+                ovf_is_accessible = False
42024e
+                empty_md = {'local-hostname': ''}
42024e
+                empty_cfg = dict(
42024e
+                    system_info=dict(
42024e
+                        default_user=dict(
42024e
+                            name=''
42024e
+                        )
42024e
+                    )
42024e
+                )
42024e
+                ret = (empty_md, '', empty_cfg, {})
42024e
 
42024e
             report_diagnostic_event("Found provisioning metadata in %s" % cdev,
42024e
                                     logger_func=LOG.debug)
42024e
@@ -490,6 +511,10 @@ class DataSourceAzure(sources.DataSource):
42024e
                 self.fallback_interface,
42024e
                 retries=10
42024e
             )
42024e
+            if not imds_md and not ovf_is_accessible:
42024e
+                msg = 'No OVF or IMDS available'
42024e
+                report_diagnostic_event(msg)
42024e
+                raise sources.InvalidMetaDataException(msg)
42024e
             (md, userdata_raw, cfg, files) = ret
42024e
             self.seed = cdev
42024e
             crawled_data.update({
42024e
@@ -498,6 +523,21 @@ class DataSourceAzure(sources.DataSource):
42024e
                 'metadata': util.mergemanydict(
42024e
                     [md, {'imds': imds_md}]),
42024e
                 'userdata_raw': userdata_raw})
42024e
+            imds_username = _username_from_imds(imds_md)
42024e
+            imds_hostname = _hostname_from_imds(imds_md)
42024e
+            imds_disable_password = _disable_password_from_imds(imds_md)
42024e
+            if imds_username:
42024e
+                LOG.debug('Username retrieved from IMDS: %s', imds_username)
42024e
+                cfg['system_info']['default_user']['name'] = imds_username
42024e
+            if imds_hostname:
42024e
+                LOG.debug('Hostname retrieved from IMDS: %s', imds_hostname)
42024e
+                crawled_data['metadata']['local-hostname'] = imds_hostname
42024e
+            if imds_disable_password:
42024e
+                LOG.debug(
42024e
+                    'Disable password retrieved from IMDS: %s',
42024e
+                    imds_disable_password
42024e
+                )
42024e
+                crawled_data['metadata']['disable_password'] = imds_disable_password  # noqa: E501
42024e
             found = cdev
42024e
 
42024e
             report_diagnostic_event(
42024e
@@ -676,6 +716,13 @@ class DataSourceAzure(sources.DataSource):
42024e
 
42024e
     @azure_ds_telemetry_reporter
42024e
     def get_public_ssh_keys(self):
42024e
+        """
42024e
+        Retrieve public SSH keys.
42024e
+        """
42024e
+
42024e
+        return self._get_public_ssh_keys_and_source().ssh_keys
42024e
+
42024e
+    def _get_public_ssh_keys_and_source(self):
42024e
         """
42024e
         Try to get the ssh keys from IMDS first, and if that fails
42024e
         (i.e. IMDS is unavailable) then fallback to getting the ssh
42024e
@@ -685,30 +732,50 @@ class DataSourceAzure(sources.DataSource):
42024e
         advantage, so this is a strong preference. But we must keep
42024e
         OVF as a second option for environments that don't have IMDS.
42024e
         """
42024e
+
42024e
         LOG.debug('Retrieving public SSH keys')
42024e
         ssh_keys = []
42024e
+        keys_from_imds = True
42024e
+        LOG.debug('Attempting to get SSH keys from IMDS')
42024e
         try:
42024e
-            raise KeyError(
42024e
-                "Not using public SSH keys from IMDS"
42024e
-            )
42024e
-            # pylint:disable=unreachable
42024e
             ssh_keys = [
42024e
                 public_key['keyData']
42024e
                 for public_key
42024e
                 in self.metadata['imds']['compute']['publicKeys']
42024e
             ]
42024e
-            LOG.debug('Retrieved SSH keys from IMDS')
42024e
+            for key in ssh_keys:
42024e
+                if not _key_is_openssh_formatted(key=key):
42024e
+                    keys_from_imds = False
42024e
+                    break
42024e
+
42024e
+            if not keys_from_imds:
42024e
+                log_msg = 'Keys not in OpenSSH format, using OVF'
42024e
+            else:
42024e
+                log_msg = 'Retrieved {} keys from IMDS'.format(
42024e
+                    len(ssh_keys)
42024e
+                    if ssh_keys is not None
42024e
+                    else 0
42024e
+                )
42024e
         except KeyError:
42024e
             log_msg = 'Unable to get keys from IMDS, falling back to OVF'
42024e
+            keys_from_imds = False
42024e
+        finally:
42024e
             report_diagnostic_event(log_msg, logger_func=LOG.debug)
42024e
+
42024e
+        if not keys_from_imds:
42024e
+            LOG.debug('Attempting to get SSH keys from OVF')
42024e
             try:
42024e
                 ssh_keys = self.metadata['public-keys']
42024e
-                LOG.debug('Retrieved keys from OVF')
42024e
+                log_msg = 'Retrieved {} keys from OVF'.format(len(ssh_keys))
42024e
             except KeyError:
42024e
                 log_msg = 'No keys available from OVF'
42024e
+            finally:
42024e
                 report_diagnostic_event(log_msg, logger_func=LOG.debug)
42024e
 
42024e
-        return ssh_keys
42024e
+        return SSHKeys(
42024e
+            keys_from_imds=keys_from_imds,
42024e
+            ssh_keys=ssh_keys
42024e
+        )
42024e
 
42024e
     def get_config_obj(self):
42024e
         return self.cfg
42024e
@@ -1325,30 +1392,21 @@ class DataSourceAzure(sources.DataSource):
42024e
         self.bounce_network_with_azure_hostname()
42024e
 
42024e
         pubkey_info = None
42024e
-        try:
42024e
-            raise KeyError(
42024e
-                "Not using public SSH keys from IMDS"
42024e
-            )
42024e
-            # pylint:disable=unreachable
42024e
-            public_keys = self.metadata['imds']['compute']['publicKeys']
42024e
-            LOG.debug(
42024e
-                'Successfully retrieved %s key(s) from IMDS',
42024e
-                len(public_keys)
42024e
-                if public_keys is not None
42024e
+        ssh_keys_and_source = self._get_public_ssh_keys_and_source()
42024e
+
42024e
+        if not ssh_keys_and_source.keys_from_imds:
42024e
+            pubkey_info = self.cfg.get('_pubkeys', None)
42024e
+            log_msg = 'Retrieved {} fingerprints from OVF'.format(
42024e
+                len(pubkey_info)
42024e
+                if pubkey_info is not None
42024e
                 else 0
42024e
             )
42024e
-        except KeyError:
42024e
-            LOG.debug(
42024e
-                'Unable to retrieve SSH keys from IMDS during '
42024e
-                'negotiation, falling back to OVF'
42024e
-            )
42024e
-            pubkey_info = self.cfg.get('_pubkeys', None)
42024e
+            report_diagnostic_event(log_msg, logger_func=LOG.debug)
42024e
 
42024e
         metadata_func = partial(get_metadata_from_fabric,
42024e
                                 fallback_lease_file=self.
42024e
                                 dhclient_lease_file,
42024e
-                                pubkey_info=pubkey_info,
42024e
-                                iso_dev=self.iso_dev)
42024e
+                                pubkey_info=pubkey_info)
42024e
 
42024e
         LOG.debug("negotiating with fabric via agent command %s",
42024e
                   self.ds_cfg['agent_command'])
42024e
@@ -1404,6 +1462,41 @@ class DataSourceAzure(sources.DataSource):
42024e
         return self.metadata.get('imds', {}).get('compute', {}).get('location')
42024e
 
42024e
 
42024e
+def _username_from_imds(imds_data):
42024e
+    try:
42024e
+        return imds_data['compute']['osProfile']['adminUsername']
42024e
+    except KeyError:
42024e
+        return None
42024e
+
42024e
+
42024e
+def _hostname_from_imds(imds_data):
42024e
+    try:
42024e
+        return imds_data['compute']['osProfile']['computerName']
42024e
+    except KeyError:
42024e
+        return None
42024e
+
42024e
+
42024e
+def _disable_password_from_imds(imds_data):
42024e
+    try:
42024e
+        return imds_data['compute']['osProfile']['disablePasswordAuthentication'] == 'true'  # noqa: E501
42024e
+    except KeyError:
42024e
+        return None
42024e
+
42024e
+
42024e
+def _key_is_openssh_formatted(key):
42024e
+    """
42024e
+    Validate whether or not the key is OpenSSH-formatted.
42024e
+    """
42024e
+
42024e
+    parser = ssh_util.AuthKeyLineParser()
42024e
+    try:
42024e
+        akl = parser.parse(key)
42024e
+    except TypeError:
42024e
+        return False
42024e
+
42024e
+    return akl.keytype is not None
42024e
+
42024e
+
42024e
 def _partitions_on_device(devpath, maxnum=16):
42024e
     # return a list of tuples (ptnum, path) for each part on devpath
42024e
     for suff in ("-part", "p", ""):
42024e
diff --git a/tests/unittests/test_datasource/test_azure.py b/tests/unittests/test_datasource/test_azure.py
42024e
index 320fa857..d9817d84 100644
42024e
--- a/tests/unittests/test_datasource/test_azure.py
42024e
+++ b/tests/unittests/test_datasource/test_azure.py
42024e
@@ -108,7 +108,7 @@ NETWORK_METADATA = {
42024e
         "zone": "",
42024e
         "publicKeys": [
42024e
             {
42024e
-                "keyData": "key1",
42024e
+                "keyData": "ssh-rsa key1",
42024e
                 "path": "path1"
42024e
             }
42024e
         ]
42024e
@@ -1761,8 +1761,29 @@ scbus-1 on xpt0 bus 0
42024e
         dsrc.get_data()
42024e
         dsrc.setup(True)
42024e
         ssh_keys = dsrc.get_public_ssh_keys()
42024e
-        # Temporarily alter this test so that SSH public keys
42024e
-        # from IMDS are *not* going to be in use to fix a regression.
42024e
+        self.assertEqual(ssh_keys, ["ssh-rsa key1"])
42024e
+        self.assertEqual(m_parse_certificates.call_count, 0)
42024e
+
42024e
+    @mock.patch(
42024e
+        'cloudinit.sources.helpers.azure.OpenSSLManager.parse_certificates')
42024e
+    @mock.patch(MOCKPATH + 'get_metadata_from_imds')
42024e
+    def test_get_public_ssh_keys_with_no_openssh_format(
42024e
+            self,
42024e
+            m_get_metadata_from_imds,
42024e
+            m_parse_certificates):
42024e
+        imds_data = copy.deepcopy(NETWORK_METADATA)
42024e
+        imds_data['compute']['publicKeys'][0]['keyData'] = 'no-openssh-format'
42024e
+        m_get_metadata_from_imds.return_value = imds_data
42024e
+        sys_cfg = {'datasource': {'Azure': {'apply_network_config': True}}}
42024e
+        odata = {'HostName': "myhost", 'UserName': "myuser"}
42024e
+        data = {
42024e
+            'ovfcontent': construct_valid_ovf_env(data=odata),
42024e
+            'sys_cfg': sys_cfg
42024e
+        }
42024e
+        dsrc = self._get_ds(data)
42024e
+        dsrc.get_data()
42024e
+        dsrc.setup(True)
42024e
+        ssh_keys = dsrc.get_public_ssh_keys()
42024e
         self.assertEqual(ssh_keys, [])
42024e
         self.assertEqual(m_parse_certificates.call_count, 0)
42024e
 
42024e
@@ -1818,6 +1839,66 @@ scbus-1 on xpt0 bus 0
42024e
         self.assertIsNotNone(dsrc.metadata)
42024e
         self.assertFalse(dsrc.failed_desired_api_version)
42024e
 
42024e
+    @mock.patch(MOCKPATH + 'get_metadata_from_imds')
42024e
+    def test_hostname_from_imds(self, m_get_metadata_from_imds):
42024e
+        sys_cfg = {'datasource': {'Azure': {'apply_network_config': True}}}
42024e
+        odata = {'HostName': "myhost", 'UserName': "myuser"}
42024e
+        data = {
42024e
+            'ovfcontent': construct_valid_ovf_env(data=odata),
42024e
+            'sys_cfg': sys_cfg
42024e
+        }
42024e
+        imds_data_with_os_profile = copy.deepcopy(NETWORK_METADATA)
42024e
+        imds_data_with_os_profile["compute"]["osProfile"] = dict(
42024e
+            adminUsername="username1",
42024e
+            computerName="hostname1",
42024e
+            disablePasswordAuthentication="true"
42024e
+        )
42024e
+        m_get_metadata_from_imds.return_value = imds_data_with_os_profile
42024e
+        dsrc = self._get_ds(data)
42024e
+        dsrc.get_data()
42024e
+        self.assertEqual(dsrc.metadata["local-hostname"], "hostname1")
42024e
+
42024e
+    @mock.patch(MOCKPATH + 'get_metadata_from_imds')
42024e
+    def test_username_from_imds(self, m_get_metadata_from_imds):
42024e
+        sys_cfg = {'datasource': {'Azure': {'apply_network_config': True}}}
42024e
+        odata = {'HostName': "myhost", 'UserName': "myuser"}
42024e
+        data = {
42024e
+            'ovfcontent': construct_valid_ovf_env(data=odata),
42024e
+            'sys_cfg': sys_cfg
42024e
+        }
42024e
+        imds_data_with_os_profile = copy.deepcopy(NETWORK_METADATA)
42024e
+        imds_data_with_os_profile["compute"]["osProfile"] = dict(
42024e
+            adminUsername="username1",
42024e
+            computerName="hostname1",
42024e
+            disablePasswordAuthentication="true"
42024e
+        )
42024e
+        m_get_metadata_from_imds.return_value = imds_data_with_os_profile
42024e
+        dsrc = self._get_ds(data)
42024e
+        dsrc.get_data()
42024e
+        self.assertEqual(
42024e
+            dsrc.cfg["system_info"]["default_user"]["name"],
42024e
+            "username1"
42024e
+        )
42024e
+
42024e
+    @mock.patch(MOCKPATH + 'get_metadata_from_imds')
42024e
+    def test_disable_password_from_imds(self, m_get_metadata_from_imds):
42024e
+        sys_cfg = {'datasource': {'Azure': {'apply_network_config': True}}}
42024e
+        odata = {'HostName': "myhost", 'UserName': "myuser"}
42024e
+        data = {
42024e
+            'ovfcontent': construct_valid_ovf_env(data=odata),
42024e
+            'sys_cfg': sys_cfg
42024e
+        }
42024e
+        imds_data_with_os_profile = copy.deepcopy(NETWORK_METADATA)
42024e
+        imds_data_with_os_profile["compute"]["osProfile"] = dict(
42024e
+            adminUsername="username1",
42024e
+            computerName="hostname1",
42024e
+            disablePasswordAuthentication="true"
42024e
+        )
42024e
+        m_get_metadata_from_imds.return_value = imds_data_with_os_profile
42024e
+        dsrc = self._get_ds(data)
42024e
+        dsrc.get_data()
42024e
+        self.assertTrue(dsrc.metadata["disable_password"])
42024e
+
42024e
 
42024e
 class TestAzureBounce(CiTestCase):
42024e
 
42024e
-- 
42024e
2.27.0
42024e