Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(parameters): Configure max_age and decrypt parameters via environment variables #2088

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 7 commits into from
Apr 14, 2023
Merged

feat(parameters): Configure max_age and decrypt parameters via environment variables #2088

merged 7 commits into from
Apr 14, 2023

Conversation

leandrodamascena
Copy link
Contributor

Issue number: #2041

Summary

Changes

In this PR we changed the utility parameter code to accept environment variables to configure the default TTL and the decrypt parameter.

User experience

Before this PR the customer had to inform the max_age parameter every time they wanted a standard TTL different than 5 seconds (default value). If the user had retrieved dozens/hundreds of parameters, this made the developer's experience bad. This PR creates an environment variable called POWERTOOLS_PARAMETERS_MAX_AGE that accepts an integer value to be used as the default TTL.

This PR also introduces a new environment variable called POWERTOOLS_PARAMETERS_SSM_DECRYPT. This environment variable can be used to set a default value for the decrypt parameter in the SSM Provider. This is extremely useful in cases where the customer needs to recover dozens/hundreds of SecureString type parameters and you need to decrypt them. If a value for this environment variable is not informed, the default value False remains.

This PR aims to make the developer experience simpler. Below is a code snippet of how a Lambda template can be configured to use these environment variables.

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: HelloWorldFunction
      Handler: app.lambda_handler
      Runtime: python3.9
      Environment:
        Variables:
          POWERTOOLS_PARAMETERS_MAX_AGE: 10
          POWERTOOLS_PARAMETERS_SSM_DECRYPT: true
      Architectures:
      - x86_64
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /hello
            Method: get

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.

@leandrodamascena leandrodamascena requested a review from a team as a code owner April 5, 2023 22:06
@leandrodamascena leandrodamascena requested review from heitorlessa and removed request for a team April 5, 2023 22:06
@boring-cyborg boring-cyborg bot added commons documentation Improvements or additions to documentation tests labels Apr 5, 2023
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Apr 5, 2023
@github-actions github-actions bot added the feature New feature or functionality label Apr 5, 2023
@@ -547,6 +547,44 @@ def test_ssm_provider_get_with_custom_client(mock_name, mock_value, mock_version
stubber.deactivate()


def test_ssm_provider_get_with_decrypt_environment_variable(monkeypatch, mock_name, mock_value, mock_version, config):
Copy link
Contributor

@heitorlessa heitorlessa Apr 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing test for max_age env var?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hei @heitorlessa, yes! It was missing and refactored the code adding a specific function to resolve the max_age and testing it. I tried to use the function resolve_env_var_choice but looks like that mypy doesn't like overload with very similar types like float and int.

@leandrodamascena
Copy link
Contributor Author

@rubenfonseca can you review this PR, please?!

Copy link
Contributor

@rubenfonseca rubenfonseca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! It's a little bit tedious because the same logic is present in multiple places. I left some suggestions for improvement.

@leandrodamascena
Copy link
Contributor Author

Great job! It's a little bit tedious because the same logic is present in multiple places. I left some suggestions for improvement.

Thank you for this super review, Ruben! I really liked it!
I have the same feeling: it's a bit distracting to use the same logic, but it seems like there's no other option (other than refactoring everything) to check if the customer is using the max_age/decrypt parameters when getting a specific parameter from a specific provider.

I addressed all feedback and let me know if I need to change something else.

@codecov-commenter
Copy link

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (3e313bb) 97.47% compared to head (639c24f) 97.48%.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #2088   +/-   ##
========================================
  Coverage    97.47%   97.48%           
========================================
  Files          147      147           
  Lines         6809     6835   +26     
  Branches       483      483           
========================================
+ Hits          6637     6663   +26     
  Misses         136      136           
  Partials        36       36           
Impacted Files Coverage Δ
aws_lambda_powertools/shared/constants.py 100.00% <100.00%> (ø)
aws_lambda_powertools/shared/functions.py 96.55% <100.00%> (+0.12%) ⬆️
...ambda_powertools/utilities/parameters/appconfig.py 94.59% <100.00%> (+0.15%) ⬆️
aws_lambda_powertools/utilities/parameters/base.py 99.11% <100.00%> (+0.04%) ⬆️
..._lambda_powertools/utilities/parameters/secrets.py 100.00% <100.00%> (ø)
aws_lambda_powertools/utilities/parameters/ssm.py 97.59% <100.00%> (+0.18%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Contributor

@rubenfonseca rubenfonseca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small thing...

Comment on lines +709 to +710
| **POWERTOOLS_PARAMETERS_MAX_AGE** | Adjust how long values are kept in cache (in seconds) | [Parameters](./utilities/parameters/#adjusting-cache-ttl) | `5` |
| **POWERTOOLS_PARAMETERS_SSM_DECRYPT** | Sets whether to decrypt or not values retrieved from AWS SSM Parameters Store | [Parameters](./utilities/parameters/#ssmprovider) | `false` |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the environment variable names need to be updated too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No no Ruben, we don't need to. In the documentation is the name of the environment variable that will be configured in Lambda, what we changed was the constant in the constant.py file.
We put the constants to make easy the replication of the same value throughout the project's code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need more coffee

@rubenfonseca rubenfonseca self-requested a review April 14, 2023 07:29
Copy link
Contributor

@rubenfonseca rubenfonseca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@leandrodamascena leandrodamascena merged commit ed73aef into aws-powertools:develop Apr 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
commons documentation Improvements or additions to documentation feature New feature or functionality size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: ability to set maxAge and decrypt via environment variables for Parameters
4 participants