From 35a90fd3b599186ef49da2908f9ac452446339db Mon Sep 17 00:00:00 2001 From: Eli Qiao Date: Thu, 6 Aug 2015 16:58:21 +0800 Subject: [PATCH] Handle KeyManager exception when deleting a volume This patch adds exception handler when deleting key of an encryption volume. Raise 400 invalid volume instead of 500 to give the user a better understanding why deleting failed. Closes-bug: #1482120 Change-Id: I591f65823d1f212e92434323905cc9c4fbe6fd96 --- cinder/tests/unit/test_volume.py | 13 +++++++++++++ cinder/volume/api.py | 6 +++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/cinder/tests/unit/test_volume.py b/cinder/tests/unit/test_volume.py index 537e15869..8ffd4b239 100644 --- a/cinder/tests/unit/test_volume.py +++ b/cinder/tests/unit/test_volume.py @@ -5592,6 +5592,19 @@ class VolumeTestCase(BaseVolumeTestCase): discover) self.assertEqual(expected, capabilities['properties']) + def test_delete_encryptied_volume(self): + self.volume_params['status'] = 'active' + volume = tests_utils.create_volume(self.context, + **self.volume_params) + vol_api = cinder.volume.api.API() + with mock.patch.object( + vol_api.key_manager, + 'delete_key', + side_effect=Exception): + self.assertRaises(exception.InvalidVolume, + vol_api.delete, + self.context, volume) + class CopyVolumeToImageTestCase(BaseVolumeTestCase): def fake_local_path(self, volume): diff --git a/cinder/volume/api.py b/cinder/volume/api.py index 18500e479..d2d26de3b 100644 --- a/cinder/volume/api.py +++ b/cinder/volume/api.py @@ -394,7 +394,11 @@ class API(base.Base): # because the volume cannot be decrypted without its key. encryption_key_id = volume.get('encryption_key_id', None) if encryption_key_id is not None: - self.key_manager.delete_key(context, encryption_key_id) + try: + self.key_manager.delete_key(context, encryption_key_id) + except Exception as e: + msg = _("Unable to delete encrypted volume: %s.") % e.msg + raise exception.InvalidVolume(reason=msg) now = timeutils.utcnow() vref = self.db.volume_update(context, -- 2.45.2