Skip to content
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

fix(parameters): distinct cache key for single vs path with same name #2839

Merged

Conversation

heitorlessa
Copy link
Contributor

@heitorlessa heitorlessa commented Jul 25, 2023

Issue number: #2438

Summary

This PR addresses an edge case that a customer tries to get multiple parameters by path followed by getting a single
parameter using the path name, where GetParameterError isn't raised in the latter due to cache.

Previously reproduced as:

from aws_lambda_powertools.utilities.parameters import get_parameters, get_parameter

get_parameters("/dev")  # get all parameters under path
get_parameter("/dev")    # mistakenly tries to get /dev but no error raised; it gets cache results instead

Changes

Please provide a summary of what's being changed

  • Update code to use newer self.add_to_cache()
  • Add self.fetch_from_cache to isolate getter
  • Centralize cache key naming in new method: _build_cache_key
  • Add defaults to build_cache_key - transform, recursive
  • Update cache test to use new _build_cache_key
  • Think of a better name to differentiate cache key name
    • Context futureproofs it, but impacts readability
    • is_recursive, is_multiple

User experience

Please share what the user experience looks like before and after this change

Exception raised as it should

Traceback (most recent call last):
  File "DEV/aws-lambda-powertools-python/aws_lambda_powertools/utilities/parameters/base.py", line 135, in get
    value = self._get(name, **sdk_options)
  File "DEV/aws-lambda-powertools-python/aws_lambda_powertools/utilities/parameters/ssm.py", line 189, in _get
    return self.client.get_parameter(**sdk_options)["Parameter"]["Value"]
  File "DEV/aws-lambda-powertools-python/.venv/lib/python3.9/site-packages/botocore/client.py", line 534, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "DEV/aws-lambda-powertools-python/.venv/lib/python3.9/site-packages/botocore/client.py", line 976, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.errorfactory.ParameterNotFound: An error occurred (ParameterNotFound) when calling the GetParameter operation: 

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "DEV/aws-lambda-powertools-python/bla.py", line 4, in <module>
    get_parameter("/dev", max_age=100)
  File "DEV/aws-lambda-powertools-python/aws_lambda_powertools/utilities/parameters/ssm.py", line 607, in get_parameter
    return DEFAULT_PROVIDERS["ssm"].get(
  File "DEV/aws-lambda-powertools-python/aws_lambda_powertools/utilities/parameters/ssm.py", line 169, in get
    return super().get(name, max_age, transform, force_fetch, **sdk_options)
  File "DEV/aws-lambda-powertools-python/aws_lambda_powertools/utilities/parameters/base.py", line 138, in get
    raise GetParameterError(str(exc))
aws_lambda_powertools.utilities.parameters.exceptions.GetParameterError: An error occurred (ParameterNotFound) when calling the GetParameter operation:

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@heitorlessa heitorlessa requested a review from a team as a code owner July 25, 2023 13:24
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jul 25, 2023
@boring-cyborg boring-cyborg bot added the tests label Jul 25, 2023
@github-actions github-actions bot added the bug Something isn't working label Jul 25, 2023
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
4.6% 4.6% Duplication

@heitorlessa heitorlessa added the parameters Parameters utility label Jul 25, 2023
@heitorlessa heitorlessa merged commit 64c60a0 into aws-powertools:develop Jul 25, 2023
@heitorlessa heitorlessa deleted the fix/parameters-nested-path branch July 25, 2023 14:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working parameters Parameters utility size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: parameters confusing is combining get_parameter and get_parameters results
2 participants