From 1a27bcab802431ff408154a6e246a8ff9697338c Mon Sep 17 00:00:00 2001
From: Kaitlin Farr <kaitlin.farr@jhuapl.edu>
Date: Mon, 23 Oct 2017 16:39:43 -0400
Subject: [PATCH] Fix key_manager API call

If you pass importutils.import_class a string that is not actually a
reference to a python class, you get an "empty module" error.
Cinder's keymgr.API call used to try to import the castellan backend
directly, but this was not necessary as the castellan key_manager.API
call will return the correct backend based on the api_class or backend
option in the config file. If no api_class or backend option is
specified, castellan key_manager API call will return a barbican backend
by default [1].

Castellan used to require the full class name to specify the backend (i.e.
api_class=castellan.key_manager.barbican_key_manager.BarbicanKeyManager),
so the import_class call in cinder was no issue. Recently castellan
was updated to use stevedore entry points and the config option name was changed
to "backend" [2]. The change in castellan is backwards compatible so that if
you use a full module name instead of the stevedore entry point name, it will
still get the correct backend. However, the cinder code was not updated accordingly,
so any time the shorthand stevedore entry point name for the backend is used
instead of the full module name, the import_class call in cinder will fail.

This change removes the unnecessary import_class call and adds an appropriate
unit test that shows the "empty module" error does not occur anymore.

1. https://github.com/openstack/castellan/blob/b13187b34ddcc88bee57e4e99b0115cb92540b97/castellan/key_manager/__init__.py#L25
2. https://github.com/openstack/castellan/commit/8980bf7da55dd084ad84c84534fe937f0d43b9c0

Closes-Bug: #1724952
Change-Id: I3f95cfe439d65d9cf746d4e3844bc70bc705625e
---
 cinder/keymgr/__init__.py             | 5 ++---
 cinder/tests/unit/keymgr/test_init.py | 9 +++++++++
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/cinder/keymgr/__init__.py b/cinder/keymgr/__init__.py
index e7111afd21e..9ebb72f2860 100644
--- a/cinder/keymgr/__init__.py
+++ b/cinder/keymgr/__init__.py
@@ -13,10 +13,10 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+from castellan import key_manager
 from castellan import options as castellan_opts
 from oslo_config import cfg
 from oslo_log import log as logging
-from oslo_utils import importutils
 
 LOG = logging.getLogger(__name__)
 
@@ -26,5 +26,4 @@ castellan_opts.set_defaults(CONF)
 
 
 def API(conf=CONF):
-    cls = importutils.import_class(conf.key_manager.backend)
-    return cls(conf)
+    return key_manager.API(conf)
diff --git a/cinder/tests/unit/keymgr/test_init.py b/cinder/tests/unit/keymgr/test_init.py
index 7f87edb14d7..69f8155196d 100644
--- a/cinder/tests/unit/keymgr/test_init.py
+++ b/cinder/tests/unit/keymgr/test_init.py
@@ -13,6 +13,7 @@
 #    License for the specific language governing permissions and limitations
 #    under the License.
 
+from castellan.key_manager import barbican_key_manager
 from castellan import options as castellan_opts
 
 from oslo_config import cfg
@@ -34,6 +35,14 @@ class InitTestCase(test.TestCase):
         kmgr = keymgr.API(self.config)
         self.assertEqual(type(kmgr), keymgr.conf_key_mgr.ConfKeyManager)
 
+    def test_barbican_backend(self):
+        self.config.set_override(
+            'backend',
+            'barbican',
+            group='key_manager')
+        kmgr = keymgr.API(self.config)
+        self.assertEqual(type(kmgr), barbican_key_manager.BarbicanKeyManager)
+
     def test_set_conf_key_manager(self):
         self.config.set_override(
             'backend',