-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat: Add a built-in AWS_IAM authorizer for HTTP APIs #1924
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jfuss
merged 1 commit into
aws:develop
from
harrisonhjones:feature-iam-authorizer-http-api-redux-2
Apr 13, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.