mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(apigatewayv2): pin the checked address and cover WebSocket HTTP integrations (#3978)
Connect http:// hostname targets to the address that was checked instead of resolving the name a second time, and apply the link-local and metadata check to the WebSocket HTTP and HTTP_PROXY integrations. The pinned socket path now returns an empty body for HEAD, 204, 304 and 1xx responses.
This commit is contained in:
1 parent
2697868e86
commit
0e02d34c6b
5 files changed
+196
-18
No files matched your search
+50
@@ -8,16 +8,20 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
@@ -294,4 +298,50 @@ class HttpProxyInvokerTest {
|
||||
assertEquals(200, result.statusCode());
|
||||
assertNotNull(received.get(), "loopback backends must stay reachable");
|
||||
}
|
||||
|
||||
@Test
|
||||
void connectsToTheAddressItCheckedInsteadOfResolvingTheNameAgain() throws Exception {
|
||||
AtomicInteger lookups = new AtomicInteger();
|
||||
HttpProxyInvoker invoker = new HttpProxyInvoker(host -> {
|
||||
if (lookups.incrementAndGet() == 1) {
|
||||
return new InetAddress[] {InetAddress.getByName("127.0.0.1")};
|
||||
}
|
||||
return new InetAddress[] {InetAddress.getByName("169.254.169.254")};
|
||||
});
|
||||
Integration integration = httpProxyIntegration("http://rebind.example.test:" + backendPort + "/ok", null);
|
||||
RequestContext ctx = ctxFor("GET", "/wallet/x", "x", Map.of(), Map.of(), null, Map.of());
|
||||
|
||||
ProxyResult result = invoker.invoke(integration, ctx);
|
||||
|
||||
assertEquals(200, result.statusCode());
|
||||
assertEquals(1, lookups.get(), "the name must be resolved once, then the checked address used");
|
||||
assertEquals("rebind.example.test:" + backendPort, received.get().headers().getFirst("Host"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsANameThatResolvesToAMetadataAddress() {
|
||||
HttpProxyInvoker invoker = new HttpProxyInvoker(
|
||||
host -> new InetAddress[] {InetAddress.getByAddress(new byte[] {(byte) 169, (byte) 254, (byte) 169, (byte) 254})});
|
||||
Integration integration = httpProxyIntegration("http://metadata.example.test/latest/", null);
|
||||
RequestContext ctx = ctxFor("GET", "/wallet/x", "x", Map.of(), Map.of(), null, Map.of());
|
||||
|
||||
ProxyResult result = invoker.invoke(integration, ctx);
|
||||
|
||||
assertEquals(502, result.statusCode());
|
||||
assertTrue(new String(result.body(), StandardCharsets.UTF_8).contains("link-local or metadata address"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void headRequestToANamedHostDoesNotWaitForABody() {
|
||||
HttpProxyInvoker invoker = new HttpProxyInvoker(
|
||||
host -> new InetAddress[] {InetAddress.getByAddress(new byte[] {127, 0, 0, 1})});
|
||||
Integration integration = httpProxyIntegration("http://named.example.test:" + backendPort + "/ok", null);
|
||||
RequestContext ctx = ctxFor("HEAD", "/wallet/x", "x", Map.of(), Map.of(), null, Map.of());
|
||||
|
||||
ProxyResult result = assertTimeoutPreemptively(
|
||||
Duration.ofSeconds(5), () -> invoker.invoke(integration, ctx));
|
||||
|
||||
assertEquals(200, result.statusCode());
|
||||
assertEquals(0, result.body().length);
|
||||
}
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
package io.github.hectorvent.floci.services.apigatewayv2.websocket;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import io.github.hectorvent.floci.services.apigateway.AwsServiceRouter;
|
||||
import io.github.hectorvent.floci.services.apigateway.VtlTemplateEngine;
|
||||
import io.github.hectorvent.floci.services.apigatewayv2.model.Integration;
|
||||
import io.github.hectorvent.floci.services.apigatewayv2.websocket.WebSocketIntegrationInvoker.IntegrationResult;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
class WebSocketIntegrationInvokerHttpTargetTest {
|
||||
|
||||
private WebSocketIntegrationInvoker invoker;
|
||||
private HttpServer backend;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws IOException {
|
||||
invoker = new WebSocketIntegrationInvoker(
|
||||
mock(LambdaService.class),
|
||||
mock(AwsServiceRouter.class),
|
||||
new ObjectMapper(),
|
||||
mock(VtlTemplateEngine.class));
|
||||
backend = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
backend.createContext("/", exchange -> {
|
||||
byte[] body = "ok".getBytes(StandardCharsets.UTF_8);
|
||||
exchange.sendResponseHeaders(200, body.length);
|
||||
exchange.getResponseBody().write(body);
|
||||
exchange.close();
|
||||
});
|
||||
backend.start();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
backend.stop(0);
|
||||
}
|
||||
|
||||
private IntegrationResult invoke(String type, String uri) {
|
||||
Integration integration = new Integration();
|
||||
integration.setIntegrationType(type);
|
||||
integration.setIntegrationUri(uri);
|
||||
return invoker.invoke("us-east-1", integration, "{}", Map.of(), Map.of(), Map.of());
|
||||
}
|
||||
|
||||
@Test
|
||||
void httpProxyRejectsMetadataTarget() {
|
||||
IntegrationResult result = invoke("HTTP_PROXY", "http://169.254.169.254/latest/meta-data/");
|
||||
|
||||
assertEquals(500, result.statusCode());
|
||||
assertNotNull(result.functionError());
|
||||
assertTrue(result.functionError().contains("link-local or metadata address"), result.functionError());
|
||||
}
|
||||
|
||||
@Test
|
||||
void httpRejectsMetadataTarget() {
|
||||
IntegrationResult result = invoke("HTTP", "http://169.254.169.254/latest/meta-data/");
|
||||
|
||||
assertEquals(500, result.statusCode());
|
||||
assertNotNull(result.functionError());
|
||||
assertTrue(result.functionError().contains("link-local or metadata address"), result.functionError());
|
||||
}
|
||||
|
||||
@Test
|
||||
void httpProxyStillReachesLoopbackBackend() {
|
||||
IntegrationResult result = invoke("HTTP_PROXY", "http://127.0.0.1:" + backend.getAddress().getPort() + "/");
|
||||
|
||||
assertEquals(200, result.statusCode());
|
||||
assertEquals("ok", result.body());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user