refactor: 백엔드 리펙토링 진행 중

This commit is contained in:
DongHyeonka
2026-09-25 17:57:07 +09:00
parent 5e44be9ab4
commit 4d4cc35e6c
19 changed files with 1205 additions and 139 deletions
@@ -169,7 +169,7 @@ class MessagingLiveRoundTripQualificationTest {
* same constant: a round trip that read a different topic from the one the transport wrote would
* pass or fail for reasons unrelated to the platform.
*/
private static dev.caskeleton.messaging.policy.DestinationProfile profile(
static dev.caskeleton.messaging.policy.DestinationProfile profile(
DestinationName name, DestinationName deadLetter) {
return new dev.caskeleton.messaging.policy.DestinationProfile(
name,
@@ -198,7 +198,7 @@ class MessagingLiveRoundTripQualificationTest {
false);
}
private static MessageEnvelope<String> envelope() {
static MessageEnvelope<String> envelope() {
return new MessageEnvelope<>(
MessageId.newId(),
new MessageType("order.created"),
@@ -0,0 +1,396 @@
package dev.caskeleton.messaging.autoconfigure;
import static org.assertj.core.api.Assertions.assertThat;
import dev.caskeleton.messaging.api.MessageType;
import dev.caskeleton.messaging.api.SchemaVersion;
import dev.caskeleton.messaging.api.destination.DestinationName;
import dev.caskeleton.messaging.api.destination.MessageDestination;
import dev.caskeleton.messaging.api.publish.MessagePublisher;
import dev.caskeleton.messaging.api.publish.PublishCompletion;
import dev.caskeleton.messaging.api.publish.PublishOptions;
import dev.caskeleton.messaging.api.publish.PublishResult;
import dev.caskeleton.messaging.security.CredentialProvider;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.kafka.KafkaContainer;
import org.testcontainers.utility.MountableFile;
/**
* A publish reaches a broker that trusts nobody by default (Review Focus 5, M-2e/NEW-4b, M-2d).
*
* <p>Both brokers present a certificate signed by a CA generated for this test, which no JVM trusts
* — so a publish is confirmed only if the configured trust store reached the client. The first
* broker then requires SASL PLAIN, and accepts only the user name the credential provider returns;
* the second requires a client certificate, which only the configured key store can present.
*
* <p>No secret is written in this file: the store and broker passwords are generated per run.
*/
@EnabledIf(value = "dockerAvailable", disabledReason = "Docker not available for the live broker")
class MessagingSecuredBrokerQualificationTest {
private static final String IMAGE = "apache/kafka:4.1.0";
private static final String BROKER_USER = "svc-orders";
private static final String BROKER_STORE = "/etc/kafka/secrets/broker.p12";
private static final String BROKER_TRUST = "/etc/kafka/secrets/truststore.p12";
static boolean dockerAvailable() {
return DockerClientFactory.instance().isDockerAvailable();
}
@Test
void aPublishIsConfirmedByAPrivateCaBrokerThatRequiresSaslAsTheProviderUser(
@TempDir Path directory) {
// given
PrivateCa ca = new PrivateCa(directory);
String userPassword = UUID.randomUUID().toString();
try (KafkaContainer kafka =
broker(ca, "SASL_SSL")
.withEnv("KAFKA_SASL_ENABLED_MECHANISMS", "PLAIN")
.withEnv(
"KAFKA_LISTENER_NAME_PLAINTEXT_PLAIN_SASL_JAAS_CONFIG",
"org.apache.kafka.common.security.plain.PlainLoginModule required user_"
+ BROKER_USER
+ "=\""
+ userPassword
+ "\";")) {
kafka.start();
ApplicationContextRunner runner =
platform(kafka, ca, Map.of("orders-producer", userPassword, "orders-consumer", "unused"))
.withPropertyValues(
"app.messaging.security.kafka.producer.type=USERNAME_PASSWORD",
"app.messaging.security.kafka.producer.credential-id=orders-producer",
"app.messaging.security.kafka.consumer.type=USERNAME_PASSWORD",
"app.messaging.security.kafka.consumer.credential-id=orders-consumer");
// when
List<PublishResult> results = new ArrayList<>();
runner.run(context -> results.add(publish(context.getBean(MessagePublisher.class))));
// then
assertThat(results)
.extracting(PublishResult::completion)
.containsExactly(PublishCompletion.CONFIRMED);
}
}
@Test
void aPublishIsConfirmedByAPrivateCaBrokerThatRequiresAClientCertificate(
@TempDir Path directory) {
// given
PrivateCa ca = new PrivateCa(directory);
Path clientStore = ca.issue("orders-producer", "dns:orders-producer");
try (KafkaContainer kafka =
broker(ca, "SSL")
.withEnv("KAFKA_SSL_CLIENT_AUTH", "required")
.withEnv("KAFKA_SSL_TRUSTSTORE_LOCATION", BROKER_TRUST)
.withEnv("KAFKA_SSL_TRUSTSTORE_TYPE", "PKCS12")
.withEnv("KAFKA_SSL_TRUSTSTORE_PASSWORD", ca.password)
.withCopyToContainer(MountableFile.forHostPath(ca.trustStore, 0644), BROKER_TRUST)) {
kafka.start();
ApplicationContextRunner runner =
platform(
kafka,
ca,
Map.of("orders-producer-tls", ca.password, "orders-consumer-tls", ca.password))
.withPropertyValues(
"app.messaging.security.kafka.producer.type=MUTUAL_TLS",
"app.messaging.security.kafka.producer.credential-id=orders-producer-tls",
"app.messaging.security.kafka.producer.key-store.location=" + clientStore,
"app.messaging.security.kafka.consumer.type=MUTUAL_TLS",
"app.messaging.security.kafka.consumer.credential-id=orders-consumer-tls",
"app.messaging.security.kafka.consumer.key-store.location=" + clientStore);
// when
List<PublishResult> results = new ArrayList<>();
runner.run(context -> results.add(publish(context.getBean(MessagePublisher.class))));
// then
assertThat(results)
.extracting(PublishResult::completion)
.containsExactly(PublishCompletion.CONFIRMED);
}
}
/**
* The Testcontainers broker with its external listener switched to a secured protocol.
*
* <p>The container names its host-facing listener {@code PLAINTEXT} and advertises it by that
* name; only the protocol that name maps to changes here.
*/
@SuppressWarnings("resource")
private static KafkaContainer broker(PrivateCa ca, String protocol) {
Path brokerStore = ca.issue("broker", ca.brokerSubjectAlternativeNames());
return new KafkaContainer(IMAGE)
.withEnv(
"KAFKA_LISTENER_SECURITY_PROTOCOL_MAP",
"BROKER:PLAINTEXT,PLAINTEXT:" + protocol + ",CONTROLLER:PLAINTEXT")
.withEnv("KAFKA_SSL_KEYSTORE_LOCATION", BROKER_STORE)
.withEnv("KAFKA_SSL_KEYSTORE_TYPE", "PKCS12")
.withEnv("KAFKA_SSL_KEYSTORE_PASSWORD", ca.password)
.withEnv("KAFKA_SSL_KEY_PASSWORD", ca.password)
.withCopyToContainer(MountableFile.forHostPath(brokerStore, 0644), BROKER_STORE);
}
/** The assembled platform, pointed at the broker through the private CA's trust store. */
private static ApplicationContextRunner platform(
KafkaContainer kafka, PrivateCa ca, Map<String, String> secrets) {
Map<String, String> all = new java.util.HashMap<>(secrets);
all.put("kafka-trust", ca.password);
return new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(MessagingPlatformRootAutoConfiguration.class))
.withPropertyValues(
"app.messaging.enabled=true",
"app.messaging.broker=kafka",
"spring.kafka.bootstrap-servers=" + kafka.getBootstrapServers(),
"app.messaging.brokers.kafka.type=kafka",
"app.messaging.brokers.kafka.bootstrap-servers[0]=" + kafka.getBootstrapServers(),
"app.messaging.brokers.kafka.tls-enabled=true",
"app.messaging.brokers.kafka.authentication-enabled=true",
"app.messaging.security.kafka.trust-store.location=" + ca.trustStore,
"app.messaging.security.kafka.trust-store.password-credential-id=kafka-trust",
"app.messaging.security.kafka.access.publishable[0]=order-events")
.withBean(CredentialProvider.class, () -> provider(all))
.withBean(
MessageContracts.class,
() ->
new MessageContracts(
Map.of(
new dev.caskeleton.messaging.schema.MessageContractKey(
new MessageType("order.created"), new SchemaVersion(1)),
String.class)))
.withBean(
"ordersProfile",
dev.caskeleton.messaging.policy.DestinationProfile.class,
() ->
MessagingLiveRoundTripQualificationTest.profile(
new DestinationName("order-events"), new DestinationName("order-events-dlq")))
.withBean(
"ordersDlqProfile",
dev.caskeleton.messaging.policy.DestinationProfile.class,
() ->
MessagingLiveRoundTripQualificationTest.profile(
new DestinationName("order-events-dlq"), null));
}
private static PublishResult publish(MessagePublisher publisher) {
return publisher
.publish(
new MessageDestination<>(
new DestinationName("order-events"),
new MessageType("order.created"),
String.class),
MessagingLiveRoundTripQualificationTest.envelope(),
PublishOptions.defaults())
.toCompletableFuture()
.join();
}
private static CredentialProvider provider(Map<String, String> secrets) {
return new CredentialProvider() {
@Override
public char[] resolve(String credentialId) {
return secrets.get(credentialId).toCharArray();
}
@Override
public Optional<Instant> expiresAt(String credentialId) {
return Optional.empty();
}
@Override
public Optional<String> username(String credentialId) {
return Optional.of(BROKER_USER);
}
};
}
/**
* A certificate authority made for one test run with the JDK's {@code keytool}.
*
* <p>Every store shares one generated password, which is also what the credential provider hands
* the client for the trust store and the client key store.
*/
private static final class PrivateCa {
private final Path directory;
private final String password = UUID.randomUUID().toString();
private final Path authority;
private final Path certificate;
private final Path trustStore;
private PrivateCa(Path directory) {
this.directory = directory;
this.authority = directory.resolve("ca.p12");
this.certificate = directory.resolve("ca.pem");
this.trustStore = directory.resolve("truststore.p12");
keytool(
"-genkeypair",
"-alias",
"ca",
"-keyalg",
"RSA",
"-keysize",
"2048",
"-validity",
"2",
"-dname",
"CN=messaging-test-ca",
"-ext",
"bc:c",
"-keystore",
authority.toString(),
"-storetype",
"PKCS12",
"-storepass",
password);
keytool(
"-exportcert",
"-rfc",
"-alias",
"ca",
"-keystore",
authority.toString(),
"-storepass",
password,
"-file",
certificate.toString());
keytool(
"-importcert",
"-noprompt",
"-alias",
"ca",
"-file",
certificate.toString(),
"-keystore",
trustStore.toString(),
"-storetype",
"PKCS12",
"-storepass",
password);
}
/** The names the client may reach the broker by, so hostname verification can pass. */
private String brokerSubjectAlternativeNames() {
String host = DockerClientFactory.instance().dockerHostIpAddress();
String dockerHost =
host.chars().allMatch(c -> Character.isDigit(c) || c == '.')
? "ip:" + host
: "dns:" + host;
return "dns:localhost,ip:127.0.0.1," + dockerHost;
}
/** Issues a key store holding a CA-signed certificate for {@code name}. */
private Path issue(String name, String subjectAlternativeNames) {
Path store = directory.resolve(name + ".p12");
Path request = directory.resolve(name + ".csr");
Path signed = directory.resolve(name + ".pem");
keytool(
"-genkeypair",
"-alias",
name,
"-keyalg",
"RSA",
"-keysize",
"2048",
"-validity",
"2",
"-dname",
"CN=" + name,
"-keystore",
store.toString(),
"-storetype",
"PKCS12",
"-storepass",
password);
keytool(
"-certreq",
"-alias",
name,
"-keystore",
store.toString(),
"-storepass",
password,
"-file",
request.toString());
keytool(
"-gencert",
"-rfc",
"-alias",
"ca",
"-keystore",
authority.toString(),
"-storepass",
password,
"-infile",
request.toString(),
"-outfile",
signed.toString(),
"-validity",
"2",
"-ext",
"SAN=" + subjectAlternativeNames);
keytool(
"-importcert",
"-noprompt",
"-alias",
"ca",
"-file",
certificate.toString(),
"-keystore",
store.toString(),
"-storepass",
password);
keytool(
"-importcert",
"-alias",
name,
"-file",
signed.toString(),
"-keystore",
store.toString(),
"-storepass",
password);
return store;
}
private static void keytool(String... arguments) {
List<String> command = new ArrayList<>();
command.add(Path.of(System.getProperty("java.home"), "bin", "keytool").toString());
command.addAll(List.of(arguments));
try {
Process process = new ProcessBuilder(command).redirectErrorStream(true).start();
String output = new String(process.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
if (!process.waitFor(60, TimeUnit.SECONDS) || process.exitValue() != 0) {
throw new IllegalStateException("keytool " + arguments[0] + " failed: " + output);
}
} catch (IOException failure) {
throw new IllegalStateException("keytool could not run", failure);
} catch (InterruptedException interrupted) {
Thread.currentThread().interrupt();
throw new IllegalStateException("keytool was interrupted", interrupted);
}
}
}
}
@@ -8,7 +8,9 @@ import org.springframework.boot.context.properties.bind.DefaultValue;
*
* <p>No field here holds a secret, and that is a structural rule rather than a convention: the
* platform stores a credential id and resolves the material through a {@code CredentialProvider} at
* connect time, so a configuration dump or a heap dump yields a name and nothing usable.
* connect time, so a configuration dump or a heap dump yields a name and nothing usable. Key and
* trust store passwords follow the same rule — the stores are named by path, their passwords by
* credential id.
*
* <p>The admin credential is bindable and its presence on a production broker is a startup failure.
* Leaving it unbindable would have been the safer-looking choice and the wrong one: an operator who
@@ -19,6 +21,7 @@ import org.springframework.boot.context.properties.bind.DefaultValue;
* @param consumer the consumer credential
* @param admin the admin credential, which an application runtime must not hold in production
* @param hostnameVerification whether the server certificate hostname is verified
* @param trustStore the certificates the connection trusts; absent means the JVM default store
* @param access which destinations each role may touch
*/
public record BrokerSecuritySettings(
@@ -26,6 +29,7 @@ public record BrokerSecuritySettings(
Credential consumer,
Credential admin,
@DefaultValue("true") boolean hostnameVerification,
TrustStore trustStore,
@DefaultValue Access access) {
/**
@@ -33,12 +37,35 @@ public record BrokerSecuritySettings(
*
* @param type the authentication mechanism
* @param credentialId the identifier the credential provider resolves
* @param keyStore the client certificate, for {@code MUTUAL_TLS} and nothing else
*/
public record Credential(String type, String credentialId) {}
public record Credential(String type, String credentialId, KeyStore keyStore) {}
/**
* A client certificate store, unlocked by the credential it belongs to.
*
* @param location the key store file
* @param type the key store format
*/
public record KeyStore(String location, @DefaultValue("PKCS12") String type) {}
/**
* The certificates the connection trusts.
*
* @param location the trust store file
* @param type the trust store format
* @param passwordCredentialId the credential that unlocks the store, when it is locked
*/
public record TrustStore(
String location, @DefaultValue("PKCS12") String type, String passwordCredentialId) {}
/**
* Which destinations each role may touch.
*
* <p>{@code publishable} is enforced: once a broker has a security entry, the platform publishes
* only to the declared destinations of that broker that this list names. An empty list therefore
* publishes nothing.
*
* @param publishable destinations the producer credential may publish to
* @param consumable destinations the consumer credential may consume
* @param administrable destinations the admin credential may operate on
@@ -14,6 +14,7 @@ import dev.caskeleton.messaging.policy.RetryPolicy;
import dev.caskeleton.messaging.policy.SchemaPolicy;
import dev.caskeleton.messaging.security.BrokerCredentialProfile;
import dev.caskeleton.messaging.security.BrokerSecurityProfile;
import dev.caskeleton.messaging.security.BrokerTrustStore;
import dev.caskeleton.messaging.security.DestinationAccessPolicy;
import java.util.ArrayList;
import java.util.LinkedHashSet;
@@ -235,7 +236,22 @@ public final class MessagingConfigurationCompiler {
new DestinationAccessPolicy(
destinationNames(security.access().publishable()),
destinationNames(security.access().consumable()),
destinationNames(security.access().administrable())));
destinationNames(security.access().administrable())),
Optional.ofNullable(security.trustStore()).map(store -> trustStore(name, store)));
}
private static BrokerTrustStore trustStore(
String broker, BrokerSecuritySettings.TrustStore store) {
if (blankToNull(store.location()) == null) {
throw configurationError(
key("security", broker, "trust-store.location"),
"a trust store entry needs the file it names; without one the JVM default store is used,"
+ " which is what leaving the entry out already says");
}
return new BrokerTrustStore(
store.location(),
store.type(),
Optional.ofNullable(blankToNull(store.passwordCredentialId())));
}
private static BrokerCredentialProfile credential(
@@ -252,10 +268,25 @@ public final class MessagingConfigurationCompiler {
}
String type = credential.type() == null ? "" : credential.type().toUpperCase(Locale.ROOT);
String id = credential.credentialId();
BrokerSecuritySettings.KeyStore keyStore = credential.keyStore();
boolean hasKeyStore = keyStore != null && blankToNull(keyStore.location()) != null;
if ("MUTUAL_TLS".equals(type) && !hasKeyStore) {
throw configurationError(
key("security", broker, role + ".key-store"),
"MUTUAL_TLS presents a client certificate, and without a key store there is none to"
+ " present; the broker refuses the handshake");
}
if (!"MUTUAL_TLS".equals(type) && keyStore != null) {
throw configurationError(
key("security", broker, role + ".key-store"),
"only a MUTUAL_TLS credential presents a key store; a %s credential would ignore it"
.formatted(credential.type()));
}
return switch (type) {
case "SASL_SCRAM" -> new BrokerCredentialProfile.SaslScram(id);
case "OAUTH2" -> new BrokerCredentialProfile.OAuth2(id);
case "MUTUAL_TLS" -> new BrokerCredentialProfile.MutualTls(id);
case "MUTUAL_TLS" ->
new BrokerCredentialProfile.MutualTls(id, keyStore.location(), keyStore.type());
case "USERNAME_PASSWORD" -> new BrokerCredentialProfile.UsernamePassword(id);
case "NKEY" -> new BrokerCredentialProfile.Nkey(id);
default ->
@@ -408,14 +408,48 @@ public class MessagingCoreAutoConfiguration {
* Declaring a destination profile is already an act of configuration, so a message to one nobody
* declared is a typo or a module reaching past its contract — which is what this check is for.
*
* <p>A broker with a security entry narrows that further to its {@code access.publishable} list.
* The list was compiled and then read by nothing, so a destination the security entry left out
* was published to anyway.
*
* @param destinations the validated registry
* @param compiled the security profiles compiled from configuration
* @param declared the security profiles the application declared as beans
* @return the access policy
*/
@Bean
@ConditionalOnMissingBean
public dev.caskeleton.messaging.security.DestinationAccessPolicy messagingDestinationAccess(
ValidatedDestinationRegistry destinations) {
return dev.caskeleton.messaging.runtime.DeclaredDestinationAccess.of(destinations.all());
ValidatedDestinationRegistry destinations,
CompiledMessagingConfiguration compiled,
ObjectProvider<dev.caskeleton.messaging.security.BrokerSecurityProfile> declared) {
java.util.Map<String, dev.caskeleton.messaging.security.DestinationAccessPolicy> byBroker =
new java.util.HashMap<>();
java.util.stream.Stream.concat(compiled.security().stream(), declared.orderedStream())
.forEach(
profile -> {
if (byBroker.putIfAbsent(profile.broker(), profile.access()) != null) {
throw new IllegalStateException(
"broker '"
+ profile.broker()
+ "' has more than one security profile; its access list is ambiguous");
}
});
java.util.Map<
dev.caskeleton.messaging.api.destination.DestinationName,
dev.caskeleton.messaging.policy.DestinationProfile>
permitted = new java.util.HashMap<>();
destinations
.all()
.forEach(
(name, profile) -> {
dev.caskeleton.messaging.security.DestinationAccessPolicy acl =
byBroker.get(profile.broker());
if (acl == null || acl.mayPublish(name)) {
permitted.put(name, profile);
}
});
return dev.caskeleton.messaging.runtime.DeclaredDestinationAccess.of(permitted);
}
/**
@@ -0,0 +1,103 @@
package dev.caskeleton.messaging.autoconfigure;
import static org.assertj.core.api.Assertions.assertThat;
import dev.caskeleton.messaging.api.ContentType;
import dev.caskeleton.messaging.api.MessageEnvelope;
import dev.caskeleton.messaging.api.MessageId;
import dev.caskeleton.messaging.api.MessageType;
import dev.caskeleton.messaging.api.ProducerId;
import dev.caskeleton.messaging.api.SchemaVersion;
import dev.caskeleton.messaging.api.TraceContext;
import dev.caskeleton.messaging.api.destination.DestinationName;
import dev.caskeleton.messaging.api.destination.MessageDestination;
import dev.caskeleton.messaging.api.error.FailureDescriptor;
import dev.caskeleton.messaging.api.header.MessageHeaders;
import dev.caskeleton.messaging.api.publish.MessagePublisher;
import dev.caskeleton.messaging.api.publish.PublishOptions;
import dev.caskeleton.messaging.api.publish.PublishResult;
import java.time.Instant;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
/**
* The broker's {@code access.publishable} list is enforced by the publish path, not only compiled.
*
* <p>The list was bound, validated and turned into a {@code DestinationAccessPolicy} that nothing
* read: the publisher's access check was built from the declared destinations alone, so a
* destination the security entry left out was published to anyway.
*/
class BrokerAccessListTest {
private final ApplicationContextRunner runner =
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(MessagingPlatformRootAutoConfiguration.class))
.withUserConfiguration(KafkaProducerSecurityTest.Credentials.class)
.withPropertyValues(
"app.messaging.enabled=true",
"app.messaging.broker=kafka",
"spring.kafka.bootstrap-servers=localhost:9092",
"app.messaging.brokers.kafka-primary.type=kafka",
"app.messaging.brokers.kafka-primary.bootstrap-servers[0]=localhost:9092",
"app.messaging.security.kafka-primary.producer.type=SASL_SCRAM",
"app.messaging.security.kafka-primary.producer.credential-id=kafka-producer",
"app.messaging.security.kafka-primary.consumer.type=SASL_SCRAM",
"app.messaging.security.kafka-primary.consumer.credential-id=kafka-consumer",
"app.messaging.security.kafka-primary.access.publishable[0]=order-events")
.withPropertyValues(destination("order-events"))
.withPropertyValues(destination("audit-events"));
@Test
void aDeclaredDestinationTheSecurityEntryDoesNotListIsRejectedAsForbidden() {
runner.run(
context -> {
// given
MessagePublisher publisher = context.getBean(MessagePublisher.class);
// when
PublishResult result = publish(publisher, "audit-events");
// then
assertThat(result.failure().map(FailureDescriptor::code)).contains("PUBLISH_FORBIDDEN");
});
}
private static PublishResult publish(MessagePublisher publisher, String destination) {
return publisher
.publish(
new MessageDestination<>(
new DestinationName(destination), new MessageType("order.created"), String.class),
new MessageEnvelope<>(
MessageId.newId(),
new MessageType("order.created"),
new SchemaVersion(1),
Instant.parse("2026-09-25T00:00:00Z"),
Optional.empty(),
new ProducerId("orders"),
Optional.empty(),
Optional.empty(),
ContentType.JSON,
Optional.empty(),
Optional.empty(),
Optional.empty(),
TraceContext.none(),
MessageHeaders.empty(),
"{}"),
PublishOptions.defaults())
.toCompletableFuture()
.join();
}
private static String[] destination(String name) {
String prefix = "app.messaging.destinations." + name + ".";
return new String[] {
prefix + "broker=kafka-primary",
prefix + "kind=EVENT_STREAM",
prefix + "physical.topic=" + name,
prefix + "schema.message-types[0]=order.created",
prefix + "guarantees.delivery=AT_MOST_ONCE",
};
}
}
@@ -6,8 +6,14 @@ import dev.caskeleton.messaging.kafka.KafkaSecurityConfigurer;
import dev.caskeleton.messaging.security.BrokerSecurityProfile;
import dev.caskeleton.messaging.security.CredentialProvider;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import org.apache.kafka.common.security.auth.AuthenticateCallbackHandler;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
@@ -44,6 +50,16 @@ class KafkaProducerSecurityTest {
"app.messaging.security.kafka-primary.consumer.credential-id=kafka-consumer",
};
private static final String[] MUTUAL_TLS_CREDENTIALS = {
"app.messaging.brokers.kafka-primary.tls-enabled=true",
"app.messaging.security.kafka-primary.producer.type=MUTUAL_TLS",
"app.messaging.security.kafka-primary.producer.credential-id=kafka-producer-tls",
"app.messaging.security.kafka-primary.producer.key-store.location=/etc/kafka/producer.p12",
"app.messaging.security.kafka-primary.consumer.type=MUTUAL_TLS",
"app.messaging.security.kafka-primary.consumer.credential-id=kafka-consumer-tls",
"app.messaging.security.kafka-primary.consumer.key-store.location=/etc/kafka/consumer.p12",
};
@Test
void aTlsBrokerWithScramCredentialsConfiguresTheProducerForSaslSsl() {
runner
@@ -81,16 +97,107 @@ class KafkaProducerSecurityTest {
}
@Test
void theProducerLogsInWithTheProducerCredentialResolvedFromTheProvider() {
void theProducerAuthenticatesAsTheUsernameTheProviderReturnsNotAsTheCredentialId() {
runner
.withUserConfiguration(Credentials.class)
.withPropertyValues(SCRAM_CREDENTIALS)
.withPropertyValues("app.messaging.brokers.kafka-primary.tls-enabled=true")
.run(
context -> {
// given
Map<String, Object> config = producerConfig(context);
// when
Answer answer = authenticate(config);
// then
assertThat(answer).isEqualTo(new Answer(Credentials.USERNAME, Credentials.SECRET));
});
}
@Test
void aRotatedPasswordReachesTheNextAuthenticationWithoutRebuildingTheProducer() {
runner
.withUserConfiguration(RotatingCredentials.class)
.withPropertyValues(SCRAM_CREDENTIALS)
.withPropertyValues("app.messaging.brokers.kafka-primary.tls-enabled=true")
.run(
context -> {
// given
Map<String, Object> config = producerConfig(context);
RotatingCredentials.CURRENT.set("rotated-secret");
// when
Answer answer = authenticate(config);
// then
assertThat(answer.password()).isEqualTo("rotated-secret");
});
}
@Test
void aPrivateCaTrustStoreReachesTheProducer() {
runner
.withUserConfiguration(Credentials.class, StoreFilesAbsent.class)
.withPropertyValues(SCRAM_CREDENTIALS)
.withPropertyValues(
"app.messaging.brokers.kafka-primary.tls-enabled=true",
"app.messaging.security.kafka-primary.trust-store.location=/etc/kafka/private-ca.p12",
"app.messaging.security.kafka-primary.trust-store.password-credential-id=kafka-trust")
.run(
context ->
assertThat(String.valueOf(producerConfig(context).get("sasl.jaas.config")))
.startsWith("org.apache.kafka.common.security.scram.ScramLoginModule required")
.contains("username=\"kafka-producer\"", "password=\"" + Credentials.SECRET));
assertThat(producerConfig(context))
.extractingByKeys(
"ssl.truststore.location", "ssl.truststore.type", "ssl.truststore.password")
.containsExactly("/etc/kafka/private-ca.p12", "PKCS12", Credentials.SECRET));
}
@Test
void mutualTlsPresentsTheProducerKeyStoreUnlockedByItsCredential() {
runner
.withUserConfiguration(Credentials.class, StoreFilesAbsent.class)
.withPropertyValues(MUTUAL_TLS_CREDENTIALS)
.run(
context ->
assertThat(producerConfig(context))
.extractingByKeys(
"security.protocol",
"ssl.keystore.location",
"ssl.keystore.type",
"ssl.keystore.password")
.containsExactly(
"SSL", "/etc/kafka/producer.p12", "PKCS12", Credentials.SECRET));
}
@Test
void mutualTlsWithoutAKeyStoreFailsStartupNamingTheKey() {
runner
.withUserConfiguration(Credentials.class)
.withPropertyValues(MUTUAL_TLS_CREDENTIALS)
.withPropertyValues("app.messaging.security.kafka-primary.producer.key-store.location=")
.run(
context ->
assertThat(context)
.hasFailed()
.getFailure()
.as("a client certificate nobody configured cannot be presented")
.hasMessageContaining(
"app.messaging.security.kafka-primary.producer.key-store: MUTUAL_TLS"));
}
@Test
void mutualTlsOnABrokerWithoutTlsFailsStartup() {
runner
.withUserConfiguration(Credentials.class)
.withPropertyValues(MUTUAL_TLS_CREDENTIALS)
.withPropertyValues("app.messaging.brokers.kafka-primary.tls-enabled=false")
.run(
context ->
assertThat(context)
.hasFailed()
.getFailure()
.as("a certificate only authenticates inside a TLS handshake")
.hasMessageContaining("authenticates with a client certificate but does not"));
}
@Test
@@ -127,12 +234,34 @@ class KafkaProducerSecurityTest {
context.getBeanProvider(KafkaSecurityConfigurer.class));
}
/** A credential source that answers every id with the same test secret. */
/** What a SASL client is told when it asks for a name and a password. */
private record Answer(String name, String password) {}
/**
* Authenticates the way Kafka's SASL client does: instantiate the configured handler class,
* configure it with the client's own configuration, then ask it for a name and a password.
*/
private static Answer authenticate(Map<String, Object> config) throws Exception {
assertThat(config).containsKey("sasl.client.callback.handler.class");
Class<?> type = (Class<?>) config.get("sasl.client.callback.handler.class");
AuthenticateCallbackHandler handler =
(AuthenticateCallbackHandler) type.getDeclaredConstructor().newInstance();
handler.configure(config, String.valueOf(config.get("sasl.mechanism")), List.of());
NameCallback name = new NameCallback("name");
PasswordCallback password = new PasswordCallback("password", false);
handler.handle(new Callback[] {name, password});
handler.close();
return new Answer(name.getName(), new String(password.getPassword()));
}
/** A credential source that answers every id with the same test principal and secret. */
@Configuration(proxyBeanMethods = false)
static class Credentials {
static final String SECRET = "test-only-secret";
static final String USERNAME = "svc-orders";
@Bean
CredentialProvider credentialProvider() {
return new CredentialProvider() {
@@ -145,6 +274,55 @@ class KafkaProducerSecurityTest {
public Optional<Instant> expiresAt(String credentialId) {
return Optional.empty();
}
@Override
public Optional<String> username(String credentialId) {
return Optional.of(USERNAME);
}
};
}
}
/**
* Stands in for the producer when the store paths in a test name no real file.
*
* <p>A real producer opens its key and trust stores while it is constructed, so a path that does
* not exist fails startup — which is the behaviour a deployment wants, and not what these tests
* are about. They read the configuration the producer would have been built from.
*/
@Configuration(proxyBeanMethods = false)
static class StoreFilesAbsent {
@Bean
org.apache.kafka.clients.producer.Producer<byte[], byte[]> messagingKafkaProducer() {
return new org.apache.kafka.clients.producer.MockProducer<>();
}
}
/** A credential store whose secret has already expired, so every resolution reads it again. */
@Configuration(proxyBeanMethods = false)
static class RotatingCredentials {
static final AtomicReference<String> CURRENT = new AtomicReference<>("first-secret");
@Bean
CredentialProvider credentialProvider() {
CURRENT.set("first-secret");
return new CredentialProvider() {
@Override
public char[] resolve(String credentialId) {
return CURRENT.get().toCharArray();
}
@Override
public Optional<Instant> expiresAt(String credentialId) {
return Optional.of(Instant.EPOCH);
}
@Override
public Optional<String> username(String credentialId) {
return Optional.of(Credentials.USERNAME);
}
};
}
}