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. b13187b34d/castellan/key_manager/__init__.py (L25)
2. 8980bf7da5

Closes-Bug: #1724952
Change-Id: I3f95cfe439d65d9cf746d4e3844bc70bc705625e
This commit is contained in:
Kaitlin Farr 2017-10-23 16:39:43 -04:00
parent 0ff3fb31f0
commit 1a27bcab80
2 changed files with 11 additions and 3 deletions

View File

@ -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)

View File

@ -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',