Skip to content

Commit 802334b

Browse files
authored
Add checks for authorizer event source types (#2307)
1 parent f37ba41 commit 802334b

7 files changed

+95
-17
lines changed

samtranslator/model/eventsources/push.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -704,14 +704,9 @@ def _add_swagger_integration(self, api, function, intrinsics_resolver):
704704
),
705705
)
706706

707-
if not isinstance(method_authorizer, str):
708-
raise InvalidEventException(
709-
self.relative_id,
710-
"Unable to set Authorizer [{authorizer}] on API method [{method}] for path [{path}] "
711-
"because it wasn't defined with acceptable values in the API's Authorizers.".format(
712-
authorizer=method_authorizer, method=self.Method, path=self.Path
713-
),
714-
)
707+
_check_valid_authorizer_types(
708+
self.relative_id, self.Method, self.Path, method_authorizer, api_authorizers
709+
)
715710

716711
if method_authorizer != "NONE" and not api_authorizers.get(method_authorizer):
717712
raise InvalidEventException(
@@ -1198,13 +1193,6 @@ def _add_auth_to_openapi_integration(self, api, editor):
11981193
:param editor: OpenApiEditor object that contains the OpenApi definition
11991194
"""
12001195
method_authorizer = self.Auth.get("Authorizer")
1201-
1202-
if method_authorizer is not None and not isinstance(method_authorizer, str):
1203-
raise InvalidEventException(
1204-
self.relative_id,
1205-
"'Authorizer' in the 'Auth' section must be a string.",
1206-
)
1207-
12081196
api_auth = api.get("Auth", {})
12091197
if not method_authorizer:
12101198
if api_auth.get("DefaultAuthorizer"):
@@ -1221,6 +1209,8 @@ def _add_auth_to_openapi_integration(self, api, editor):
12211209
# Default auth should already be applied, so apply any other auth here or scope override to default
12221210
api_authorizers = api_auth and api_auth.get("Authorizers")
12231211

1212+
_check_valid_authorizer_types(self.relative_id, self.Method, self.Path, method_authorizer, api_authorizers)
1213+
12241214
if method_authorizer != "NONE" and not api_authorizers:
12251215
raise InvalidEventException(
12261216
self.relative_id,
@@ -1270,3 +1260,19 @@ def _build_apigw_integration_uri(function, partition):
12701260
if function_arn.get("Fn::GetAtt") and isinstance(function_arn["Fn::GetAtt"][0], Py27UniStr):
12711261
arn = Py27UniStr(arn)
12721262
return Py27Dict(fnSub(arn))
1263+
1264+
1265+
def _check_valid_authorizer_types(relative_id, method, path, method_authorizer, api_authorizers):
1266+
if method_authorizer == "NONE":
1267+
# If the method authorizer is "NONE" then this check
1268+
# isn't needed since DefaultAuthorizer needs to be used.
1269+
return
1270+
1271+
if not isinstance(method_authorizer, str) or not isinstance(api_authorizers, dict):
1272+
raise InvalidEventException(
1273+
relative_id,
1274+
"Unable to set Authorizer [{authorizer}] on API method [{method}] for path [{path}]. "
1275+
"The method authorizer must be a string with a corresponding dict entry in the api authorizer.".format(
1276+
authorizer=method_authorizer, method=method, path=path
1277+
),
1278+
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Resources:
2+
SignInFunction:
3+
Type: AWS::Serverless::Function
4+
Properties:
5+
CodeUri: s3://bucket/key
6+
Handler: main.main
7+
Runtime: python3.9
8+
Events:
9+
MainFuncPostV1:
10+
Type: Api
11+
Properties:
12+
Auth:
13+
Authorizer:
14+
- CognitoAuthorizer
15+
Path: /v1/signin
16+
RestApiId: AuthorizedApi
17+
Method: post
18+
AuthorizedApi:
19+
Type: 'AWS::Serverless::Api'
20+
Properties:
21+
StageName: Prod
22+
Auth:
23+
DefaultAuthorizer: NONE
24+
Authorizers:
25+
- CognitoAuthorizer: null
26+
UserPoolArn: !GetAtt 'CognitoUserPool.Arn'
27+
AuthorizationScopes:
28+
- aws.cognito.signin.user.admin
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Resources:
2+
SignInFunction:
3+
Type: AWS::Serverless::Function
4+
Properties:
5+
CodeUri: s3://bucket/key
6+
Handler: main.main
7+
Runtime: python3.9
8+
Events:
9+
MainFuncPostV1:
10+
Type: HttpApi
11+
Properties:
12+
Auth:
13+
Authorizer:
14+
- CognitoAuthorizer
15+
Path: /v1/signin
16+
ApiId: AuthorizedApi
17+
Method: post
18+
AuthorizedApi:
19+
Type: 'AWS::Serverless::HttpApi'
20+
Properties:
21+
StageName: Prod
22+
Auth:
23+
DefaultAuthorizer: NONE
24+
Authorizers:
25+
- CognitoAuthorizer: null
26+
UserPoolArn: !GetAtt 'CognitoUserPool.Arn'
27+
AuthorizationScopes:
28+
- aws.cognito.signin.user.admin
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"errors": [
3+
{
4+
"errorMessage": "Resource with id [AuthorizedApi] is invalid. Authorizers must be a dictionary. Resource with id [SignInFunction] is invalid. Event with id [MainFuncPostV1] is invalid. Unable to set Authorizer [['CognitoAuthorizer']] on API method [post] for path [/v1/signin]. The method authorizer must be a string with a corresponding dict entry in the api authorizer."
5+
}
6+
],
7+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 2. Resource with id [AuthorizedApi] is invalid. Authorizers must be a dictionary. Resource with id [SignInFunction] is invalid. Event with id [MainFuncPostV1] is invalid. Unable to set Authorizer [['CognitoAuthorizer']] on API method [post] for path [/v1/signin]. The method authorizer must be a string with a corresponding dict entry in the api authorizer."
8+
}

tests/translator/output/error_api_with_invalid_auth_scopes_openapi.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"errorMessage": "Resource with id [MyApiWithCognitoAuth] is invalid. AuthorizationScopes must be a list."
55
}
66
],
7-
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 2. Resource with id [MyApiWithCognitoAuth] is invalid. AuthorizationScopes must be a list. Resource with id [MyFn] is invalid. Event with id [CognitoAuthorizerNotString] is invalid. Unable to set Authorizer [['NotString']] on API method [get] for path [/cognitoauthorizernotstring] because it wasn't defined with acceptable values in the API's Authorizers."
7+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 2. Resource with id [MyApiWithCognitoAuth] is invalid. AuthorizationScopes must be a list. Resource with id [MyFn] is invalid. Event with id [CognitoAuthorizerNotString] is invalid. Unable to set Authorizer [['NotString']] on API method [get] for path [/cognitoauthorizernotstring]. The method authorizer must be a string with a corresponding dict entry in the api authorizer."
88
}
99

tests/translator/output/error_http_api_invalid_auth.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
"errorMessage": "Resource with id [Function] is invalid. Event with id [Api] is invalid. Unable to set Authorizer [myAuth] on API method [x-amazon-apigateway-any-method] for path [$default] because the related API does not define any Authorizers. Resource with id [Function2] is invalid. Event with id [Api2] is invalid. Unable to set Authorizer [myAuth] on API method [x-amazon-apigateway-any-method] for path [$default] because it wasn't defined in the API's Authorizers. Resource with id [Function3] is invalid. Event with id [Api3] is invalid. Unable to set Authorizer on API method [x-amazon-apigateway-any-method] for path [$default] because 'NONE' is only a valid value when a DefaultAuthorizer on the API is specified. Resource with id [Function4] is invalid. Event with id [Api4] is invalid. Unable to set Authorizer on API method [x-amazon-apigateway-any-method] for path [$default] because 'AuthorizationScopes' must be a list of strings."
55
}
66
],
7-
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 6. Resource with id [Function] is invalid. Event with id [Api] is invalid. Unable to set Authorizer [myAuth] on API method [x-amazon-apigateway-any-method] for path [$default] because the related API does not define any Authorizers. Resource with id [Function2] is invalid. Event with id [Api2] is invalid. Unable to set Authorizer [myAuth] on API method [x-amazon-apigateway-any-method] for path [$default] because it wasn't defined in the API's Authorizers. Resource with id [Function3] is invalid. Event with id [Api3] is invalid. Unable to set Authorizer on API method [x-amazon-apigateway-any-method] for path [$default] because 'NONE' is only a valid value when a DefaultAuthorizer on the API is specified. Resource with id [Function4] is invalid. Event with id [Api4] is invalid. Unable to set Authorizer on API method [x-amazon-apigateway-any-method] for path [$default] because 'AuthorizationScopes' must be a list of strings. Resource with id [MyApi5] is invalid. 'OpenIdConnectUrl' is no longer a supported property for authorizer 'OIDC'. Please refer to the AWS SAM documentation. Resource with id [NonStringAuthFunction] is invalid. Event with id [GetRoot] is invalid. 'Authorizer' in the 'Auth' section must be a string."
7+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 6. Resource with id [Function] is invalid. Event with id [Api] is invalid. Unable to set Authorizer [myAuth] on API method [x-amazon-apigateway-any-method] for path [$default] because the related API does not define any Authorizers. Resource with id [Function2] is invalid. Event with id [Api2] is invalid. Unable to set Authorizer [myAuth] on API method [x-amazon-apigateway-any-method] for path [$default] because it wasn't defined in the API's Authorizers. Resource with id [Function3] is invalid. Event with id [Api3] is invalid. Unable to set Authorizer on API method [x-amazon-apigateway-any-method] for path [$default] because 'NONE' is only a valid value when a DefaultAuthorizer on the API is specified. Resource with id [Function4] is invalid. Event with id [Api4] is invalid. Unable to set Authorizer on API method [x-amazon-apigateway-any-method] for path [$default] because 'AuthorizationScopes' must be a list of strings. Resource with id [MyApi5] is invalid. 'OpenIdConnectUrl' is no longer a supported property for authorizer 'OIDC'. Please refer to the AWS SAM documentation. Resource with id [NonStringAuthFunction] is invalid. Event with id [GetRoot] is invalid. Unable to set Authorizer [{'Ref': 'MyAuth'}] on API method [get] for path [/]. The method authorizer must be a string with a corresponding dict entry in the api authorizer."
88
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"errors": [
3+
{
4+
"errorMessage": "Resource with id [AuthorizedApi] is invalid. Authorizers must be a dictionary. Resource with id [SignInFunction] is invalid. Event with id [MainFuncPostV1] is invalid. Unable to set Authorizer [['CognitoAuthorizer']] on API method [post] for path [/v1/signin]. The method authorizer must be a string with a corresponding dict entry in the api authorizer."
5+
}
6+
],
7+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 2. Resource with id [AuthorizedApi] is invalid. Authorizers must be a dictionary. Resource with id [SignInFunction] is invalid. Event with id [MainFuncPostV1] is invalid. Unable to set Authorizer [['CognitoAuthorizer']] on API method [post] for path [/v1/signin]. The method authorizer must be a string with a corresponding dict entry in the api authorizer."
8+
}

0 commit comments

Comments
 (0)