feat(lambdaweb): set the WebFunctionAuthType condition keys on endpoint routes (#6893)

This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-10-29 00:54:49 -04:00
1 parent d7204696d4
commit 385ffcfdd0
14 files changed
+604 -12

No files matched your search

@@ -16,6 +16,8 @@ import io.github.hectorvent.floci.services.iam.IamPolicyEvaluator.Decision;
import io.github.hectorvent.floci.services.iam.IamService;
import io.github.hectorvent.floci.services.iam.model.IamRole;
import io.github.hectorvent.floci.services.iam.model.IamUser;
import io.github.hectorvent.floci.services.lambdaweb.LambdaWebService;
import io.github.hectorvent.floci.services.lambdaweb.model.WebFunctionEndpoint;
import io.github.hectorvent.floci.services.s3.S3Service;
import jakarta.enterprise.inject.Instance;
import jakarta.ws.rs.container.ContainerRequestContext;
@@ -64,6 +66,7 @@ class IamConditionContextResolverTest {
private S3Service s3Service;
private CloudWatchLogsService logsService;
private IamService iamService;
private LambdaWebService lambdaWebService;
private RequestContext requestContext;
private EmulatorConfig config;
private IamConditionContextResolver resolver;
@@ -94,6 +97,10 @@ class IamConditionContextResolverTest {
Instance<IamService> iamServiceInstance = mock(Instance.class);
when(iamServiceInstance.isResolvable()).thenReturn(true);
when(iamServiceInstance.get()).thenReturn(iamService);
lambdaWebService = mock(LambdaWebService.class);
Instance<LambdaWebService> lambdaWebServiceInstance = mock(Instance.class);
when(lambdaWebServiceInstance.isResolvable()).thenReturn(true);
when(lambdaWebServiceInstance.get()).thenReturn(lambdaWebService);
requestContext = new RequestContext();
requestContext.setAccountId("123456789012");
requestContext.setRegion("us-east-1");
@@ -108,10 +115,48 @@ class IamConditionContextResolverTest {
when(serviceCatalog.matchTarget(any())).thenAnswer(call -> catalog.matchTarget(call.getArgument(0)));
resolver = new IamConditionContextResolver(
dynamoDbFacadeInstance, ec2ServiceInstance, s3ServiceInstance, logsServiceInstance, iamServiceInstance,
lambdaWebServiceInstance,
requestContext, config,
null, serviceCatalog);
}
@Test
void aWebFunctionEndpointRequestCarriesTheAuthTypeItNamesAndTheOneItHas() throws Exception {
String function = "arn:aws:lambda:us-east-1:123456789012:web-function/app";
WebFunctionEndpoint stored = new WebFunctionEndpoint();
stored.authType = "ApplicationManaged";
when(lambdaWebService.getEndpoint("us-east-1", function, "dev")).thenReturn(stored);
when(lambdaWebService.getEndpoint("us-east-1", function, "gone"))
.thenThrow(new AwsException("ResourceNotFoundException", "The endpoint gone does not exist.", 404));
String request = "lambda:Request/WebFunctionAuthType";
String resource = "lambda:Resource/WebFunctionAuthType";
assertEquals(Map.of(request, List.of("IamAuth")), resolver.resolve("lambda", "lambda:CreateWebFunction",
webRequest("/2025-03-07/web-functions", "{\"endpointConfig\":{\"authType\":\"IamAuth\"}}")));
assertEquals(Map.of(request, List.of("IamAuth")), resolver.resolve("lambda",
"lambda:CreateWebFunctionEndpoint", webRequest("/2025-03-07/web-functions/app/endpoints",
"{\"authType\":\"IamAuth\"}")));
assertEquals(Map.of(request, List.of("IamAuth"), resource, List.of("ApplicationManaged")),
resolver.resolve("lambda", "lambda:UpdateWebFunctionEndpoint",
webRequest("/2025-03-07/web-functions/app/endpoints/dev", "{\"authType\":\"IamAuth\"}")));
assertEquals(Map.of(resource, List.of("ApplicationManaged")), resolver.resolve("lambda",
"lambda:UpdateWebFunctionEndpoint", webRequest("/2025-03-07/web-functions/app/endpoints/dev",
"{\"description\":\"d\"}")), "an update without authType names none");
for (String action : List.of("lambda:GetWebFunctionEndpoint", "lambda:DeleteWebFunctionEndpoint")) {
assertEquals(Map.of(resource, List.of("ApplicationManaged")), resolver.resolve("lambda", action,
webRequest("/2025-03-07/web-functions/" + function + "/endpoints/" + function + "/endpoint/dev",
"")));
assertNull(resolver.resolve("lambda", action,
webRequest("/2025-03-07/web-functions/app/endpoints/gone", "")), "a missing endpoint has none");
}
assertNull(resolver.resolve("lambda", "lambda:GetWebFunctionEndpoint",
webRequest("/2025-03-07/web-functions/arn:bad/endpoints/dev", "")), "a malformed ARN has none");
assertNull(resolver.resolve("lambda", "lambda:GetWebFunction",
webRequest("/2025-03-07/web-functions/app", "")));
assertNull(resolver.resolve("lambda", "lambda:ListWebFunctionEndpoints",
webRequest("/2025-03-07/web-functions/app/list-endpoints", "{}")));
}
@Test
void logStreamTagsUseRawParentNameAndRequestRegion() throws Exception {
ContainerRequestContext request = mock(ContainerRequestContext.class);
@@ -161,6 +206,16 @@ class IamConditionContextResolverTest {
return containerRequest;
}
private ContainerRequestContext webRequest(String path, String body) throws Exception {
ContainerRequestContext containerRequest = mock(ContainerRequestContext.class);
UriInfo uriInfo = mock(UriInfo.class);
when(containerRequest.getUriInfo()).thenReturn(uriInfo);
when(uriInfo.getPath()).thenReturn(path);
when(containerRequest.getProperty("floci.bufferedJsonBody"))
.thenReturn(body.isEmpty() ? null : mapper.readTree(body));
return containerRequest;
}
private static ContainerRequestContext s3Request(String path, String body) {
ContainerRequestContext containerRequest = mock(ContainerRequestContext.class);
UriInfo uriInfo = mock(UriInfo.class);
@@ -550,8 +605,8 @@ class IamConditionContextResolverTest {
@SuppressWarnings("unchecked")
private IamConditionContextResolver resolverAt(Instant now) {
return new IamConditionContextResolver(mock(Instance.class), mock(Instance.class), mock(Instance.class),
mock(Instance.class), mock(Instance.class), requestContext, config, null, serviceCatalog,
Clock.fixed(now, ZoneOffset.UTC));
mock(Instance.class), mock(Instance.class), mock(Instance.class), requestContext, config, null,
serviceCatalog, Clock.fixed(now, ZoneOffset.UTC));
}
@Test
@@ -152,7 +152,8 @@ class DynamoDbFacadeTest {
RequestContext requestContext = new RequestContext();
requestContext.setRegion(OTHER_REGION);
IamConditionContextResolver resolver = new IamConditionContextResolver(dynamoDb,
mock(Instance.class), mock(Instance.class), mock(Instance.class), mock(Instance.class), requestContext,
mock(Instance.class), mock(Instance.class), mock(Instance.class), mock(Instance.class), mock(Instance.class),
requestContext,
mock(EmulatorConfig.class),
null, mock(io.github.hectorvent.floci.core.common.ResolvedServiceCatalog.class));
ContainerRequestContext request = mock(ContainerRequestContext.class);
@@ -0,0 +1,131 @@
package io.github.hectorvent.floci.services.lambdaweb;
import io.github.hectorvent.floci.services.iam.IamService;
import io.github.hectorvent.floci.services.s3.S3Service;
import io.github.hectorvent.floci.testing.EnforcementFixtures;
import io.github.hectorvent.floci.testing.EnforcementFixtures.Session;
import io.github.hectorvent.floci.testing.IamEnforcementProfile;
import io.github.hectorvent.floci.testing.PartitionCleanup;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.specification.RequestSpecification;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import java.net.URI;
import java.util.Map;
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
/** Under IAM enforcement the endpoint routes carry the authType they name and the one the endpoint has. */
@QuarkusTest
@TestProfile(IamEnforcementProfile.class)
class LambdaWebAuthTypeConditionKeysIntegrationTest {
private static final String ACCOUNT = "000000000000";
private static final String ROLE = "arn:aws:iam::000000000000:role/web-auth-type";
private static final String BUCKET = "web-code-auth-type";
private static final String BASE = "/2025-03-07/web-functions";
private static final String BUILD = """
{"codeConfig": {"s3Object": {"bucket": "%s", "key": "app.zip"}}, "runtimeConfig": {"runtime": "nodejs24.x"}}
""".formatted(BUCKET);
private static final String POLICY = """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":["lambda:*","iam:PassRole"],"Resource":"*"},
{"Effect":"Deny","Action":["lambda:CreateWebFunction","lambda:CreateWebFunctionEndpoint"],"Resource":"*",
"Condition":{"StringEquals":{"lambda:Request/WebFunctionAuthType":"ApplicationManaged"}}},
{"Effect":"Deny","Action":["lambda:GetWebFunctionEndpoint","lambda:DeleteWebFunctionEndpoint"],
"Resource":"*",
"Condition":{"StringEquals":{"lambda:Resource/WebFunctionAuthType":"ApplicationManaged"}}},
{"Effect":"Deny","Action":"lambda:UpdateWebFunctionEndpoint","Resource":"*",
"Condition":{"Null":{"lambda:Request/WebFunctionAuthType":"true"}}}]}""";
@TestHTTPResource("/")
static URI baseUri;
@RegisterExtension
final PartitionCleanup cleanup = new PartitionCleanup();
@Inject
IamService iamService;
@Inject
S3Service s3Service;
@Test
void anEndpointRouteIsDeniedByTheAuthTypeItNamesOrHas() {
int port = baseUri.getPort();
String name = "auth-type-" + Long.toString(System.nanoTime(), 36);
LambdaWebRoles.trustLambda(iamService, ROLE);
LambdaWebCodeBuckets.create(s3Service, BUCKET);
s3Service.putObject(BUCKET, "app.zip", LambdaWebZips.indexJs(""), "application/zip", Map.of());
cleanup.register(() -> given().header("Authorization", auth(ACCOUNT, "lambda"))
.when().delete(BASE + "/" + name));
given().header("Authorization", auth(ACCOUNT, "lambda")).contentType("application/json")
.body(function(name, "ApplicationManaged"))
.when().put(BASE)
.then().statusCode(202);
String roleArn = EnforcementFixtures.createRole(cleanup, port, ACCOUNT, "/", name, POLICY);
Session session = EnforcementFixtures.assumeRole(port, ACCOUNT, roleArn);
String endpoints = BASE + "/" + name + "/endpoints";
signed(session).contentType("application/json").body(endpoint("am", "ApplicationManaged"))
.when().put(endpoints)
.then().statusCode(403)
.header("X-Amzn-Errortype", "AccessDeniedException");
signed(session).contentType("application/json").body(endpoint("iam", "IamAuth"))
.when().put(endpoints)
.then().statusCode(202);
signed(session).contentType("application/json").body(function(name + "-b", "ApplicationManaged"))
.when().put(BASE)
.then().statusCode(403)
.header("X-Amzn-Errortype", "AccessDeniedException");
signed(session).when().get(endpoints + "/dev")
.then().statusCode(403)
.header("X-Amzn-Errortype", "AccessDeniedException");
signed(session).when().get(endpoints + "/iam").then().statusCode(200);
signed(session).when().get(BASE + "/arn:bad/endpoints/dev")
.then().statusCode(400)
.header("X-Amzn-Errortype", "ValidationException");
signed(session).when().get(endpoints + "/missing")
.then().statusCode(404)
.body("message", equalTo("The endpoint missing does not exist."));
signed(session).contentType("application/json").body("{\"description\": \"d\"}")
.when().patch(endpoints + "/iam")
.then().statusCode(403)
.header("X-Amzn-Errortype", "AccessDeniedException");
signed(session).contentType("application/json").body("{\"authType\": \"IamAuth\"}")
.when().patch(endpoints + "/iam")
.then().statusCode(202);
signed(session).when().delete(endpoints + "/dev")
.then().statusCode(403)
.header("X-Amzn-Errortype", "AccessDeniedException");
signed(session).when().delete(endpoints + "/iam").then().statusCode(204);
}
private static String function(String name, String authType) {
return """
{"functionName": "%s",
"revisionConfig": {"buildConfig": %s, "serviceConfig": {"executionRoleArn": "%s"}},
"endpointConfig": %s}
""".formatted(name, BUILD, ROLE, endpoint("dev", authType));
}
private static String endpoint(String name, String authType) {
return """
{"endpointName": "%s", "endpointType": "HomeRegion", "authType": "%s",
"autoDeploymentMode": "LatestRevision"}""".formatted(name, authType);
}
private static RequestSpecification signed(Session session) {
return given().header("Authorization", auth(session.accessKeyId(), "lambda"))
.header("X-Amz-Security-Token", session.sessionToken());
}
}