Skip to content

Commit

Permalink
feat(parser): add support for API Gateway Lambda authorizer events (#…
Browse files Browse the repository at this point in the history
…4718)

* Adding Authorizer parser

* Make pydantic v2 happy
  • Loading branch information
leandrodamascena authored Jul 9, 2024
1 parent 7b98667 commit 955e385
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 5 deletions.
6 changes: 6 additions & 0 deletions aws_lambda_powertools/utilities/parser/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

from .alb import AlbModel, AlbRequestContext, AlbRequestContextData
from .apigw import (
ApiGatewayAuthorizerRequest,
ApiGatewayAuthorizerToken,
APIGatewayEventAuthorizer,
APIGatewayEventIdentity,
APIGatewayEventRequestContext,
APIGatewayProxyEventModel,
)
from .apigwv2 import (
ApiGatewayAuthorizerRequestV2,
APIGatewayProxyEventV2Model,
RequestContextV2,
RequestContextV2Authorizer,
Expand Down Expand Up @@ -105,6 +108,7 @@

__all__ = [
"APIGatewayProxyEventV2Model",
"ApiGatewayAuthorizerRequestV2",
"RequestContextV2",
"RequestContextV2Http",
"RequestContextV2Authorizer",
Expand Down Expand Up @@ -165,6 +169,8 @@
"APIGatewayEventRequestContext",
"APIGatewayEventAuthorizer",
"APIGatewayEventIdentity",
"ApiGatewayAuthorizerRequest",
"ApiGatewayAuthorizerToken",
"KafkaSelfManagedEventModel",
"KafkaRecordModel",
"KafkaMskEventModel",
Expand Down
13 changes: 12 additions & 1 deletion aws_lambda_powertools/utilities/parser/models/apigw.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,16 @@ class APIGatewayProxyEventModel(BaseModel):
requestContext: APIGatewayEventRequestContext
pathParameters: Optional[Dict[str, str]] = None
stageVariables: Optional[Dict[str, str]] = None
isBase64Encoded: bool
isBase64Encoded: Optional[bool] = None
body: Optional[Union[str, Type[BaseModel]]] = None


class ApiGatewayAuthorizerToken(BaseModel):
type: Literal["TOKEN"]
methodArn: str
authorizationToken: str


class ApiGatewayAuthorizerRequest(APIGatewayProxyEventModel):
type: Literal["REQUEST"]
methodArn: str
8 changes: 7 additions & 1 deletion aws_lambda_powertools/utilities/parser/models/apigwv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,10 @@ class APIGatewayProxyEventV2Model(BaseModel):
stageVariables: Optional[Dict[str, str]] = None
requestContext: RequestContextV2
body: Optional[Union[str, Type[BaseModel]]] = None
isBase64Encoded: bool
isBase64Encoded: Optional[bool] = None


class ApiGatewayAuthorizerRequestV2(APIGatewayProxyEventV2Model):
type: Literal["REQUEST"]
routeArn: str
identitySource: List[str]
3 changes: 3 additions & 0 deletions docs/utilities/parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ Parser comes with the following built-in models:
| ------------------------------------------- | ------------------------------------------------------------------------------------- |
| **AlbModel** | Lambda Event Source payload for Amazon Application Load Balancer |
| **APIGatewayProxyEventModel** | Lambda Event Source payload for Amazon API Gateway |
| **ApiGatewayAuthorizerToken** | Lambda Event Source payload for Amazon API Gateway Lambda Authorizer with Token |
| **ApiGatewayAuthorizerRequest** | Lambda Event Source payload for Amazon API Gateway Lambda Authorizer with Request |
| **APIGatewayProxyEventV2Model** | Lambda Event Source payload for Amazon API Gateway v2 payload |
| **ApiGatewayAuthorizerRequestV2** | Lambda Event Source payload for Amazon API Gateway v2 Lambda Authorizer |
| **BedrockAgentEventModel** | Lambda Event Source payload for Bedrock Agents |
| **CloudFormationCustomResourceCreateModel** | Lambda Event Source payload for AWS CloudFormation `CREATE` operation |
| **CloudFormationCustomResourceUpdateModel** | Lambda Event Source payload for AWS CloudFormation `UPDATE` operation |
Expand Down
14 changes: 13 additions & 1 deletion tests/events/apiGatewayAuthorizerRequestEvent.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
"CloudFront-Is-Mobile-Viewer": "false",
"User-Agent": "..."
},
"multiValueHeaders": {
"Header1": [
"value1"
],
"Origin": [
"https://aws.amazon.com"
],
"Header2": [
"value1",
"value2"
]
},
"queryStringParameters": {
"QueryString1": "queryValue1"
},
Expand All @@ -42,7 +54,7 @@
"cognitoIdentityPoolId": null,
"principalOrgId": null,
"apiKey": "...",
"sourceIp": "...",
"sourceIp": "test-invoke-source-ip",
"user": null,
"userAgent": "PostmanRuntime/7.28.3",
"userArn": null,
Expand Down
2 changes: 1 addition & 1 deletion tests/events/apiGatewayAuthorizerV2Event.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"method": "POST",
"path": "/merchants",
"protocol": "HTTP/1.1",
"sourceIp": "IP",
"sourceIp": "10.10.10.10",
"userAgent": "agent"
},
"requestId": "id",
Expand Down
23 changes: 22 additions & 1 deletion tests/unit/parser/_pydantic/test_apigw.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
from pydantic import ValidationError

