feat(apigateway): trust FLOCI_TLS_EXTRA_CA_PATH for HTTP and HTTP_PROXY integrations (#8285)

This commit is contained in:
Saim Zaib authored and GitHub committed 2026-11-14 10:45:45 -04:00
1 parent fdb2d7fe44
commit b2bdb2bbe9
20 files changed
+263 -33

No files matched your search

@@ -8,6 +8,7 @@ import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.RequestContext;
import io.floci.aws.services.apigatewayv2.ApiGatewayV2Service;
import io.floci.aws.services.apigatewayv2.model.Api;
import io.floci.aws.services.apigatewayv2.proxy.HttpProxyInvoker;
import io.floci.aws.services.lambda.model.InvokeResult;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
@@ -59,7 +60,7 @@ class ApiGatewayExecuteControllerTest {
return new ApiGatewayExecuteController(
null, null, null, null,
regionResolver, objectMapper, null,
null, null, null, null, routeContext, null, null, null, null, null, null);
null, null, null, null, routeContext, null, null, null, null, null, null, new HttpProxyInvoker());
}
@ParameterizedTest
@@ -490,7 +491,7 @@ class ApiGatewayExecuteControllerTest {
ApiGatewayExecuteController controller = new ApiGatewayExecuteController(
apiGatewayService, null, apiGatewayV2Service, null,
regionResolver, new ObjectMapper(), null,
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null, null, null, null, null);
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null, null, null, null, null, new HttpProxyInvoker());
Response response = controller.dispatch("GET", "abc123", "prod", "hello", headers, null, null);
@@ -522,7 +523,7 @@ class ApiGatewayExecuteControllerTest {
ApiGatewayExecuteController controller = new ApiGatewayExecuteController(
apiGatewayService, null, apiGatewayV2Service, null,
regionResolver, new ObjectMapper(), null,
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, requestContext, null, null, null, null);
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, requestContext, null, null, null, null, new HttpProxyInvoker());
controller.dispatch("GET", "abc123", "prod", "hello", headers, null, null);
@@ -557,7 +558,7 @@ class ApiGatewayExecuteControllerTest {
ApiGatewayExecuteController controller = new ApiGatewayExecuteController(
apiGatewayService, null, apiGatewayV2Service, null,
regionResolver, new ObjectMapper(), null,
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null, null, null, null, null);
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null, null, null, null, null, new HttpProxyInvoker());
controller.dispatch("GET", "restapi1", "prod", "hello", headers, null, null);
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.apigateway.model.ApiGatewayResource;
import io.floci.aws.services.apigatewayv2.ApiGatewayV2Service;
import io.floci.aws.services.apigatewayv2.proxy.HttpProxyInvoker;
import io.floci.aws.services.apigatewayv2.websocket.WebSocketConnectionManager;
import io.floci.aws.services.elbv2.ElbV2Service;
import io.floci.aws.services.lambda.LambdaService;
@@ -36,7 +37,7 @@ class ApiGatewayProxyMatchTest {
ctrl = new ApiGatewayExecuteController(apiGatewayService, null, apiGatewayV2Service, lambdaService,
new RegionResolver("us-east-1", "000000000000"),
new ObjectMapper(), vtlEngine, serviceRouter, webSocketConnectionManager, elbV2Service, null,
new ApiGatewayExecuteRouteContext(), null, null, null, null, null, null);
new ApiGatewayExecuteRouteContext(), null, null, null, null, null, null, new HttpProxyInvoker());
}
private ApiGatewayResource resource(String id, String parentId, String pathPart, String path) {
@@ -3,6 +3,7 @@ package io.floci.aws.services.apigateway;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.apigatewayv2.proxy.HttpProxyInvoker;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.UriInfo;
@@ -48,7 +49,7 @@ class BuildV2AuthorizerEventTrailingSlashTest {
regionResolver, new ObjectMapper(), null,
null, null, null, null, new ApiGatewayExecuteRouteContext(), null,
null, null
, null, null, null);
, null, null, null, new HttpProxyInvoker());
}
private JsonNode buildEvent(String normalizedPath, String rawRequestUri) throws Exception {
@@ -3,6 +3,7 @@ package io.floci.aws.services.apigateway;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.apigatewayv2.proxy.HttpProxyInvoker;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.UriInfo;
@@ -51,7 +52,7 @@ class BuildV2ProxyEventBodyEncodingTest {
null, null, null, null,
regionResolver, objectMapper, null,
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null, null
, null, null, null);
, null, null, null, new HttpProxyInvoker());
}
@ParameterizedTest(name = "{0}")
@@ -3,6 +3,7 @@ package io.floci.aws.services.apigateway;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.apigatewayv2.proxy.HttpProxyInvoker;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.UriInfo;
@@ -47,7 +48,7 @@ class BuildV2ProxyEventJwtClaimsTest {
null, null, null, null,
regionResolver, new ObjectMapper(), null,
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null, null
, null, null, null);
, null, null, null, new HttpProxyInvoker());
}
@Test
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.apigatewayv2.proxy.HttpProxyInvoker;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.UriInfo;
@@ -51,7 +52,7 @@ class BuildV2ProxyEventLambdaAuthorizerTest {
null, null, null, null,
regionResolver, MAPPER, null,
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null, null
, null, null, null);
, null, null, null, new HttpProxyInvoker());
}
@Test
@@ -3,6 +3,7 @@ package io.floci.aws.services.apigateway;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.apigatewayv2.proxy.HttpProxyInvoker;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
@@ -54,7 +55,7 @@ class BuildV2ProxyEventMultiValueTest {
controller = new ApiGatewayExecuteController(
null, null, null, null,
regionResolver, new ObjectMapper(), null,
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null, null, null, null, null);
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null, null, null, null, null, new HttpProxyInvoker());
}
enum Builder { INTEGRATION, AUTHORIZER }
@@ -3,6 +3,7 @@ package io.floci.aws.services.apigateway;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.apigatewayv2.proxy.HttpProxyInvoker;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.UriInfo;
@@ -42,7 +43,7 @@ class BuildV2ProxyEventPathParametersTest {
null, null, null, null,
regionResolver, new ObjectMapper(), null,
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null, null
, null, null, null);
, null, null, null, new HttpProxyInvoker());
}
@Test
@@ -3,6 +3,7 @@ package io.floci.aws.services.apigateway;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.apigatewayv2.proxy.HttpProxyInvoker;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.UriInfo;
@@ -42,7 +43,7 @@ class BuildV2ProxyEventStageVariablesTest {
controller = new ApiGatewayExecuteController(
null, null, null, null,
regionResolver, MAPPER, null,
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null, null, null, null, null);
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null, null, null, null, null, new HttpProxyInvoker());
}
@Test
@@ -3,6 +3,7 @@ package io.floci.aws.services.apigateway;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.apigatewayv2.proxy.HttpProxyInvoker;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.UriInfo;
@@ -47,7 +48,7 @@ class BuildV2ProxyEventTrailingSlashTest {
regionResolver, new ObjectMapper(), null,
null, null, null, null, new ApiGatewayExecuteRouteContext(), null,
null, null
, null, null, null);
, null, null, null, new HttpProxyInvoker());
}
private JsonNode buildEvent(String normalizedPath, String rawRequestUri, String routeKey) throws Exception {
@@ -9,6 +9,7 @@ import io.floci.aws.services.apigatewayv2.model.Api;
import io.floci.aws.services.apigatewayv2.model.Authorizer;
import io.floci.aws.services.apigatewayv2.model.Integration;
import io.floci.aws.services.apigatewayv2.model.Route;
import io.floci.aws.services.apigatewayv2.proxy.HttpProxyInvoker;
import io.floci.aws.services.iam.IamPolicyEvaluator;
import io.floci.aws.services.lambda.LambdaService;
import io.floci.aws.services.lambda.model.InvocationType;
@@ -182,7 +183,7 @@ class HttpApiAuthorizerPolicyTest {
routeContext.routeToHttpApi("us-east-1");
return new ApiGatewayExecuteController(null, null, gateway, lambda,
new RegionResolver("us-east-1", "000000000000"), mapper, null, null, null, null, null,
routeContext, null, null, null, null, new AuthorizerPolicyEvaluator(new IamPolicyEvaluator(mapper)), null);
routeContext, null, null, null, null, new AuthorizerPolicyEvaluator(new IamPolicyEvaluator(mapper)), null, new HttpProxyInvoker());
}
private InvokeResult result(String payload) {
@@ -2,6 +2,7 @@ package io.floci.aws.services.apigateway;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.Base64Url;
import io.floci.aws.services.apigatewayv2.proxy.HttpProxyInvoker;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -29,7 +30,7 @@ class JwtClaimsWireFormatTest {
null, new ObjectMapper(), null,
null, null, null, null, new ApiGatewayExecuteRouteContext(), null,
null, null
, null, null, null);
, null, null, null, new HttpProxyInvoker());
}
private static String unsignedToken(String claimsJson) {
@@ -3,6 +3,7 @@ package io.floci.aws.services.apigateway;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.apigatewayv2.proxy.HttpProxyInvoker;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
@@ -63,7 +64,7 @@ class ProxyEventIamIdentityTest {
controller = new ApiGatewayExecuteController(
null, null, null, null,
regionResolver, MAPPER, null,
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null, null, null, null, null);
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null, null, null, null, null, new HttpProxyInvoker());
}
@Test
@@ -3,6 +3,7 @@ package io.floci.aws.services.apigateway;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.apigatewayv2.proxy.HttpProxyInvoker;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.UriInfo;
@@ -50,7 +51,7 @@ class ProxyEventPathParametersTest {
controller = new ApiGatewayExecuteController(
null, null, null, null,
regionResolver, MAPPER, null,
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null, null, null, null, null);
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null, null, null, null, null, new HttpProxyInvoker());
}
private JsonNode pathParametersFor(String path, String resourcePath) throws Exception {
@@ -0,0 +1,164 @@
package io.floci.aws.services.apigatewayv2;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.net.httpserver.HttpsServer;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.config.FlociCertificateAuthority;
import io.floci.aws.services.apigateway.AwsServiceRouter;
import io.floci.aws.services.apigateway.VtlTemplateEngine;
import io.floci.aws.services.apigatewayv2.model.Integration;
import io.floci.aws.services.apigatewayv2.proxy.HttpProxyInvoker;
import io.floci.aws.services.apigatewayv2.proxy.HttpProxyInvoker.ProxyOptions;
import io.floci.aws.services.apigatewayv2.proxy.ProxyResult;
import io.floci.aws.services.apigatewayv2.proxy.RequestContext;
import io.floci.aws.services.apigatewayv2.websocket.WebSocketIntegrationInvoker;
import io.floci.aws.services.apigatewayv2.websocket.WebSocketIntegrationInvoker.IntegrationResult;
import io.floci.aws.services.lambda.LambdaService;
import io.floci.aws.testing.LocalHttpsServer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import static io.floci.aws.testing.LocalHttpsServer.hookUrl;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* HTTP and HTTP_PROXY integrations reach an https:// backend through Floci's outbound trust, so a certificate from
* a CA in floci.tls.extra-ca-path is accepted, with TLS off.
*/
class ApiGatewayHttpsBackendTrustTest {
@TempDir
Path dir;
private FlociCertificateAuthority privateCa;
private HttpsServer backend;
private final AtomicInteger hits = new AtomicInteger();
@BeforeEach
void startBackend() throws Exception {
privateCa = FlociCertificateAuthority.loadOrCreate(dir.resolve("private-ca"));
backend = LocalHttpsServer.hook(privateCa, exchange -> hits.incrementAndGet());
}
@AfterEach
void stopBackend() {
backend.stop(0);
}
/**
* REST HTTP and HTTP_PROXY integrations and HTTP API HTTP_PROXY integrations call the backend through
* HttpProxyInvoker. A backend whose certificate chains to a private CA answers 502 while only the JVM's roots
* are trusted, and is reached once that CA is in the floci.tls.extra-ca-path file.
*/
@Test
void httpProxyReachesABackendWhoseCertificateChainsToAnExtraCa() {
assertProxyRefused(Optional.empty());
assertEquals(200, proxy(privateCaPath(), ProxyOptions.DEFAULTS).statusCode());
assertEquals(1, hits.get());
}
@Test
void httpProxyRefusesABackendWhoseCaIsNotInTheExtraCaFile() {
assertProxyRefused(unrelatedCaPath());
}
/** insecureSkipVerification keeps its own client: a backend whose CA is not in the extra CA file is still reached. */
@Test
void insecureSkipVerificationStillReachesABackendOutsideTheExtraCas() {
ProxyResult result = proxy(unrelatedCaPath(), new ProxyOptions(Duration.ofSeconds(30), true));
assertEquals(200, result.statusCode());
assertEquals(1, hits.get());
}
/** WebSocket API HTTP and HTTP_PROXY integrations post to an https:// backend through their own client. */
@ParameterizedTest
@ValueSource(strings = {"HTTP", "HTTP_PROXY"})
void webSocketIntegrationReachesABackendWhoseCertificateChainsToAnExtraCa(String integrationType) {
assertWebSocketRefused(integrationType, Optional.empty());
assertEquals(200, webSocket(integrationType, privateCaPath()).statusCode());
assertEquals(1, hits.get());
}
@ParameterizedTest
@ValueSource(strings = {"HTTP", "HTTP_PROXY"})
void webSocketIntegrationRefusesABackendWhoseCaIsNotInTheExtraCaFile(String integrationType) {
assertWebSocketRefused(integrationType, unrelatedCaPath());
}
// Asserts that the TLS handshake with the backend fails, so the integration answers 502 and the backend never
// sees a request.
private void assertProxyRefused(Optional<String> extraCaPath) {
ProxyResult result = proxy(extraCaPath, ProxyOptions.DEFAULTS);
String body = new String(result.body(), StandardCharsets.UTF_8);
assertEquals(502, result.statusCode(), body);
assertTrue(body.contains("PKIX path"), body);
assertEquals(0, hits.get());
}
// As above, for a WebSocket integration, which reports the failure as a 500 naming its type.
private void assertWebSocketRefused(String integrationType, Optional<String> extraCaPath) {
IntegrationResult result = webSocket(integrationType, extraCaPath);
assertEquals(500, result.statusCode(), result.functionError());
assertTrue(result.functionError().startsWith(integrationType + " integration error: ")
&& result.functionError().contains("PKIX path"), result.functionError());
assertEquals(0, hits.get());
}
// Calls the backend as an HTTP_PROXY integration, through an invoker built the way CDI builds it.
private ProxyResult proxy(Optional<String> extraCaPath, ProxyOptions options) {
Integration integration = integration("HTTP_PROXY");
integration.setIntegrationMethod("GET");
RequestContext ctx = new RequestContext("api1", "$default", "GET", "/hook", null, "GET /hook",
"req-test", "127.0.0.1", Map.of(), Map.of(), Map.of(), null, Map.of(), Map.of());
return new HttpProxyInvoker(config(extraCaPath)).invoke(integration, ctx, options);
}
// Calls the backend as a WebSocket integration of integrationType, through an invoker built the way CDI builds it.
private IntegrationResult webSocket(String integrationType, Optional<String> extraCaPath) {
WebSocketIntegrationInvoker invoker = new WebSocketIntegrationInvoker(mock(LambdaService.class),
mock(AwsServiceRouter.class), new ObjectMapper(), mock(VtlTemplateEngine.class), config(extraCaPath));
return invoker.invoke("us-east-1", integration(integrationType), "{}", Map.of(), Map.of(), Map.of());
}
private Integration integration(String integrationType) {
Integration integration = new Integration();
integration.setIntegrationType(integrationType);
integration.setIntegrationUri(hookUrl(backend));
return integration;
}
// TLS off, with extraCaPath as the extra CA setting.
private static EmulatorConfig config(Optional<String> extraCaPath) {
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
when(config.tls().enabled()).thenReturn(false);
when(config.tls().extraCaPath()).thenReturn(extraCaPath);
return config;
}
private Optional<String> privateCaPath() {
return Optional.of(privateCa.certificatePath().toString());
}
private Optional<String> unrelatedCaPath() {
return Optional.of(FlociCertificateAuthority.loadOrCreate(dir.resolve("unrelated-ca"))
.certificatePath().toString());
}
}