Skip to content

Commit 25ee8ef

Browse files
authored
fix(apigateway): lambda authorizer does not enforce default cache TTL (#27873)
Adds default `authorizerResultTtlInSeconds` value of 300 seconds to prevent cache TTL value retention when first specified and then removed. Closes #27826. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent cfa2d76 commit 25ee8ef

File tree

6 files changed

+43
-9
lines changed

6 files changed

+43
-9
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-apigateway/test/authorizers/integ.request-authorizer.lit.js.snapshot/RequestAuthorizerInteg.template.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@
188188
"UpdateReplacePolicy": "Retain",
189189
"DeletionPolicy": "Retain"
190190
},
191-
"MyRestApiDeploymentB555B582d83364d66d67f510f848797cd89349d5": {
191+
"MyRestApiDeploymentB555B58276a4103e7ef38befb395a9ace5fdce44": {
192192
"Type": "AWS::ApiGateway::Deployment",
193193
"Properties": {
194194
"Description": "Automatically created by the RestApi construct",
@@ -208,7 +208,7 @@
208208
"Type": "AWS::ApiGateway::Stage",
209209
"Properties": {
210210
"DeploymentId": {
211-
"Ref": "MyRestApiDeploymentB555B582d83364d66d67f510f848797cd89349d5"
211+
"Ref": "MyRestApiDeploymentB555B58276a4103e7ef38befb395a9ace5fdce44"
212212
},
213213
"RestApiId": {
214214
"Ref": "MyRestApi2D1F47A9"
@@ -306,6 +306,7 @@
306306
"MyAuthorizer6575980E": {
307307
"Type": "AWS::ApiGateway::Authorizer",
308308
"Properties": {
309+
"AuthorizerResultTtlInSeconds": 300,
309310
"AuthorizerUri": {
310311
"Fn::Join": [
311312
"",
@@ -366,6 +367,7 @@
366367
"MySecondAuthorizer25A69B96": {
367368
"Type": "AWS::ApiGateway::Authorizer",
368369
"Properties": {
370+
"AuthorizerResultTtlInSeconds": 300,
369371
"AuthorizerUri": {
370372
"Fn::Join": [
371373
"",

packages/@aws-cdk-testing/framework-integ/test/aws-apigateway/test/authorizers/integ.token-authorizer-iam-role.js.snapshot/TokenAuthorizerIAMRoleInteg.template.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"Arn"
8080
]
8181
},
82+
"AuthorizerResultTtlInSeconds": 300,
8283
"AuthorizerUri": {
8384
"Fn::Join": [
8485
"",
@@ -233,7 +234,7 @@
233234
"UpdateReplacePolicy": "Retain",
234235
"DeletionPolicy": "Retain"
235236
},
236-
"MyRestApiDeploymentB555B582694e8eb3fdb7b5f988ba347d35601979": {
237+
"MyRestApiDeploymentB555B58259401a546b13c99de2d05e5e255a9ede": {
237238
"Type": "AWS::ApiGateway::Deployment",
238239
"Properties": {
239240
"Description": "Automatically created by the RestApi construct",
@@ -251,7 +252,7 @@
251252
"Type": "AWS::ApiGateway::Stage",
252253
"Properties": {
253254
"DeploymentId": {
254-
"Ref": "MyRestApiDeploymentB555B582694e8eb3fdb7b5f988ba347d35601979"
255+
"Ref": "MyRestApiDeploymentB555B58259401a546b13c99de2d05e5e255a9ede"
255256
},
256257
"RestApiId": {
257258
"Ref": "MyRestApi2D1F47A9"

packages/@aws-cdk-testing/framework-integ/test/aws-apigateway/test/authorizers/integ.token-authorizer.js.snapshot/TokenAuthorizerInteg.template.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
"MyAuthorizer6575980E": {
9797
"Type": "AWS::ApiGateway::Authorizer",
9898
"Properties": {
99+
"AuthorizerResultTtlInSeconds": 600,
99100
"AuthorizerUri": {
100101
"Fn::Join": [
101102
"",
@@ -208,7 +209,7 @@
208209
"UpdateReplacePolicy": "Retain",
209210
"DeletionPolicy": "Retain"
210211
},
211-
"MyRestApiDeploymentB555B582e0e53f2547b469b538202de55968eaf0": {
212+
"MyRestApiDeploymentB555B5827a9cde8f137f97e5aa74fca164d09d74": {
212213
"Type": "AWS::ApiGateway::Deployment",
213214
"Properties": {
214215
"Description": "Automatically created by the RestApi construct",
@@ -226,7 +227,7 @@
226227
"Type": "AWS::ApiGateway::Stage",
227228
"Properties": {
228229
"DeploymentId": {
229-
"Ref": "MyRestApiDeploymentB555B582e0e53f2547b469b538202de55968eaf0"
230+
"Ref": "MyRestApiDeploymentB555B5827a9cde8f137f97e5aa74fca164d09d74"
230231
},
231232
"RestApiId": {
232233
"Ref": "MyRestApi2D1F47A9"

packages/@aws-cdk-testing/framework-integ/test/aws-apigateway/test/authorizers/integ.token-authorizer.ts

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const authorizerFn = new lambda.Function(stack, 'MyAuthorizerFunction', {
1616

1717
const authorizer = new TokenAuthorizer(stack, 'MyAuthorizer', {
1818
handler: authorizerFn,
19+
resultsCacheTtl: Duration.minutes(10),
1920
});
2021

2122
const restapi = new RestApi(stack, 'MyRestApi', {

packages/aws-cdk-lib/aws-apigateway/lib/authorizers/lambda.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface LambdaAuthorizerProps {
3232
* How long APIGateway should cache the results. Max 1 hour.
3333
* Disable caching by setting this to 0.
3434
*
35-
* @default Duration.minutes(5)
35+
* @default - Duration.minutes(5)
3636
*/
3737
readonly resultsCacheTtl?: Duration;
3838

@@ -215,7 +215,7 @@ export class TokenAuthorizer extends LambdaAuthorizer {
215215
type: 'TOKEN',
216216
authorizerUri: lambdaAuthorizerArn(props.handler),
217217
authorizerCredentials: props.assumeRole?.roleArn,
218-
authorizerResultTtlInSeconds: props.resultsCacheTtl?.toSeconds(),
218+
authorizerResultTtlInSeconds: props.resultsCacheTtl?.toSeconds() ?? Duration.minutes(5).toSeconds(),
219219
identitySource: props.identitySource || 'method.request.header.Authorization',
220220
identityValidationExpression: props.validationRegex,
221221
};
@@ -284,7 +284,7 @@ export class RequestAuthorizer extends LambdaAuthorizer {
284284
type: 'REQUEST',
285285
authorizerUri: lambdaAuthorizerArn(props.handler),
286286
authorizerCredentials: props.assumeRole?.roleArn,
287-
authorizerResultTtlInSeconds: props.resultsCacheTtl?.toSeconds(),
287+
authorizerResultTtlInSeconds: props.resultsCacheTtl?.toSeconds() ?? Duration.minutes(5).toSeconds(),
288288
identitySource: props.identitySources.map(is => is.toString()).join(','),
289289
};
290290

packages/aws-cdk-lib/aws-apigateway/test/authorizers/lambda.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe('lambda authorizer', () => {
2828
Type: 'TOKEN',
2929
RestApiId: stack.resolve(restApi.restApiId),
3030
IdentitySource: 'method.request.header.Authorization',
31+
AuthorizerResultTtlInSeconds: 300,
3132
AuthorizerUri: {
3233
'Fn::Join': [
3334
'',
@@ -102,6 +103,7 @@ describe('lambda authorizer', () => {
102103
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::Authorizer', {
103104
Type: 'REQUEST',
104105
RestApiId: stack.resolve(restApi.restApiId),
106+
AuthorizerResultTtlInSeconds: 0,
105107
AuthorizerUri: {
106108
'Fn::Join': [
107109
'',
@@ -153,6 +155,33 @@ describe('lambda authorizer', () => {
153155

154156
});
155157

158+
test('request authorizer with default cache TTL', () => {
159+
const stack = new Stack();
160+
161+
const func = new lambda.Function(stack, 'myfunction', {
162+
handler: 'handler',
163+
code: lambda.Code.fromInline('foo'),
164+
runtime: lambda.Runtime.NODEJS_LATEST,
165+
});
166+
167+
const auth = new RequestAuthorizer(stack, 'myauthorizer', {
168+
handler: func,
169+
identitySources: [IdentitySource.header('whoami')],
170+
});
171+
172+
const restApi = new RestApi(stack, 'myrestapi');
173+
restApi.root.addMethod('ANY', undefined, {
174+
authorizer: auth,
175+
authorizationType: AuthorizationType.CUSTOM,
176+
});
177+
178+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGateway::Authorizer', {
179+
Type: 'REQUEST',
180+
RestApiId: stack.resolve(restApi.restApiId),
181+
AuthorizerResultTtlInSeconds: 300,
182+
});
183+
});
184+
156185
test('invalid request authorizer config', () => {
157186
const stack = new Stack();
158187

0 commit comments

Comments
 (0)