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