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

This commit is contained in:
DongHyeonka
2026-09-25 17:57:07 +09:00
parent fd80de369d
commit 2c7d1d58f3
44 changed files with 315 additions and 1852 deletions
@@ -10,11 +10,11 @@ import dev.caskeleton.grpc.client.GrpcNamedChannelProfile;
import dev.caskeleton.grpc.context.GrpcContextBinder;
import dev.caskeleton.grpc.context.GrpcContextPropagationPolicy;
import dev.caskeleton.grpc.error.GrpcErrorMapper;
import dev.caskeleton.grpc.security.GrpcTlsProfile;
import dev.caskeleton.grpc.server.GrpcAdmissionController;
import dev.caskeleton.grpc.server.GrpcExecutorProfile;
import dev.caskeleton.grpc.server.GrpcServerProfile;
import java.time.Duration;
import java.util.Set;
import java.util.UUID;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.SmartInitializingSingleton;
@@ -80,16 +80,23 @@ public class GrpcPlatformAutoConfiguration {
return new GrpcServiceHealthRegistry(GrpcHealthPolicy.standalone());
}
/** The reflection policy, defaulted from the environment. */
/**
* The reflection policy, defaulted from the environment. An unset environment gets the strictest
* floor here; the startup check then refuses the configuration with the reason.
*/
@Bean
@ConditionalOnMissingBean
public GrpcReflectionPolicy grpcReflectionPolicy(GrpcPlatformProperties properties) {
return properties.getReflectionMode() == null
? GrpcReflectionPolicy.defaultFor(properties.getEnvironment())
: new GrpcReflectionPolicy(
properties.getReflectionMode(),
java.util.Set.of("admin"),
java.util.Set.of("ROLE_PLATFORM_ADMIN"));
if (properties.getReflectionMode() != null) {
return new GrpcReflectionPolicy(
properties.getReflectionMode(),
java.util.Set.of("admin"),
java.util.Set.of("ROLE_PLATFORM_ADMIN"));
}
return GrpcReflectionPolicy.defaultFor(
properties.getEnvironment() == null
? GrpcTlsProfile.Environment.PROD
: properties.getEnvironment());
}
/** The admin exposure gates. */
@@ -116,9 +123,9 @@ public class GrpcPlatformAutoConfiguration {
/**
* Runs the Stable startup validator against the bean graph after singleton assembly.
*
* <p>The Gradle architecture gate owns dependency-graph isolation. Runtime validation therefore
* passes no synthetic dependency list; it validates only inputs a running process can actually
* observe: typed properties, the method-policy catalog and named client channels.
* <p>The Gradle architecture gate owns dependency-graph isolation. Runtime validation checks only
* inputs a running process can actually observe: typed properties, the method-policy catalog and
* named client channels.
*/
@Bean
public SmartInitializingSingleton grpcPlatformStartupCheck(
@@ -129,8 +136,7 @@ public class GrpcPlatformAutoConfiguration {
GrpcPlatformStartupValidator.requireValid(
properties,
catalogs.getIfAvailable(() -> GrpcMethodPolicyCatalog.builder().build()),
channelProfiles.orderedStream().toList(),
Set.of());
channelProfiles.orderedStream().toList());
}
/** The shared error mapper. Its execution ids are random and carry no derived content. */
@@ -22,8 +22,12 @@ public class GrpcPlatformProperties {
/** Whether the platform is assembled at all. */
private boolean enabled;
/** Which environment this deployment is, which decides the TLS and reflection floors. */
private GrpcTlsProfile.Environment environment = GrpcTlsProfile.Environment.LOCAL;
/**
* Which environment this deployment is, which decides the TLS and reflection floors. No default:
* a missing value used to mean LOCAL, which silently switched every TLS and reflection check off,
* so an enabled platform without it is refused at startup.
*/
private GrpcTlsProfile.Environment environment;
/** The server transport. Only a production transport is accepted outside tests. */
private GrpcServerTransport transport = GrpcServerTransport.NETTY_SHADED;
@@ -120,8 +124,16 @@ public class GrpcPlatformProperties {
this.operationLedgerEnabled = operationLedgerEnabled;
}
/** The reflection mode this deployment runs, defaulting to the environment's floor. */
/**
* The reflection mode this deployment runs, defaulting to the environment's floor, and to
* disabled while the environment is unset (the startup check refuses that state).
*/
public GrpcReflectionMode effectiveReflectionMode() {
return reflectionMode == null ? GrpcReflectionMode.defaultFor(environment) : reflectionMode;
if (reflectionMode != null) {
return reflectionMode;
}
return environment == null
? GrpcReflectionMode.DISABLED
: GrpcReflectionMode.defaultFor(environment);
}
}
@@ -6,7 +6,6 @@ import dev.caskeleton.grpc.api.policy.GrpcMethodPolicyCatalog;
import dev.caskeleton.grpc.api.policy.RpcIdempotencyProfile;
import dev.caskeleton.grpc.client.GrpcNamedChannelProfile;
import dev.caskeleton.grpc.core.GrpcMethodName;
import dev.caskeleton.grpc.core.GrpcStableBuildInvariant;
import dev.caskeleton.grpc.core.RpcType;
import dev.caskeleton.grpc.discovery.GrpcDiscoveryPolicyValidator;
import dev.caskeleton.grpc.resilience.GrpcRetryOwner;
@@ -33,20 +32,17 @@ public final class GrpcPlatformStartupValidator {
/**
* Every reason this configuration is refused.
*
* @param stableModuleDependencies dependency ids supplied by a caller that can observe a runtime
* dependency set; the Spring auto-configuration passes an empty set because the Gradle
* registry gate is authoritative for the compiled graph
* <p>Stable-to-Advanced module isolation is not checked here: the Gradle registry gate ({@code
* verifyCleanArchitectureDependencies}) owns the compiled graph, and a running process has no
* dependency list to check it against.
*
* @return an empty list when the configuration is safe to start
*/
public static List<String> violations(
GrpcPlatformProperties properties,
GrpcMethodPolicyCatalog catalog,
List<GrpcNamedChannelProfile> channelProfiles,
Set<String> stableModuleDependencies) {
if (properties == null
|| catalog == null
|| channelProfiles == null
|| stableModuleDependencies == null) {
List<GrpcNamedChannelProfile> channelProfiles) {
if (properties == null || catalog == null || channelProfiles == null) {
throw new IllegalArgumentException("startup validation needs every input");
}
List<String> violations = new ArrayList<>();
@@ -55,7 +51,6 @@ public final class GrpcPlatformStartupValidator {
validateExecutor(properties, violations);
validateMethods(properties, catalog, violations);
validateChannels(channelProfiles, violations);
validateAdvancedIsolation(stableModuleDependencies, violations);
return List.copyOf(violations);
}
@@ -67,10 +62,8 @@ public final class GrpcPlatformStartupValidator {
public static void requireValid(
GrpcPlatformProperties properties,
GrpcMethodPolicyCatalog catalog,
List<GrpcNamedChannelProfile> channelProfiles,
Set<String> stableModuleDependencies) {
List<String> violations =
violations(properties, catalog, channelProfiles, stableModuleDependencies);
List<GrpcNamedChannelProfile> channelProfiles) {
List<String> violations = violations(properties, catalog, channelProfiles);
if (!violations.isEmpty()) {
throw new GrpcPlatformConfigurationException(violations);
}
@@ -84,6 +77,12 @@ public final class GrpcPlatformStartupValidator {
+ properties.getTransport()
+ " may not serve production traffic; in-process evidence is not network evidence");
}
if (properties.getEnvironment() == null) {
violations.add(
"ca-skeleton.grpc.platform.environment is not set; the TLS and reflection floors depend on"
+ " it, so it is refused rather than assumed LOCAL");
return;
}
boolean deployed = properties.getEnvironment().tlsRequired();
if (deployed && !properties.isTlsEnabled()) {
violations.add("TLS is required in " + properties.getEnvironment() + " and is disabled");
@@ -172,16 +171,6 @@ public final class GrpcPlatformStartupValidator {
}
}
private static void validateAdvancedIsolation(
Set<String> stableModuleDependencies, List<String> violations) {
try {
GrpcStableBuildInvariant.requireNoAdvancedDependency(
"grpc-spring-boot-starter", stableModuleDependencies);
} catch (IllegalStateException leaked) {
violations.add(leaked.getMessage());
}
}
private static List<GrpcMethodName> sorted(Set<GrpcMethodName> methods) {
return methods.stream()
.sorted(java.util.Comparator.comparing(GrpcMethodName::canonical))
@@ -2,6 +2,7 @@ package dev.caskeleton.grpc.boot;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.catchThrowable;
import java.util.Map;
import org.junit.jupiter.api.Test;
@@ -26,6 +27,25 @@ class GrpcPlatformAutoConfigurationContextTest {
.hasMessageContaining("providing none");
}
@Test
void enabledPlatformWithoutAnEnvironmentRefusesToStartInsteadOfAssumingLocal() {
// when
Throwable failure =
catchThrowable(
() ->
start(
Map.of(
"ca-skeleton.grpc.platform.enabled", "true",
"ca-skeleton.grpc.platform.transport", "NETTY_SHADED",
"ca-skeleton.grpc.platform.tls-enabled", "false",
"ca-skeleton.grpc.platform.reflection-mode", "ENABLED")));
// then
assertThat(failure)
.isInstanceOf(GrpcPlatformConfigurationException.class)
.hasMessageContaining("ca-skeleton.grpc.platform.environment is not set");
}
@Test
void coherentLocalConfigurationRunsTheStartupValidatorAndStarts() {
try (AnnotationConfigApplicationContext context =
@@ -23,7 +23,6 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.List;
import java.util.Set;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@@ -36,9 +35,6 @@ class GrpcPlatformStartupValidatorTest {
private static final GrpcTlsProfile PROD_TLS =
GrpcTlsProfile.serverAuthenticated(GrpcTlsProfile.Environment.PROD, "trust-bundle");
private static final Set<String> STABLE_DEPENDENCIES =
Set.of("grpc-core-api", "grpc-policy", "grpc-server", "grpc-client");
private static GrpcPlatformProperties productionProperties() {
GrpcPlatformProperties properties = new GrpcPlatformProperties();
properties.setEnabled(true);
@@ -73,10 +69,10 @@ class GrpcPlatformStartupValidatorTest {
void aCoherentConfigurationStarts() {
assertThat(
GrpcPlatformStartupValidator.violations(
productionProperties(), healthyCatalog(), oneChannel(), STABLE_DEPENDENCIES))
productionProperties(), healthyCatalog(), oneChannel()))
.isEmpty();
GrpcPlatformStartupValidator.requireValid(
productionProperties(), healthyCatalog(), oneChannel(), STABLE_DEPENDENCIES);
productionProperties(), healthyCatalog(), oneChannel());
}
@Test
@@ -91,9 +87,7 @@ class GrpcPlatformStartupValidatorTest {
GrpcPlatformProperties properties = productionProperties();
properties.setTransport(GrpcServerTransport.IN_PROCESS);
assertThat(
GrpcPlatformStartupValidator.violations(
properties, healthyCatalog(), oneChannel(), STABLE_DEPENDENCIES))
assertThat(GrpcPlatformStartupValidator.violations(properties, healthyCatalog(), oneChannel()))
.anySatisfy(violation -> assertThat(violation).contains("not network evidence"));
}
@@ -105,13 +99,9 @@ class GrpcPlatformStartupValidatorTest {
GrpcPlatformProperties trustAll = productionProperties();
trustAll.setTrustAllCertificates(true);
assertThat(
GrpcPlatformStartupValidator.violations(
noTls, healthyCatalog(), oneChannel(), STABLE_DEPENDENCIES))
assertThat(GrpcPlatformStartupValidator.violations(noTls, healthyCatalog(), oneChannel()))
.anySatisfy(violation -> assertThat(violation).contains("TLS is required"));
assertThat(
GrpcPlatformStartupValidator.violations(
trustAll, healthyCatalog(), oneChannel(), STABLE_DEPENDENCIES))
assertThat(GrpcPlatformStartupValidator.violations(trustAll, healthyCatalog(), oneChannel()))
.anySatisfy(violation -> assertThat(violation).contains("providing none"));
}
@@ -121,9 +111,7 @@ class GrpcPlatformStartupValidatorTest {
GrpcPlatformProperties properties = productionProperties();
properties.setReflectionMode(GrpcReflectionMode.ENABLED);
assertThat(
GrpcPlatformStartupValidator.violations(
properties, healthyCatalog(), oneChannel(), STABLE_DEPENDENCIES))
assertThat(GrpcPlatformStartupValidator.violations(properties, healthyCatalog(), oneChannel()))
.anySatisfy(violation -> assertThat(violation).contains("published to anyone"));
assertThat(productionProperties().effectiveReflectionMode())
.isEqualTo(GrpcReflectionMode.DISABLED);
@@ -135,9 +123,7 @@ class GrpcPlatformStartupValidatorTest {
GrpcPlatformProperties properties = productionProperties();
properties.setExecutorQueueCapacity(0);
assertThat(
GrpcPlatformStartupValidator.violations(
properties, healthyCatalog(), oneChannel(), STABLE_DEPENDENCIES))
assertThat(GrpcPlatformStartupValidator.violations(properties, healthyCatalog(), oneChannel()))
.anySatisfy(violation -> assertThat(violation).contains("unbounded latency"));
}
@@ -160,9 +146,7 @@ class GrpcPlatformStartupValidatorTest {
1024))
.build();
assertThat(
GrpcPlatformStartupValidator.violations(
properties, catalog, oneChannel(), STABLE_DEPENDENCIES))
assertThat(GrpcPlatformStartupValidator.violations(properties, catalog, oneChannel()))
.anySatisfy(violation -> assertThat(violation).contains("never honoured"));
}
@@ -184,8 +168,7 @@ class GrpcPlatformStartupValidatorTest {
.build();
assertThat(
GrpcPlatformStartupValidator.violations(
productionProperties(), catalog, oneChannel(), STABLE_DEPENDENCIES))
GrpcPlatformStartupValidator.violations(productionProperties(), catalog, oneChannel()))
.anySatisfy(violation -> assertThat(violation).contains("Advanced capability"));
}
@@ -207,21 +190,10 @@ class GrpcPlatformStartupValidatorTest {
assertThat(
GrpcPlatformStartupValidator.violations(
productionProperties(), healthyCatalog(), twoOwners, STABLE_DEPENDENCIES))
productionProperties(), healthyCatalog(), twoOwners))
.anySatisfy(violation -> assertThat(violation).contains("multiply the load"));
}
@Test
@DisplayName("the Stable starter reaching an Advanced module is refused at startup too")
void advancedLeakIsRefusedAtStartup() {
Set<String> leaked = Set.of("grpc-core-api", "grpc-policy", "grpc-advanced-streaming");
assertThat(
GrpcPlatformStartupValidator.violations(
productionProperties(), healthyCatalog(), oneChannel(), leaked))
.anySatisfy(violation -> assertThat(violation).contains("grpc-advanced-streaming"));
}
@Test
@DisplayName("a refusal names every violation at once")
void aRefusalNamesEveryViolation() {
@@ -231,9 +203,7 @@ class GrpcPlatformStartupValidatorTest {
broken.setExecutorQueueCapacity(0);
assertThatThrownBy(
() ->
GrpcPlatformStartupValidator.requireValid(
broken, healthyCatalog(), oneChannel(), STABLE_DEPENDENCIES))
() -> GrpcPlatformStartupValidator.requireValid(broken, healthyCatalog(), oneChannel()))
.isInstanceOf(GrpcPlatformConfigurationException.class)
.satisfies(
failure ->