Skip to content

Commit dc7413d

Browse files
daftstermoelasmar
authored andcommitted
Issue 1508 remove check requiring identity to be required if ReauthorizeEvery equals zero (aws#1577)
* remove check requiring identity to be required Check removed to avoid must specify Identity with at least one of Headers, QueryStrings, StageVariables, or Context. error. This is allowed to be removed from aws console. * set identity to empty dictionary Revert back removal of code section and set identity to empty dictionary instead when function_payload_type is "REQUEST" and no identity defined. * use the correct identity variable fix issue catched by unit test. * Update apigateway.py just set the identity to None * undo change. * remove extra spaces * remove some more spaces * Update test_translator.py remove from test case error_api_invalid_auth as this should be valid. * 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) * add unit testing to cover the InvalidResourceException in case if the identity values are not exist, and not cached * black reformat Co-authored-by: Mohamed Elasmar <[email protected]>
1 parent 701e3d1 commit dc7413d

File tree

6 files changed

+437
-11
lines changed

6 files changed

+437
-11
lines changed

samtranslator/model/apigateway.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,9 @@ def _is_missing_identity_source(self, identity):
267267
query_strings = identity.get("QueryStrings")
268268
stage_variables = identity.get("StageVariables")
269269
context = identity.get("Context")
270+
ttl = identity.get("ReauthorizeEvery")
270271

271-
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:
272273
return True
273274

274275
return False
@@ -311,7 +312,9 @@ def generate_swagger(self):
311312
swagger[APIGATEWAY_AUTHORIZER_KEY]["authorizerCredentials"] = function_invoke_role
312313

313314
if self._get_function_payload_type() == "REQUEST":
314-
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()
315318

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

tests/model/test_api.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,18 @@ def test_create_authorizer_fails_with_string_authorization_scopes(self):
1717
auth = ApiGatewayAuthorizer(
1818
api_logical_id="logicalId", name="authName", authorization_scopes="invalid_scope"
1919
)
20+
21+
def test_create_authorizer_fails_with_missing_identity_values_and_not_cached(self):
22+
with pytest.raises(InvalidResourceException):
23+
auth = ApiGatewayAuthorizer(
24+
api_logical_id="logicalId",
25+
name="authName",
26+
identity={"ReauthorizeEvery": 10},
27+
function_payload_type="REQUEST",
28+
)
29+
30+
def test_create_authorizer_fails_with_empty_identity(self):
31+
with pytest.raises(InvalidResourceException):
32+
auth = ApiGatewayAuthorizer(
33+
api_logical_id="logicalId", name="authName", identity={}, function_payload_type="REQUEST"
34+
)

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)