from aws_lambda_powertools.utilities.parser import envelopes, parse
from aws_lambda_powertools.utilities.parser.models import APIGatewayProxyEventModel
from aws_lambda_powertools.utilities.parser.models import (
ApiGatewayAuthorizerRequest,
ApiGatewayAuthorizerToken,
APIGatewayProxyEventModel,
)
from tests.functional.utils import load_event
from tests.unit.parser._pydantic.schemas import MyApiGatewayBusiness

Expand Down Expand Up @@ -148,3 +152,20 @@ def test_apigw_event_empty_body():
event = load_event("apiGatewayProxyEvent.json")
event["body"] = None
parse(event=event, model=APIGatewayProxyEventModel)


def test_apigw_event_authorizer_token():
raw_event = load_event("apiGatewayAuthorizerTokenEvent.json")
parsed_event: ApiGatewayAuthorizerToken = ApiGatewayAuthorizerToken(**raw_event)

assert parsed_event.type == raw_event["type"]
assert parsed_event.methodArn == raw_event["methodArn"]
assert parsed_event.authorizationToken == raw_event["authorizationToken"]


def test_apigw_event_authorizer_event():
raw_event = load_event("apiGatewayAuthorizerRequestEvent.json")
parsed_event: ApiGatewayAuthorizerRequest = ApiGatewayAuthorizerRequest(**raw_event)

assert parsed_event.type == raw_event["type"]
assert parsed_event.methodArn == raw_event["methodArn"]
10 changes: 10 additions & 0 deletions tests/unit/parser/_pydantic/test_apigwv2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from aws_lambda_powertools.utilities.parser import envelopes, parse
from aws_lambda_powertools.utilities.parser.models import (
ApiGatewayAuthorizerRequestV2,
APIGatewayProxyEventV2Model,
RequestContextV2,
RequestContextV2Authorizer,
Expand Down Expand Up @@ -120,3 +121,12 @@ def test_apigw_event_empty_query_strings():
raw_event["rawQueryString"] = ""
raw_event.pop("queryStringParameters") # API GW v2 removes certain keys when no data is passed
parse(event=raw_event, model=APIGatewayProxyEventV2Model)


def test_apigw_v2_request_authorizer():
raw_event = load_event("apiGatewayAuthorizerV2Event.json")
parsed_event: ApiGatewayAuthorizerRequestV2 = ApiGatewayAuthorizerRequestV2(**raw_event)

assert parsed_event.type == raw_event["type"]
assert parsed_event.identitySource == raw_event["identitySource"]
assert parsed_event.routeArn == raw_event["routeArn"]

0 comments on commit 955e385

Please sign in to comment.