Skip to content

Commit

Permalink
Fix broken v4 caching due to leader-get asymmetry
Browse files Browse the repository at this point in the history
leader-get decodes using json, but leader-set just sets the keys. This
wasn't taken into consideration when fetching all the keys to filter for
cached keys when a relation is leaving.  This is resolved in this patch.

func-test-pr: openstack-charmers/zaza-openstack-tests#1153

Change-Id: I2d44ec0c43c1ecffd9ac77a1162ead4e4a01aabe
(cherry picked from commit d925ac7)
(cherry picked from commit 0a18ac2)
  • Loading branch information
ajkavanagh authored and xtrusia committed Nov 29, 2023
1 parent a3ff396 commit 9c32533
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/lib/charm/vault_pki.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,14 @@ def _fetch(key):
"""
value = hookenv.leader_get(key)
if value:
return json.loads(value)
if key is not None:
# load the value that was json serialised in _store()
return json.loads(value)
else:
# due to a weird asymetry been leader_set and leader_get,
# leader_get() already deserialises as json so if no key was
# specified, it's already been deserialised.
return value
return ""

@staticmethod
Expand Down
15 changes: 15 additions & 0 deletions unit_tests/test_lib_charm_vault_pki.py
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,21 @@ def test_certcache__fetch(self, mock_leader_get):
self.assertEqual(vault_pki.CertCache(request)._fetch("mine"),
'the-value')

@patch.object(vault_pki.hookenv, 'leader_get')
def test_certcache__fetch_none(self, mock_leader_get):
request = self._default_request()
# due to weird asymetry between leader_get and leader_set, if no
# attribute is passed to leader_get() then the result is the
# deserialised set of key, values as a dictionary.
leader_get = {
'a': 'an-a',
'b': 'an-b',
}

mock_leader_get.return_value = leader_get
self.assertEqual(vault_pki.CertCache(request)._fetch(None), leader_get)
mock_leader_get.assert_called_once_with(None)

@patch.object(vault_pki.hookenv, 'leader_set')
def test_certcache__store(self, mock_leader_set):
request = self._default_request()
Expand Down

0 comments on commit 9c32533

Please sign in to comment.