From 01b7c8728583e7e0d0a2e9a716d8397001f86b9d Mon Sep 17 00:00:00 2001 From: Jamie Lennox <jamielennox@gmail.com> Date: Wed, 24 Aug 2016 10:55:05 +1000 Subject: [PATCH] Implement caching for the generic plugins. When caching was implemented it was added to the v2 and v3 plugins but for some reason the generic plugins were missed. To do generic plugin caching we'll skip even trying to figure out the inner plugin for now and just load the authentication for the generic. Closes-Bug: #1616261 Change-Id: Icbb8acde0dca084f4a221f8ebff5503d5bdc219a --- keystoneauth1/identity/generic/base.py | 40 ++++++++++++++++--- keystoneauth1/identity/generic/password.py | 26 ++++++++++++ keystoneauth1/identity/generic/token.py | 5 +++ .../tests/unit/identity/test_password.py | 33 +++++++++++++++ .../tests/unit/identity/test_token.py | 29 ++++++++++++++ 5 files changed, 128 insertions(+), 5 deletions(-) diff --git a/keystoneauth1/identity/generic/base.py b/keystoneauth1/identity/generic/base.py index fb20e1da..4b9b6f0e 100644 --- a/keystoneauth1/identity/generic/base.py +++ b/keystoneauth1/identity/generic/base.py @@ -98,17 +98,30 @@ class BaseGenericPlugin(base.BaseIdentityPlugin): @property def _v3_params(self): """Return the parameters that are common to v3 plugins.""" - pr_domain_id = self._project_domain_id or self._default_domain_id - pr_domain_name = self._project_domain_name or self._default_domain_name - return {'trust_id': self._trust_id, 'project_id': self._project_id, 'project_name': self._project_name, - 'project_domain_id': pr_domain_id, - 'project_domain_name': pr_domain_name, + 'project_domain_id': self.project_domain_id, + 'project_domain_name': self.project_domain_name, 'domain_id': self._domain_id, 'domain_name': self._domain_name} + @property + def project_domain_id(self): + return self._project_domain_id or self._default_domain_id + + @project_domain_id.setter + def project_domain_id(self, value): + self._project_domain_id = value + + @property + def project_domain_name(self): + return self._project_domain_name or self._default_domain_name + + @project_domain_name.setter + def project_domain_name(self, value): + self._project_domain_name = value + def _do_create_plugin(self, session): plugin = None @@ -179,3 +192,20 @@ class BaseGenericPlugin(base.BaseIdentityPlugin): self._plugin = self._do_create_plugin(session) return self._plugin.get_auth_ref(session, **kwargs) + + def get_cache_id_elements(self, _implemented=False): + # NOTE(jamielennox): implemented here is just a way to make sure that + # something overrides this method. We don't want the base + # implementation to respond with a dict without the subclass modifying + # it to add their own data in case the subclass doesn't support caching + if not _implemented: + raise NotImplemented() + + return {'auth_url': self.auth_url, + 'project_id': self._project_id, + 'project_name': self._project_name, + 'project_domain_id': self.project_domain_id, + 'project_domain_name': self.project_domain_name, + 'domain_id': self._domain_id, + 'domain_name': self._domain_name, + 'trust_id': self._trust_id} diff --git a/keystoneauth1/identity/generic/password.py b/keystoneauth1/identity/generic/password.py index d6fc9d0a..4af64111 100644 --- a/keystoneauth1/identity/generic/password.py +++ b/keystoneauth1/identity/generic/password.py @@ -62,3 +62,29 @@ class Password(base.BaseGenericPlugin): user_domain_name=u_domain_name, password=self._password, **self._v3_params) + + @property + def user_domain_id(self): + return self._user_domain_id or self._default_domain_id + + @user_domain_id.setter + def user_domain_id(self, value): + self._user_domain_id = value + + @property + def user_domain_name(self): + return self._user_domain_name or self._default_domain_name + + @user_domain_name.setter + def user_domain_name(self, value): + self._user_domain_name = value + + def get_cache_id_elements(self): + elements = super(Password, self).get_cache_id_elements( + _implemented=True) + elements['username'] = self._username + elements['user_id'] = self._user_id + elements['password'] = self._password + elements['user_domain_id'] = self.user_domain_id + elements['user_domain_name'] = self.user_domain_name + return elements diff --git a/keystoneauth1/identity/generic/token.py b/keystoneauth1/identity/generic/token.py index 8ce33a05..00239b61 100644 --- a/keystoneauth1/identity/generic/token.py +++ b/keystoneauth1/identity/generic/token.py @@ -32,3 +32,8 @@ class Token(base.BaseGenericPlugin): elif discover.version_match((3,), version): return v3.Token(url, self._token, **self._v3_params) + + def get_cache_id_elements(self): + elements = super(Token, self).get_cache_id_elements(_implemented=True) + elements['token'] = self._token + return elements diff --git a/keystoneauth1/tests/unit/identity/test_password.py b/keystoneauth1/tests/unit/identity/test_password.py index 04f8be8e..7c910186 100644 --- a/keystoneauth1/tests/unit/identity/test_password.py +++ b/keystoneauth1/tests/unit/identity/test_password.py @@ -71,3 +71,36 @@ class PasswordTests(utils.GenericPluginTestCase): self.assertEqual(default_domain_name, p._plugin.auth_methods[0].user_domain_name) + + def test_password_cache_id(self): + username = uuid.uuid4().hex + the_password = uuid.uuid4().hex + project_name = uuid.uuid4().hex + default_domain_id = uuid.uuid4().hex + + a = password.Password(self.TEST_URL, + username=username, + password=the_password, + project_name=project_name, + default_domain_id=default_domain_id) + + b = password.Password(self.TEST_URL, + username=username, + password=the_password, + project_name=project_name, + default_domain_id=default_domain_id) + + a_id = a.get_cache_id() + b_id = b.get_cache_id() + + self.assertEqual(a_id, b_id) + + c = password.Password(self.TEST_URL, + username=username, + password=uuid.uuid4().hex, # different + project_name=project_name, + default_domain_id=default_domain_id) + + c_id = c.get_cache_id() + + self.assertNotEqual(a_id, c_id) diff --git a/keystoneauth1/tests/unit/identity/test_token.py b/keystoneauth1/tests/unit/identity/test_token.py index c70be03a..0303fb15 100644 --- a/keystoneauth1/tests/unit/identity/test_token.py +++ b/keystoneauth1/tests/unit/identity/test_token.py @@ -32,3 +32,32 @@ class TokenTests(utils.GenericPluginTestCase): def test_symbols(self): self.assertIs(v3.Token, v3_token.Token) self.assertIs(v3.TokenMethod, v3_token.TokenMethod) + + def test_token_cache_id(self): + the_token = uuid.uuid4().hex + project_name = uuid.uuid4().hex + default_domain_id = uuid.uuid4().hex + + a = token.Token(self.TEST_URL, + token=the_token, + project_name=project_name, + default_domain_id=default_domain_id) + + b = token.Token(self.TEST_URL, + token=the_token, + project_name=project_name, + default_domain_id=default_domain_id) + + a_id = a.get_cache_id() + b_id = b.get_cache_id() + + self.assertEqual(a_id, b_id) + + c = token.Token(self.TEST_URL, + token=the_token, + project_name=uuid.uuid4().hex, # different + default_domain_id=default_domain_id) + + c_id = c.get_cache_id() + + self.assertNotEqual(a_id, c_id)