test: fix CloudFormationVirtualHostTests.java (#467)

This commit is contained in:
Conor Heine authored and GitHub committed 2026-04-15 15:35:38 -05:00
1 parent 25f360002e
commit 4d1f8b4907
1 file changed
+36 -10
@@ -4,36 +4,40 @@ import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.core.interceptor.Context;
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
import software.amazon.awssdk.http.SdkHttpRequest;
import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
import software.amazon.awssdk.services.cloudformation.model.ListStacksResponse;
import java.net.URI;
import static org.assertj.core.api.Assertions.*;
/**
* Ensures that requests to non-S3 service hostnames (e.g. cloudformation.amazonaws.com)
* are not incorrectly hijacked by the S3 virtual host filter.
*
* <p>The TCP connection lands on the configured Floci endpoint; an execution
* interceptor overrides the Host header so Floci sees the request as though it
* came from {@code cloudformation.us-east-1.amazonaws.com}. This decouples the
* test from DNS setup so it works equally against localhost, a docker service
* name (e.g. {@code http://floci:4566}), or a remote Floci deployment.
*/
@DisplayName("CloudFormation Virtual Host")
class CloudFormationVirtualHostTests {
private static final String VIRTUAL_HOST = "cloudformation.us-east-1.amazonaws.com";
private static CloudFormationClient cfn;
@BeforeAll
static void setup() {
// We override the endpoint to simulate a virtual host style request for CloudFormation
URI endpoint = TestFixtures.endpoint();
URI cfnEndpoint = URI.create("http://cloudformation.us-east-1.amazonaws.com:" + endpoint.getPort());
if (endpoint.getHost().equals("localhost")) {
cfnEndpoint = URI.create("http://cloudformation.localhost:" + endpoint.getPort());
}
cfn = CloudFormationClient.builder()
.endpointOverride(cfnEndpoint)
.endpointOverride(TestFixtures.endpoint())
.region(software.amazon.awssdk.regions.Region.US_EAST_1)
.credentialsProvider(software.amazon.awssdk.auth.credentials.StaticCredentialsProvider.create(
software.amazon.awssdk.auth.credentials.AwsBasicCredentials.create("test", "test")))
.overrideConfiguration(c -> c.addExecutionInterceptor(new HostHeaderSpoofInterceptor(VIRTUAL_HOST)))
.build();
}
@@ -50,4 +54,26 @@ class CloudFormationVirtualHostTests {
ListStacksResponse resp = cfn.listStacks();
assertThat(resp.sdkHttpResponse().isSuccessful()).isTrue();
}
/**
* Rewrites the Host header on outbound requests so Floci sees the incoming
* request as virtual-hosted under {@code hostHeader}, while the underlying
* TCP connection still goes to the endpoint configured via
* {@code endpointOverride}.
*/
private static final class HostHeaderSpoofInterceptor implements ExecutionInterceptor {
private final String hostHeader;
HostHeaderSpoofInterceptor(String hostHeader) {
this.hostHeader = hostHeader;
}
@Override
public SdkHttpRequest modifyHttpRequest(Context.ModifyHttpRequest context,
ExecutionAttributes executionAttributes) {
return context.httpRequest().toBuilder()
.putHeader("Host", hostHeader)
.build();
}
}
}