-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(http-api): Add a built-in AWS_IAM authorizer for HTTP APIs (#1924)
- Loading branch information
1 parent
c1a6690
commit e4ffc6e
Showing
36 changed files
with
2,497 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
integration/resources/expected/single/function_with_http_api_events_and_auth.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
[ | ||
{ | ||
"LogicalResourceId": "MyDefaultIamAuthHttpApi", | ||
"ResourceType": "AWS::ApiGatewayV2::Api" | ||
}, | ||
{ | ||
"LogicalResourceId": "MyDefaultIamAuthHttpApiApiGatewayDefaultStage", | ||
"ResourceType": "AWS::ApiGatewayV2::Stage" | ||
}, | ||
{ | ||
"LogicalResourceId": "MyIamAuthEnabledHttpApi", | ||
"ResourceType": "AWS::ApiGatewayV2::Api" | ||
}, | ||
{ | ||
"LogicalResourceId": "MyIamAuthEnabledHttpApiApiGatewayDefaultStage", | ||
"ResourceType": "AWS::ApiGatewayV2::Stage" | ||
}, | ||
{ | ||
"LogicalResourceId": "MyLambdaFunction", | ||
"ResourceType": "AWS::Lambda::Function" | ||
}, | ||
{ | ||
"LogicalResourceId": "MyLambdaFunctionImplicitApiDefaultAuthEventPermission", | ||
"ResourceType": "AWS::Lambda::Permission" | ||
}, | ||
{ | ||
"LogicalResourceId": "MyLambdaFunctionImplicitApiIamAuthEventPermission", | ||
"ResourceType": "AWS::Lambda::Permission" | ||
}, | ||
{ | ||
"LogicalResourceId": "MyLambdaFunctionMyDefaultIamAuthHttpApiDefaultAuthEventPermission", | ||
"ResourceType": "AWS::Lambda::Permission" | ||
}, | ||
{ | ||
"LogicalResourceId": "MyLambdaFunctionMyDefaultIamAuthHttpApiIamAuthEventPermission", | ||
"ResourceType": "AWS::Lambda::Permission" | ||
}, | ||
{ | ||
"LogicalResourceId": "MyLambdaFunctionMyDefaultIamAuthHttpApiNoAuthEventPermission", | ||
"ResourceType": "AWS::Lambda::Permission" | ||
}, | ||
{ | ||
"LogicalResourceId": "MyLambdaFunctionMyIamAuthEnabledHttpApiDefaultAuthEventPermission", | ||
"ResourceType": "AWS::Lambda::Permission" | ||
}, | ||
{ | ||
"LogicalResourceId": "MyLambdaFunctionMyIamAuthEnabledHttpApiIamAuthEventPermission", | ||
"ResourceType": "AWS::Lambda::Permission" | ||
}, | ||
{ | ||
"LogicalResourceId": "MyLambdaFunctionRole", | ||
"ResourceType": "AWS::IAM::Role" | ||
}, | ||
{ | ||
"LogicalResourceId": "ServerlessHttpApi", | ||
"ResourceType": "AWS::ApiGatewayV2::Api" | ||
}, | ||
{ | ||
"LogicalResourceId": "ServerlessHttpApiApiGatewayDefaultStage", | ||
"ResourceType": "AWS::ApiGatewayV2::Stage" | ||
} | ||
] |
96 changes: 96 additions & 0 deletions
96
integration/resources/templates/single/function_with_http_api_events_and_auth.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
Globals: | ||
HttpApi: | ||
Auth: | ||
EnableIamAuthorizer: true | ||
Resources: | ||
####### | ||
# Serverless function that use the implicit AWS::Serverless::HttpApi called "ServerlessHttpApi". | ||
# IAM Authorizer of the implicit AWS::Serverless::HttpApi is enabled using the global above. | ||
####### | ||
MyLambdaFunction: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
Handler: index.handler | ||
Runtime: nodejs12.x | ||
CodeUri: ${codeuri} | ||
Events: | ||
# The following events use the implicit AWS::Serverless::HttpApi called "ServerlessHttpApi". | ||
# The Iam Authorizer of the implicit AWS::Serverless::HttpApi is enabled using the global above. | ||
# Should not have any auth enabled because there is no one set as the default. | ||
ImplicitApiDefaultAuthEvent: | ||
Type: HttpApi | ||
Properties: | ||
Path: /default-auth | ||
Method: GET | ||
# Should have Iam auth as it is set here. | ||
ImplicitApiIamAuthEvent: | ||
Type: HttpApi | ||
Properties: | ||
Auth: | ||
Authorizer: AWS_IAM | ||
Path: /iam-auth | ||
Method: GET | ||
|
||
# The following events use the defined AWS::Serverless::HttpApi further down. | ||
# Should not have any auth enabled. | ||
MyDefaultIamAuthHttpApiNoAuthEvent: | ||
Type: HttpApi | ||
Properties: | ||
ApiId: | ||
Ref: MyDefaultIamAuthHttpApi | ||
Auth: | ||
Authorizer: NONE | ||
Path: /no-auth | ||
Method: GET | ||
# Should have Iam auth as it is set as the default for the Api. | ||
MyDefaultIamAuthHttpApiDefaultAuthEvent: | ||
Type: HttpApi | ||
Properties: | ||
ApiId: | ||
Ref: MyDefaultIamAuthHttpApi | ||
Path: /default-auth | ||
Method: GET | ||
# Should have Iam auth as it is set here. | ||
MyDefaultIamAuthHttpApiIamAuthEvent: | ||
Type: HttpApi | ||
Properties: | ||
ApiId: | ||
Ref: MyDefaultIamAuthHttpApi | ||
Auth: | ||
Authorizer: AWS_IAM | ||
Path: /iam-auth | ||
Method: GET | ||
# The following events use the defined AWS::Serverless::HttpApi further down. | ||
# Should not have any auth enabled because there is no one set as the default. | ||
MyIamAuthEnabledHttpApiDefaultAuthEvent: | ||
Type: HttpApi | ||
Properties: | ||
ApiId: | ||
Ref: MyIamAuthEnabledHttpApi | ||
Path: /default-auth | ||
Method: GET | ||
# Should have Iam auth as it is set here. | ||
MyIamAuthEnabledHttpApiIamAuthEvent: | ||
Type: HttpApi | ||
Properties: | ||
ApiId: | ||
Ref: MyIamAuthEnabledHttpApi | ||
Auth: | ||
Authorizer: AWS_IAM | ||
Path: /iam-auth | ||
Method: GET | ||
|
||
# HTTP API resource with the Iam authorizer enabled and set to the default. | ||
MyDefaultIamAuthHttpApi: | ||
Type: AWS::Serverless::HttpApi | ||
Properties: | ||
Auth: | ||
EnableIamAuthorizer: true | ||
DefaultAuthorizer: AWS_IAM | ||
|
||
# HTTP API resource with the Iam authorizer enabled and NOT set to the default. | ||
MyIamAuthEnabledHttpApi: | ||
Type: AWS::Serverless::HttpApi | ||
Properties: | ||
Auth: | ||
EnableIamAuthorizer: true |
29 changes: 29 additions & 0 deletions
29
integration/single/test_function_with_http_api_and_auth.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import requests | ||
from parameterized import parameterized | ||
from integration.helpers.base_test import BaseTest | ||
|
||
|
||
class TestFunctionWithHttpApiAndAuth(BaseTest): | ||
""" | ||
AWS::Lambda::Function tests with http api events and auth | ||
""" | ||
|
||
def test_function_with_http_api_and_auth(self): | ||
# If the request is not signed, which none of the below are, IAM will respond with a "Forbidden" message. | ||
# We are not testing that IAM auth works here, we are simply testing if it was applied. | ||
IAM_AUTH_OUTPUT = '{"message":"Forbidden"}' | ||
|
||
self.create_and_verify_stack("function_with_http_api_events_and_auth") | ||
|
||
implicitEndpoint = self.get_api_v2_endpoint("ServerlessHttpApi") | ||
self.assertEqual(requests.get(implicitEndpoint + "/default-auth").text, self.FUNCTION_OUTPUT) | ||
self.assertEqual(requests.get(implicitEndpoint + "/iam-auth").text, IAM_AUTH_OUTPUT) | ||
|
||
defaultIamEndpoint = self.get_api_v2_endpoint("MyDefaultIamAuthHttpApi") | ||
self.assertEqual(requests.get(defaultIamEndpoint + "/no-auth").text, self.FUNCTION_OUTPUT) | ||
self.assertEqual(requests.get(defaultIamEndpoint + "/default-auth").text, IAM_AUTH_OUTPUT) | ||
self.assertEqual(requests.get(defaultIamEndpoint + "/iam-auth").text, IAM_AUTH_OUTPUT) | ||
|
||
iamEnabledEndpoint = self.get_api_v2_endpoint("MyIamAuthEnabledHttpApi") | ||
self.assertEqual(requests.get(iamEnabledEndpoint + "/default-auth").text, self.FUNCTION_OUTPUT) | ||
self.assertEqual(requests.get(iamEnabledEndpoint + "/iam-auth").text, IAM_AUTH_OUTPUT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.