feat: jpa, messaging, notification, mongo, graphql 어댑터터 구현체 추가

This commit is contained in:
DongHyeonka
2026-08-15 13:01:58 +09:00
parent e23ed9486e
commit f56dd3e4eb
174 changed files with 10338 additions and 1274 deletions
@@ -19,8 +19,8 @@ import java.util.Set;
public final class BrokerTlsPolicy {
/** Protocol versions with known practical attacks. */
private static final Set<String> FORBIDDEN_PROTOCOLS =
Set.of("SSLv2", "SSLv3", "TLSv1", "TLSv1.1");
/** The only protocol versions this platform will negotiate. */
private static final Set<String> ACCEPTED_PROTOCOLS = Set.of("TLSv1.2", "TLSv1.3");
/** The minimum acceptable protocol version. */
public static final String MINIMUM_PROTOCOL = "TLSv1.2";
@@ -69,13 +69,22 @@ public final class BrokerTlsPolicy {
.formatted(profile.broker()));
}
List<String> forbidden =
enabledProtocols.stream().filter(FORBIDDEN_PROTOCOLS::contains).toList();
if (!forbidden.isEmpty()) {
// An allowlist, not a denylist.
//
// The denylist named the old versions somebody thought of, so `SSL`, `TLSv0.9`, `PLAINTEXT`
// and any typo passed — and a protocol string the JVM does not recognise is negotiated as
// whatever the JVM defaults to, which is the outcome this policy exists to prevent. Naming the
// two acceptable versions means an unknown string fails here rather than at connect time on a
// production broker.
List<String> unsupported =
enabledProtocols.stream()
.filter(protocol -> !ACCEPTED_PROTOCOLS.contains(protocol))
.toList();
if (!unsupported.isEmpty()) {
throw new MessagingConfigurationException(
"TLS_PROTOCOL_TOO_OLD",
"broker %s enables %s; the minimum is %s"
.formatted(profile.broker(), forbidden, MINIMUM_PROTOCOL));
"TLS_PROTOCOL_NOT_ACCEPTED",
"broker %s enables %s; only %s are accepted"
.formatted(profile.broker(), unsupported, ACCEPTED_PROTOCOLS));
}
if (enabledProtocols.isEmpty()) {
throw new MessagingConfigurationException(
@@ -93,6 +102,6 @@ public final class BrokerTlsPolicy {
* @return true when the version may be negotiated
*/
public static boolean isAcceptable(String protocol) {
return protocol != null && !FORBIDDEN_PROTOCOLS.contains(protocol);
return protocol != null && ACCEPTED_PROTOCOLS.contains(protocol);
}
}
@@ -59,14 +59,31 @@ public final class CredentialRuntimeRegistry {
if (current != null && !current.isDueForRotation(now)) {
return current;
}
CredentialRuntime replacement = fetch(credentialId, now);
resolved.put(credentialId, replacement);
if (current != null) {
// Clear only after the replacement is installed, so a concurrent reader never observes a
// window with no usable credential.
current.clear();
}
return replacement;
// Single-flight, keyed by credential id.
//
// get → fetch → put → clear had no synchronization at all. Two callers rotating the same
// credential both read the same old runtime and both fetched a replacement: one replacement
// was lost from the map without ever being cleared — a secret left in memory that nothing owns
// — and the caller that lost the race could clear material the winner was still using.
//
// compute holds the bin lock for this key, so exactly one fetch publishes and the previous
// generation is retired by that same caller.
return resolved.compute(
credentialId,
(key, existing) -> {
if (existing != null && !existing.isDueForRotation(now)) {
// Another thread rotated while this one waited; its replacement is the current one.
return existing;
}
CredentialRuntime replacement = fetch(key, now);
if (existing != null) {
// Cleared only after the replacement is installed, so no reader sees a window with no
// usable credential — and only by the thread that replaced it, so the material a
// concurrent reader holds is never wiped underneath it.
existing.clear();
}
return replacement;
});
}
/**
@@ -0,0 +1,109 @@
package dev.caskeleton.messaging.security;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
/**
* Rotation under concurrency, and the protocol allowlist.
*
* <p>{@code resolve} was get → fetch → put → clear with no synchronization. Two callers rotating
* the same credential both read the same old runtime and both fetched a replacement: one
* replacement was dropped from the map without ever being cleared — a secret left in memory that
* nothing owns — and the loser could clear material the winner was still using.
*/
class CredentialRotationContractTest {
private static final Instant NOW = Instant.parse("2026-08-14T00:00:00Z");
@Test
@DisplayName("a hundred concurrent resolves fetch once and publish one generation")
void concurrentResolvesFetchOnce() throws Exception {
AtomicInteger fetches = new AtomicInteger();
CredentialRuntimeRegistry registry = registry(fetches);
ExecutorService pool = Executors.newFixedThreadPool(16);
try {
List<Callable<CredentialRuntime>> work =
IntStream.range(0, 100)
.<Callable<CredentialRuntime>>mapToObj(
index -> () -> registry.resolve("kafka-app", NOW))
.toList();
List<CredentialRuntime> resolved = new java.util.ArrayList<>();
for (Future<CredentialRuntime> future : pool.invokeAll(work)) {
resolved.add(future.get());
}
assertThat(fetches)
.as("every caller fetching its own replacement leaves secrets nobody owns")
.hasValue(1);
assertThat(resolved).allSatisfy(runtime -> assertThat(runtime).isSameAs(resolved.get(0)));
} finally {
pool.shutdownNow();
}
}
@Test
@DisplayName("the surviving generation still has its material")
void theSurvivingGenerationKeepsItsMaterial() {
CredentialRuntimeRegistry registry = registry(new AtomicInteger());
CredentialRuntime resolved = registry.resolve("kafka-app", NOW);
assertThatCode(resolved::material)
.as("the losing caller used to clear material the winner was still using")
.doesNotThrowAnyException();
}
@Test
@DisplayName("only TLS 1.2 and 1.3 are acceptable")
void onlyModernTlsIsAcceptable() {
assertThat(BrokerTlsPolicy.isAcceptable("TLSv1.2")).isTrue();
assertThat(BrokerTlsPolicy.isAcceptable("TLSv1.3")).isTrue();
// Each of these passed the denylist: an unrecognised protocol string negotiates whatever the
// JVM defaults to, which is what the policy exists to prevent.
assertThat(BrokerTlsPolicy.isAcceptable("SSL")).isFalse();
assertThat(BrokerTlsPolicy.isAcceptable("TLSv0.9")).isFalse();
assertThat(BrokerTlsPolicy.isAcceptable("PLAINTEXT")).isFalse();
assertThat(BrokerTlsPolicy.isAcceptable("tlsv1.3")).isFalse();
assertThat(BrokerTlsPolicy.isAcceptable(null)).isFalse();
}
private static CredentialRuntimeRegistry registry(AtomicInteger fetches) {
return new CredentialRuntimeRegistry(
new CredentialProvider() {
@Override
public char[] resolve(String credentialId) {
fetches.incrementAndGet();
return "s3cr3t".toCharArray();
}
@Override
public java.util.Optional<Instant> expiresAt(String credentialId) {
return java.util.Optional.of(NOW.plus(Duration.ofHours(1)));
}
},
Duration.ofMinutes(5));
}
@Test
@DisplayName("an unknown credential id is refused rather than fetched")
void anUnknownCredentialIdIsRefused() {
CredentialRuntimeRegistry registry = registry(new AtomicInteger());
assertThatThrownBy(() -> registry.resolve(" ", NOW))
.isInstanceOf(IllegalArgumentException.class);
}
}