Skip to content

Commit dbd2837

Browse files
authored
Add error handling for OverrideApiAuth property without an authorizer (#3350)
1 parent 47c1f6a commit dbd2837

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

samtranslator/model/eventsources/push.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,11 @@ def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def]
755755
# We make the call to add_auth_to_swagger() in two separate places because _add_swagger_integration() deals
756756
# specifically with cases where DefinitionBody is not defined, and below for when DefinitionBody is defined.
757757
if swagger_body and self.Auth and self.Auth.get("OverrideApiAuth"):
758-
# TODO: refactor to remove this cast
758+
if not (self.Auth.get("Authorizer") or self.Auth.get("ApiKeyRequired") or self.Auth.get("ResourcePolicy")):
759+
raise InvalidEventException(
760+
self.relative_id,
761+
"Must define one of: Authorizer, ApiKeyRequired or ResourcePolicy when using the OverrideApiAuth property.",
762+
)
759763
stage = cast(str, self.Stage)
760764
editor = SwaggerEditor(swagger_body)
761765
self.add_auth_to_swagger(
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
Resources:
2+
MyApiWithLambdaRequestAuth:
3+
Type: AWS::Serverless::Api
4+
Properties:
5+
StageName: Prod
6+
DefinitionBody:
7+
swagger: 2.0
8+
info:
9+
version: '1.0'
10+
title: !Ref AWS::StackName
11+
schemes:
12+
- https
13+
paths:
14+
/lambda-request:
15+
get:
16+
x-amazon-apigateway-integration:
17+
httpMethod: POST
18+
type: aws_proxy
19+
uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyFn.Arn}/invocations
20+
passthroughBehavior: when_no_match
21+
responses: {}
22+
Auth:
23+
Authorizers:
24+
MyLambdaRequestAuth:
25+
FunctionPayloadType: REQUEST
26+
FunctionArn: !GetAtt MyAuthFn.Arn
27+
Identity:
28+
Headers:
29+
- Authorization1
30+
DefaultAuthorizer: MyLambdaRequestAuth
31+
32+
MyAuthFn:
33+
Type: AWS::Serverless::Function
34+
Properties:
35+
InlineCode: |
36+
exports.handler = async (event) => {
37+
return {
38+
statusCode: 200,
39+
body: JSON.stringify(event),
40+
headers: {}
41+
}
42+
}
43+
Handler: index.handler
44+
Runtime: nodejs8.10
45+
46+
MyFn:
47+
Type: AWS::Serverless::Function
48+
Properties:
49+
InlineCode: |
50+
exports.handler = async (event) => {
51+
return {
52+
statusCode: 200,
53+
body: JSON.stringify(event),
54+
headers: {}
55+
}
56+
}
57+
Handler: index.handler
58+
Runtime: nodejs8.10
59+
Events:
60+
LambdaRequest:
61+
Type: Api
62+
Properties:
63+
RestApiId: !Ref MyApiWithLambdaRequestAuth
64+
Auth:
65+
OverrideApiAuth: true
66+
Method: get
67+
Path: /lambda-request
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"_autoGeneratedBreakdownErrorMessage": [
3+
"Invalid Serverless Application Specification document. ",
4+
"Number of errors found: 1. ",
5+
"Resource with id [MyFn] is invalid. ",
6+
"Event with id [LambdaRequest] is invalid. ",
7+
"Must define one of: Authorizer, ApiKeyRequired or ResourcePolicy when using the OverrideApiAuth property."
8+
],
9+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [MyFn] is invalid. Event with id [LambdaRequest] is invalid. Must define one of: Authorizer, ApiKeyRequired or ResourcePolicy when using the OverrideApiAuth property.",
10+
"errors": [
11+
{
12+
"errorMessage": "Resource with id [MyFn] is invalid. Event with id [LambdaRequest] is invalid. Must define one of: Authorizer, ApiKeyRequired or ResourcePolicy when using the OverrideApiAuth property."
13+
}
14+
]
15+
}

0 commit comments

Comments
 (0)