Skip to content

Commit ffca91a

Browse files
authored
chore: Add an error when AuthorizationScopes is not used with Authorizer (#3394)
1 parent 26aea83 commit ffca91a

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

samtranslator/model/eventsources/push.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,8 +1116,11 @@ def add_auth_to_swagger( # noqa: PLR0912, PLR0913
11161116
)
11171117

11181118
auth_scopes = event_auth.get("AuthorizationScopes")
1119+
11191120
if auth_scopes:
11201121
sam_expect(auth_scopes, event_id, "Auth.AuthorizationScopes", is_sam_event=True).to_be_a_list()
1122+
if not method_authorizer:
1123+
raise InvalidEventException(event_id, "AuthorizationScopes works only when Authorizer is set")
11211124

11221125
apikey_required_setting = event_auth.get("ApiKeyRequired")
11231126
apikey_required_setting_is_false = apikey_required_setting is not None and not apikey_required_setting

tests/translator/input/error_api_invalid_auth.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
Resources:
2+
InvalidAuthorizationScopes:
3+
Type: AWS::Serverless::Function
4+
Properties:
5+
Runtime: python3
6+
Handler: index.handler
7+
CodeUri: s3://bucket/key
8+
Events:
9+
Api:
10+
Type: Api
11+
Properties:
12+
Auth:
13+
AuthorizationScopes:
14+
- https://foobar.acme.net/full-api
15+
Method: POST
16+
Path: /hello/world
17+
218
NoAuthApi:
319
Type: AWS::Serverless::Api
420
Properties:

tests/translator/output/error_api_invalid_auth.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"_autoGeneratedBreakdownErrorMessage": [
33
"Invalid Serverless Application Specification document. ",
4-
"Number of errors found: 21. ",
4+
"Number of errors found: 22. ",
55
"Resource with id [AuthNotDictApi] is invalid. ",
66
"Property 'Auth' should be a map. ",
77
"Resource with id [AuthWithAdditionalPropertyApi] is invalid. ",
@@ -19,7 +19,9 @@
1919
"Property 'Auth.Authorizers' should be a map. ",
2020
"Resource with id [IntrinsicDefaultAuthorizerApi] is invalid. ",
2121
"Property 'Auth.DefaultAuthorizer' should be a string. ",
22-
"Resource with id [InvalidFunctionPayloadTypeApi] is invalid. ",
22+
"Resource with id [InvalidAuthorizationScopes] is invalid. ",
23+
"Event with id [Api] is invalid. ",
24+
"AuthorizationScopes works only when Authorizer is set Resource with id [InvalidFunctionPayloadTypeApi] is invalid. ",
2325
"MyLambdaAuthorizer Authorizer has invalid 'FunctionPayloadType': INVALID. ",
2426
"Resource with id [MissingAuthorizerFn] is invalid. ",
2527
"Event with id [GetRoot] is invalid. ",
@@ -49,5 +51,5 @@
4951
"Resource with id [NonStringDefaultAuthorizerApi] is invalid. ",
5052
"Property 'Auth.DefaultAuthorizer' should be a string."
5153
],
52-
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 21. Resource with id [AuthNotDictApi] is invalid. Property 'Auth' should be a map. Resource with id [AuthWithAdditionalPropertyApi] is invalid. Invalid value for 'Auth' property Resource with id [AuthWithDefinitionUriApi] is invalid. Auth works only with inline Swagger specified in 'DefinitionBody' property. Resource with id [AuthWithInvalidDefinitionBodyApi] is invalid. Unable to add Auth configuration because 'DefinitionBody' does not contain a valid Swagger definition. Resource with id [AuthWithMissingDefaultAuthorizerApi] is invalid. Unable to set DefaultAuthorizer because 'NotThere' was not defined in 'Authorizers'. Resource with id [AuthorizerNotDict] is invalid. Property 'Auth.Authorizers.MyCognitoAuthorizer' should be a map. Resource with id [AuthorizerWithBadDisableFunctionDefaultPermissionsType] is invalid. Property 'Authorizers.MyAuth.DisableFunctionDefaultPermissions' should be a boolean. Resource with id [AuthorizersNotDictApi] is invalid. Property 'Auth.Authorizers' should be a map. Resource with id [IntrinsicDefaultAuthorizerApi] is invalid. Property 'Auth.DefaultAuthorizer' should be a string. Resource with id [InvalidFunctionPayloadTypeApi] is invalid. MyLambdaAuthorizer Authorizer has invalid 'FunctionPayloadType': INVALID. Resource with id [MissingAuthorizerFn] is invalid. Event with id [GetRoot] is invalid. Unable to set Authorizer [UnspecifiedAuthorizer] on API method [get] for path [/] because it wasn't defined in the API's Authorizers. Resource with id [NoApiAuthorizerFn] is invalid. Event with id [GetRoot] is invalid. Unable to set Authorizer [MyAuth] on API method [get] for path [/] because the related API does not define any Authorizers. Resource with id [NoAuthFn] is invalid. Event with id [GetRoot] is invalid. Unable to set Authorizer [MyAuth] on API method [get] for path [/] because the related API does not define any Authorizers. Resource with id [NoAuthorizersFn] is invalid. Event with id [GetRoot] is invalid. Unable to set Authorizer [MyAuth] on API method [get] for path [/] because the related API does not define any Authorizers. Resource with id [NoDefaultAuthorizerWithNoneFn] is invalid. Event with id [GetRoot] is invalid. Unable to set Authorizer on API method [get] for path [/] because 'NONE' is only a valid value when a DefaultAuthorizer on the API is specified. Resource with id [NoIdentityOnRequestAuthorizer] is invalid. MyLambdaRequestAuthorizer Authorizer must specify Identity with at least one of Headers, QueryStrings, StageVariables, or Context. Resource with id [NoIdentitySourceOnRequestAuthorizer] is invalid. MyLambdaRequestAuthorizer Authorizer must specify Identity with at least one of Headers, QueryStrings, StageVariables, or Context. Resource with id [NonDictAuthorizerApi] is invalid. Property 'Auth.Authorizers.MyAuth' should be a map. Resource with id [NonDictAuthorizerRestApi] is invalid. Property 'Auth.Authorizers.MyAuth' should be a map. Resource with id [NonDictExternalApiAuthorizer] is invalid. Property 'Auth.Authorizers' should be a map. Resource with id [NonStringDefaultAuthorizerApi] is invalid. Property 'Auth.DefaultAuthorizer' should be a string."
54+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 22. Resource with id [AuthNotDictApi] is invalid. Property 'Auth' should be a map. Resource with id [AuthWithAdditionalPropertyApi] is invalid. Invalid value for 'Auth' property Resource with id [AuthWithDefinitionUriApi] is invalid. Auth works only with inline Swagger specified in 'DefinitionBody' property. Resource with id [AuthWithInvalidDefinitionBodyApi] is invalid. Unable to add Auth configuration because 'DefinitionBody' does not contain a valid Swagger definition. Resource with id [AuthWithMissingDefaultAuthorizerApi] is invalid. Unable to set DefaultAuthorizer because 'NotThere' was not defined in 'Authorizers'. Resource with id [AuthorizerNotDict] is invalid. Property 'Auth.Authorizers.MyCognitoAuthorizer' should be a map. Resource with id [AuthorizerWithBadDisableFunctionDefaultPermissionsType] is invalid. Property 'Authorizers.MyAuth.DisableFunctionDefaultPermissions' should be a boolean. Resource with id [AuthorizersNotDictApi] is invalid. Property 'Auth.Authorizers' should be a map. Resource with id [IntrinsicDefaultAuthorizerApi] is invalid. Property 'Auth.DefaultAuthorizer' should be a string. Resource with id [InvalidAuthorizationScopes] is invalid. Event with id [Api] is invalid. AuthorizationScopes works only when Authorizer is set Resource with id [InvalidFunctionPayloadTypeApi] is invalid. MyLambdaAuthorizer Authorizer has invalid 'FunctionPayloadType': INVALID. Resource with id [MissingAuthorizerFn] is invalid. Event with id [GetRoot] is invalid. Unable to set Authorizer [UnspecifiedAuthorizer] on API method [get] for path [/] because it wasn't defined in the API's Authorizers. Resource with id [NoApiAuthorizerFn] is invalid. Event with id [GetRoot] is invalid. Unable to set Authorizer [MyAuth] on API method [get] for path [/] because the related API does not define any Authorizers. Resource with id [NoAuthFn] is invalid. Event with id [GetRoot] is invalid. Unable to set Authorizer [MyAuth] on API method [get] for path [/] because the related API does not define any Authorizers. Resource with id [NoAuthorizersFn] is invalid. Event with id [GetRoot] is invalid. Unable to set Authorizer [MyAuth] on API method [get] for path [/] because the related API does not define any Authorizers. Resource with id [NoDefaultAuthorizerWithNoneFn] is invalid. Event with id [GetRoot] is invalid. Unable to set Authorizer on API method [get] for path [/] because 'NONE' is only a valid value when a DefaultAuthorizer on the API is specified. Resource with id [NoIdentityOnRequestAuthorizer] is invalid. MyLambdaRequestAuthorizer Authorizer must specify Identity with at least one of Headers, QueryStrings, StageVariables, or Context. Resource with id [NoIdentitySourceOnRequestAuthorizer] is invalid. MyLambdaRequestAuthorizer Authorizer must specify Identity with at least one of Headers, QueryStrings, StageVariables, or Context. Resource with id [NonDictAuthorizerApi] is invalid. Property 'Auth.Authorizers.MyAuth' should be a map. Resource with id [NonDictAuthorizerRestApi] is invalid. Property 'Auth.Authorizers.MyAuth' should be a map. Resource with id [NonDictExternalApiAuthorizer] is invalid. Property 'Auth.Authorizers' should be a map. Resource with id [NonStringDefaultAuthorizerApi] is invalid. Property 'Auth.DefaultAuthorizer' should be a string."
5355
}

0 commit comments

Comments
 (0)