-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(event_sources): add Secrets Manager secret rotation event (#3061)
Co-authored-by: Leandro Damascena <[email protected]>
- Loading branch information
1 parent
0f3cebf
commit ea30084
Showing
7 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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"] |