Skip to content

Commit a777384

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "libvirt: disconnect volume when encryption fails"
2 parents dbfb6de + 79bcb4e commit a777384

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

nova/tests/unit/virt/libvirt/test_driver.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7450,6 +7450,66 @@ def test_get_volume_config(self, _set_cache_mode, get_config):
74507450
_set_cache_mode.assert_called_once_with(config)
74517451
self.assertEqual(config_guest_disk.to_xml(), config.to_xml())
74527452

7453+
@mock.patch.object(libvirt_driver.LibvirtDriver, '_get_volume_driver')
7454+
@mock.patch.object(libvirt_driver.LibvirtDriver, '_attach_encryptor')
7455+
def test_connect_volume_encryption_success(
7456+
self, mock_attach_encryptor, mock_get_volume_driver):
7457+
7458+
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
7459+
mock_volume_driver = mock.MagicMock(
7460+
spec=volume_drivers.LibvirtBaseVolumeDriver)
7461+
mock_get_volume_driver.return_value = mock_volume_driver
7462+
7463+
connection_info = {'driver_volume_type': 'fake',
7464+
'data': {'device_path': '/fake',
7465+
'access_mode': 'rw',
7466+
'volume_id': uuids.volume_id}}
7467+
encryption = {'provider': encryptors.LUKS,
7468+
'encryption_key_id': uuids.encryption_key_id}
7469+
instance = mock.sentinel.instance
7470+
7471+
drvr._connect_volume(self.context, connection_info, instance,
7472+
encryption=encryption)
7473+
7474+
mock_get_volume_driver.assert_called_once_with(connection_info)
7475+
mock_volume_driver.connect_volume.assert_called_once_with(
7476+
connection_info, instance)
7477+
mock_attach_encryptor.assert_called_once_with(
7478+
self.context, connection_info, encryption, True)
7479+
mock_volume_driver.disconnect_volume.assert_not_called()
7480+
7481+
@mock.patch.object(libvirt_driver.LibvirtDriver, '_get_volume_driver')
7482+
@mock.patch.object(libvirt_driver.LibvirtDriver, '_attach_encryptor')
7483+
def test_connect_volume_encryption_fail(
7484+
self, mock_attach_encryptor, mock_get_volume_driver):
7485+
7486+
drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
7487+
mock_volume_driver = mock.MagicMock(
7488+
spec=volume_drivers.LibvirtBaseVolumeDriver)
7489+
mock_get_volume_driver.return_value = mock_volume_driver
7490+
7491+
connection_info = {'driver_volume_type': 'fake',
7492+
'data': {'device_path': '/fake',
7493+
'access_mode': 'rw',
7494+
'volume_id': uuids.volume_id}}
7495+
encryption = {'provider': encryptors.LUKS,
7496+
'encryption_key_id': uuids.encryption_key_id}
7497+
instance = mock.sentinel.instance
7498+
mock_attach_encryptor.side_effect = processutils.ProcessExecutionError
7499+
7500+
self.assertRaises(processutils.ProcessExecutionError,
7501+
drvr._connect_volume,
7502+
self.context, connection_info, instance,
7503+
encryption=encryption)
7504+
7505+
mock_get_volume_driver.assert_called_once_with(connection_info)
7506+
mock_volume_driver.connect_volume.assert_called_once_with(
7507+
connection_info, instance)
7508+
mock_attach_encryptor.assert_called_once_with(
7509+
self.context, connection_info, encryption, True)
7510+
mock_volume_driver.disconnect_volume.assert_called_once_with(
7511+
connection_info, instance)
7512+
74537513
@mock.patch.object(key_manager, 'API')
74547514
@mock.patch.object(libvirt_driver.LibvirtDriver, '_get_volume_encryption')
74557515
@mock.patch.object(libvirt_driver.LibvirtDriver, '_use_native_luks')

nova/virt/libvirt/driver.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,8 +1301,15 @@ def _connect_volume(self, context, connection_info, instance,
13011301
encryption=None, allow_native_luks=True):
13021302
vol_driver = self._get_volume_driver(connection_info)
13031303
vol_driver.connect_volume(connection_info, instance)
1304-
self._attach_encryptor(context, connection_info, encryption,
1305-
allow_native_luks)
1304+
try:
1305+
self._attach_encryptor(
1306+
context, connection_info, encryption, allow_native_luks)
1307+
except Exception:
1308+
# Encryption failed so rollback the volume connection.
1309+
with excutils.save_and_reraise_exception(logger=LOG):
1310+
LOG.exception("Failure attaching encryptor; rolling back "
1311+
"volume connection", instance=instance)
1312+
vol_driver.disconnect_volume(connection_info, instance)
13061313

13071314
def _should_disconnect_target(self, context, connection_info, instance):
13081315
connection_count = 0

0 commit comments

Comments
 (0)