-
Notifications
You must be signed in to change notification settings - Fork 429
feat(parser): Add S3 Object Lambda Event #362
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
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
57 changes: 57 additions & 0 deletions
57
aws_lambda_powertools/utilities/parser/models/s3_object_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,57 @@ | ||
from typing import Dict, Optional | ||
|
||
from pydantic import BaseModel, HttpUrl | ||
|
||
|
||
class S3ObjectContext(BaseModel): | ||
inputS3Url: HttpUrl | ||
outputRoute: str | ||
outputToken: str | ||
|
||
|
||
class S3ObjectConfiguration(BaseModel): | ||
accessPointArn: str | ||
supportingAccessPointArn: str | ||
payload: str | ||
|
||
|
||
class S3ObjectUserRequest(BaseModel): | ||
url: str | ||
headers: Dict[str, str] | ||
|
||
|
||
class S3ObjectSessionIssuer(BaseModel): | ||
type: str # noqa: A003, VNE003 | ||
userName: Optional[str] | ||
principalId: str | ||
arn: str | ||
accountId: str | ||
|
||
|
||
class S3ObjectSessionAttributes(BaseModel): | ||
creationDate: str | ||
mfaAuthenticated: bool | ||
|
||
|
||
class S3ObjectSessionContext(BaseModel): | ||
sessionIssuer: S3ObjectSessionIssuer | ||
attributes: S3ObjectSessionAttributes | ||
|
||
|
||
class S3ObjectUserIdentity(BaseModel): | ||
type: str # noqa003 | ||
accountId: str | ||
accessKeyId: str | ||
userName: Optional[str] | ||
principalId: str | ||
arn: str | ||
sessionContext: Optional[S3ObjectSessionContext] | ||
|
||
|
||
class S3ObjectLambdaEvent(BaseModel): | ||
xAmzRequestId: str | ||
getObjectContext: S3ObjectContext | ||
configuration: S3ObjectConfiguration | ||
userRequest: S3ObjectUserRequest | ||
userIdentity: S3ObjectUserIdentity | ||
protocolVersion: str |
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,67 @@ | ||
from aws_lambda_powertools.utilities.parser import event_parser | ||
from aws_lambda_powertools.utilities.parser.models import S3ObjectLambdaEvent | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
from tests.functional.parser.utils import load_event | ||
|
||
|
||
@event_parser(model=S3ObjectLambdaEvent) | ||
def handle_s3_object_event_iam(event: S3ObjectLambdaEvent, _: LambdaContext): | ||
return event | ||
|
||
|
||
def test_s3_object_event(): | ||
event = load_event("s3ObjectEventIAMUser.json") | ||
parsed_event: S3ObjectLambdaEvent = handle_s3_object_event_iam(event, LambdaContext()) | ||
assert parsed_event.xAmzRequestId == event["xAmzRequestId"] | ||
assert parsed_event.getObjectContext is not None | ||
object_context = parsed_event.getObjectContext | ||
assert str(object_context.inputS3Url) == event["getObjectContext"]["inputS3Url"] | ||
assert object_context.outputRoute == event["getObjectContext"]["outputRoute"] | ||
assert object_context.outputToken == event["getObjectContext"]["outputToken"] | ||
assert parsed_event.configuration is not None | ||
configuration = parsed_event.configuration | ||
assert configuration.accessPointArn == event["configuration"]["accessPointArn"] | ||
assert configuration.supportingAccessPointArn == event["configuration"]["supportingAccessPointArn"] | ||
assert configuration.payload == event["configuration"]["payload"] | ||
assert parsed_event.userRequest is not None | ||
user_request = parsed_event.userRequest | ||
assert user_request.url == event["userRequest"]["url"] | ||
assert user_request.headers == event["userRequest"]["headers"] | ||
assert user_request.headers["Accept-Encoding"] == "identity" | ||
assert parsed_event.userIdentity is not None | ||
user_identity = parsed_event.userIdentity | ||
assert user_identity.type == event["userIdentity"]["type"] | ||
assert user_identity.principalId == event["userIdentity"]["principalId"] | ||
assert user_identity.arn == event["userIdentity"]["arn"] | ||
assert user_identity.accountId == event["userIdentity"]["accountId"] | ||
assert user_identity.accessKeyId == event["userIdentity"]["accessKeyId"] | ||
assert user_identity.userName == event["userIdentity"]["userName"] | ||
assert user_identity.sessionContext is None | ||
assert parsed_event.protocolVersion == event["protocolVersion"] | ||
|
||
|
||
@event_parser(model=S3ObjectLambdaEvent) | ||
def handle_s3_object_event_temp_creds(event: S3ObjectLambdaEvent, _: LambdaContext): | ||
return event | ||
|
||
|
||
def test_s3_object_event_temp_credentials(): | ||
event = load_event("s3ObjectEventTempCredentials.json") | ||
parsed_event: S3ObjectLambdaEvent = handle_s3_object_event_temp_creds(event, LambdaContext()) | ||
assert parsed_event.xAmzRequestId == event["xAmzRequestId"] | ||
session_context = parsed_event.userIdentity.sessionContext | ||
assert session_context is not None | ||
session_issuer = session_context.sessionIssuer | ||
assert session_issuer is not None | ||
assert session_issuer.type == event["userIdentity"]["sessionContext"]["sessionIssuer"]["type"] | ||
assert session_issuer.userName == event["userIdentity"]["sessionContext"]["sessionIssuer"]["userName"] | ||
assert session_issuer.principalId == event["userIdentity"]["sessionContext"]["sessionIssuer"]["principalId"] | ||
assert session_issuer.arn == event["userIdentity"]["sessionContext"]["sessionIssuer"]["arn"] | ||
assert session_issuer.accountId == event["userIdentity"]["sessionContext"]["sessionIssuer"]["accountId"] | ||
session_attributes = session_context.attributes | ||
assert session_attributes is not None | ||
assert ( | ||
str(session_attributes.mfaAuthenticated).lower() | ||
== event["userIdentity"]["sessionContext"]["attributes"]["mfaAuthenticated"] | ||
) | ||
assert session_attributes.creationDate == event["userIdentity"]["sessionContext"]["attributes"]["creationDate"] |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thx! pushed the code fix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@heitorlessa BTW, more key differences between parser and validator are: