Skip to content

Commit d09ed7b

Browse files
authored
Adding authorization scopes as list validation in ApiGatewayAuthorizer (v1 and v2). (#1670)
* Adding authorization scopes as list validation in ApiGatewayAuthorizer and ApiGatewayV2Authorizer. * make black. * Adding functional test for invalid auth scope. * adding error condition for invalid test. * removing test template file.
1 parent 39be0a6 commit d09ed7b

File tree

7 files changed

+132
-0
lines changed

7 files changed

+132
-0
lines changed

samtranslator/model/apigateway.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ def __init__(
244244
"of Headers, QueryStrings, StageVariables, or Context.",
245245
)
246246

247+
if authorization_scopes is not None and not isinstance(authorization_scopes, list):
248+
raise InvalidResourceException(api_logical_id, "AuthorizationScopes must be a list.")
249+
247250
self.api_logical_id = api_logical_id
248251
self.name = name
249252
self.user_pool_arn = user_pool_arn

samtranslator/model/apigatewayv2.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ def __init__(
6262
"""
6363
Creates an authorizer for use in V2 Http Apis
6464
"""
65+
if authorization_scopes is not None and not isinstance(authorization_scopes, list):
66+
raise InvalidResourceException(api_logical_id, "AuthorizationScopes must be a list.")
67+
6568
# Currently only one type of auth
6669
self.auth_type = "oauth2"
6770

tests/model/test_api.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from unittest import TestCase
2+
import pytest
3+
4+
from samtranslator.model import InvalidResourceException
5+
from samtranslator.model.apigateway import ApiGatewayAuthorizer
6+
7+
8+
class TestApiGatewayAuthorizer(TestCase):
9+
def test_create_oauth2_auth(self):
10+
auth = ApiGatewayAuthorizer(
11+
api_logical_id="logicalId", name="authName", authorization_scopes=["scope1", "scope2"]
12+
)
13+
self.assertIsNotNone(auth)
14+
15+
def test_create_authorizer_fails_with_string_authorization_scopes(self):
16+
with pytest.raises(InvalidResourceException):
17+
auth = ApiGatewayAuthorizer(
18+
api_logical_id="logicalId", name="authName", authorization_scopes="invalid_scope"
19+
)

tests/model/test_api_v2.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def test_create_oauth2_auth(self):
1212
name="authName",
1313
jwt_configuration={"config": "value"},
1414
id_source="https://example.com",
15+
authorization_scopes=["scope1", "scope2"],
1516
)
1617
self.assertEquals(auth.auth_type, "oauth2")
1718

@@ -24,3 +25,12 @@ def test_create_authorizer_no_id_source(self):
2425
def test_create_authorizer_no_jwt_config(self):
2526
with pytest.raises(InvalidResourceException):
2627
auth = ApiGatewayV2Authorizer(api_logical_id="logicalId", name="authName", id_source="https://example.com")
28+
29+
def test_create_authorizer_fails_with_string_authorization_scopes(self):
30+
with pytest.raises(InvalidResourceException):
31+
auth = ApiGatewayV2Authorizer(
32+
api_logical_id="logicalId",
33+
name="authName",
34+
jwt_configuration={"config": "value"},
35+
authorization_scopes="invalid_scope",
36+
)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
Resources:
2+
MyApiWithCognitoAuth:
3+
Type: "AWS::Serverless::Api"
4+
Properties:
5+
StageName: Prod
6+
OpenApiVersion: '3.0.1'
7+
Auth:
8+
DefaultAuthorizer: MyDefaultCognitoAuth
9+
Authorizers:
10+
MyDefaultCognitoAuth:
11+
UserPoolArn: arn:aws:1
12+
AuthorizationScopes:
13+
- default.write
14+
- default.read
15+
MyCognitoAuthWithDefaultScopes:
16+
UserPoolArn: arn:aws:2
17+
AuthorizationScopes: default.delete
18+
19+
MyFn:
20+
Type: AWS::Serverless::Function
21+
Properties:
22+
CodeUri: s3://bucket/key
23+
Handler: index.handler
24+
Runtime: nodejs12.x
25+
Events:
26+
CognitoAuthorizerWithDefaultScopes:
27+
Type: Api
28+
Properties:
29+
RestApiId: !Ref MyApiWithCognitoAuth
30+
Method: get
31+
Path: /cognitoauthorizerwithdefaultscopes
32+
Auth:
33+
Authorizer: MyCognitoAuthWithDefaultScopes
34+
CognitoDefaultScopesDefaultAuthorizer:
35+
Type: Api
36+
Properties:
37+
RestApiId: !Ref MyApiWithCognitoAuth
38+
Method: get
39+
Path: /cognitodefaultscopesdefaultauthorizer
40+
CognitoWithAuthNone:
41+
Type: Api
42+
Properties:
43+
RestApiId: !Ref MyApiWithCognitoAuth
44+
Method: get
45+
Path: /cognitowithauthnone
46+
Auth:
47+
Authorizer: NONE
48+
CognitoDefaultScopesWithOverwritten:
49+
Type: Api
50+
Properties:
51+
RestApiId: !Ref MyApiWithCognitoAuth
52+
Method: get
53+
Path: /cognitodefaultscopesoverwritten
54+
Auth:
55+
Authorizer: MyDefaultCognitoAuth
56+
AuthorizationScopes:
57+
- overwritten.read
58+
- overwritten.write
59+
CognitoAuthorizerScopesOverwritten:
60+
Type: Api
61+
Properties:
62+
RestApiId: !Ref MyApiWithCognitoAuth
63+
Method: get
64+
Path: /cognitoauthorizercopesoverwritten
65+
Auth:
66+
Authorizer: MyCognitoAuthWithDefaultScopes
67+
AuthorizationScopes:
68+
- overwritten.read
69+
- overwritten.write
70+
CognitoDefaultScopesNone:
71+
Type: Api
72+
Properties:
73+
RestApiId: !Ref MyApiWithCognitoAuth
74+
Method: get
75+
Path: /cognitodefaultscopesnone
76+
Auth:
77+
Authorizer: MyDefaultCognitoAuth
78+
AuthorizationScopes: []
79+
CognitoDefaultAuthDefaultScopesNone:
80+
Type: Api
81+
Properties:
82+
RestApiId: !Ref MyApiWithCognitoAuth
83+
Method: get
84+
Path: /cognitodefaultauthdefaultscopesnone
85+
Auth:
86+
Authorizer: MyCognitoAuthWithDefaultScopes
87+
AuthorizationScopes: []
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"errors": [
3+
{
4+
"errorMessage": "Resource with id [MyApiWithCognitoAuth] is invalid. AuthorizationScopes must be a list."
5+
}
6+
],
7+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [MyApiWithCognitoAuth] is invalid. AuthorizationScopes must be a list."
8+
}
9+

tests/translator/test_translator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ def _generate_new_deployment_hash(self, logical_id, dict_to_hash, rest_api_to_sw
629629
"error_function_with_invalid_condition_name",
630630
"error_invalid_document_empty_semantic_version",
631631
"error_api_with_invalid_open_api_version_type",
632+
"error_api_with_invalid_auth_scopes_openapi",
632633
"error_api_with_custom_domains_invalid",
633634
"error_api_with_custom_domains_route53_invalid",
634635
"error_api_event_import_vaule_reference",

0 commit comments

Comments
 (0)