mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
feat(cloudfront): Support HTTPS locally with correct key id (#3845)
* fix(core): route by the Host header on HTTP/2 requests HTTP/2 carries the authority in :authority and sends no Host header, so every lookup that reads Host fell through when a client negotiated HTTP/2. curl, the AWS SDK for Java and Go, and browsers all do that over HTTPS, which left CloudFront distribution domains, API Gateway execute-api and custom domains, Lambda function URLs and Cognito custom domains unreachable over https://. Copy the authority into a Host header before routing. S3 already covered this in its own filter. * feat(cloudfront): serve distributions over HTTPS on local hostnames A distribution's assigned domain name resolves to nothing local, so a signed URL could not be downloaded over https:// the way a viewer downloads one from CloudFront. Serve every distribution under <id>.cloudfront.localhost.floci.io and <id>.cloudfront.localhost as well, and add both to the SANs of the generated server certificate. The first resolves to 127.0.0.1 through public DNS on Linux, macOS and in containers. The second needs no DNS at all but only resolves where the runtime handles .localhost itself, which macOS and browsers do and Debian-based images do not. * fix(cloudfront): assign lower-case distribution domain names AWS writes a distribution's domain name in lower case and keeps the id upper case. Floci put the upper-case id straight into the host name, and a browser lower-cases the authority it sends, so the rebuilt resource never matched the one a signed URL was signed for and the download failed with 403. * refactor(cloudfront): derive the local delivery hosts from the DNS suffixes The suffix list was a fourth hardcoded copy of the emulator host suffixes, so a configured FLOCI_HOSTNAME or FLOCI_DNS_EXTRA_SUFFIXES resolved and was covered by TLS but did not route, and localhost.localstack.cloud did not either. Build it from the same source the embedded DNS uses. Match the host inside the scan findByHost already does, by comparing the host to <id><suffix>, which drops a store read per request and the assumption that the store keys distributions by an upper-case id. The tests read the default SAN list from TlsConfigSource rather than copying it. * fix(cloudfront): issue AWS-shaped public key ids CreatePublicKey answered a UUID where AWS answers K followed by 13 characters, the value a signed URL carries as Key-Pair-Id. Verified on AWS us-east-1 2026-09-18: a public key is K2VKB3XV74876Q, while key groups, cache policies, origin request policies and response headers policies are UUIDs, which Floci already matches. * refactor(core): resolve the HTTP/2 authority in each host routing filter The global Http2HostHeaderFilter made every service depend on a Host header it wrote, and no service file showed that. RequestHost now returns the Host header, or the HTTP/2 :authority when there is none. Each filter that routes by host calls it. S3 and Cognito had their own copy of this fallback and now use the helper. * test(core): check host routing over HTTPS and HTTP/2 against the real SANs The HTTP/2 routing test now boots with a CA-issued leaf that carries the default SANs. The client trusts only the Floci CA and checks each CloudFront local delivery host against that leaf. This replaces the HTTP/1.1 local delivery test, which only set a Host header and never reached TLS. S3 drops three resolveHost tests that RequestHostTest now covers. Its HTTP/2 bucket tests still guard the call to the helper.
This commit is contained in:
1 parent
c664b9e136
commit
c603639e0b
20 files changed
+557
-73
No files matched your search
@@ -62,11 +62,15 @@ public class TlsConfigSource implements ConfigSource {
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
||||
// host.docker.internal: how Lambda containers reach Floci when it runs on the host (not in a container).
|
||||
private static final List<String> DEFAULT_SAN_HOSTNAMES = List.of(
|
||||
// A wildcard SAN covers one label, so every two-label service form needs its own entry.
|
||||
// Package-private so the tests assert against this list instead of copying it.
|
||||
static final List<String> DEFAULT_SAN_HOSTNAMES = List.of(
|
||||
"localhost", "127.0.0.1", "0.0.0.0", "*.localhost",
|
||||
"localhost.floci.io", "*.localhost.floci.io",
|
||||
"*.execute-api.localhost.floci.io",
|
||||
"*.execute-api.localhost.localstack.cloud", "host.docker.internal");
|
||||
"*.execute-api.localhost.localstack.cloud",
|
||||
"*.cloudfront.localhost.floci.io", "*.cloudfront.localhost",
|
||||
"host.docker.internal");
|
||||
|
||||
private static volatile Path resolvedTlsDir;
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import io.vertx.core.http.HttpServerRequest;
|
||||
import io.vertx.core.net.HostAndPort;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
/**
|
||||
* The host a client addressed. HTTP/2 (RFC 9113 section 8.3.1) sends {@code :authority} and no
|
||||
* {@code Host} header, and curl and browsers use HTTP/2 over HTTPS.
|
||||
*/
|
||||
public final class RequestHost {
|
||||
|
||||
private RequestHost() {
|
||||
}
|
||||
|
||||
public static String of(ContainerRequestContext requestContext) {
|
||||
return of(requestContext.getHeaderString("Host"), requestContext.getUriInfo().getRequestUri());
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@code Host} header when present, else the request URI authority, which the container
|
||||
* builds from {@code :authority} on HTTP/2.
|
||||
*/
|
||||
public static String of(String hostHeader, URI requestUri) {
|
||||
if (hostHeader != null && !hostHeader.isBlank()) {
|
||||
return hostHeader.trim();
|
||||
}
|
||||
return requestUri != null ? requestUri.getAuthority() : null;
|
||||
}
|
||||
|
||||
public static String of(HttpServerRequest request) {
|
||||
String hostHeader = request.getHeader("Host");
|
||||
if (hostHeader != null && !hostHeader.isBlank()) {
|
||||
return hostHeader.trim();
|
||||
}
|
||||
HostAndPort authority = request.authority();
|
||||
return authority != null ? authority.toString() : null;
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.apigateway;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.RequestHost;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.BasePathMapping;
|
||||
import io.github.hectorvent.floci.services.apigateway.model.CustomDomain;
|
||||
import jakarta.annotation.Priority;
|
||||
@@ -41,7 +42,7 @@ public class ApiGatewayCustomDomainFilter implements ContainerRequestFilter {
|
||||
|
||||
@Override
|
||||
public void filter(ContainerRequestContext requestContext) {
|
||||
String host = requestContext.getHeaderString("Host");
|
||||
String host = RequestHost.of(requestContext);
|
||||
if (host == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
+2
-1
@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.RequestContext;
|
||||
import io.github.hectorvent.floci.core.common.RequestHost;
|
||||
import io.github.hectorvent.floci.services.apigatewayv2.ApiGatewayV2Service;
|
||||
import jakarta.annotation.Priority;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
@@ -101,7 +102,7 @@ public class ApiGatewayExecuteApiHostFilter implements ContainerRequestFilter {
|
||||
|
||||
@Override
|
||||
public void filter(ContainerRequestContext requestContext) {
|
||||
String host = requestContext.getHeaderString("Host");
|
||||
String host = RequestHost.of(requestContext);
|
||||
if (host == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.cloudfront;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.RequestHost;
|
||||
import io.github.hectorvent.floci.services.cloudfront.model.Distribution;
|
||||
import jakarta.annotation.Priority;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
@@ -49,7 +50,7 @@ public class CloudFrontDistributionFilter implements ContainerRequestFilter {
|
||||
|
||||
@Override
|
||||
public void filter(ContainerRequestContext requestContext) {
|
||||
String host = requestContext.getHeaderString("Host");
|
||||
String host = RequestHost.of(requestContext);
|
||||
if (host == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
+65
-6
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.cloudfront.model.CacheBehavior;
|
||||
@@ -41,6 +42,7 @@ import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.SequencedSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Function;
|
||||
@@ -101,6 +103,16 @@ public class CloudFrontService {
|
||||
private final StorageBackend<String, MonitoringSubscription> monitoringStore;
|
||||
private final String accountId;
|
||||
private final String domainSuffix;
|
||||
/**
|
||||
* Host suffixes under which every distribution is served as {@code <id><suffix>}, whatever
|
||||
* {@code floci.services.cloudfront.domain-suffix} makes the assigned domain name. They carry the
|
||||
* {@code cloudfront} service label on the endpoint hosts the embedded DNS resolves, the same
|
||||
* derivation API Gateway uses for {@code <id>.execute-api.<host>}, so a name that resolves to
|
||||
* Floci is also routed by it. {@code <id>.cloudfront.localhost.floci.io} and
|
||||
* {@code <id>.cloudfront.localhost} are additionally covered by the generated HTTPS
|
||||
* certificate, so a signed URL for either can be downloaded over HTTPS.
|
||||
*/
|
||||
private final List<String> localDeliverySuffixes;
|
||||
|
||||
@Inject
|
||||
public CloudFrontService(StorageFactory factory, EmulatorConfig config) {
|
||||
@@ -140,6 +152,7 @@ public class CloudFrontService {
|
||||
new TypeReference<Map<String, MonitoringSubscription>>() {});
|
||||
this.accountId = config.defaultAccountId();
|
||||
this.domainSuffix = config.services().cloudfront().domainSuffix();
|
||||
this.localDeliverySuffixes = localDeliverySuffixes(config);
|
||||
}
|
||||
|
||||
// ── Distributions ─────────────────────────────────────────────────────────
|
||||
@@ -152,7 +165,7 @@ public class CloudFrontService {
|
||||
String id = generateDistributionId();
|
||||
dist.setId(id);
|
||||
dist.setArn(AwsArnUtils.Arn.of("cloudfront", "", accountId, "distribution/" + id).toString());
|
||||
dist.setDomainName(id + "." + domainSuffix);
|
||||
dist.setDomainName(domainNameFor(id));
|
||||
dist.setStatus("Deployed");
|
||||
dist.setLastModifiedTime(Instant.now());
|
||||
dist.setEtag(UUID.randomUUID().toString());
|
||||
@@ -310,15 +323,17 @@ public class CloudFrontService {
|
||||
/**
|
||||
* Finds the distribution whose data-plane requests should be served for the given {@code Host}
|
||||
* header. A distribution matches when the host equals its assigned CloudFront domain name
|
||||
* ({@code <id>.cloudfront.net}) or one of its alternate domain names (CNAME aliases). Any port
|
||||
* suffix is ignored and matching is case-insensitive. Returns {@code null} when nothing matches.
|
||||
* ({@code <id>.cloudfront.net}), one of its alternate domain names (CNAME aliases), or one of
|
||||
* the local delivery hostnames {@code <id>.cloudfront.localhost.floci.io} and
|
||||
* {@code <id>.cloudfront.localhost}. Any port suffix is ignored and matching is
|
||||
* case-insensitive. Returns {@code null} when nothing matches.
|
||||
*/
|
||||
public Distribution findByHost(String host) {
|
||||
if (host == null || host.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
String hostname = stripPort(host);
|
||||
List<Distribution> distributions = new ArrayList<>(distStore.scan(k -> true));
|
||||
List<Distribution> distributions = distStore.scan(k -> true);
|
||||
for (Distribution dist : distributions) {
|
||||
if (hostname.equalsIgnoreCase(dist.getDomainName())) {
|
||||
return dist;
|
||||
@@ -334,6 +349,11 @@ public class CloudFrontService {
|
||||
}
|
||||
}
|
||||
}
|
||||
for (Distribution dist : distributions) {
|
||||
if (matchesLocalDeliveryHost(hostname, dist.getId())) {
|
||||
return dist;
|
||||
}
|
||||
}
|
||||
Distribution best = null;
|
||||
int bestSpecificity = -1;
|
||||
for (Distribution dist : distributions) {
|
||||
@@ -351,6 +371,36 @@ public class CloudFrontService {
|
||||
return best;
|
||||
}
|
||||
|
||||
/**
|
||||
* The domain name a new distribution is served under. AWS assigns a lower-case host, and a
|
||||
* browser lower-cases the authority it sends, so an upper-case id in the host would never match
|
||||
* the resource a signed URL was signed for.
|
||||
*/
|
||||
private String domainNameFor(String id) {
|
||||
return id.toLowerCase(Locale.ROOT) + "." + domainSuffix;
|
||||
}
|
||||
|
||||
private static List<String> localDeliverySuffixes(EmulatorConfig config) {
|
||||
SequencedSet<String> endpointHosts = new LinkedHashSet<>();
|
||||
endpointHosts.add("localhost");
|
||||
EmbeddedDnsServer.BUILTIN_SUFFIXES.forEach(endpointHosts::add);
|
||||
config.hostname().ifPresent(endpointHosts::add);
|
||||
config.dns().extraSuffixes().ifPresent(endpointHosts::addAll);
|
||||
return endpointHosts.stream()
|
||||
.map(host -> ".cloudfront." + host.toLowerCase(Locale.ROOT))
|
||||
.toList();
|
||||
}
|
||||
|
||||
/** True when {@code hostname} is the local delivery host of the distribution with this id. */
|
||||
private boolean matchesLocalDeliveryHost(String hostname, String id) {
|
||||
for (String suffix : localDeliverySuffixes) {
|
||||
if (hostname.equalsIgnoreCase(id + suffix)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void ensureAliasesAvailable(DistributionConfig config, String currentDistributionId) {
|
||||
if (config == null || config.getAliases() == null) {
|
||||
return;
|
||||
@@ -1230,7 +1280,7 @@ public class CloudFrontService {
|
||||
"A public key with this caller reference already exists.",
|
||||
409);
|
||||
}
|
||||
key.setId(UUID.randomUUID().toString());
|
||||
key.setId(generatePublicKeyId());
|
||||
key.setCreatedTime(Instant.now());
|
||||
key.setEtag(UUID.randomUUID().toString());
|
||||
publicKeyStore.put(key.getId(), key);
|
||||
@@ -1596,7 +1646,7 @@ public class CloudFrontService {
|
||||
String id = generateDistributionId();
|
||||
sd.setId(id);
|
||||
sd.setArn(AwsArnUtils.Arn.of("cloudfront", "", accountId, "streaming-distribution/" + id).toString());
|
||||
sd.setDomainName(id + "." + domainSuffix);
|
||||
sd.setDomainName(domainNameFor(id));
|
||||
sd.setStatus("Deployed");
|
||||
sd.setLastModifiedTime(Instant.now());
|
||||
sd.setEtag(UUID.randomUUID().toString());
|
||||
@@ -1767,6 +1817,15 @@ public class CloudFrontService {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/** AWS issues public key ids as K + 13 characters; the value travels in Key-Pair-Id. */
|
||||
private static String generatePublicKeyId() {
|
||||
StringBuilder sb = new StringBuilder("K");
|
||||
for (int i = 0; i < 13; i++) {
|
||||
sb.append(CHARS.charAt(RANDOM.nextInt(CHARS.length())));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static String generateInvalidationId() {
|
||||
StringBuilder sb = new StringBuilder("I");
|
||||
for (int i = 0; i < 13; i++) {
|
||||
|
||||
+5
-4
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.cloudfront;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RequestHost;
|
||||
import io.github.hectorvent.floci.services.cloudfront.model.Distribution;
|
||||
import io.github.hectorvent.floci.services.cloudfront.model.DistributionConfig;
|
||||
import io.github.hectorvent.floci.services.cloudfront.model.Origin;
|
||||
@@ -113,7 +114,7 @@ public class CloudFrontServingController {
|
||||
String rawViewerPath = rawViewerPath(request.uri());
|
||||
return serve(distId, rawViewerPath, decodedViewerPath(rawViewerPath),
|
||||
uriInfo.getRequestUri().getScheme(),
|
||||
headers.getHeaderString("Host"),
|
||||
RequestHost.of(request),
|
||||
headers.getHeaderString(HttpHeaders.AUTHORIZATION),
|
||||
request.getHeader("Origin"), "GET", null, null,
|
||||
request.getHeader("Pragma"));
|
||||
@@ -127,7 +128,7 @@ public class CloudFrontServingController {
|
||||
String rawViewerPath = rawViewerPath(request.uri());
|
||||
return serve(distId, rawViewerPath, decodedViewerPath(rawViewerPath),
|
||||
uriInfo.getRequestUri().getScheme(),
|
||||
headers.getHeaderString("Host"),
|
||||
RequestHost.of(request),
|
||||
headers.getHeaderString(HttpHeaders.AUTHORIZATION),
|
||||
request.getHeader("Origin"), "HEAD", null, null,
|
||||
request.getHeader("Pragma"));
|
||||
@@ -141,7 +142,7 @@ public class CloudFrontServingController {
|
||||
String rawViewerPath = rawViewerPath(request.uri());
|
||||
return serve(distId, rawViewerPath, decodedViewerPath(rawViewerPath),
|
||||
uriInfo.getRequestUri().getScheme(),
|
||||
headers.getHeaderString("Host"),
|
||||
RequestHost.of(request),
|
||||
headers.getHeaderString(HttpHeaders.AUTHORIZATION),
|
||||
request.getHeader("Origin"), "OPTIONS",
|
||||
request.getHeader("Access-Control-Request-Method"),
|
||||
@@ -274,7 +275,7 @@ public class CloudFrontServingController {
|
||||
io.vertx.core.http.HttpServerRequest request,
|
||||
String rawViewerPath) {
|
||||
String scheme = request.scheme() != null ? request.scheme() : "https";
|
||||
String host = request.getHeader("Host");
|
||||
String host = RequestHost.of(request);
|
||||
if (host == null || host.isBlank()) {
|
||||
host = distribution.getDomainName();
|
||||
}
|
||||
|
||||
+2
-5
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.cognito;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AccountContextFilter;
|
||||
import io.github.hectorvent.floci.core.common.RequestHost;
|
||||
import io.github.hectorvent.floci.services.cognito.model.UserPoolDomain;
|
||||
import jakarta.annotation.Priority;
|
||||
import jakarta.inject.Inject;
|
||||
@@ -51,11 +52,7 @@ public class CognitoCustomDomainFilter implements ContainerRequestFilter {
|
||||
if (path == null || !path.startsWith(OAUTH_PREFIX)) {
|
||||
return;
|
||||
}
|
||||
// HTTP/2 has no Host header; its :authority arrives as the request URI authority.
|
||||
String host = requestContext.getHeaderString("Host");
|
||||
if (host == null) {
|
||||
host = originalUri.getAuthority();
|
||||
}
|
||||
String host = RequestHost.of(requestContext);
|
||||
if (host == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.emrserverless;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.RequestHost;
|
||||
import jakarta.annotation.Priority;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.container.ContainerRequestFilter;
|
||||
@@ -34,7 +35,7 @@ public class EmrServerlessRouteFilter implements ContainerRequestFilter {
|
||||
return;
|
||||
}
|
||||
|
||||
String host = ctx.getHeaderString("Host");
|
||||
String host = RequestHost.of(ctx);
|
||||
boolean isEmrServerlessHost = host != null && host.startsWith("emr-serverless.");
|
||||
|
||||
String auth = ctx.getHeaderString("Authorization");
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.lambda;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.RequestHost;
|
||||
import jakarta.annotation.Priority;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.container.ContainerRequestFilter;
|
||||
@@ -26,7 +27,7 @@ public class LambdaUrlRoutingFilter implements ContainerRequestFilter {
|
||||
|
||||
@Override
|
||||
public void filter(ContainerRequestContext requestContext) throws IOException {
|
||||
String host = requestContext.getHeaderString("Host");
|
||||
String host = RequestHost.of(requestContext);
|
||||
if (host == null) return;
|
||||
|
||||
// Pattern: <urlId>.lambda-url.<region>.<anything>
|
||||
|
||||
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.s3;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsRegions;
|
||||
import io.github.hectorvent.floci.core.common.RequestHost;
|
||||
import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
|
||||
import io.github.hectorvent.floci.services.cloudfront.CloudFrontDistributionFilter;
|
||||
@@ -203,10 +204,7 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
|
||||
return first;
|
||||
}
|
||||
}
|
||||
if (hostHeader != null && !hostHeader.isBlank()) {
|
||||
return hostHeader.trim();
|
||||
}
|
||||
return requestUri != null ? requestUri.getAuthority() : null;
|
||||
return RequestHost.of(hostHeader, requestUri);
|
||||
}
|
||||
|
||||
static String resolveHost(String hostHeader, URI requestUri) {
|
||||
|
||||
Reference in new issue
Block a user