Skip to content

fix: decorators not refreshing cached secrets on subsequent calls (#27) #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2025
Merged
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
35 changes: 20 additions & 15 deletions src/aws_secretsmanager_caching/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""Decorators for use with caching library """
"""Decorators for use with caching library"""

import json


Expand Down Expand Up @@ -40,12 +41,12 @@ def __call__(self, func):
:return The function with the injected argument.
"""

secret = self.cache.get_secret_string(secret_id=self.secret_id)

def _wrapped_func(*args, **kwargs):
"""
Internal function to execute wrapped function
"""
secret = self.cache.get_secret_string(secret_id=self.secret_id)

return func(secret, *args, **kwargs)

return _wrapped_func
Expand Down Expand Up @@ -82,22 +83,26 @@ def __call__(self, func):
:return The original function with injected keyword arguments
"""

try:
secret = json.loads(self.cache.get_secret_string(secret_id=self.secret_id))
except json.decoder.JSONDecodeError:
raise RuntimeError('Cached secret is not valid JSON') from None

resolved_kwargs = {}
for orig_kwarg, secret_key in self.kwarg_map.items():
try:
resolved_kwargs[orig_kwarg] = secret[secret_key]
except KeyError:
raise RuntimeError(f'Cached secret does not contain key {secret_key}') from None

def _wrapped_func(*args, **kwargs):
"""
Internal function to execute wrapped function
"""
try:
secret = json.loads(
self.cache.get_secret_string(secret_id=self.secret_id)
)
except json.decoder.JSONDecodeError:
raise RuntimeError("Cached secret is not valid JSON") from None

resolved_kwargs = {}
for orig_kwarg, secret_key in self.kwarg_map.items():
try:
resolved_kwargs[orig_kwarg] = secret[secret_key]
except KeyError:
raise RuntimeError(
f"Cached secret does not contain key {secret_key}"
) from None

return func(*args, **resolved_kwargs, **kwargs)

return _wrapped_func