Skip to content

fix(ci): add auth to API HTTP Gateway and Lambda Function Url #1882

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
merged 2 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pytest-xdist = "^3.1.0"
aws-cdk-lib = "^2.62.2"
"aws-cdk.aws-apigatewayv2-alpha" = "^2.38.1-alpha.0"
"aws-cdk.aws-apigatewayv2-integrations-alpha" = "^2.38.1-alpha.0"
"aws-cdk.aws-apigatewayv2-authorizers-alpha" = "^2.38.1-alpha.0"
pytest-benchmark = "^4.0.0"
python-snappy = "^0.6.1"
mypy-boto3-appconfig = "^1.26.0"
Expand All @@ -81,6 +82,7 @@ importlib-metadata = "^6.0"
ijson = "^3.2.0"
typed-ast = { version = "^1.5.4", python = "< 3.8"}
hvac = "^1.0.2"
aws-requests-auth = "^0.4.3"

[tool.poetry.extras]
parser = ["pydantic"]
Expand Down
10 changes: 8 additions & 2 deletions tests/e2e/event_handler/infrastructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from aws_cdk import CfnOutput
from aws_cdk import aws_apigateway as apigwv1
from aws_cdk import aws_apigatewayv2_alpha as apigwv2
from aws_cdk import aws_apigatewayv2_authorizers_alpha as apigwv2authorizers
from aws_cdk import aws_apigatewayv2_integrations_alpha as apigwv2integrations
from aws_cdk import aws_ec2 as ec2
from aws_cdk import aws_elasticloadbalancingv2 as elbv2
Expand Down Expand Up @@ -57,7 +58,12 @@ def _create_alb_listener(
CfnOutput(self.stack, f"ALB{name}ListenerPort", value=str(port))

def _create_api_gateway_http(self, function: Function):
apigw = apigwv2.HttpApi(self.stack, "APIGatewayHTTP", create_default_stage=True)
apigw = apigwv2.HttpApi(
self.stack,
"APIGatewayHTTP",
create_default_stage=True,
default_authorizer=apigwv2authorizers.HttpIamAuthorizer(),
)
apigw.add_routes(
path="/todos",
methods=[apigwv2.HttpMethod.POST],
Expand All @@ -76,5 +82,5 @@ def _create_api_gateway_rest(self, function: Function):

def _create_lambda_function_url(self, function: Function):
# Maintenance: move auth to IAM when we create sigv4 builders
function_url = function.add_function_url(auth_type=FunctionUrlAuthType.NONE)
function_url = function.add_function_url(auth_type=FunctionUrlAuthType.AWS_IAM)
CfnOutput(self.stack, "LambdaFunctionUrl", value=function_url.url)
3 changes: 3 additions & 0 deletions tests/e2e/event_handler/test_header_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from aws_lambda_powertools.shared.cookies import Cookie
from tests.e2e.utils import data_fetcher
from tests.e2e.utils.auth import build_iam_auth


@pytest.fixture
Expand Down Expand Up @@ -168,6 +169,7 @@ def test_api_gateway_http_headers_serializer(apigw_http_endpoint):
method="POST",
url=url,
json={"body": body, "status_code": status_code, "headers": headers, "cookies": list(map(str, cookies))},
auth=build_iam_auth(url=url, aws_service="execute-api"),
)
)

Expand Down Expand Up @@ -204,6 +206,7 @@ def test_lambda_function_url_headers_serializer(lambda_function_url_endpoint):
method="POST",
url=url,
json={"body": body, "status_code": status_code, "headers": headers, "cookies": list(map(str, cookies))},
auth=build_iam_auth(url=url, aws_service="lambda"),
)
)

Expand Down
5 changes: 5 additions & 0 deletions tests/e2e/event_handler/test_paths_ending_with_slash.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from requests import HTTPError, Request

from tests.e2e.utils import data_fetcher
from tests.e2e.utils.auth import build_iam_auth


@pytest.fixture
Expand Down Expand Up @@ -45,6 +46,7 @@ def test_api_gateway_rest_trailing_slash(apigw_rest_endpoint):
method="POST",
url=url,
json={"body": body},
auth=build_iam_auth(url=url, aws_service="lambda"),
)
)

Expand All @@ -65,6 +67,7 @@ def test_api_gateway_http_trailing_slash(apigw_http_endpoint):
method="POST",
url=url,
json={"body": body},
auth=build_iam_auth(url=url, aws_service="lambda"),
)
)

Expand All @@ -82,6 +85,7 @@ def test_lambda_function_url_trailing_slash(lambda_function_url_endpoint):
method="POST",
url=url,
json={"body": body},
auth=build_iam_auth(url=url, aws_service="lambda"),
)
)

Expand All @@ -99,5 +103,6 @@ def test_alb_url_trailing_slash(alb_multi_value_header_listener_endpoint):
method="POST",
url=url,
json={"body": body},
auth=build_iam_auth(url=url, aws_service="lambda"),
)
)
13 changes: 13 additions & 0 deletions tests/e2e/utils/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from urllib.parse import urlparse

import boto3
from aws_requests_auth.boto_utils import BotoAWSRequestsAuth


def build_iam_auth(url: str, aws_service: str) -> BotoAWSRequestsAuth:
"""Generates IAM auth keys for a given hostname and service.
This can be directly passed on to the requests library to authenticate the request.
"""
hostname = urlparse(url).hostname
region = boto3.Session().region_name
return BotoAWSRequestsAuth(aws_host=hostname, aws_region=region, aws_service=aws_service)