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
This commit is contained in:
Dmitry Tantsur 2021-01-22 16:53:01 +01:00
parent ebdf6784cb
commit 0704bcdd9b
2 changed files with 6 additions and 9 deletions

View File

@ -548,9 +548,11 @@ def _get_configdrive(configdrive, node_uuid, tempdir=None):
try: try:
data = io.BytesIO(base64.decode_as_bytes(data)) data = io.BytesIO(base64.decode_as_bytes(data))
except TypeError: except Exception as exc:
error_msg = (_('Config drive for node %s is not base64 encoded ' error_msg = (_('Config drive for node %(node)s is not base64 encoded '
'or the content is malformed.') % node_uuid) 'or the content is malformed. %(cls)s: %(err)s.')
% {'node': node_uuid, 'err': exc,
'cls': type(exc).__name__})
if is_url: if is_url:
error_msg += _(' Downloaded from "%s".') % configdrive error_msg += _(' Downloaded from "%s".') % configdrive
raise exception.InstanceDeployFailure(error_msg) raise exception.InstanceDeployFailure(error_msg)

View File

@ -22,7 +22,6 @@ from unittest import mock
from oslo_concurrency import processutils from oslo_concurrency import processutils
from oslo_config import cfg from oslo_config import cfg
from oslo_serialization import base64
from oslo_utils import imageutils from oslo_utils import imageutils
import requests import requests
@ -961,14 +960,10 @@ class GetConfigdriveTestCase(base.IronicLibTestCase):
'http://1.2.3.4/cd', 'fake-node-uuid') 'http://1.2.3.4/cd', 'fake-node-uuid')
self.assertFalse(mock_copy.called) self.assertFalse(mock_copy.called)
@mock.patch.object(base64, 'decode_as_bytes', autospec=True) def test_get_configdrive_base64_error(self, mock_requests, mock_copy):
def test_get_configdrive_base64_error(self, mock_b64, mock_requests,
mock_copy):
mock_b64.side_effect = TypeError
self.assertRaises(exception.InstanceDeployFailure, self.assertRaises(exception.InstanceDeployFailure,
disk_utils._get_configdrive, disk_utils._get_configdrive,
'malformed', 'fake-node-uuid') 'malformed', 'fake-node-uuid')
mock_b64.assert_called_once_with('malformed')
self.assertFalse(mock_copy.called) self.assertFalse(mock_copy.called)
@mock.patch.object(gzip, 'GzipFile', autospec=True) @mock.patch.object(gzip, 'GzipFile', autospec=True)