-
Notifications
You must be signed in to change notification settings - Fork 433
feat(event_sources): add Secrets Manager secret rotation event #3061
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
leandrodamascena
merged 9 commits into
aws-powertools:develop
from
roger-zhangg:secret-event
Sep 12, 2023
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0622127
add secrets_event
roger-zhangg 0c790c0
add example
roger-zhangg 790f6ba
Merge branch 'develop' into secret-event
leandrodamascena abf6a93
address typo, example fixes
roger-zhangg 07a6ccf
Using parameter utility in the example
leandrodamascena 3feaeeb
Merge branch 'develop' into secret-event
leandrodamascena b8d98de
Merge branch 'develop' into secret-event
leandrodamascena b69f098
Merge branch 'develop' into secret-event
leandrodamascena b7fd980
Addressing Heitor feedback
leandrodamascena File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
aws_lambda_powertools/utilities/data_classes/secrets_manager_event.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from typing_extensions import Literal | ||
|
||
from aws_lambda_powertools.utilities.data_classes.common import DictWrapper | ||
|
||
|
||
class SecretsManagerEvent(DictWrapper): | ||
@property | ||
def secret_id(self) -> str: | ||
"""SecretId: The secret ARN or identifier""" | ||
return self["SecretId"] | ||
|
||
@property | ||
def client_request_token(self) -> str: | ||
"""ClientRequestToken: The ClientRequestToken associated with the secret version""" | ||
return self["ClientRequestToken"] | ||
|
||
@property | ||
def version_id(self) -> str: | ||
"""Alias to ClientRequestToken to get token associated to version""" | ||
return self["ClientRequestToken"] | ||
|
||
@property | ||
def step(self) -> Literal["createSecret", "setSecret", "testSecret", "finishSecret"]: | ||
"""Step: The rotation step (one of createSecret, setSecret, testSecret, or finishSecret)""" | ||
return self["Step"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from aws_lambda_powertools.utilities import parameters | ||
from aws_lambda_powertools.utilities.data_classes import SecretsManagerEvent, event_source | ||
|
||
secrets_provider = parameters.SecretsProvider() | ||
|
||
|
||
@event_source(data_class=SecretsManagerEvent) | ||
def lambda_handler(event: SecretsManagerEvent, context): | ||
# Getting secret value using Parameter utility | ||
# See https://docs.powertools.aws.dev/lambda/python/latest/utilities/parameters/ | ||
secret = secrets_provider.get(event.secret_id, VersionId=event.version_id, VersionStage="AWSCURRENT") | ||
|
||
# You need to work with secrets afterwards | ||
# Check more examples: https://github.com/aws-samples/aws-secrets-manager-rotation-lambdas | ||
|
||
return secret |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"SecretId":"arn:aws:secretsmanager:us-west-2:123456789012:secret:MyTestDatabaseSecret-a1b2c3", | ||
"ClientRequestToken":"550e8400-e29b-41d4-a716-446655440000", | ||
"Step":"createSecret" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"SecretId":"arn:aws:secretsmanager:us-west-2:123456789012:secret:MyTestDatabaseSecret-a1b2c3", | ||
"ClientRequestToken":"550e8400-e29b-41d4-a716-446655440000", | ||
"Step":"createSecret" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from aws_lambda_powertools.utilities.data_classes.secrets_manager_event import SecretsManagerEvent | ||
from tests.functional.utils import load_event | ||
|
||
|
||
def test_secrets_manager_event(): | ||
raw_event = load_event("secretsManagerEvent.json") | ||
parsed_event = SecretsManagerEvent(raw_event) | ||
|
||
assert parsed_event.secret_id == raw_event["SecretId"] | ||
assert parsed_event.client_request_token == raw_event["ClientRequestToken"] | ||
assert parsed_event.version_id == raw_event["ClientRequestToken"] | ||
assert parsed_event.step == raw_event["Step"] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.