Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/aws_secretsmanager_caching/cache/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
from .lru import LRUCache


# A hook may use None as its stored representation of a cached result.
_NOT_CACHED = object()


class SecretCacheObject: # pylint: disable=too-many-instance-attributes
"""Secret cache object that handles the common refresh logic."""
# Jitter max for refresh now
Expand All @@ -47,7 +51,7 @@ def __init__(self, config, client, secret_id):
self._config = config
self._client = client
self._secret_id = secret_id
self._result = None
self._result = _NOT_CACHED
self._exception = None
self._exception_count = 0
self._refresh_needed = True
Expand Down Expand Up @@ -158,6 +162,8 @@ def refresh_secret_now(self):

def _get_result(self):
"""Get the stored result using a hook if present"""
if self._result is _NOT_CACHED:
return None
if self._config.secret_cache_hook is None:
return self._result

Expand Down
46 changes: 46 additions & 0 deletions test/unit/test_cache_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
Unit test suite for items module
"""
import unittest
from unittest.mock import Mock

import botocore
from aws_secretsmanager_caching.cache.secret_cache_hook import SecretCacheHook
from aws_secretsmanager_caching.config import SecretCacheConfig
from aws_secretsmanager_caching.secret_cache import SecretCache
from botocore.stub import Stubber
from aws_secretsmanager_caching.cache.items import SecretCacheVersion


class DummySecretCacheHook(SecretCacheHook):
Expand Down Expand Up @@ -52,6 +54,50 @@ def get(self, cached_obj):

class TestSecretCacheHook(unittest.TestCase):

def test_initial_service_error_is_not_masked_by_hook(self):
version_id = '01234567890123456789012345678901'
for operation in ('describe_secret', 'get_secret_value'):
with self.subTest(operation=operation):
client = botocore.session.get_session().create_client(
'secretsmanager', region_name='us-west-2',
aws_access_key_id='testing', aws_secret_access_key='testing')
hook = DummySecretCacheHook()
config = SecretCacheConfig(secret_cache_hook=hook,
exception_retry_delay_base=3600)
cache = SecretCache(config=config, client=client)
with Stubber(client) as stubber:
params = {'SecretId': 'test'}
if operation == 'get_secret_value':
stubber.add_response('describe_secret', {
'VersionIdsToStages': {version_id: ['AWSCURRENT']}
}, params)
params = dict(params, VersionId=version_id)
stubber.add_client_error(
operation, service_error_code='ResourceNotFoundException',
service_message='Secret does not exist',
expected_params=params)
for _ in range(2):
with self.assertRaises(client.exceptions.ResourceNotFoundException):
cache.get_secret_string('test')
stubber.assert_no_pending_responses()

def test_failed_refresh_keeps_hook_cached_value(self):
for token in (None, 0):
with self.subTest(token=token):
value = {'SecretString': 'cached value'}
hook = Mock(spec=SecretCacheHook)
hook.put.return_value = token
hook.get.return_value = value
client = Mock()
client.get_secret_value.side_effect = [value, RuntimeError('unavailable')]
version = SecretCacheVersion(
SecretCacheConfig(secret_cache_hook=hook), client, 'test', 'version')
self.assertEqual(version.get_secret_value(), value)
version._refresh_needed = True
self.assertEqual(version.get_secret_value(), value)
hook.get.assert_called_with(token)
self.assertEqual(hook.get.call_count, 2)

def setUp(self):
pass

Expand Down