Skip to content

Commit 971f095

Browse files
authored
fix(ci): add auth to API HTTP Gateway and Lambda Function Url (#1882)
1 parent bc3d046 commit 971f095

File tree

6 files changed

+67
-3
lines changed

6 files changed

+67
-3
lines changed

poetry.lock

Lines changed: 36 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pytest-xdist = "^3.1.0"
5959
aws-cdk-lib = "^2.62.2"
6060
"aws-cdk.aws-apigatewayv2-alpha" = "^2.38.1-alpha.0"
6161
"aws-cdk.aws-apigatewayv2-integrations-alpha" = "^2.38.1-alpha.0"
62+
"aws-cdk.aws-apigatewayv2-authorizers-alpha" = "^2.38.1-alpha.0"
6263
pytest-benchmark = "^4.0.0"
6364
python-snappy = "^0.6.1"
6465
mypy-boto3-appconfig = "^1.26.0"
@@ -81,6 +82,7 @@ importlib-metadata = "^6.0"
8182
ijson = "^3.2.0"
8283
typed-ast = { version = "^1.5.4", python = "< 3.8"}
8384
hvac = "^1.0.2"
85+
aws-requests-auth = "^0.4.3"
8486

8587
[tool.poetry.extras]
8688
parser = ["pydantic"]

tests/e2e/event_handler/infrastructure.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from aws_cdk import CfnOutput
44
from aws_cdk import aws_apigateway as apigwv1
55
from aws_cdk import aws_apigatewayv2_alpha as apigwv2
6+
from aws_cdk import aws_apigatewayv2_authorizers_alpha as apigwv2authorizers
67
from aws_cdk import aws_apigatewayv2_integrations_alpha as apigwv2integrations
78
from aws_cdk import aws_ec2 as ec2
89
from aws_cdk import aws_elasticloadbalancingv2 as elbv2
@@ -57,7 +58,12 @@ def _create_alb_listener(
5758
CfnOutput(self.stack, f"ALB{name}ListenerPort", value=str(port))
5859

5960
def _create_api_gateway_http(self, function: Function):
60-
apigw = apigwv2.HttpApi(self.stack, "APIGatewayHTTP", create_default_stage=True)
61+
apigw = apigwv2.HttpApi(
62+
self.stack,
63+
"APIGatewayHTTP",
64+
create_default_stage=True,
65+
default_authorizer=apigwv2authorizers.HttpIamAuthorizer(),
66+
)
6167
apigw.add_routes(
6268
path="/todos",
6369
methods=[apigwv2.HttpMethod.POST],
@@ -76,5 +82,5 @@ def _create_api_gateway_rest(self, function: Function):
7682

7783
def _create_lambda_function_url(self, function: Function):
7884
# Maintenance: move auth to IAM when we create sigv4 builders
79-
function_url = function.add_function_url(auth_type=FunctionUrlAuthType.NONE)
85+
function_url = function.add_function_url(auth_type=FunctionUrlAuthType.AWS_IAM)
8086
CfnOutput(self.stack, "LambdaFunctionUrl", value=function_url.url)

tests/e2e/event_handler/test_header_serializer.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from aws_lambda_powertools.shared.cookies import Cookie
77
from tests.e2e.utils import data_fetcher
8+
from tests.e2e.utils.auth import build_iam_auth
89

910

1011
@pytest.fixture
@@ -168,6 +169,7 @@ def test_api_gateway_http_headers_serializer(apigw_http_endpoint):
168169
method="POST",
169170
url=url,
170171
json={"body": body, "status_code": status_code, "headers": headers, "cookies": list(map(str, cookies))},
172+
auth=build_iam_auth(url=url, aws_service="execute-api"),
171173
)
172174
)
173175

@@ -204,6 +206,7 @@ def test_lambda_function_url_headers_serializer(lambda_function_url_endpoint):
204206
method="POST",
205207
url=url,
206208
json={"body": body, "status_code": status_code, "headers": headers, "cookies": list(map(str, cookies))},
209+
auth=build_iam_auth(url=url, aws_service="lambda"),
207210
)
208211
)
209212

tests/e2e/event_handler/test_paths_ending_with_slash.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from requests import HTTPError, Request
33

44
from tests.e2e.utils import data_fetcher
5+
from tests.e2e.utils.auth import build_iam_auth
56

67

78
@pytest.fixture
@@ -45,6 +46,7 @@ def test_api_gateway_rest_trailing_slash(apigw_rest_endpoint):
4546
method="POST",
4647
url=url,
4748
json={"body": body},
49+
auth=build_iam_auth(url=url, aws_service="lambda"),
4850
)
4951
)
5052

@@ -65,6 +67,7 @@ def test_api_gateway_http_trailing_slash(apigw_http_endpoint):
6567
method="POST",
6668
url=url,
6769
json={"body": body},
70+
auth=build_iam_auth(url=url, aws_service="lambda"),
6871
)
6972
)
7073

@@ -82,6 +85,7 @@ def test_lambda_function_url_trailing_slash(lambda_function_url_endpoint):
8285
method="POST",
8386
url=url,
8487
json={"body": body},
88+
auth=build_iam_auth(url=url, aws_service="lambda"),
8589
)
8690
)
8791

@@ -99,5 +103,6 @@ def test_alb_url_trailing_slash(alb_multi_value_header_listener_endpoint):
99103
method="POST",
100104
url=url,
101105
json={"body": body},
106+
auth=build_iam_auth(url=url, aws_service="lambda"),
102107
)
103108
)

tests/e2e/utils/auth.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from urllib.parse import urlparse
2+
3+
import boto3
4+
from aws_requests_auth.boto_utils import BotoAWSRequestsAuth
5+
6+
7+
def build_iam_auth(url: str, aws_service: str) -> BotoAWSRequestsAuth:
8+
"""Generates IAM auth keys for a given hostname and service.
9+
This can be directly passed on to the requests library to authenticate the request.
10+
"""
11+
hostname = urlparse(url).hostname
12+
region = boto3.Session().region_name
13+
return BotoAWSRequestsAuth(aws_host=hostname, aws_region=region, aws_service=aws_service)

0 commit comments

Comments
 (0)