Browse Source

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
(cherry picked from commit 0704bcdd9b)
changes/11/773011/1 4.4.1
Dmitry Tantsur 3 months ago
parent
commit
7ac01e1f99
2 changed files with 6 additions and 9 deletions
  1. +5
    -3
      ironic_lib/disk_utils.py
  2. +1
    -6
      ironic_lib/tests/test_disk_utils.py

+ 5
- 3
ironic_lib/disk_utils.py View File

@ -561,9 +561,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)


+ 1
- 6
ironic_lib/tests/test_disk_utils.py View File

@ -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
@ -1013,14 +1012,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)


Loading…
Cancel
Save