feat(eventbridge): trust extra CAs from FLOCI_TLS_EXTRA_CA_PATH for API destinations (#7850)

Add floci.tls.extra-ca-path (FLOCI_TLS_EXTRA_CA_PATH): a PEM file of CA
certificates that Floci's own outbound HTTPS clients trust on top of the
JVM's default roots, and Floci's CA when TLS is on. It applies whether or
not TLS is enabled.

ContainerCaBundle.sslContext(config) merges the extra CAs and is now also
built with TLS off. floci-ca-bundle.pem and its .p12 are unchanged, so
containers Floci launches are unaffected in both modes. EventBridge API
destination delivery uses the context, and JwtSignatureVerifier already
shares it, so HTTP API JWT authorizers and AppSync OIDC providers trust
the extra CAs too.

TlsExtraCaStartupValidator refuses to start when the file is missing,
unreadable, or holds no certificate, from any config source. A private
key in the file is ignored.

Closes #5259

With floci.tls.extra-ca-path set and TLS on, a bundle that could not be
read or parsed made outboundTrustAnchors throw. sslContext(config) then
logged the extra CA file as the unreadable one, fell back to the bundle
alone, failed on it again and returned empty, so the valid extra CAs
were dropped.

The bundle is now read on its own: on failure Floci warns with the
bundle's path and trusts the JVM roots plus the extra CAs, as the
sslContext(config) Javadoc already describes.

ContainerCaBundle builds every SSL context and trust manager array from a
KeyStore, so sslContext(Path), trustManagers(Path) and the extra-CA
context no longer repeat the JCA calls.

Tests share LocalHttpsServer in testing for the localhost HTTPS server
issued by a CA, the two JWT issuer-trust cases share one helper, and the
JVM roots check in ContainerCaBundleTest is written once.

outboundTrustAnchors and trustStore(Path) now read a certificate file
through one method, which also refuses a file with no certificate.

In the tests added for the extra CA file, the certificate-and-key file
comes from one helper in TlsTestSupport, the two missing-file cases are
one parameterized test, and the refused EventBridge delivery is checked
by one helper.

TlsExtraCaStartupValidator now calls ContainerCaBundle.certificates
instead of reading the file and checking for a certificate on its own.
outboundTrustAnchors reads the bundle through the same method, and
ContainerCaBundle.extraCaFile is the one place that decides whether the
setting is set, for both the SSL context and the startup check. The
startup message still names the path and gives the reason.

Both EventBridgeInvoker constructors now get the API destination
HttpClient from one method, which adds Floci's outbound SSL context
when there is one.
This commit is contained in:
Saim Zaib authored and GitHub committed 2026-11-10 11:56:34 -04:00
1 parent 65b09d0aac
commit 9f94fcc974
15 files changed
+611 -41

No files matched your search

@@ -235,13 +235,29 @@ public final class ContainerCaBundle {
}
/**
* An SSL context that trusts what the bundle carries: the JVM's roots and Floci's own trust
* anchor. For Floci's own outbound HTTPS to a URL Floci serves, such as a Cognito pool's issuer,
* which on AWS is a public endpoint every client already trusts. Empty when there is no bundle,
* or when it cannot be read, and the JVM's roots alone are trusted then.
* An SSL context for Floci's own outbound HTTPS. It trusts what the bundle carries: the JVM's
* roots and Floci's own trust anchor, for a URL Floci serves, such as a Cognito pool's issuer,
* which on AWS is a public endpoint every client already trusts. It also trusts the CAs in the
* {@code floci.tls.extra-ca-path} file, for an endpoint whose certificate chains to a private or
* corporate CA, and then it is built with TLS off too, over the JVM's roots and those CAs. The
* extra CAs are added here only, never to the bundle, so containers Floci launches do not
* receive them.
*
* <p>Empty when there is neither a bundle nor an extra CA file, and the JVM's roots alone are
* trusted then. An extra CA file that cannot be read leaves the context the bundle alone gives,
* and a bundle that cannot be read leaves the JVM's roots in its place.
*/
public static Optional<SSLContext> sslContext(EmulatorConfig config) {
Optional<Path> bundle = hostPath(config);
Optional<Path> extraCas = extraCaFile(config);
if (extraCas.isPresent()) {
try {
return Optional.of(sslContext(trustStore(outboundTrustAnchors(bundle.orElse(null), extraCas.get()))));
} catch (IOException | GeneralSecurityException e) {
LOG.warnv(e, "Cannot read the extra CAs in {0}; outbound HTTPS from Floci does not trust them",
extraCas.get());
}
}
if (bundle.isEmpty()) {
return Optional.empty();
}
@@ -253,6 +269,41 @@ public final class ContainerCaBundle {
}
}
/** The {@code floci.tls.extra-ca-path} file, when one is set; a blank value counts as unset. */
static Optional<Path> extraCaFile(EmulatorConfig config) {
return config.tls().extraCaPath().filter(path -> !path.isBlank()).map(Path::of);
}
/**
* What {@link #sslContext(EmulatorConfig)} trusts when there is an extra CA file: the certificates
* of {@code bundle}, or the JVM's roots when {@code bundle} is null, cannot be read or holds no
* certificate, followed by every certificate in {@code extraCas}. A private key in that file is
* skipped. A damaged bundle therefore costs Floci's own CA, never the extra CAs.
*
* @throws CertificateException when {@code extraCas} holds no certificate
* @throws IOException when {@code extraCas} cannot be read
*/
static List<X509Certificate> outboundTrustAnchors(Path bundle, Path extraCas)
throws IOException, GeneralSecurityException {
List<X509Certificate> extras = certificates(extraCas);
List<X509Certificate> anchors = new ArrayList<>();
if (bundle != null) {
try {
anchors.addAll(certificates(bundle));
} catch (IOException | GeneralSecurityException e) {
LOG.warnv(e, "Cannot use {0}; outbound HTTPS from Floci trusts the JVM roots and the extra CAs"
+ " instead", bundle);
}
}
if (anchors.isEmpty()) {
anchors.addAll(systemRoots());
}
anchors.addAll(extras);
LOG.debugv("TLS: outbound HTTPS from Floci trusts {0} anchor(s), {1} of them from {2}",
anchors.size(), extras.size(), extraCas);
return anchors;
}
/**
* An SSL context that trusts exactly the certificates in {@code bundle}.
*
@@ -260,24 +311,48 @@ public final class ContainerCaBundle {
* trust nothing, public endpoints included
*/
public static SSLContext sslContext(Path bundle) throws IOException, GeneralSecurityException {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, trustManagers(bundle), null);
return context;
return sslContext(trustStore(bundle));
}
/** The trust managers of {@link #sslContext(Path)}, over exactly the certificates in {@code bundle}. */
static TrustManager[] trustManagers(Path bundle) throws IOException, GeneralSecurityException {
return trustManagers(trustStore(bundle));
}
/** An SSL context that trusts exactly the entries of {@code trustStore}. */
private static SSLContext sslContext(KeyStore trustStore) throws GeneralSecurityException {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, trustManagers(trustStore), null);
return context;
}
/** The trust managers over exactly the entries of {@code trustStore}. */
private static TrustManager[] trustManagers(KeyStore trustStore) throws GeneralSecurityException {
TrustManagerFactory factory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
factory.init(trustStore(bundle));
factory.init(trustStore);
return factory.getTrustManagers();
}
/** A PKCS12 key store of exactly the certificates in {@code bundle}, each a trusted entry. */
private static KeyStore trustStore(Path bundle) throws IOException, GeneralSecurityException {
List<X509Certificate> certificates = parseCertificates(Files.readString(bundle));
return trustStore(certificates(bundle));
}
/**
* The certificates in {@code file}, in order; a private key or any other PEM object in it is skipped.
*
* @throws CertificateException when {@code file} holds no certificate
*/
static List<X509Certificate> certificates(Path file) throws IOException, GeneralSecurityException {
List<X509Certificate> certificates = parseCertificates(Files.readString(file));
if (certificates.isEmpty()) {
throw new CertificateException(bundle + " holds no certificate");
throw new CertificateException(file + " holds no certificate");
}
return certificates;
}
/** A PKCS12 key store of exactly {@code certificates}, each a trusted entry. */
private static KeyStore trustStore(List<X509Certificate> certificates) throws IOException, GeneralSecurityException {
KeyStore trustStore = KeyStore.getInstance("PKCS12");
trustStore.load(null, null);
for (int i = 0; i < certificates.size(); i++) {
@@ -3994,6 +3994,13 @@ public interface EmulatorConfig {
/** Path to PEM private key file. Env: FLOCI_TLS_KEY_PATH */
Optional<String> keyPath();
/**
* PEM file of extra CA certificates that Floci's own outbound HTTPS clients trust, on top of
* the JVM's default roots (and Floci's CA when TLS is on). Applies whether or not TLS is enabled.
* Containers Floci launches are not affected. Env: FLOCI_TLS_EXTRA_CA_PATH
*/
Optional<String> extraCaPath();
/**
* Auto-generate a server certificate when no cert-path/key-path is provided. The leaf is
* issued by Floci's local root CA; both live under {@code {storage.persistent-path}/tls/}
@@ -0,0 +1,57 @@
package io.floci.aws.config;
import io.quarkus.runtime.StartupEvent;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.GeneralSecurityException;
import java.util.Optional;
/**
* Refuses to start when {@code floci.tls.extra-ca-path} names a file Floci's own outbound HTTPS
* clients could not trust anything from: a missing or unreadable file, or one that holds no
* certificate. Checked whether or not TLS is enabled, as the extra CAs apply in both modes, and
* at startup rather than on the first outbound call, so a mistyped path is reported at once
* instead of surfacing later as a refused handshake. A private key kept in the file is ignored.
*/
@ApplicationScoped
public class TlsExtraCaStartupValidator {
private static final Logger LOG = Logger.getLogger(TlsExtraCaStartupValidator.class);
private final EmulatorConfig config;
@Inject
public TlsExtraCaStartupValidator(EmulatorConfig config) {
this.config = config;
}
void onStart(@Observes StartupEvent ignored) {
Optional<Path> extraCaFile = ContainerCaBundle.extraCaFile(config);
if (extraCaFile.isPresent()) {
validate(extraCaFile.get());
LOG.infov("TLS: outbound HTTPS from Floci also trusts the CAs in {0}", extraCaFile.get());
}
}
/**
* Checks that {@code file} can be read and holds at least one certificate.
*
* @throws IllegalStateException when it cannot, naming the file and the reason
*/
private static void validate(Path file) {
if (!Files.isReadable(file)) {
throw new IllegalStateException("TLS extra CA file not found or not readable: " + file);
}
try {
ContainerCaBundle.certificates(file);
} catch (IOException | GeneralSecurityException e) {
throw new IllegalStateException("TLS extra CA file cannot be used: " + file + ": " + e.getMessage(), e);
}
}
}
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.MissingNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.floci.aws.config.ContainerCaBundle;
import io.floci.aws.config.EmulatorConfig;
import io.floci.aws.core.common.AwsArnUtils;
import io.floci.aws.core.common.AwsException;
@@ -39,6 +40,7 @@ import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.InetAddress;
@@ -108,7 +110,17 @@ public class EventBridgeInvoker {
this(lambdaService, sqsService, snsService, batchService, firehoseService, eventBridgeService,
ecsService, ecsJsonHandler, stepFunctionsService, redshiftDataService, redshiftTargetAuthorizer,
serviceRegistry, regionResolver, objectMapper, config,
HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5)).build(), appSyncTargetInvoker);
apiDestinationClient(ContainerCaBundle.sslContext(config).orElse(null)), appSyncTargetInvoker);
}
// The client API destinations are delivered with: sslContext, when given, adds the extra CAs in
// floci.tls.extra-ca-path, and Floci's CA when TLS is on, to the JVM's roots
private static HttpClient apiDestinationClient(SSLContext sslContext) {
HttpClient.Builder builder = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(5));
if (sslContext != null) {
builder.sslContext(sslContext);
}
return builder.build();
}
public EventBridgeInvoker(LambdaService lambdaService,
@@ -128,7 +140,7 @@ public class EventBridgeInvoker {
EmulatorConfig config) {
this(lambdaService, sqsService, snsService, batchService, firehoseService, eventBridgeService,
ecsService, ecsJsonHandler, stepFunctionsService, redshiftDataService, redshiftTargetAuthorizer,
serviceRegistry, regionResolver, objectMapper, config, (AppSyncTargetInvoker) null);
serviceRegistry, regionResolver, objectMapper, config, apiDestinationClient(null), null);
}
EventBridgeInvoker(LambdaService lambdaService,