feat(cloudfront): forward origin custom headers (#1832)

* feat(cloudfront): serve distribution requests from S3 and custom origins

* feat(cloudfront): forward origin custom headers

* fix(cloudfront): omit Allow-Credentials and Vary for wildcard S3 CORS rules

withS3CorsHeaders emitted Access-Control-Allow-Credentials: true for every matched
rule, including one whose AllowedOrigin is "*" (where evaluateCors echoes "*").
The CORS specification forbids that pairing and browsers reject it for credentialed
requests, so the emulator produced a response that fails in a browser while passing
against Floci.

Verified against real S3 rather than inferred. Bucket with AllowedOrigins ["*"]:

  Access-Control-Allow-Origin: *
  Access-Control-Allow-Methods: GET
  Access-Control-Max-Age: 3000

and with a concrete AllowedOrigin it additionally returns

  Access-Control-Allow-Credentials: true
  Vary: Origin, Access-Control-Request-Headers, Access-Control-Request-Method

Both preflight and actual GET behave identically. Vary is now also conditional: the
wildcard response is byte-identical for every origin, so there is nothing to vary on,
and real S3 omits it there too.

* chore: re-run CI

The Build and Test failure on the previous head was RuntimeApiServerTest's
extension/stop race, a pre-existing flake unrelated to this change — it also failed
on #1820, #1823 and on #2131's own branch (the PR that tried to de-flake it).
Reproduced locally under `--cpus=1` and fixed separately.

* fix(cloudfront): wildcard aliases match exactly one subdomain level

wildcardAliasMatches did a plain suffix check, so "*.example.com" also matched
a.b.example.com. AWS is explicit that a wildcard alternate domain name replaces the
wildcard at that level only:

  "you can't add alternate domain names that are at levels higher or lower than the
   wildcard. For example, you can't add the alternate domain names example.com or
   marketing.product.example.com."

  https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/CNAMEs.html

Practical effect: a distribution owning "*.example.com" would answer for
a.b.example.com, which on AWS belongs to whoever owns "*.b.example.com" — or to
nobody. Require the label standing in for the wildcard to contain no dot.

wildcardAliasesMatchSubdomainsAndPreferMoreSpecificNames asserted the old behaviour
(a.b.example.test resolving to the *.example.test distribution); it now asserts no
match, plus the same for a deeper wildcard.

* test(dynamodb): retain Quarkus port for cleanup

* fix(cloudfront): isolate mocked storage backends

* fix(ec2): wire managed prefix list storage in public IP test

* fix(cloudfront): allow authorization origin custom headers

* fix(cloudfront): preserve S3 preflight CORS headers
This commit is contained in:
Alex Banna authored and GitHub committed 2026-08-20 22:21:51 -04:00
1 parent 520c0f4da2
commit f2ad05ce22
12 files changed
+979 -17

No files matched your search

@@ -0,0 +1,131 @@
package com.floci.test;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.cloudfront.CloudFrontClient;
import software.amazon.awssdk.services.cloudfront.model.CreateDistributionResponse;
import software.amazon.awssdk.services.cloudfront.model.CustomHeaders;
import software.amazon.awssdk.services.cloudfront.model.DistributionConfig;
import software.amazon.awssdk.services.cloudfront.model.GetDistributionConfigResponse;
import software.amazon.awssdk.services.cloudfront.model.Origin;
import software.amazon.awssdk.services.cloudfront.model.OriginCustomHeader;
import software.amazon.awssdk.services.cloudfront.model.S3OriginConfig;
import software.amazon.awssdk.services.cloudfront.model.ViewerProtocolPolicy;
import java.util.List;
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.Assumptions.assumeFalse;
class CloudFrontOriginCustomHeadersTest {
@Test
void roundTripsOriginCustomHeadersWithTheAwsSdk() {
assumeFalse(
TestFixtures.isRealAws(),
"The immediate distribution lifecycle in this test is emulator-specific");
String suffix = TestFixtures.uniqueName("cloudfront-origin-headers");
String distributionId = null;
try (CloudFrontClient cloudFront = TestFixtures.cloudFrontClient()) {
Origin configuredOrigin = Origin.builder()
.id("configured-origin")
.domainName("configured-content.s3.amazonaws.com")
.customHeaders(CustomHeaders.builder()
.quantity(1)
.items(OriginCustomHeader.builder()
.headerName("X-Origin-Verify")
.headerValue("configured-value")
.build())
.build())
.s3OriginConfig(S3OriginConfig.builder()
.originAccessIdentity("")
.build())
.build();
Origin emptyOrigin = Origin.builder()
.id("empty-origin")
.domainName("empty-content.s3.amazonaws.com")
.s3OriginConfig(S3OriginConfig.builder()
.originAccessIdentity("")
.build())
.build();
DistributionConfig configuration = DistributionConfig.builder()
.callerReference("distribution-reference-" + suffix)
.enabled(true)
.origins(origins -> origins
.quantity(2)
.items(configuredOrigin, emptyOrigin))
.defaultCacheBehavior(behavior -> behavior
.targetOriginId(configuredOrigin.id())
.viewerProtocolPolicy(ViewerProtocolPolicy.ALLOW_ALL))
.build();
CreateDistributionResponse created = cloudFront.createDistribution(
request -> request.distributionConfig(configuration));
distributionId = created.distribution().id();
assertOriginShapes(created.distribution()
.distributionConfig()
.origins()
.items());
GetDistributionConfigResponse fetched = cloudFront.getDistributionConfig(
request -> request.id(created.distribution().id()));
assertOriginShapes(fetched.distributionConfig().origins().items());
var listed = cloudFront.listDistributions(request -> request.maxItems("100"))
.distributionList()
.items()
.stream()
.filter(summary -> created.distribution().id().equals(summary.id()))
.findFirst()
.orElseThrow();
assertOriginShapes(listed.origins().items());
} finally {
if (distributionId != null) {
try (CloudFrontClient cloudFront = TestFixtures.cloudFrontClient()) {
String id = distributionId;
GetDistributionConfigResponse current = cloudFront.getDistributionConfig(
request -> request.id(id));
var disabled = current.distributionConfig()
.toBuilder()
.enabled(false)
.build();
var updated = cloudFront.updateDistribution(request -> request
.id(id)
.ifMatch(current.eTag())
.distributionConfig(disabled));
cloudFront.deleteDistribution(request -> request
.id(id)
.ifMatch(updated.eTag()));
}
}
}
}
private static void assertOriginShapes(List<Origin> origins) {
Origin configured = originById(origins, "configured-origin");
assertNotNull(configured.customHeaders());
assertEquals(1, configured.customHeaders().quantity());
assertEquals(1, configured.customHeaders().items().size());
assertEquals(
"X-Origin-Verify",
configured.customHeaders().items().get(0).headerName());
assertEquals(
"configured-value",
configured.customHeaders().items().get(0).headerValue());
Origin empty = originById(origins, "empty-origin");
assertNotNull(empty.customHeaders());
assertEquals(0, empty.customHeaders().quantity());
assertFalse(empty.customHeaders().hasItems());
}
private static Origin originById(List<Origin> origins, String id) {
return origins.stream()
.filter(origin -> id.equals(origin.id()))
.findFirst()
.orElseThrow();
}
}