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

@@ -48,6 +48,7 @@ import io.floci.aws.services.apigatewayv2.model.Route;
import io.floci.aws.services.apigatewayv2.proxy.AwsServiceIntegrationMapper;
import io.floci.aws.services.apigatewayv2.proxy.CloudMapInstanceResolver;
import io.floci.aws.services.apigatewayv2.proxy.HttpApiForwardedHeaders;
import io.floci.aws.services.apigatewayv2.proxy.HttpProxyInvoker;
import io.floci.aws.services.apigatewayv2.websocket.ConnectionInfo;
import io.floci.aws.services.apigatewayv2.websocket.WebSocketConnectionManager;
import io.floci.aws.services.elbv2.ElbV2Service;
@@ -146,6 +147,7 @@ public class ApiGatewayExecuteController {
private final AuthorizerPolicyEvaluator authorizerPolicyEvaluator;
private final CloudMapInstanceResolver cloudMapInstanceResolver;
private final AwsServiceIntegrationMapper awsServiceIntegrationMapper;
private final HttpProxyInvoker httpProxyInvoker;
@Inject
public ApiGatewayExecuteController(ApiGatewayService apiGatewayService, CognitoUserPoolAuthorizer cognitoAuthorizer,
@@ -161,7 +163,8 @@ public class ApiGatewayExecuteController {
RequestContext requestContext,
ExecuteApiSigV4Authorizer sigV4Authorizer, RestLambdaAuthorizer restLambdaAuthorizer,
AuthorizerPolicyEvaluator authorizerPolicyEvaluator,
CloudMapInstanceResolver cloudMapInstanceResolver) {
CloudMapInstanceResolver cloudMapInstanceResolver,
HttpProxyInvoker httpProxyInvoker) {
this.apiGatewayService = apiGatewayService;
this.cognitoAuthorizer = cognitoAuthorizer;
this.apiGatewayV2Service = apiGatewayV2Service;
@@ -185,6 +188,7 @@ public class ApiGatewayExecuteController {
this.restLambdaAuthorizer = restLambdaAuthorizer;
this.authorizerPolicyEvaluator = authorizerPolicyEvaluator;
this.cloudMapInstanceResolver = cloudMapInstanceResolver;
this.httpProxyInvoker = httpProxyInvoker;
}
/** Matches an ELBv2 listener ARN (ALB {@code app/} or NLB {@code net/}); group 1 = region. */
@@ -3062,9 +3066,6 @@ public class ApiGatewayExecuteController {
.type(MediaType.APPLICATION_JSON).build();
}
private final io.floci.aws.services.apigatewayv2.proxy.HttpProxyInvoker httpProxyInvoker =
new io.floci.aws.services.apigatewayv2.proxy.HttpProxyInvoker();
/** REST integrations default to AWS's 29s and may opt out of backend TLS verification. */
private static io.floci.aws.services.apigatewayv2.proxy.HttpProxyInvoker.ProxyOptions
proxyOptions(Integration integration) {
@@ -1,7 +1,11 @@
package io.floci.aws.services.apigatewayv2.proxy;
import io.floci.aws.config.ContainerCaBundle;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.SsrfProtection;
import io.floci.aws.services.apigatewayv2.model.Integration;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import javax.net.ssl.SSLContext;
@@ -48,6 +52,7 @@ import java.util.StringJoiner;
* <p>If the backend is unreachable or times out, returns a 502 Bad Gateway
* ProxyResult so the controller can relay a clean error to the original client.
*/
@ApplicationScoped
public class HttpProxyInvoker {
private static final Logger LOG = Logger.getLogger(HttpProxyInvoker.class);
@@ -74,14 +79,7 @@ public class HttpProxyInvoker {
public static final ProxyOptions DEFAULTS = new ProxyOptions(Duration.ofSeconds(30), false);
}
// Pin to HTTP/1.1: the default HTTP_2 setting attempts cleartext-HTTP/2 negotiation
// against http:// backends, which hangs against plain HTTP/1.1 servers (notably the
// in-JVM Vertx HttpServer used by ELBv2 listeners for HttpAlbIntegration).
private final HttpClient client = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.connectTimeout(Duration.ofSeconds(10))
.followRedirects(HttpClient.Redirect.NEVER)
.build();
private final HttpClient client;
/** Built on first use: an integration opting out of TLS verification is the exception. */
private volatile HttpClient insecureClient;
@@ -276,12 +274,38 @@ public class HttpProxyInvoker {
private final HostResolver resolver;
@Inject
public HttpProxyInvoker(EmulatorConfig config) {
this(InetAddress::getAllByName, ContainerCaBundle.sslContext(config).orElse(null));
}
public HttpProxyInvoker() {
this(InetAddress::getAllByName);
}
HttpProxyInvoker(HostResolver resolver) {
this(resolver, null);
}
private HttpProxyInvoker(HostResolver resolver, SSLContext sslContext) {
this.resolver = resolver;
this.client = proxyClient(sslContext);
}
// Pin to HTTP/1.1: the default HTTP_2 setting attempts cleartext-HTTP/2 negotiation
// against http:// backends, which hangs against plain HTTP/1.1 servers (notably the
// in-JVM Vertx HttpServer used by ELBv2 listeners for HttpAlbIntegration). sslContext,
// when given, adds the extra CAs in floci.tls.extra-ca-path, and Floci's CA when TLS is
// on, to the JVM's roots.
private static HttpClient proxyClient(SSLContext sslContext) {
HttpClient.Builder builder = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.connectTimeout(Duration.ofSeconds(10))
.followRedirects(HttpClient.Redirect.NEVER);
if (sslContext != null) {
builder.sslContext(sslContext);
}
return builder.build();
}
public ProxyResult invoke(Integration integration, RequestContext ctx) {
@@ -2,6 +2,8 @@ package io.floci.aws.services.apigatewayv2.websocket;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.config.ContainerCaBundle;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.SsrfProtection;
import io.floci.aws.services.apigateway.AwsServiceRouter;
import io.floci.aws.services.apigateway.VtlTemplateEngine;
@@ -16,6 +18,7 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URI;
@@ -59,6 +62,13 @@ public class WebSocketIntegrationInvoker {
private final AddressResolver addressResolver;
@Inject
public WebSocketIntegrationInvoker(LambdaService lambdaService, AwsServiceRouter serviceRouter,
ObjectMapper objectMapper, VtlTemplateEngine vtlEngine,
EmulatorConfig config) {
this(lambdaService, serviceRouter, objectMapper, vtlEngine, InetAddress::getAllByName,
ContainerCaBundle.sslContext(config).orElse(null));
}
public WebSocketIntegrationInvoker(LambdaService lambdaService, AwsServiceRouter serviceRouter,
ObjectMapper objectMapper, VtlTemplateEngine vtlEngine) {
this(lambdaService, serviceRouter, objectMapper, vtlEngine, InetAddress::getAllByName);
@@ -67,14 +77,31 @@ public class WebSocketIntegrationInvoker {
WebSocketIntegrationInvoker(LambdaService lambdaService, AwsServiceRouter serviceRouter,
ObjectMapper objectMapper, VtlTemplateEngine vtlEngine,
AddressResolver addressResolver) {
this(lambdaService, serviceRouter, objectMapper, vtlEngine, addressResolver, null);
}
private WebSocketIntegrationInvoker(LambdaService lambdaService, AwsServiceRouter serviceRouter,
ObjectMapper objectMapper, VtlTemplateEngine vtlEngine,
AddressResolver addressResolver, SSLContext sslContext) {
this.lambdaService = lambdaService;
this.serviceRouter = serviceRouter;
this.objectMapper = objectMapper;
this.vtlEngine = vtlEngine;
this.httpClient = HttpClient.newHttpClient();
this.httpClient = backendClient(sslContext);
this.addressResolver = addressResolver;
}
// The client HTTP and HTTP_PROXY integrations reach https:// backends with. sslContext, when
// given, adds the extra CAs in floci.tls.extra-ca-path, and Floci's CA when TLS is on, to the
// JVM's roots.
private static HttpClient backendClient(SSLContext sslContext) {
HttpClient.Builder builder = HttpClient.newBuilder();
if (sslContext != null) {
builder.sslContext(sslContext);
}
return builder.build();
}
@FunctionalInterface
interface AddressResolver {
InetAddress[] resolve(String host) throws IOException;
@@ -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());
}
}