mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(sns): trust FLOCI_TLS_EXTRA_CA_PATH for HTTPS subscriptions (#8063)
* feat(sns): trust FLOCI_TLS_EXTRA_CA_PATH for HTTPS subscriptions SnsService built its delivery HttpClient without an SSLContext, so an HTTPS subscription whose endpoint has a certificate from a private CA never got the SubscriptionConfirmation or any notification. Floci only logged "Failed to reach SNS endpoint". The @Inject constructor now passes ContainerCaBundle.sslContext(config) down to the constructor that builds the client, as EventBridgeInvoker does. The SubscriptionConfirmation and notification delivery share that one client. It stays pinned to HTTP/1.1, and hostname verification stays on. The other constructors build the client as before. Closes #8033 * test: share the /hook HTTPS endpoint between the extra CA delivery tests LocalHttpsServer.hook starts a localhost HTTPS server that passes each request to /hook to a callback and answers 200, and hookUrl gives its URL. EventBridgeInvokerApiDestinationTest and the new SNS test use them instead of each writing the endpoint out.
This commit is contained in:
1 parent
14bcb85bcb
commit
35bdfdcb3f
6 files changed
+178
-28
No files matched your search
+3
-19
@@ -28,6 +28,7 @@ import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static io.floci.aws.testing.LocalHttpsServer.hookUrl;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
@@ -149,7 +150,7 @@ class EventBridgeInvokerApiDestinationTest {
|
||||
void deliversToAnEndpointWhoseCertificateChainsToAnExtraCa(@TempDir Path dir) throws Exception {
|
||||
FlociCertificateAuthority privateCa = FlociCertificateAuthority.loadOrCreate(dir.resolve("private-ca"));
|
||||
AtomicInteger delivered = new AtomicInteger();
|
||||
HttpsServer server = httpsEndpoint(privateCa, delivered);
|
||||
HttpsServer server = LocalHttpsServer.hook(privateCa, exchange -> delivered.incrementAndGet());
|
||||
try {
|
||||
assertRefused(hookUrl(server), Optional.empty(), delivered);
|
||||
|
||||
@@ -166,7 +167,7 @@ class EventBridgeInvokerApiDestinationTest {
|
||||
FlociCertificateAuthority privateCa = FlociCertificateAuthority.loadOrCreate(dir.resolve("private-ca"));
|
||||
FlociCertificateAuthority unrelatedCa = FlociCertificateAuthority.loadOrCreate(dir.resolve("unrelated-ca"));
|
||||
AtomicInteger delivered = new AtomicInteger();
|
||||
HttpsServer server = httpsEndpoint(privateCa, delivered);
|
||||
HttpsServer server = LocalHttpsServer.hook(privateCa, exchange -> delivered.incrementAndGet());
|
||||
try {
|
||||
assertRefused(hookUrl(server), Optional.of(unrelatedCa.certificatePath().toString()), delivered);
|
||||
} finally {
|
||||
@@ -186,10 +187,6 @@ class EventBridgeInvokerApiDestinationTest {
|
||||
invoker(endpoint, extraCaPath).invokeTarget(apiDestinationTarget(), "{}", REGION);
|
||||
}
|
||||
|
||||
private static String hookUrl(HttpsServer server) {
|
||||
return "https://localhost:" + server.getAddress().getPort() + "/hook";
|
||||
}
|
||||
|
||||
// An invoker built the way CDI builds it, its API destination pointing at endpoint, with TLS off.
|
||||
private static EventBridgeInvoker invoker(String endpoint, Optional<String> extraCaPath) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS);
|
||||
@@ -208,19 +205,6 @@ class EventBridgeInvokerApiDestinationTest {
|
||||
return new Target("tls-target", DESTINATION_ARN, null, null);
|
||||
}
|
||||
|
||||
// An HTTPS endpoint on localhost serving a certificate the CA issued, counting the requests to /hook.
|
||||
private static HttpsServer httpsEndpoint(FlociCertificateAuthority ca, AtomicInteger requests) throws Exception {
|
||||
HttpsServer server = LocalHttpsServer.issuedBy(ca);
|
||||
server.createContext("/hook", exchange -> {
|
||||
exchange.getRequestBody().readAllBytes();
|
||||
requests.incrementAndGet();
|
||||
exchange.sendResponseHeaders(200, -1);
|
||||
exchange.close();
|
||||
});
|
||||
server.start();
|
||||
return server;
|
||||
}
|
||||
|
||||
private static ApiDestination rateLimited(String name, int limit) {
|
||||
return new ApiDestination(name, "arn:aws:events:us-east-1:000000000000:api-destination/" + name + "/1",
|
||||
"arn:aws:events:us-east-1:000000000000:connection/c/1", "https://api.example.com", "POST",
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
package io.floci.aws.services.sns;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.sun.net.httpserver.HttpsServer;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.config.FlociCertificateAuthority;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.testing.LocalHttpsServer;
|
||||
import io.floci.aws.testing.StorageFactories;
|
||||
import io.floci.aws.testutil.LogCapture;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
import static io.floci.aws.testing.LocalHttpsServer.hookUrl;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* An HTTPS subscription's endpoint is reached through Floci's outbound trust, so a certificate from a CA in
|
||||
* floci.tls.extra-ca-path is accepted, with TLS off.
|
||||
*/
|
||||
class SnsHttpsSubscriptionTrustTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
private static final String BASE_URL = "http://localhost:4566";
|
||||
|
||||
@TempDir
|
||||
Path dir;
|
||||
|
||||
/**
|
||||
* An endpoint whose certificate chains to a private CA is refused while only the JVM's roots are trusted, and
|
||||
* gets the SubscriptionConfirmation once that CA is in the floci.tls.extra-ca-path file.
|
||||
*/
|
||||
@Test
|
||||
void sendsTheConfirmationToAnEndpointWhoseCertificateChainsToAnExtraCa() throws Exception {
|
||||
FlociCertificateAuthority privateCa = FlociCertificateAuthority.loadOrCreate(dir.resolve("private-ca"));
|
||||
BlockingQueue<String> received = new LinkedBlockingQueue<>();
|
||||
HttpsServer server = httpsEndpoint(privateCa, received);
|
||||
try {
|
||||
assertRefused(hookUrl(server), Optional.empty(), received);
|
||||
|
||||
subscribe(hookUrl(server), Optional.of(privateCa.certificatePath().toString()));
|
||||
assertEquals("SubscriptionConfirmation", received.poll(5, TimeUnit.SECONDS));
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
/** The extra CAs add trust; they do not loosen it: a CA the endpoint's certificate does not chain to is no help. */
|
||||
@Test
|
||||
void refusesAnEndpointWhoseCaIsNotInTheExtraCaFile() throws Exception {
|
||||
FlociCertificateAuthority privateCa = FlociCertificateAuthority.loadOrCreate(dir.resolve("private-ca"));
|
||||
FlociCertificateAuthority unrelatedCa = FlociCertificateAuthority.loadOrCreate(dir.resolve("unrelated-ca"));
|
||||
BlockingQueue<String> received = new LinkedBlockingQueue<>();
|
||||
HttpsServer server = httpsEndpoint(privateCa, received);
|
||||
try {
|
||||
assertRefused(hookUrl(server), Optional.of(unrelatedCa.certificatePath().toString()), received);
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
// Asserts that subscribing endpoint fails the TLS handshake, so the endpoint never sees a request.
|
||||
private void assertRefused(String endpoint, Optional<String> extraCaPath, BlockingQueue<String> received) {
|
||||
LogCapture.awaitRecord(SnsService.class, Duration.ofSeconds(5), record -> refuses(record, endpoint), () -> {
|
||||
subscribe(endpoint, extraCaPath);
|
||||
return null;
|
||||
});
|
||||
assertTrue(received.isEmpty(), "the endpoint got " + received);
|
||||
}
|
||||
|
||||
// Whether record is the warning that a send to endpoint failed its TLS handshake.
|
||||
private static boolean refuses(LogRecord record, String endpoint) {
|
||||
Object[] parameters = record.getParameters();
|
||||
return record.getMessage().contains("Failed to reach SNS endpoint")
|
||||
&& parameters != null && parameters.length == 2
|
||||
&& endpoint.equals(parameters[0])
|
||||
&& String.valueOf(parameters[1]).contains("SSLHandshakeException");
|
||||
}
|
||||
|
||||
// Subscribes endpoint over HTTPS to a new topic, with extraCaPath as the extra CA setting.
|
||||
private void subscribe(String endpoint, Optional<String> extraCaPath) {
|
||||
SnsService service = service(extraCaPath);
|
||||
String topicArn = service.createTopic("trust", null, null, REGION).getTopicArn();
|
||||
service.subscribe(topicArn, "https", endpoint, REGION, Map.of());
|
||||
}
|
||||
|
||||
// A service built the way CDI builds it, with TLS off.
|
||||
private SnsService service(Optional<String> extraCaPath) {
|
||||
EmulatorConfig config = StorageFactories.config(dir.resolve("data"), ACCOUNT);
|
||||
when(config.effectiveBaseUrl()).thenReturn(BASE_URL);
|
||||
when(config.tls().enabled()).thenReturn(false);
|
||||
when(config.tls().extraCaPath()).thenReturn(extraCaPath);
|
||||
return new SnsService(StorageFactories.open(config, "memory"), config, new RegionResolver(REGION, ACCOUNT),
|
||||
null, null, null, new ObjectMapper(), SnsMessageSigner.inMemory(BASE_URL));
|
||||
}
|
||||
|
||||
// A localhost HTTPS endpoint serving a certificate ca issued, queueing the SNS message type of each request.
|
||||
private static HttpsServer httpsEndpoint(FlociCertificateAuthority ca, BlockingQueue<String> received)
|
||||
throws Exception {
|
||||
return LocalHttpsServer.hook(ca, exchange -> {
|
||||
List<String> type = exchange.getRequestHeaders().get("x-amz-sns-message-type");
|
||||
received.add(type == null ? "" : type.get(0));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.floci.aws.testing;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpsConfigurator;
|
||||
import com.sun.net.httpserver.HttpsServer;
|
||||
import io.floci.aws.config.FlociCertificateAuthority;
|
||||
@@ -9,6 +10,7 @@ import io.floci.aws.services.acm.model.KeyAlgorithm;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/** An HTTPS server on a free localhost port, so a test sees a client verify a certificate a given CA issued. */
|
||||
public final class LocalHttpsServer {
|
||||
@@ -25,4 +27,22 @@ public final class LocalHttpsServer {
|
||||
Pem.parseCertificate(issued.certificatePem()), Pem.parsePrivateKey(issued.privateKeyPem()))));
|
||||
return server;
|
||||
}
|
||||
|
||||
/** Started, passing each request to {@code /hook} to {@code onRequest} and answering it with 200. */
|
||||
public static HttpsServer hook(FlociCertificateAuthority ca, Consumer<HttpExchange> onRequest) throws Exception {
|
||||
HttpsServer server = issuedBy(ca);
|
||||
server.createContext("/hook", exchange -> {
|
||||
exchange.getRequestBody().readAllBytes();
|
||||
onRequest.accept(exchange);
|
||||
exchange.sendResponseHeaders(200, -1);
|
||||
exchange.close();
|
||||
});
|
||||
server.start();
|
||||
return server;
|
||||
}
|
||||
|
||||
/** The URL of {@code server}'s {@code /hook}, by the name its certificate is issued for. */
|
||||
public static String hookUrl(HttpsServer server) {
|
||||
return "https://localhost:" + server.getAddress().getPort() + "/hook";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user