From 0704bcdd9b4fafa50a41a29393209fc1bca2ede8 Mon Sep 17 00:00:00 2001 From: Dmitry Tantsur Date: Fri, 22 Jan 2021 16:53:01 +0100 Subject: [PATCH] Correct base64 error handling The base64 module can raise many exceptions, TypeError doesn't even seem one of them. Capture all errors and provide the exact message. Change-Id: Id3748e729f8dd31aa6d2d7084bd01088cd9a7ecd --- ironic_lib/disk_utils.py | 8 +++++--- ironic_lib/tests/test_disk_utils.py | 7 +------ 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/ironic_lib/disk_utils.py b/ironic_lib/disk_utils.py index a31390bb..fbe7e953 100644 --- a/ironic_lib/disk_utils.py +++ b/ironic_lib/disk_utils.py @@ -548,9 +548,11 @@ def _get_configdrive(configdrive, node_uuid, tempdir=None): try: data = io.BytesIO(base64.decode_as_bytes(data)) - except TypeError: - error_msg = (_('Config drive for node %s is not base64 encoded ' - 'or the content is malformed.') % node_uuid) + except Exception as exc: + error_msg = (_('Config drive for node %(node)s is not base64 encoded ' + 'or the content is malformed. %(cls)s: %(err)s.') + % {'node': node_uuid, 'err': exc, + 'cls': type(exc).__name__}) if is_url: error_msg += _(' Downloaded from "%s".') % configdrive raise exception.InstanceDeployFailure(error_msg) diff --git a/ironic_lib/tests/test_disk_utils.py b/ironic_lib/tests/test_disk_utils.py index d163c8e2..0f2ce0e9 100644 --- a/ironic_lib/tests/test_disk_utils.py +++ b/ironic_lib/tests/test_disk_utils.py @@ -22,7 +22,6 @@ from unittest import mock from oslo_concurrency import processutils from oslo_config import cfg -from oslo_serialization import base64 from oslo_utils import imageutils import requests @@ -961,14 +960,10 @@ class GetConfigdriveTestCase(base.IronicLibTestCase): 'http://1.2.3.4/cd', 'fake-node-uuid') self.assertFalse(mock_copy.called) - @mock.patch.object(base64, 'decode_as_bytes', autospec=True) - def test_get_configdrive_base64_error(self, mock_b64, mock_requests, - mock_copy): - mock_b64.side_effect = TypeError + def test_get_configdrive_base64_error(self, mock_requests, mock_copy): self.assertRaises(exception.InstanceDeployFailure, disk_utils._get_configdrive, 'malformed', 'fake-node-uuid') - mock_b64.assert_called_once_with('malformed') self.assertFalse(mock_copy.called) @mock.patch.object(gzip, 'GzipFile', autospec=True)