fix(cloudformation): name an https ResponseURL for a custom resource when TLS is on (#6993)

This commit is contained in:
Charles Swanberg authored and GitHub committed 2026-11-06 08:44:43 -04:00
1 parent 626af42d45
commit a0b39a4ad0
11 files changed
+353 -17

No files matched your search

@@ -98,7 +98,7 @@ public class TlsCertificateManager implements Resettable {
return;
}
synchronized (lock) {
if (!loadOnce() || covers(name)) {
if (!loadOnce() || covers(knownHostnames, name)) {
return;
}
if (!isAllowed(name)) {
@@ -118,6 +118,32 @@ public class TlsCertificateManager implements Resettable {
}
}
/**
* Whether the certificate Floci serves names {@code host}, exactly or under a one-label
* wildcard, so a client that verifies the name accepts it: the managed leaf with every name it
* learned, or a user-provided certificate as it is on disk. False with TLS off, for a host it
* does not name, such as an IP from {@code docker-host-override} or the container-IP fallback,
* and when the certificate cannot be read.
*/
public boolean serves(String host) {
String name = normalize(host);
if (name == null || !config.tls().enabled()) {
return false;
}
if (managesServerCertificate()) {
synchronized (lock) {
return loadOnce() && covers(knownHostnames, name);
}
}
String certPath = config.tls().certPath().orElse("");
try {
return covers(sansOf(Pem.parseCertificate(Files.readString(Path.of(certPath)))), name);
} catch (Exception e) {
LOG.warnv("TLS: could not read the certificate {0} to check it names {1}: {2}", certPath, name, e.getMessage());
return false;
}
}
/** Every SAN on the served leaf, configured and learned, lower-cased. Empty when not managed. */
public Set<String> knownHostnames() {
if (!managesServerCertificate()) {
@@ -231,12 +257,12 @@ public class TlsCertificateManager implements Resettable {
certificateUpdated.fire(new CertificateUpdatedEvent(TlsConfig.DEFAULT_NAME, cfg));
}
private boolean covers(String name) {
if (knownHostnames.contains(name)) {
private static boolean covers(Set<String> sans, String name) {
if (sans.contains(name)) {
return true;
}
int dot = name.indexOf('.');
return dot > 0 && knownHostnames.contains("*" + name.substring(dot));
return dot > 0 && sans.contains("*" + name.substring(dot));
}
private boolean isAllowed(String name) {
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.core.common.docker;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.config.TlsCertificateManager;
import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@@ -33,16 +34,19 @@ public class ContainerReachableEndpoint {
private final EmulatorConfig config;
private final DockerHostResolver dockerHostResolver;
private final EmbeddedDnsServer embeddedDnsServer;
private final TlsCertificateManager tlsCertificates;
private final int httpPort;
@Inject
public ContainerReachableEndpoint(EmulatorConfig config,
DockerHostResolver dockerHostResolver,
EmbeddedDnsServer embeddedDnsServer,
TlsCertificateManager tlsCertificates,
@ConfigProperty(name = "quarkus.http.port") int httpPort) {
this.config = config;
this.dockerHostResolver = dockerHostResolver;
this.embeddedDnsServer = embeddedDnsServer;
this.tlsCertificates = tlsCertificates;
this.httpPort = httpPort;
}
@@ -62,14 +66,38 @@ public class ContainerReachableEndpoint {
/** Uses the Docker host when the container does not receive Floci's DNS resolver. */
public String baseUrl(boolean useEmbeddedDns) {
String flociHostname = useEmbeddedDns && embeddedDnsServer.getServerIp().isPresent()
return "http://" + host(useEmbeddedDns) + ":" + port();
}
private String host(boolean useEmbeddedDns) {
return useEmbeddedDns && embeddedDnsServer.getServerIp().isPresent()
? config.hostname().orElse(EmbeddedDnsServer.DEFAULT_SUFFIX)
: dockerHostResolver.resolve();
return "http://" + flociHostname + ":" + port();
}
/** The port of {@link #baseUrl()}: the one a launched container can reach Floci on. */
public int port() {
return config.tls().enabled() ? config.port() : httpPort;
}
/**
* The base URL a custom resource's {@code ResponseURL} names: {@link #baseUrl()}, over https
* where Floci serves TLS under a certificate that names the host.
*
* <p>AWS hands a custom resource a presigned Amazon S3 URL to PUT its response to
* (CloudFormation User Guide, CloudFormation custom resource request and response reference,
* ResponseURL), so a handler that PUTs with Node's {@code https} module, as AWS's own
* {@code cfn-response} does, refuses an http URL ({@code Protocol "http:" not supported}). With
* TLS enabled, {@code TlsProxyServer} serves https beside http on {@link EmulatorConfig#port()},
* and every container Floci launches trusts Floci's CA through {@code NODE_EXTRA_CA_CERTS} and
* the other bundle variables, so the https URL is reachable with no shim in the handler. A host
* the certificate does not name, an IP from {@code docker-host-override} or the container-IP
* fallback when the embedded DNS server cannot start, would fail the handler's certificate
* check where http works, so that URL stays http.
*/
public String customResourceResponseBaseUrl() {
String host = host(true);
boolean https = tlsCertificates.serves(host);
return (https ? "https://" : "http://") + host + ":" + port();
}
}
@@ -323,7 +323,7 @@ public class CustomResourceCfnProvisioner implements CfnResourceProvisioner {
try {
ObjectNode event = objectMapper.createObjectNode();
event.put("RequestType", requestType);
event.put("ResponseURL", reachableEndpoint.baseUrl() + "/cfn-response/" + token);
event.put("ResponseURL", reachableEndpoint.customResourceResponseBaseUrl() + "/cfn-response/" + token);
event.put("StackId", stackId);
event.put("RequestId", UUID.randomUUID().toString());
event.put("ResourceType", resourceType);