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
@@ -44,13 +44,28 @@ public sealed interface BrokerCredentialProfile
}
/**
* Mutual TLS authentication.
* Mutual TLS authentication: the client certificate in a key store, unlocked by the credential.
*
* @param credentialId the credential id
* <p>The key store was not part of the profile, so a {@code MUTUAL_TLS} credential configured a
* TLS connection with no client certificate — one every broker that asks for a certificate
* refuses. The path is not a secret and lives here; the password is, and resolves through the
* credential id like every other secret.
*
* @param credentialId the credential whose material unlocks the key store
* @param keyStoreLocation the key store holding the client certificate and private key
* @param keyStoreType the key store format, such as {@code PKCS12}
*/
record MutualTls(String credentialId) implements BrokerCredentialProfile {
record MutualTls(String credentialId, String keyStoreLocation, String keyStoreType)
implements BrokerCredentialProfile {
public MutualTls {
CredentialIds.require(credentialId);
if (keyStoreLocation == null || keyStoreLocation.isBlank()) {
throw new IllegalArgumentException(
"mutual TLS presents a client certificate, and without a key store there is none");
}
if (keyStoreType == null || keyStoreType.isBlank()) {
throw new IllegalArgumentException("keyStoreType must not be blank");
}
}
}
@@ -18,6 +18,7 @@ import java.util.Optional;
* @param consumer the consumer credential
* @param admin the admin credential, absent in an application runtime
* @param access the destination access policy
* @param trustStore the certificates the connection trusts; empty means the JVM default trust store
*/
public record BrokerSecurityProfile(
String broker,
@@ -27,15 +28,39 @@ public record BrokerSecurityProfile(
BrokerCredentialProfile producer,
BrokerCredentialProfile consumer,
Optional<BrokerCredentialProfile> admin,
DestinationAccessPolicy access) {
DestinationAccessPolicy access,
Optional<BrokerTrustStore> trustStore) {
public BrokerSecurityProfile {
Objects.requireNonNull(producer, "producer credential must not be null");
Objects.requireNonNull(consumer, "consumer credential must not be null");
Objects.requireNonNull(admin, "admin credential must not be null");
Objects.requireNonNull(access, "access policy must not be null");
Objects.requireNonNull(trustStore, "trust store must not be null");
if (broker == null || broker.isBlank()) {
throw new IllegalArgumentException("broker must not be blank");
}
}
/** A profile that trusts what the JVM's default trust store trusts. */
public BrokerSecurityProfile(
String broker,
boolean production,
boolean tlsEnabled,
boolean hostnameVerification,
BrokerCredentialProfile producer,
BrokerCredentialProfile consumer,
Optional<BrokerCredentialProfile> admin,
DestinationAccessPolicy access) {
this(
broker,
production,
tlsEnabled,
hostnameVerification,
producer,
consumer,
admin,
access,
Optional.empty());
}
}
@@ -0,0 +1,30 @@
package dev.caskeleton.messaging.security;
import java.util.Objects;
import java.util.Optional;
/**
* The certificates a broker connection trusts, when the JVM's default trust store is not enough.
*
* <p>A broker signed by a private CA could not be reached at all: there was no setting for a trust
* store, so the client verified the broker against the JVM defaults and refused it. The path is not
* a secret; the store password, when the format has one, resolves through a credential id.
*
* @param location the trust store file
* @param type the trust store format, such as {@code PKCS12} or {@code PEM}
* @param passwordCredentialId the credential whose material unlocks the store, when it is locked
*/
public record BrokerTrustStore(
String location, String type, Optional<String> passwordCredentialId) {
public BrokerTrustStore {
Objects.requireNonNull(passwordCredentialId, "passwordCredentialId must not be null");
if (location == null || location.isBlank()) {
throw new IllegalArgumentException("a trust store needs a location");
}
if (type == null || type.isBlank()) {
throw new IllegalArgumentException("a trust store needs a type");
}
passwordCredentialId.ifPresent(CredentialIds::require);
}
}
@@ -26,4 +26,21 @@ public interface CredentialProvider {
* @return the expiry instant when known
*/
Optional<Instant> expiresAt(String credentialId);
/**
* Returns the user name the secret authenticates, for mechanisms that send one.
*
* <p>SASL PLAIN and SCRAM send a user name and a password. The credential id is a reference into
* the store, not the principal the broker knows, and sending it as the user name authenticated as
* whoever happened to share the reference's spelling. A store that rotates by alternating between
* two users rotates the name together with the secret, so the name is resolved alongside it.
*
* <p>Empty by default. A SASL credential whose provider names no user is refused at startup.
*
* @param credentialId the identifier
* @return the user name, when this credential has one
*/
default Optional<String> username(String credentialId) {
return Optional.empty();
}
}
@@ -26,12 +26,14 @@ public final class CredentialRuntime {
private final Instant resolvedAt;
private final Optional<Instant> expiresAt;
private final Duration rotationLead;
private final Optional<String> username;
private char[] material;
/**
* Creates a runtime entry.
*
* @param credentialId the credential reference
* @param username the user name the secret authenticates, when the mechanism sends one
* @param material the resolved secret, taken by reference and cleared by {@link #clear()}
* @param resolvedAt when the material was resolved
* @param expiresAt when it stops being valid, when known
@@ -39,11 +41,13 @@ public final class CredentialRuntime {
*/
public CredentialRuntime(
String credentialId,
Optional<String> username,
char[] material,
Instant resolvedAt,
Optional<Instant> expiresAt,
Duration rotationLead) {
CredentialIds.require(credentialId);
Objects.requireNonNull(username, "username must not be null");
Objects.requireNonNull(material, "material must not be null");
Objects.requireNonNull(resolvedAt, "resolvedAt must not be null");
Objects.requireNonNull(expiresAt, "expiresAt must not be null");
@@ -55,6 +59,7 @@ public final class CredentialRuntime {
throw new IllegalArgumentException("credential material must not be empty");
}
this.credentialId = credentialId;
this.username = username;
this.material = material;
this.resolvedAt = resolvedAt;
this.expiresAt = expiresAt;
@@ -70,6 +75,15 @@ public final class CredentialRuntime {
return credentialId;
}
/**
* Returns the user name this secret authenticates, when the mechanism sends one.
*
* @return the user name
*/
public Optional<String> username() {
return username;
}
/**
* Returns a copy of the material.
*
@@ -134,6 +134,7 @@ public final class CredentialRuntimeRegistry {
private CredentialRuntime fetch(String credentialId, Instant now) {
char[] material = provider.resolve(credentialId);
Optional<Instant> expiresAt = provider.expiresAt(credentialId);
return new CredentialRuntime(credentialId, material, now, expiresAt, rotationLead);
return new CredentialRuntime(
credentialId, provider.username(credentialId), material, now, expiresAt, rotationLead);
}
}