Skip to content

Commit 5f50865

Browse files
committed
make the Lambda Authorizer is optional if the authorization caching is not enabled (reference https://docs.aws.amazon.com/apigateway/api-reference/resource/authorizer/#identitySource)
1 parent d5814ba commit 5f50865

File tree

6 files changed

+428
-12
lines changed

6 files changed

+428
-12
lines changed

samtranslator/model/apigateway.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,11 @@ def __init__(
240240
)
241241

242242
if function_payload_type == "REQUEST" and self._is_missing_identity_source(identity):
243-
identity = {}
243+
raise InvalidResourceException(
244+
api_logical_id,
245+
name + " Authorizer must specify Identity with at least one "
246+
"of Headers, QueryStrings, StageVariables, or Context.",
247+
)
244248

245249
if authorization_scopes is not None and not isinstance(authorization_scopes, list):
246250
raise InvalidResourceException(api_logical_id, "AuthorizationScopes must be a list.")
@@ -263,8 +267,9 @@ def _is_missing_identity_source(self, identity):
263267
query_strings = identity.get("QueryStrings")
264268
stage_variables = identity.get("StageVariables")
265269
context = identity.get("Context")
270+
ttl = identity.get("ReauthorizeEvery")
266271

267-
if not headers and not query_strings and not stage_variables and not context:
272+
if (ttl is None or int(ttl) > 0) and not headers and not query_strings and not stage_variables and not context:
268273
return True
269274

270275
return False
@@ -307,7 +312,9 @@ def generate_swagger(self):
307312
swagger[APIGATEWAY_AUTHORIZER_KEY]["authorizerCredentials"] = function_invoke_role
308313

309314
if self._get_function_payload_type() == "REQUEST":
310-
swagger[APIGATEWAY_AUTHORIZER_KEY]["identitySource"] = self._get_identity_source()
315+
identity_source = self._get_identity_source()
316+
if identity_source:
317+
swagger[APIGATEWAY_AUTHORIZER_KEY]["identitySource"] = self._get_identity_source()
311318

312319
# Authorizer Validation Expression is only allowed on COGNITO_USER_POOLS and LAMBDA_TOKEN
313320
is_lambda_token_authorizer = authorizer_type == "LAMBDA" and self._get_function_payload_type() == "TOKEN"

tests/translator/input/api_with_auth_all_minimum.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ Resources:
3232
Identity:
3333
Headers:
3434
- Authorization1
35+
36+
MyApiWithNotCachedLambdaRequestAuth:
37+
Type: "AWS::Serverless::Api"
38+
Properties:
39+
StageName: Prod
40+
Auth:
41+
DefaultAuthorizer: MyLambdaRequestAuth
42+
Authorizers:
43+
MyLambdaRequestAuth:
44+
FunctionPayloadType: REQUEST
45+
FunctionArn: !GetAtt MyAuthFn.Arn
46+
Identity:
47+
ReauthorizeEvery: 0
48+
3549
MyAuthFn:
3650
Type: AWS::Serverless::Function
3751
Properties:
@@ -63,6 +77,12 @@ Resources:
6377
RestApiId: !Ref MyApiWithLambdaRequestAuth
6478
Method: get
6579
Path: /lambda-request
80+
LambdaNotCachedRequest:
81+
Type: Api
82+
Properties:
83+
RestApiId: !Ref MyApiWithNotCachedLambdaRequestAuth
84+
Method: get
85+
Path: /not-cached-lambda-request
6686
MyUserPool:
6787
Type: AWS::Cognito::UserPool
6888
Properties:

tests/translator/output/api_with_auth_all_minimum.json

Lines changed: 128 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,19 @@
6363
},
6464
"StageName": "Prod"
6565
}
66-
},
66+
},
67+
"MyApiWithNotCachedLambdaRequestAuthProdStage": {
68+
"Type": "AWS::ApiGateway::Stage",
69+
"Properties": {
70+
"DeploymentId": {
71+
"Ref": "MyApiWithNotCachedLambdaRequestAuthDeployment444f67cd7c"
72+
},
73+
"RestApiId": {
74+
"Ref": "MyApiWithNotCachedLambdaRequestAuth"
75+
},
76+
"StageName": "Prod"
77+
}
78+
},
6779
"MyApiWithLambdaTokenAuthMyLambdaTokenAuthAuthorizerPermission": {
6880
"Type": "AWS::Lambda::Permission",
6981
"Properties": {
@@ -205,7 +217,30 @@
205217
]
206218
}
207219
}
208-
},
220+
},
221+
"MyApiWithNotCachedLambdaRequestAuthMyLambdaRequestAuthAuthorizerPermission": {
222+
"Type": "AWS::Lambda::Permission",
223+
"Properties": {
224+
"Action": "lambda:InvokeFunction",
225+
"Principal": "apigateway.amazonaws.com",
226+
"FunctionName": {
227+
"Fn::GetAtt": [
228+
"MyAuthFn",
229+
"Arn"
230+
]
231+
},
232+
"SourceArn": {
233+
"Fn::Sub": [
234+
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/authorizers/*",
235+
{
236+
"__ApiId__": {
237+
"Ref": "MyApiWithNotCachedLambdaRequestAuth"
238+
}
239+
}
240+
]
241+
}
242+
}
243+
},
209244
"MyFnLambdaTokenPermissionProd": {
210245
"Type": "AWS::Lambda::Permission",
211246
"Properties": {
@@ -236,7 +271,17 @@
236271
"Description": "RestApi deployment id: 6e52add211cda52ae10a7cc0e0afcf4afc682f9f",
237272
"StageName": "Stage"
238273
}
239-
},
274+
},
275+
"MyApiWithNotCachedLambdaRequestAuthDeployment444f67cd7c": {
276+
"Type": "AWS::ApiGateway::Deployment",
277+
"Properties": {
278+
"RestApiId": {
279+
"Ref": "MyApiWithNotCachedLambdaRequestAuth"
280+
},
281+
"Description": "RestApi deployment id: 444f67cd7c6475a698a0101480ba99b498325e90",
282+
"StageName": "Stage"
283+
}
284+
},
240285
"MyFnLambdaRequestPermissionProd": {
241286
"Type": "AWS::Lambda::Permission",
242287
"Properties": {
@@ -257,7 +302,28 @@
257302
]
258303
}
259304
}
260-
},
305+
},
306+
"MyFnLambdaNotCachedRequestPermissionProd": {
307+
"Type": "AWS::Lambda::Permission",
308+
"Properties": {
309+
"Action": "lambda:InvokeFunction",
310+
"Principal": "apigateway.amazonaws.com",
311+
"FunctionName": {
312+
"Ref": "MyFn"
313+
},
314+
"SourceArn": {
315+
"Fn::Sub": [
316+
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/GET/not-cached-lambda-request",
317+
{
318+
"__Stage__": "*",
319+
"__ApiId__": {
320+
"Ref": "MyApiWithNotCachedLambdaRequestAuth"
321+
}
322+
}
323+
]
324+
}
325+
}
326+
},
261327
"MyApiWithLambdaTokenAuth": {
262328
"Type": "AWS::ApiGateway::RestApi",
263329
"Properties": {
@@ -468,6 +534,64 @@
468534
}
469535
}
470536
}
537+
},
538+
"MyApiWithNotCachedLambdaRequestAuth": {
539+
"Type": "AWS::ApiGateway::RestApi",
540+
"Properties": {
541+
"Body": {
542+
"info": {
543+
"version": "1.0",
544+
"title": {
545+
"Ref": "AWS::StackName"
546+
}
547+
},
548+
"paths": {
549+
"/not-cached-lambda-request": {
550+
"get": {
551+
"x-amazon-apigateway-integration": {
552+
"httpMethod": "POST",
553+
"type": "aws_proxy",
554+
"uri": {
555+
"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyFn.Arn}/invocations"
556+
}
557+
},
558+
"security": [
559+
{
560+
"MyLambdaRequestAuth": []
561+
}
562+
],
563+
"responses": {}
564+
}
565+
}
566+
},
567+
"swagger": "2.0",
568+
"securityDefinitions": {
569+
"MyLambdaRequestAuth": {
570+
"in": "header",
571+
"type": "apiKey",
572+
"name": "Unused",
573+
"x-amazon-apigateway-authorizer": {
574+
"type": "request",
575+
"authorizerUri": {
576+
"Fn::Sub": [
577+
"arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${__FunctionArn__}/invocations",
578+
{
579+
"__FunctionArn__": {
580+
"Fn::GetAtt": [
581+
"MyAuthFn",
582+
"Arn"
583+
]
584+
}
585+
}
586+
]
587+
},
588+
"authorizerResultTtlInSeconds": 0
589+
},
590+
"x-amazon-apigateway-authtype": "custom"
591+
}
592+
}
593+
}
594+
}
471595
}
472596
}
473597
}

0 commit comments

Comments
 (0)