This commit is contained in:
@@ -323,6 +323,9 @@ relay 조립(`OutboxRelayAutoConfiguration`, 스케줄러·metrics·leader token
|
||||
- **시크릿 값만 가리고 키/스킴은 남긴다.** `token=abc123` → `token=****`,
|
||||
`Authorization: Bearer eyJ...` → `Authorization: Bearer ****`처럼 동작한다. 키와 인증 스킴(`Bearer`
|
||||
등)을 남기는 이유는 진단할 때 "어떤 종류의 자격증명이 있었는지"는 알아야 하기 때문이다.
|
||||
- **URI의 사용자 정보 비밀번호와 PEM 블록도 가린다.** `jdbc:postgresql://app:pw@db` → `jdbc:postgresql://app:****@db`
|
||||
(사용자 이름은 남긴다, secrets registry의 `userinfo` 규칙). `-----BEGIN …-----`부터 `-----END …-----`까지(키·인증서)는
|
||||
여러 줄이어도 본문 전체를 가리고 두 경계 줄만 남긴다. 같은 라벨의 END가 없으면 끝까지 가린다.
|
||||
- **정규식 마스킹은 보증이 아니라 심층 방어(defence-in-depth)의 보조 수단이다.** 1차 방어선은
|
||||
"로거가 애초에 본문/페이로드를 받지 않도록 설계한 것"(`FailOpenDependencyLogger`)이고, 이 정규식은
|
||||
그걸 빠져나간 누출을 잡는 그물이다. 키 접두어 없는 Base64URL 블롭 같은 난독화된 형태는 놓칠 수
|
||||
@@ -346,6 +349,10 @@ relay 조립(`OutboxRelayAutoConfiguration`, 스케줄러·metrics·leader token
|
||||
(`FilterReply.NEUTRAL`). 진단·장애 신호인 경고/오류는 100% 보장해야 하고, 샘플링 대상은 `INFO`
|
||||
이하만이라는 정책 때문이다. `INFO` 이하는 확률 `rate`로 보존하고 나머지는 `FilterReply.DENY`로
|
||||
버린다.
|
||||
- **감사·보안 기록은 레벨과 상관없이 샘플링하지 않는다.** logger 이름의 마지막 마디가 `audit`·`security`이거나
|
||||
(`notification.audit`, `dev.caskeleton.fileserver.audit`, `notification.security`) `AUDIT` marker를 단 줄이다.
|
||||
감사 logger가 INFO로 쓰므로 그렇지 않으면 prod 권장값 `0.1`에서 감사 기록 열에 아홉이 버려진다.
|
||||
다른 이름의 감사 logger는 `AUDIT` marker를 단다.
|
||||
- **단일 `rate` 노브로 제어한다.** `APP_LOG_SAMPLING_RATE`(logback `springProperty`)에서 주입되고,
|
||||
운영자가 런타임별로 설정한다(prod `0.1` = INFO 10% 샘플링, dev/local `1.0` = 전부 보존).
|
||||
고트래픽 vs 일반 엔드포인트의 더 세밀한 분기는 이 전역 rate 위에 마커 기반 확장으로 문서화만 되어
|
||||
|
||||
@@ -47,7 +47,7 @@ services:
|
||||
environment:
|
||||
PGHOST: "db"
|
||||
PGUSER: "${APP_DATASOURCE_USERNAME:-ca_skeleton}"
|
||||
PGPASSWORD: "${APP_DATASOURCE_PASSWORD:-ca_skeleton}"
|
||||
PGPASSWORD: "${APP_DATASOURCE_PASSWORD:-__LOCAL_DEV_db_password__}"
|
||||
PGDATABASE: "${POSTGRES_DB:-ca_skeleton}"
|
||||
CAPABILITY_STREAMS: "${CAPABILITY_STREAMS:-}"
|
||||
volumes:
|
||||
@@ -82,7 +82,7 @@ services:
|
||||
environment:
|
||||
PGHOST: "db"
|
||||
PGUSER: "${APP_DATASOURCE_USERNAME:-ca_skeleton}"
|
||||
PGPASSWORD: "${APP_DATASOURCE_PASSWORD:-ca_skeleton}"
|
||||
PGPASSWORD: "${APP_DATASOURCE_PASSWORD:-__LOCAL_DEV_db_password__}"
|
||||
PGDATABASE: "${POSTGRES_DB:-ca_skeleton}"
|
||||
CAPABILITY_STREAMS: "${CAPABILITY_STREAMS:-}"
|
||||
volumes:
|
||||
@@ -176,7 +176,7 @@ services:
|
||||
environment:
|
||||
MINIO_ENDPOINT: "http://minio:9000"
|
||||
MINIO_ROOT_USER: "${MINIO_ROOT_USER:-caskeleton}"
|
||||
MINIO_ROOT_PASSWORD: "${MINIO_ROOT_PASSWORD:-caskeleton-local}"
|
||||
MINIO_ROOT_PASSWORD: "${MINIO_ROOT_PASSWORD:-__LOCAL_DEV_minio_root__}"
|
||||
MINIO_BUCKET: "${MINIO_BUCKET:-ca-skeleton-objects}"
|
||||
volumes:
|
||||
- type: bind
|
||||
|
||||
+30
-3
@@ -7,7 +7,11 @@ import net.logstash.logback.mask.ValueMasker;
|
||||
|
||||
/**
|
||||
* SSOT for secret-masking regex rules (shared by the JSON-encoder and pattern-layout paths). See
|
||||
* README for the design rationale.
|
||||
* README for the design rationale. The rules are the {@code masking_rule} values of {@code
|
||||
* docs/registries/secrets-classification.yaml}: {@code full} (rules 0–3) and {@code userinfo} (rule
|
||||
* 4). Rule 1 knows the secret names that registry uses — password, secret, token, api key, salt,
|
||||
* PEM, and HMAC/signing/encryption/credential/private keys — so a {@code NAME=value} of any {@code
|
||||
* full} row is masked; {@code SecretsClassificationRegistryTest} checks every such row.
|
||||
*/
|
||||
public final class LogMaskingPatterns {
|
||||
|
||||
@@ -22,11 +26,20 @@ public final class LogMaskingPatterns {
|
||||
|
||||
private static final List<Rule> RULES =
|
||||
List.of(
|
||||
// 0. A PEM block, multi-line included: rule 1 alone stops at the first space and would
|
||||
// mask only "-----BEGIN". The armour lines stay so a line still says what it held.
|
||||
// A block cut short (no END with its own label) is masked to the end of the text: what
|
||||
// follows the armour is key material either way.
|
||||
new Rule(
|
||||
Pattern.compile(
|
||||
"(-----BEGIN ([A-Z0-9 ]{1,40})-----)[\\s\\S]*?(-----END \\2-----|\\z)"),
|
||||
"$1" + MASK + "$3"),
|
||||
// 1. key=value / "key":"value" secrets — keep the key + separator, mask the value.
|
||||
new Rule(
|
||||
Pattern.compile(
|
||||
"(?i)(password|passwd|pwd|secret|token|api[_-]?key|access[_-]?token"
|
||||
+ "|refresh[_-]?token|client[_-]?secret)("
|
||||
+ "|refresh[_-]?token|client[_-]?secret|salt|pem"
|
||||
+ "|(?:hmac|signing|encryption|credential|private)[_-]?key)("
|
||||
+ SEP
|
||||
+ ")("
|
||||
+ VALUE
|
||||
@@ -38,7 +51,21 @@ public final class LogMaskingPatterns {
|
||||
"(?i)(authorization" + SEP + ")((?:bearer|basic|negotiate)\\s+)?(" + VALUE + ")"),
|
||||
"$1$2" + MASK),
|
||||
// 3. Standalone bearer token not preceded by an "authorization" key.
|
||||
new Rule(Pattern.compile("(?i)(bearer\\s+)([A-Za-z0-9._~+/=-]{8,})"), "$1" + MASK));
|
||||
new Rule(Pattern.compile("(?i)(bearer\\s+)([A-Za-z0-9._~+/=-]{8,})"), "$1" + MASK),
|
||||
// 4. The password in a URI's userinfo (jdbc:postgresql://app:pw@db, mongodb+srv://…),
|
||||
// the secrets registry's "userinfo" rule. The user name stays, so a line still says
|
||||
// which account connected (review 2026-11-12: the rule was declared, nothing did it).
|
||||
new Rule(
|
||||
// The password ends at the first '@' that a host[:port] and a delimiter follow, so a
|
||||
// raw '@', '/', '#' or '?' in it is masked too, and an '@' later in a path or query
|
||||
// is not taken for one. "user:8080/" is a port, not a password. The password is
|
||||
// bounded and never crosses another "://", so a long line stays linear.
|
||||
Pattern.compile(
|
||||
"(?i)(?<![a-z0-9+.-])([a-z][a-z0-9+.-]*://[^:/@\\s\"']*:)"
|
||||
+ "(?!\\d{1,5}(?:[/?#\\s]|$))"
|
||||
+ "((?:(?!://)[^\\s\"]){1,256}?)(@)"
|
||||
+ "(?=[a-z0-9.-]+(?::\\d+)?(?:[/?#,;\\s\"']|$))"),
|
||||
"$1" + MASK + "$3"));
|
||||
|
||||
private LogMaskingPatterns() {}
|
||||
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package dev.caskeleton.bootstrap.logging;
|
||||
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import java.io.IOException;
|
||||
import net.logstash.logback.composite.AbstractFieldJsonProvider;
|
||||
import net.logstash.logback.composite.JsonWritingUtils;
|
||||
|
||||
/**
|
||||
* Writes the JSON {@code profile} field from the logger context property at the moment each line is
|
||||
* written.
|
||||
*
|
||||
* <p>{@code customFields} in {@code logback-spring.xml} is substituted once, when Logback reads the
|
||||
* file — before {@link ResolvedProfileLoggingContextListener} replaces the property with the
|
||||
* resolved profile. So the field kept the environment variable's value (or {@code unresolved})
|
||||
* while the property beside it was corrected, and the characterization test, which read only the
|
||||
* property, passed (review 2026-11-12). Reading the property per line is what makes the correction
|
||||
* reach the line.
|
||||
*/
|
||||
public class ResolvedProfileJsonProvider extends AbstractFieldJsonProvider<ILoggingEvent> {
|
||||
|
||||
/** The JSON field name. */
|
||||
static final String FIELD = "profile";
|
||||
|
||||
/** What the field says before any profile is known. */
|
||||
static final String UNRESOLVED = "unresolved";
|
||||
|
||||
public ResolvedProfileJsonProvider() {
|
||||
setFieldName(FIELD);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(JsonGenerator generator, ILoggingEvent event) throws IOException {
|
||||
String profile =
|
||||
getContext() == null
|
||||
? null
|
||||
: getContext().getProperty(ResolvedProfileLoggingContextListener.PROFILE_PROPERTY);
|
||||
JsonWritingUtils.writeStringField(
|
||||
generator, getFieldName(), profile == null || profile.isBlank() ? UNRESOLVED : profile);
|
||||
}
|
||||
}
|
||||
+4
@@ -30,6 +30,10 @@ import org.springframework.core.env.ConfigurableEnvironment;
|
||||
* sentinel {@code unresolved}; it no longer guesses {@code local}. Once the environment-prepared
|
||||
* event fires, this listener replaces that sentinel with the resolved profile.
|
||||
*
|
||||
* <p>The property is what {@link ResolvedProfileJsonProvider} reads for each JSON line. The JSON
|
||||
* field used to come from {@code customFields}, which Logback substitutes once when it reads the
|
||||
* file, so this correction changed the property and not the field (review 2026-11-12).
|
||||
*
|
||||
* <p>Registered in {@code META-INF/spring.factories}. A listener for this event cannot be a bean:
|
||||
* the event fires long before there is a context to hold one.
|
||||
*/
|
||||
|
||||
+37
-2
@@ -4,6 +4,10 @@ import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.turbo.TurboFilter;
|
||||
import ch.qos.logback.core.spi.FilterReply;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
import org.slf4j.Marker;
|
||||
|
||||
@@ -11,11 +15,27 @@ import org.slf4j.Marker;
|
||||
* Level-aware log sampler. {@code WARN}/{@code ERROR} are never sampled because they are
|
||||
* diagnostic/incident signals that must be guaranteed; only {@code INFO} and below are sampled. See
|
||||
* README for the design rationale.
|
||||
*
|
||||
* <p>An audit or security record is never sampled either, whatever its level: a logger whose name
|
||||
* ends in an {@code audit} or {@code security} segment ({@code notification.audit}, {@code
|
||||
* dev.caskeleton.fileserver.audit}, {@code notification.security}), or a line carrying the {@value
|
||||
* #AUDIT_MARKER} marker. The last segment only, so a library package such as {@code
|
||||
* org.springframework.security.web.FilterChainProxy} is still sampled. The answer is kept per
|
||||
* logger name: the filter runs before the level check, on every call below WARN. Those loggers
|
||||
* write at INFO, so at the recommended production rate of 0.1 nine audit records in ten were
|
||||
* dropped (review 2026-11-12). An audit trail with holes is not one.
|
||||
*/
|
||||
public class SamplingTurboFilter extends TurboFilter {
|
||||
|
||||
private static final double KEEP_ALL = 1.0d;
|
||||
|
||||
/** A marker that exempts one line from sampling, for an audit logger with another name. */
|
||||
public static final String AUDIT_MARKER = "AUDIT";
|
||||
|
||||
private static final Set<String> NEVER_SAMPLED_SEGMENTS = Set.of("audit", "security");
|
||||
|
||||
private final Map<String, Boolean> auditLoggers = new ConcurrentHashMap<>();
|
||||
|
||||
private volatile double rate = KEEP_ALL;
|
||||
|
||||
/** Logback Joran setter — {@code <rate>${LOG_SAMPLING_RATE}</rate>}. */
|
||||
@@ -46,8 +66,8 @@ public class SamplingTurboFilter extends TurboFilter {
|
||||
if (level.toInt() >= Level.WARN_INT) {
|
||||
return FilterReply.NEUTRAL;
|
||||
}
|
||||
// <= INFO: keep with probability `rate`.
|
||||
if (rate >= KEEP_ALL) {
|
||||
// <= INFO: keep with probability `rate`, audit and security records always.
|
||||
if (rate >= KEEP_ALL || isAuditRecord(marker, logger)) {
|
||||
return FilterReply.NEUTRAL;
|
||||
}
|
||||
if (rate <= 0.0d) {
|
||||
@@ -55,4 +75,19 @@ public class SamplingTurboFilter extends TurboFilter {
|
||||
}
|
||||
return ThreadLocalRandom.current().nextDouble() < rate ? FilterReply.NEUTRAL : FilterReply.DENY;
|
||||
}
|
||||
|
||||
private boolean isAuditRecord(Marker marker, Logger logger) {
|
||||
// The marker's own name: SLF4J 2 deprecates marker references, so a line carries the AUDIT
|
||||
// marker itself rather than one that refers to it.
|
||||
if (marker != null && AUDIT_MARKER.equals(marker.getName())) {
|
||||
return true;
|
||||
}
|
||||
return logger != null
|
||||
&& auditLoggers.computeIfAbsent(logger.getName(), SamplingTurboFilter::endsInAuditSegment);
|
||||
}
|
||||
|
||||
private static boolean endsInAuditSegment(String loggerName) {
|
||||
String last = loggerName.substring(loggerName.lastIndexOf('.') + 1);
|
||||
return NEVER_SAMPLED_SEGMENTS.contains(last.toLowerCase(Locale.ROOT));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ spring:
|
||||
# H2 is still available and now has to be asked for: `./gradlew :app-bootstrap:bootRunH2`.
|
||||
url: ${APP_DATASOURCE_URL:jdbc:postgresql://localhost:5433/ca_skeleton}
|
||||
username: ${APP_DATASOURCE_USERNAME:ca_skeleton}
|
||||
password: ${APP_DATASOURCE_PASSWORD:ca_skeleton}
|
||||
password: ${APP_DATASOURCE_PASSWORD:__LOCAL_DEV_db_password__}
|
||||
driver-class-name: ${APP_DATASOURCE_DRIVER:org.postgresql.Driver}
|
||||
hikari:
|
||||
maximum-pool-size: 10
|
||||
@@ -118,7 +118,9 @@ server:
|
||||
port: ${APP_SERVER_PORT:8080}
|
||||
shutdown: graceful
|
||||
# No proxy in front of a local run, so trusting X-Forwarded-* would let a caller forge its own
|
||||
# client IP. dev/prod sit behind a load balancer and set this to framework via env.
|
||||
# client IP. dev/prod behind a load balancer set APP_SERVER_FORWARD_HEADERS_STRATEGY=native and
|
||||
# list their proxies' IPs (a regex) in server.tomcat.remoteip.internal-proxies — never framework,
|
||||
# which believes every peer (config/server.yml).
|
||||
forward-headers-strategy: none
|
||||
tomcat:
|
||||
threads:
|
||||
|
||||
@@ -55,7 +55,8 @@
|
||||
<springProperty scope="context" name="JSON_LOGGER_NAME_LENGTH"
|
||||
source="app.logging.json.logger-name-length" defaultValue="0"/>
|
||||
|
||||
<!-- D5/D8: ≤INFO sampling rate (prod 0.1 = 10%); WARN/ERROR always kept. -->
|
||||
<!-- D5/D8: ≤INFO sampling rate (prod 0.1 = 10%); WARN/ERROR always kept, and so are audit and
|
||||
security records at any level (SamplingTurboFilter). -->
|
||||
<springProperty scope="context" name="LOG_SAMPLING_RATE"
|
||||
source="app.logging.sampling-rate" defaultValue="1.0"/>
|
||||
|
||||
@@ -64,7 +65,8 @@
|
||||
<conversionRule conversionWord="maskedMsg"
|
||||
class="dev.caskeleton.bootstrap.logging.SecretMaskingMessageConverter"/>
|
||||
|
||||
<!-- Sampling Policy (final): drop a share of ≤INFO events; WARN/ERROR pass unconditionally. -->
|
||||
<!-- Sampling Policy (final): drop a share of ≤INFO events; WARN/ERROR, the AUDIT marker and loggers
|
||||
whose last segment is audit or security pass unconditionally. -->
|
||||
<turboFilter class="dev.caskeleton.bootstrap.logging.SamplingTurboFilter">
|
||||
<rate>${LOG_SAMPLING_RATE}</rate>
|
||||
</turboFilter>
|
||||
@@ -86,7 +88,10 @@
|
||||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
|
||||
<includeContext>false</includeContext>
|
||||
<customFields>{"app":"${APP_NAME}","profile":"${APP_PROFILE}"}</customFields>
|
||||
<customFields>{"app":"${APP_NAME}"}</customFields>
|
||||
<!-- profile per line from the context property the resolved environment sets;
|
||||
customFields is fixed when this file is read (ResolvedProfileJsonProvider). -->
|
||||
<provider class="dev.caskeleton.bootstrap.logging.ResolvedProfileJsonProvider"/>
|
||||
<includeMdcKeyName>trace_id</includeMdcKeyName>
|
||||
<includeMdcKeyName>span_id</includeMdcKeyName>
|
||||
<includeMdcKeyName>request_id</includeMdcKeyName>
|
||||
@@ -119,7 +124,10 @@
|
||||
</rollingPolicy>
|
||||
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
|
||||
<includeContext>false</includeContext>
|
||||
<customFields>{"app":"${APP_NAME}","profile":"${APP_PROFILE}"}</customFields>
|
||||
<customFields>{"app":"${APP_NAME}"}</customFields>
|
||||
<!-- profile per line from the context property the resolved environment sets;
|
||||
customFields is fixed when this file is read (ResolvedProfileJsonProvider). -->
|
||||
<provider class="dev.caskeleton.bootstrap.logging.ResolvedProfileJsonProvider"/>
|
||||
<includeMdcKeyName>trace_id</includeMdcKeyName>
|
||||
<includeMdcKeyName>span_id</includeMdcKeyName>
|
||||
<includeMdcKeyName>request_id</includeMdcKeyName>
|
||||
|
||||
+8
-11
@@ -131,8 +131,9 @@ class DistributedTracingContractTest {
|
||||
/**
|
||||
* Asserts the {@code traceparent} row in {@code headers.yaml} has {@code generated_if_missing:
|
||||
* true}, {@code mdc_key: trace_id}, {@code envelope_meta_field: traceId}, and that the {@code
|
||||
* tracestate} row exists with {@code generated_if_missing: false}; AND that code agrees: {@code
|
||||
* MdcKeys.TRACE_ID == "trace_id"} and {@link ResponseMeta} has a {@code traceId} component.
|
||||
* tracestate} row is the framework's (the W3C propagator carries it next to traceparent; no
|
||||
* template code reads it); AND that code agrees: {@code MdcKeys.TRACE_ID == "trace_id"} and
|
||||
* {@link ResponseMeta} has a {@code traceId} component.
|
||||
*/
|
||||
@Tag("document-contract")
|
||||
@Test
|
||||
@@ -157,15 +158,11 @@ class DistributedTracingContractTest {
|
||||
.as("traceparent: envelope_meta_field must be 'traceId'")
|
||||
.isEqualTo("traceId");
|
||||
|
||||
// tracestate row: generated_if_missing=false
|
||||
Map<String, Object> tracestateRow =
|
||||
headers.stream()
|
||||
.filter(h -> "tracestate".equals(h.get("name")))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new AssertionError("headers.yaml must contain a 'tracestate' row"));
|
||||
assertThat(tracestateRow.get("generated_if_missing"))
|
||||
.as("tracestate: generated_if_missing must be false")
|
||||
.isEqualTo(false);
|
||||
// tracestate travels with traceparent through the W3C propagator; no template code reads it.
|
||||
assertThat(headers)
|
||||
.filteredOn(h -> "tracestate".equals(h.get("name")))
|
||||
.singleElement()
|
||||
.satisfies(row -> assertThat(String.valueOf(row.get("source"))).startsWith("framework:"));
|
||||
|
||||
// Code alignment: MdcKeys.TRACE_ID constant must match the registry mdc_key value
|
||||
assertThat(MdcKeys.TRACE_ID)
|
||||
|
||||
+54
-3
@@ -3,13 +3,18 @@ package dev.caskeleton.bootstrap.contract;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import dev.caskeleton.adapter.inbound.web.ratelimit.RateLimitWebConfig;
|
||||
import dev.caskeleton.bootstrap.contract.support.MainSources;
|
||||
import dev.caskeleton.bootstrap.contract.support.RepositoryContractResources;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Tag;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
@@ -18,6 +23,16 @@ import org.yaml.snakeyaml.Yaml;
|
||||
@Tag("document-contract")
|
||||
class HeaderRegistryRuntimeAlignmentTest {
|
||||
|
||||
/** A source prefix: a constant the template declares for service controllers to read. */
|
||||
private static final String SERVICE = "service: ";
|
||||
|
||||
/**
|
||||
* The only headers a {@code service:} row may name — README 「ApiHeaders」's constants for service
|
||||
* controllers. Anything else must name the main file that reads or writes it.
|
||||
*/
|
||||
private static final Set<String> FOR_SERVICE_CONTROLLERS =
|
||||
Set.of("Idempotency-Key", "If-Match", "If-None-Match");
|
||||
|
||||
private static List<Map<String, Object>> headers;
|
||||
|
||||
@BeforeAll
|
||||
@@ -40,12 +55,48 @@ class HeaderRegistryRuntimeAlignmentTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void deprecationDatesUseTheHttpDateWireType() {
|
||||
assertThat(row("Deprecation").get("type")).isEqualTo("http-date");
|
||||
assertThat(row("Sunset").get("type")).isEqualTo("http-date");
|
||||
void theRateLimitResetUsesTheRfc3339WireType() {
|
||||
assertThat(row("X-RateLimit-Reset").get("type")).isEqualTo("rfc3339-date");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("headers.yaml 의 행마다 그 헤더를 쓰는 main 파일이 있다 — 선언만 있는 상수는 정해 둔 서비스용 상수일 때만 된다")
|
||||
void everyRowNamesTheMainFileThatReadsOrWritesIt() throws IOException {
|
||||
// given
|
||||
Path root = RepositoryContractResources.fromSystemProperty().repositoryRoot();
|
||||
MainSources main = MainSources.under(root);
|
||||
|
||||
// when
|
||||
List<String> unbacked = new ArrayList<>();
|
||||
for (Map<String, Object> header : headers) {
|
||||
String name = String.valueOf(header.get("name"));
|
||||
String source = String.valueOf(header.get("source"));
|
||||
if (source.startsWith("framework:")) {
|
||||
continue;
|
||||
}
|
||||
boolean forServices = source.startsWith(SERVICE);
|
||||
Path file = root.resolve(forServices ? source.substring(SERVICE.length()) : source);
|
||||
if (!file.toString().contains("/src/main/")
|
||||
|| !Files.isRegularFile(file)
|
||||
|| !Files.readString(file).contains("\"" + name + "\"")
|
||||
|| (forServices && !FOR_SERVICE_CONTROLLERS.contains(name))
|
||||
|| (forServices && main.valueIsUsed(file, name))
|
||||
|| (!forServices && !main.valueIsUsed(file, name))) {
|
||||
unbacked.add(name + " <- " + source);
|
||||
}
|
||||
}
|
||||
|
||||
// then
|
||||
assertThat(unbacked).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("X-Request-Id·X-Correlation-Id 형식은 filter 가 받는 문법이다(ulid 가 아니다)")
|
||||
void requestAndCorrelationIdsCarryTheFiltersGrammar() {
|
||||
assertThat(List.of(row("X-Request-Id").get("type"), row("X-Correlation-Id").get("type")))
|
||||
.containsOnly("uuid-or-caller-token");
|
||||
}
|
||||
|
||||
@Test
|
||||
void rateLimitHeadersAreGeneratedOnlyInsideTheEnabledRateLimitCapability() {
|
||||
for (String name : List.of("X-RateLimit-Limit", "X-RateLimit-Remaining", "X-RateLimit-Reset")) {
|
||||
|
||||
+70
-55
@@ -18,6 +18,8 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Tag;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -215,82 +217,75 @@ class MetricsAlertingContractTest {
|
||||
|
||||
@Tag("document-contract")
|
||||
@Test
|
||||
@DisplayName("§테스트 계약 #1: http.server.requests row exists with method/status/uri_template tags")
|
||||
void httpServerRequestsRowExistsWithRequiredTags() throws Exception {
|
||||
@DisplayName(
|
||||
"http.server.requests 행은 Spring Framework convention 이 실제로 다는 tag(method·status·uri·outcome)를 적는다")
|
||||
void httpServerRequestsRowNamesTheTagsSpringRecords() throws Exception {
|
||||
// given
|
||||
loadRegistry();
|
||||
|
||||
// when
|
||||
Map<String, Object> row = findRow("http.server.requests");
|
||||
|
||||
// then
|
||||
assertThat(row).as("metrics.yaml must contain an 'http.server.requests' row").isNotNull();
|
||||
|
||||
List<String> tagNames = tagNames(row);
|
||||
assertThat(tagNames)
|
||||
.as("http.server.requests must have tags: method, status, uri_template")
|
||||
.contains("method", "status", "uri_template");
|
||||
assertThat(tagNames(row))
|
||||
.as("the tags Spring MVC's request observation records, not uri_template or 2xx buckets")
|
||||
.contains("method", "status", "uri", "outcome")
|
||||
.doesNotContain("uri_template");
|
||||
}
|
||||
|
||||
@Tag("document-contract")
|
||||
@Test
|
||||
@DisplayName(
|
||||
"§테스트 계약 #2: dependency.client.requests row exists with dependency_name/dependency_type tags and P1 alert")
|
||||
@SuppressWarnings(
|
||||
"unchecked") // SnakeYAML returns untyped Object; the registry shape is fixed by the
|
||||
// metrics.yaml contract
|
||||
void dependencyClientRequestsRowExistsWithRequiredTagsAndP1Alert() throws Exception {
|
||||
"outbound HTTP 행은 httpclient 가 내는 http.client.requests 이고 clientName·outcome tag 를 적는다")
|
||||
void outboundHttpRowIsTheOneTheClientRecords() throws Exception {
|
||||
// given
|
||||
loadRegistry();
|
||||
|
||||
// when
|
||||
Map<String, Object> row = findRow("http.client.requests");
|
||||
|
||||
// then
|
||||
assertThat(row).as("metrics.yaml must contain an 'http.client.requests' row").isNotNull();
|
||||
assertThat(tagNames(row)).contains("clientName", "outcome");
|
||||
}
|
||||
|
||||
@Tag("document-contract")
|
||||
@Test
|
||||
@DisplayName("아무 코드도 등록하지 않는 dependency.client.requests 행은 없다")
|
||||
void theUnregisteredDependencyClientRowIsGone() throws Exception {
|
||||
// given
|
||||
loadRegistry();
|
||||
|
||||
// when
|
||||
Map<String, Object> row = findRow("dependency.client.requests");
|
||||
assertThat(row).as("metrics.yaml must contain a 'dependency.client.requests' row").isNotNull();
|
||||
|
||||
List<String> tagNames = tagNames(row);
|
||||
assertThat(tagNames)
|
||||
.as("dependency.client.requests must have tags: dependency_name, dependency_type")
|
||||
.contains("dependency_name", "dependency_type");
|
||||
|
||||
//noinspection unchecked
|
||||
Map<String, Object> thresholds = (Map<String, Object>) row.get("alert_severity_thresholds");
|
||||
assertThat(thresholds)
|
||||
.as("dependency.client.requests must have non-empty alert_severity_thresholds (§테스트 계약 #4)")
|
||||
.isNotNull()
|
||||
.isNotEmpty();
|
||||
assertThat(thresholds)
|
||||
.as(
|
||||
"dependency.client.requests must have a P1 alert entry (§테스트 계약 #4 — dependency outage)")
|
||||
.containsKey("p1");
|
||||
// then
|
||||
assertThat(row)
|
||||
.as("no code registers dependency.client.requests; the row was removed")
|
||||
.isNull();
|
||||
}
|
||||
|
||||
@Tag("document-contract")
|
||||
@Test
|
||||
@DisplayName(
|
||||
"§테스트 계약 #3: hikaricp.connections.acquire row exists with outcome tag containing TIMEOUT and non-empty alerts")
|
||||
@DisplayName("DB pool 고갈은 HikariCP 의 hikaricp.connections.timeout counter 로 P1 경보를 건다")
|
||||
@SuppressWarnings(
|
||||
"unchecked") // SnakeYAML returns untyped Object; the registry shape is fixed by the
|
||||
// metrics.yaml contract
|
||||
void hikaricpConnectionsAcquireRowExistsWithTimeoutOutcome() throws Exception {
|
||||
void hikaricpTimeoutRowCarriesThePoolExhaustionAlert() throws Exception {
|
||||
// given
|
||||
loadRegistry();
|
||||
Map<String, Object> row = findRow("hikaricp.connections.acquire");
|
||||
|
||||
// when
|
||||
Map<String, Object> row = findRow("hikaricp.connections.timeout");
|
||||
|
||||
// then
|
||||
assertThat(row)
|
||||
.as(
|
||||
"metrics.yaml must contain a 'hikaricp.connections.acquire' row (DB pool exhaustion metric)")
|
||||
.as("metrics.yaml must contain a 'hikaricp.connections.timeout' row")
|
||||
.isNotNull();
|
||||
|
||||
//noinspection unchecked
|
||||
List<Map<String, Object>> tags = (List<Map<String, Object>>) row.get("tags");
|
||||
Map<String, Object> outcomeTag =
|
||||
tags.stream().filter(t -> "outcome".equals(t.get("name"))).findFirst().orElse(null);
|
||||
assertThat(outcomeTag)
|
||||
.as("hikaricp.connections.acquire must have an 'outcome' tag")
|
||||
.isNotNull();
|
||||
|
||||
//noinspection unchecked
|
||||
List<String> allowedValues = (List<String>) outcomeTag.get("allowed_values");
|
||||
assertThat(allowedValues)
|
||||
.as("hikaricp.connections.acquire outcome tag must contain 'TIMEOUT'")
|
||||
.contains("TIMEOUT");
|
||||
|
||||
//noinspection unchecked
|
||||
Map<String, Object> thresholds = (Map<String, Object>) row.get("alert_severity_thresholds");
|
||||
assertThat(thresholds)
|
||||
.as("hikaricp.connections.acquire must have non-empty alert_severity_thresholds")
|
||||
.isNotNull()
|
||||
.isNotEmpty();
|
||||
assertThat((Map<String, Object>) row.get("alert_severity_thresholds"))
|
||||
.as("pool exhaustion is a P1 alert")
|
||||
.containsKey("p1");
|
||||
}
|
||||
|
||||
@Tag("document-contract")
|
||||
@@ -375,6 +370,26 @@ class MetricsAlertingContractTest {
|
||||
.isNull();
|
||||
}
|
||||
|
||||
@Tag("document-contract")
|
||||
@Test
|
||||
@DisplayName("metrics.yaml 의 slo_driven 행과 MetricsDistributionMeterFilter 의 대상은 같은 집합이다")
|
||||
void theSloDrivenRowsAreExactlyTheFiltersTargets() throws Exception {
|
||||
// given
|
||||
loadRegistry();
|
||||
|
||||
// when
|
||||
Set<String> sloDrivenRows = new TreeSet<>();
|
||||
for (Map<String, Object> row : metrics) {
|
||||
if ("slo_driven".equals(row.get("histogram_buckets"))) {
|
||||
sloDrivenRows.add((String) row.get("name"));
|
||||
}
|
||||
}
|
||||
|
||||
// then
|
||||
assertThat(sloDrivenRows)
|
||||
.isEqualTo(new TreeSet<>(MetricsDistributionMeterFilter.sloDrivenTimers()));
|
||||
}
|
||||
|
||||
@Tag("document-contract")
|
||||
@Test
|
||||
@DisplayName(
|
||||
|
||||
+188
@@ -0,0 +1,188 @@
|
||||
package dev.caskeleton.bootstrap.contract;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import dev.caskeleton.bootstrap.contract.support.MainSources;
|
||||
import dev.caskeleton.bootstrap.contract.support.RepositoryContractResources;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Tag;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
/**
|
||||
* Every row of {@code docs/registries/metrics.yaml} is a meter some code registers.
|
||||
*
|
||||
* <p>A row's {@code source} is either {@code framework: …} — Spring Boot, Micrometer or HikariCP
|
||||
* registers it — or the repository path of the main file that holds the meter name as a string
|
||||
* literal. When that file only declares the name as a constant, the row also names the {@code
|
||||
* constant} and the {@code registered_by} file that uses it; a name constant nothing uses does not
|
||||
* count. In the other direction, every meter a main file registers has a row: under a literal name,
|
||||
* a constant of the same file or {@code SomeClass.CONSTANT}, through a builder, a registry
|
||||
* shortcut, the global {@code Metrics} shortcut or {@code Observation.createNotStarted}. A name
|
||||
* passed in a variable cannot be resolved and is not checked; a scan that resolves one name of each
|
||||
* form proves the scan itself still works. The registry used to list eleven meters nothing
|
||||
* registered and miss dozens that the code did (review 2026-11-12); without this check the two
|
||||
* drift apart again the first time a meter is renamed.
|
||||
*/
|
||||
@Tag("document-contract")
|
||||
class MetricsRegistryCodeAlignmentTest {
|
||||
|
||||
private static final String FRAMEWORK = "framework:";
|
||||
|
||||
/**
|
||||
* A meter registration and its name argument: a builder, a registry shortcut, the global {@code
|
||||
* Metrics} shortcut, or an observation (its timer carries the observation's name). The argument
|
||||
* is a literal, a constant, or {@code SomeClass.CONSTANT}.
|
||||
*/
|
||||
static final Pattern REGISTRATION =
|
||||
Pattern.compile(
|
||||
"(?:(?:Counter|Timer|Gauge|DistributionSummary|LongTaskTimer|FunctionCounter|FunctionTimer"
|
||||
+ "|TimeGauge|MultiGauge)\\.builder"
|
||||
+ "|\\b(?:\\w*[rR]egistry|Metrics)\\.(?:counter|timer|gauge|summary)"
|
||||
+ "|\\bObservation\\.createNotStarted)"
|
||||
+ "\\(\\s*(\"[^\"]+\"|[A-Za-z_][\\w.]*)");
|
||||
|
||||
/** A string constant that holds a dotted meter name. */
|
||||
private static final Pattern NAME_CONSTANT =
|
||||
Pattern.compile("\\b([A-Z][A-Z0-9_]*)\\s*=\\s*\"([a-z][a-z0-9_]*(?:\\.[a-z0-9_]+)+)\"");
|
||||
|
||||
@Test
|
||||
@DisplayName("metrics.yaml 의 행마다 등록하는 main 파일이 있고 그 파일에 meter 이름 글자가 있다")
|
||||
void everyRowNamesTheMainFileThatRegistersIt() throws IOException {
|
||||
// given
|
||||
RepositoryContractResources repository = RepositoryContractResources.fromSystemProperty();
|
||||
List<Map<String, Object>> rows = rows(repository);
|
||||
MainSources main = MainSources.under(repository.repositoryRoot());
|
||||
|
||||
// when
|
||||
List<String> unbacked = new ArrayList<>();
|
||||
for (Map<String, Object> row : rows) {
|
||||
String name = String.valueOf(row.get("name"));
|
||||
String source = String.valueOf(row.get("source"));
|
||||
if (source.startsWith(FRAMEWORK)) {
|
||||
continue;
|
||||
}
|
||||
Path file = repository.repositoryRoot().resolve(source);
|
||||
if (!source.startsWith("src/")
|
||||
|| !source.contains("/src/main/")
|
||||
|| !Files.isRegularFile(file)
|
||||
|| !Files.readString(file).contains("\"" + name + "\"")) {
|
||||
unbacked.add(name + " <- " + source);
|
||||
continue;
|
||||
}
|
||||
Object constant = row.get("constant");
|
||||
if (constant == null && !main.valueIsUsed(file, name)) {
|
||||
unbacked.add(name + " <- " + source + " only declares it in a constant nothing uses");
|
||||
continue;
|
||||
}
|
||||
if (constant != null) {
|
||||
String simpleConstant =
|
||||
String.valueOf(constant).substring(String.valueOf(constant).indexOf('.') + 1);
|
||||
Path user = repository.repositoryRoot().resolve(String.valueOf(row.get("registered_by")));
|
||||
if (!Files.readString(file).contains(simpleConstant + " = \"" + name + "\"")
|
||||
|| !Files.isRegularFile(user)
|
||||
|| !Files.readString(user).contains(String.valueOf(constant))) {
|
||||
unbacked.add(name + " <- " + constant + " used by " + row.get("registered_by"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// then
|
||||
assertThat(unbacked).as("rows whose source does not register the meter").isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("main 파일이 글자·상수 이름으로 등록하는 meter 는 모두 metrics.yaml 행이 있다")
|
||||
void everyMeterRegisteredUnderAResolvableNameHasARow() throws IOException {
|
||||
// given
|
||||
RepositoryContractResources repository = RepositoryContractResources.fromSystemProperty();
|
||||
Set<String> rows = new TreeSet<>();
|
||||
rows(repository).forEach(row -> rows.add(String.valueOf(row.get("name"))));
|
||||
|
||||
// when
|
||||
Set<String> registered = registeredMeters(repository);
|
||||
|
||||
// then
|
||||
assertThat(rows).containsAll(registered);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("등록 형태마다 meter 하나씩을 찾는다 — 아무것도 못 찾는 scan 은 위 시험을 거짓으로 통과시킨다")
|
||||
void theScanResolvesOneMeterOfEachRegistrationForm() throws IOException {
|
||||
// given
|
||||
RepositoryContractResources repository = RepositoryContractResources.fromSystemProperty();
|
||||
|
||||
// when
|
||||
Set<String> registered = registeredMeters(repository);
|
||||
|
||||
// then
|
||||
assertThat(registered)
|
||||
.contains(
|
||||
"fileserver.upload.duration", // a literal builder name
|
||||
"executor.rejected.total", // a constant of the same file
|
||||
"http.client.circuit.state.rejected", // SomeClass.CONSTANT
|
||||
"log.appender.dropped.total", // the global Metrics shortcut
|
||||
"notification.callback"); // an observation
|
||||
}
|
||||
|
||||
/** Every meter name a main file registers that the scan can resolve. */
|
||||
private static Set<String> registeredMeters(RepositoryContractResources repository) {
|
||||
Map<Path, String> sources = MainSources.under(repository.repositoryRoot()).all();
|
||||
Map<String, String> qualifiedConstants = new HashMap<>();
|
||||
sources.forEach(
|
||||
(file, text) ->
|
||||
constants(text)
|
||||
.forEach(
|
||||
(constant, value) ->
|
||||
qualifiedConstants.put(
|
||||
MainSources.simpleName(file) + "." + constant, value)));
|
||||
Set<String> registered = new TreeSet<>();
|
||||
sources.forEach(
|
||||
(file, text) -> {
|
||||
Map<String, String> local = constants(text);
|
||||
Matcher matcher = REGISTRATION.matcher(text);
|
||||
while (matcher.find()) {
|
||||
String argument = matcher.group(1);
|
||||
String meter =
|
||||
argument.startsWith("\"")
|
||||
? argument.substring(1, argument.length() - 1)
|
||||
: local.getOrDefault(argument, qualifiedConstants.get(argument));
|
||||
if (meter != null) {
|
||||
registered.add(meter);
|
||||
}
|
||||
}
|
||||
});
|
||||
return registered;
|
||||
}
|
||||
|
||||
private static Map<String, String> constants(String text) {
|
||||
Map<String, String> constants = new HashMap<>();
|
||||
Matcher matcher = NAME_CONSTANT.matcher(text);
|
||||
while (matcher.find()) {
|
||||
constants.put(matcher.group(1), matcher.group(2));
|
||||
}
|
||||
return constants;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked") // SnakeYAML returns untyped maps; the registry shape is fixed
|
||||
private static List<Map<String, Object>> rows(RepositoryContractResources repository)
|
||||
throws IOException {
|
||||
Path registry = repository.requireTrackedFile("docs/registries/metrics.yaml");
|
||||
try (InputStream in = Files.newInputStream(registry)) {
|
||||
Map<String, Object> root = new Yaml().load(in);
|
||||
return (List<Map<String, Object>>) root.get("metrics");
|
||||
}
|
||||
}
|
||||
}
|
||||
+65
@@ -3,7 +3,9 @@ package dev.caskeleton.bootstrap.contract;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import dev.caskeleton.bootstrap.contract.support.RepositoryContractResources;
|
||||
import dev.caskeleton.bootstrap.logging.LogMaskingPatterns;
|
||||
import dev.caskeleton.bootstrap.runtime.SecretSourceValidator;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@@ -11,6 +13,7 @@ import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Tag;
|
||||
@@ -115,4 +118,66 @@ class SecretsClassificationRegistryTest {
|
||||
.isIn(VALID_TIERS);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName(
|
||||
"masking_rule 은 LogMaskingPatterns 가 하는 셋(full·userinfo·none)뿐이다 — 구현 없는 규칙을 약속하지 않는다")
|
||||
void everyMaskingRuleIsOneTheMaskingLayerImplements() {
|
||||
// when
|
||||
Set<Object> rules = new LinkedHashSet<>();
|
||||
registryRows.forEach(row -> rules.add(row.get("masking_rule")));
|
||||
|
||||
// then
|
||||
assertThat(rules).isSubsetOf("full", "userinfo", "none");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("required_test 는 app-bootstrap 시험 폴더에 있는 시험 클래스 이름이다")
|
||||
void everyRequiredTestNamesATestClassThatExists() throws IOException {
|
||||
// given
|
||||
Path tests =
|
||||
RepositoryContractResources.fromSystemProperty()
|
||||
.requireTrackedDirectory("src/bootstrap/app/src/test/java");
|
||||
Set<String> classNames = new LinkedHashSet<>();
|
||||
try (Stream<Path> files = Files.walk(tests)) {
|
||||
files
|
||||
.map(path -> path.getFileName().toString())
|
||||
.filter(name -> name.endsWith("Test.java"))
|
||||
.forEach(name -> classNames.add(name.substring(0, name.length() - ".java".length())));
|
||||
}
|
||||
|
||||
// when
|
||||
Set<Object> named = new LinkedHashSet<>();
|
||||
registryRows.stream()
|
||||
.map(row -> row.get("required_test"))
|
||||
.filter(java.util.Objects::nonNull)
|
||||
.forEach(named::add);
|
||||
|
||||
// then
|
||||
assertThat(classNames).containsAll(named.stream().map(String::valueOf).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("masking_rule 이 full 인 행의 NAME=값 과 userinfo 행의 URI 비밀번호는 Layer 1 가림이 실제로 가린다")
|
||||
void everyMaskingRuleIsHonouredByTheMaskingLayer() {
|
||||
// given
|
||||
String secret = "s3cr3t-Value-1234";
|
||||
|
||||
// when
|
||||
List<String> leaked = new java.util.ArrayList<>();
|
||||
for (Map<String, Object> row : registryRows) {
|
||||
String name = String.valueOf(row.get("name"));
|
||||
Object rule = row.get("masking_rule");
|
||||
String line =
|
||||
"full".equals(rule)
|
||||
? name + "=" + secret
|
||||
: "userinfo".equals(rule) ? "scheme://user:" + secret + "@host/db" : null;
|
||||
if (line != null && LogMaskingPatterns.mask(line).contains(secret)) {
|
||||
leaked.add(name);
|
||||
}
|
||||
}
|
||||
|
||||
// then
|
||||
assertThat(leaked).isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
+45
@@ -3,13 +3,17 @@ package dev.caskeleton.bootstrap.contract;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import dev.caskeleton.bootstrap.contract.support.RepositoryContractResources;
|
||||
import dev.caskeleton.shared.observability.MdcKeys;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
@@ -72,6 +76,25 @@ class StructuredLogFieldContractTest {
|
||||
.containsAll(emittedFields);
|
||||
}
|
||||
|
||||
@Tag("document-contract")
|
||||
@Test
|
||||
@DisplayName("mdc-keys.yaml 의 key 는 코드가 MDC 에 넣는 MdcKeys 상수와 같은 집합이다")
|
||||
void theRegistryListsExactlyTheKeysTheCodePutsOnMdc() throws Exception {
|
||||
// given
|
||||
Set<String> codeKeys = new TreeSet<>();
|
||||
for (Field field : MdcKeys.class.getFields()) {
|
||||
if (Modifier.isStatic(field.getModifiers()) && field.getType() == String.class) {
|
||||
codeKeys.add((String) field.get(null));
|
||||
}
|
||||
}
|
||||
|
||||
// when
|
||||
Set<String> registryKeys = new TreeSet<>(registryKeys());
|
||||
|
||||
// then
|
||||
assertThat(registryKeys).isEqualTo(codeKeys);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Logback은 로깅 설정을 app.logging.* 한 곳에서 읽고, 프로필·앱 이름은 env 이름으로 읽는다")
|
||||
void logbackReadsLoggingKnobsFromTheSettingsTreeAndIdentityFromTheEnvironment()
|
||||
@@ -100,6 +123,28 @@ class StructuredLogFieldContractTest {
|
||||
.contains("source=\"SPRING_PROFILES_ACTIVE\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName(
|
||||
"JSON encoder 마다 profile 칸은 ResolvedProfileJsonProvider 가 쓰고 customFields 에 profile 이 없다")
|
||||
void everyJsonEncoderWritesTheProfileThroughTheResolvedProfileProvider() throws IOException {
|
||||
// given
|
||||
String logback = logbackText();
|
||||
|
||||
// when
|
||||
int encoders = occurrences(logback, "net.logstash.logback.encoder.LogstashEncoder");
|
||||
int providers =
|
||||
occurrences(logback, "dev.caskeleton.bootstrap.logging.ResolvedProfileJsonProvider");
|
||||
|
||||
// then
|
||||
assertThat(encoders).isPositive();
|
||||
assertThat(providers).isEqualTo(encoders);
|
||||
assertThat(logback).doesNotContain("\"profile\":");
|
||||
}
|
||||
|
||||
private static int occurrences(String text, String needle) {
|
||||
return text.split(Pattern.quote(needle), -1).length - 1;
|
||||
}
|
||||
|
||||
/** Distinct {@code <includeMdcKeyName>} values from the structured JSON appenders. */
|
||||
private static Set<String> includeMdcKeyNames() throws IOException {
|
||||
String logback = logbackText();
|
||||
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
package dev.caskeleton.bootstrap.contract.support;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* The repository's main Java sources, read once, for registry tests that ask whether a name the
|
||||
* registry lists is really used by shipped code.
|
||||
*
|
||||
* <p>A registry row that points at a file holding its name only proves the name is declared. When
|
||||
* the name sits in a constant ({@code DEPRECATION = "Deprecation"}), the row is backed only if
|
||||
* something uses that constant: it appears again in its own file, or another main file names it as
|
||||
* {@code DeclaringClass.CONSTANT}.
|
||||
*/
|
||||
public final class MainSources {
|
||||
|
||||
private final Map<Path, String> sources;
|
||||
|
||||
private MainSources(Map<Path, String> sources) {
|
||||
this.sources = sources;
|
||||
}
|
||||
|
||||
/** Reads every {@code src/**}{@code /src/main/java/**}{@code .java} file under the root. */
|
||||
public static MainSources under(Path repositoryRoot) {
|
||||
Map<Path, String> sources = new LinkedHashMap<>();
|
||||
try (Stream<Path> files = Files.walk(repositoryRoot.resolve("src"))) {
|
||||
for (Path file :
|
||||
files
|
||||
.filter(path -> path.toString().endsWith(".java"))
|
||||
.filter(path -> path.toString().contains("/src/main/java/"))
|
||||
.toList()) {
|
||||
sources.put(file, Files.readString(file));
|
||||
}
|
||||
} catch (IOException failure) {
|
||||
throw new UncheckedIOException(failure);
|
||||
}
|
||||
return new MainSources(sources);
|
||||
}
|
||||
|
||||
/** File → text, in walk order. */
|
||||
public Map<Path, String> all() {
|
||||
return sources;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether {@code value}, held by {@code file}, is used: a literal that is not a constant counts
|
||||
* as used; a constant must appear again in the file or as {@code FileClass.CONSTANT} elsewhere.
|
||||
*/
|
||||
public boolean valueIsUsed(Path file, String value) {
|
||||
String text = sources.get(file);
|
||||
if (text == null) {
|
||||
return false;
|
||||
}
|
||||
Matcher declaration =
|
||||
Pattern.compile("\\b([A-Z][A-Z0-9_]*)\\s*=\\s*\"" + Pattern.quote(value) + "\"")
|
||||
.matcher(text);
|
||||
if (!declaration.find()) {
|
||||
return true;
|
||||
}
|
||||
String constant = declaration.group(1);
|
||||
if (occurrences(text, constant) > 1) {
|
||||
return true;
|
||||
}
|
||||
String qualified = simpleName(file) + "." + constant;
|
||||
return sources.values().stream().anyMatch(other -> other.contains(qualified));
|
||||
}
|
||||
|
||||
/** The simple class name of a source file. */
|
||||
public static String simpleName(Path file) {
|
||||
String name = file.getFileName().toString();
|
||||
return name.substring(0, name.length() - ".java".length());
|
||||
}
|
||||
|
||||
private static int occurrences(String text, String identifier) {
|
||||
Matcher uses = Pattern.compile("\\b" + Pattern.quote(identifier) + "\\b").matcher(text);
|
||||
int count = 0;
|
||||
while (uses.find()) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package dev.caskeleton.bootstrap.contract.support;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
class MainSourcesTest {
|
||||
|
||||
@TempDir Path root;
|
||||
|
||||
@Test
|
||||
@DisplayName("이름을 선언만 한 상수는 쓰이지 않은 것이고, 같은 파일이나 다른 파일의 Class.CONST 로 쓰면 쓰인 것이다")
|
||||
void aDeclaredOnlyConstantIsNotUsedButOneReferencedAnywhereIs() throws IOException {
|
||||
// given
|
||||
Path names =
|
||||
write(
|
||||
"src/lib/src/main/java/demo/Names.java",
|
||||
"""
|
||||
class Names {
|
||||
static final String DECLARED = "declared.only";
|
||||
static final String LOCAL = "used.here";
|
||||
static final String SHARED = "used.elsewhere";
|
||||
void record() { count(LOCAL); }
|
||||
void count(String name) {}
|
||||
String literal() { return "no.constant"; }
|
||||
}
|
||||
""");
|
||||
write(
|
||||
"src/app/src/main/java/demo/Recorder.java",
|
||||
"class Recorder { void record() { new Names().count(Names.SHARED); } }\n");
|
||||
write(
|
||||
"src/lib/src/test/java/demo/NamesTest.java",
|
||||
"class NamesTest { void t() { new Names().count(Names.DECLARED); } }\n");
|
||||
MainSources main = MainSources.under(root);
|
||||
|
||||
// when
|
||||
List<Boolean> used =
|
||||
List.of(
|
||||
main.valueIsUsed(names, "declared.only"),
|
||||
main.valueIsUsed(names, "used.here"),
|
||||
main.valueIsUsed(names, "used.elsewhere"),
|
||||
main.valueIsUsed(names, "no.constant"));
|
||||
|
||||
// then
|
||||
assertThat(used).containsExactly(false, true, true, true);
|
||||
}
|
||||
|
||||
private Path write(String relative, String text) throws IOException {
|
||||
Path file = root.resolve(relative);
|
||||
Files.createDirectories(file.getParent());
|
||||
return Files.writeString(file, text);
|
||||
}
|
||||
}
|
||||
+93
@@ -2,7 +2,11 @@ package dev.caskeleton.bootstrap.logging;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
/**
|
||||
* Guards the Redaction Layer 1 secret catalog (feature-log-management-contract 테스트 계약:
|
||||
@@ -76,4 +80,93 @@ class LogMaskingPatternsTest {
|
||||
Object number = 42;
|
||||
assertThat(LogMaskingPatterns.valueMasker().mask(null, number)).isSameAs(number);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(
|
||||
delimiter = '|',
|
||||
value = {
|
||||
"connecting to jdbc:postgresql://app:hunter2@db:5432/ca | connecting to jdbc:postgresql://app:****@db:5432/ca",
|
||||
"uri=mongodb+srv://reader:s3cr3t-pw@cluster0.example.net/app | uri=mongodb+srv://reader:****@cluster0.example.net/app",
|
||||
"redis://default:pw@cache:6379 | redis://default:****@cache:6379",
|
||||
"redis://:pw@cache:6379 | redis://:****@cache:6379",
|
||||
"jdbc:postgresql://app:it's-a-secret@db/ca | jdbc:postgresql://app:****@db/ca",
|
||||
"jdbc:postgresql://app:p@ss@db:5432/ca | jdbc:postgresql://app:****@db:5432/ca",
|
||||
"redis://default:Ab/cd+ef@cache:6379 | redis://default:****@cache:6379",
|
||||
"redis://default:P#ssw0rd@cache:6379 | redis://default:****@cache:6379",
|
||||
"redis://default:pa?ss@cache:6379 | redis://default:****@cache:6379",
|
||||
"redis://u:pw@h,redis://u2:pw2@h2 | redis://u:****@h,redis://u2:****@h2",
|
||||
"mongodb://u:p@h1:27017,h2:27017/db | mongodb://u:****@h1:27017,h2:27017/db"
|
||||
})
|
||||
@DisplayName("URI 사용자 정보의 비밀번호를 가리고 사용자 이름은 남긴다(secrets registry 의 userinfo 규칙)")
|
||||
void masksThePasswordInAUrisUserinfo(String line, String masked) {
|
||||
assertThat(LogMaskingPatterns.mask(line)).isEqualTo(masked);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("PEM 블록은 여러 줄이어도 BEGIN·END 사이를 모두 가린다 — 첫 공백에서 멈추지 않는다")
|
||||
void masksAWholePemBlockAcrossLines() {
|
||||
// given
|
||||
String line =
|
||||
"trust_pem=-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASC\nSECRETBODY\n"
|
||||
+ "-----END PRIVATE KEY----- loaded";
|
||||
|
||||
// when
|
||||
String masked = LogMaskingPatterns.mask(line);
|
||||
|
||||
// then
|
||||
assertThat(masked)
|
||||
.doesNotContain("MIIEvQIBADANBgkqhkiG9w0BAQEFAASC", "SECRETBODY")
|
||||
.contains("-----END PRIVATE KEY----- loaded");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("END 가 없는 PEM 블록은 BEGIN 줄만 남기고 끝까지 가린다")
|
||||
void masksAPemBlockCutShortToTheEnd() {
|
||||
// when
|
||||
String masked =
|
||||
LogMaskingPatterns.mask(
|
||||
"key=-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASC\nSECRETBODY");
|
||||
|
||||
// then
|
||||
assertThat(masked).isEqualTo("key=-----BEGIN PRIVATE KEY-----****");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("다른 라벨의 END 만 있는 PEM 블록도 BEGIN 줄만 남기고 끝까지 가린다")
|
||||
void masksAPemBlockClosedUnderAnotherLabelToTheEnd() {
|
||||
// when
|
||||
String masked =
|
||||
LogMaskingPatterns.mask(
|
||||
"-----BEGIN RSA PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASC\n"
|
||||
+ "-----END PRIVATE KEY-----\nSECRETBODY");
|
||||
|
||||
// then
|
||||
assertThat(masked).isEqualTo("-----BEGIN RSA PRIVATE KEY-----****");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("공백 없는 긴 줄도 가림 규칙이 선형으로 끝난다 — 줄 길이의 제곱으로 느려지지 않는다")
|
||||
void aLongLineWithoutSpacesIsMaskedInLinearTime() {
|
||||
// given
|
||||
String line = "a://:".repeat(32_000) + "http://h:1/".repeat(15_000) + "a".repeat(40_000);
|
||||
|
||||
// when
|
||||
long started = System.nanoTime();
|
||||
LogMaskingPatterns.mask(line);
|
||||
long elapsedMillis = (System.nanoTime() - started) / 1_000_000;
|
||||
|
||||
// then
|
||||
assertThat(elapsedMillis).as("masking a 365 KB line").isLessThan(1_000);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(
|
||||
strings = {
|
||||
"calling https://api.example.com:8443/v1/things",
|
||||
"calling https://api.example.com:8443/v1/users?email=a@b.example",
|
||||
"GET http://localhost:8080/v1/users/alice@example.com"
|
||||
})
|
||||
void aUriWithoutUserinfoIsUntouched(String line) {
|
||||
assertThat(LogMaskingPatterns.mask(line)).isSameAs(line);
|
||||
}
|
||||
}
|
||||
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package dev.caskeleton.bootstrap.logging;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import ch.qos.logback.classic.spi.LoggingEvent;
|
||||
import com.fasterxml.jackson.core.JsonFactory;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ResolvedProfileJsonProviderTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("JSON profile 칸은 줄을 쓸 때의 context 속성이다 — 기동 중 바로잡은 profile 이 그 뒤 줄에 실린다")
|
||||
void theFieldFollowsThePropertyCorrectedAfterConfiguration() throws IOException {
|
||||
// given
|
||||
LoggerContext context = new LoggerContext();
|
||||
context.putProperty(ResolvedProfileLoggingContextListener.PROFILE_PROPERTY, "prod");
|
||||
ResolvedProfileJsonProvider provider = new ResolvedProfileJsonProvider();
|
||||
provider.setContext(context);
|
||||
provider.start();
|
||||
context.putProperty(ResolvedProfileLoggingContextListener.PROFILE_PROPERTY, "dev");
|
||||
|
||||
// when
|
||||
String json = written(provider);
|
||||
|
||||
// then
|
||||
assertThat(json).isEqualTo("{\"profile\":\"dev\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aContextWithoutTheProfileSaysUnresolved() throws IOException {
|
||||
// given
|
||||
ResolvedProfileJsonProvider provider = new ResolvedProfileJsonProvider();
|
||||
provider.setContext(new LoggerContext());
|
||||
|
||||
// when
|
||||
String json = written(provider);
|
||||
|
||||
// then
|
||||
assertThat(json).isEqualTo("{\"profile\":\"unresolved\"}");
|
||||
}
|
||||
|
||||
private static String written(ResolvedProfileJsonProvider provider) throws IOException {
|
||||
StringWriter out = new StringWriter();
|
||||
try (JsonGenerator generator = new JsonFactory().createGenerator(out)) {
|
||||
generator.writeStartObject();
|
||||
provider.writeTo(generator, new LoggingEvent());
|
||||
generator.writeEndObject();
|
||||
}
|
||||
return out.toString();
|
||||
}
|
||||
}
|
||||
+61
@@ -5,7 +5,11 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
import ch.qos.logback.core.spi.FilterReply;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.slf4j.MarkerFactory;
|
||||
|
||||
/**
|
||||
* Tests the level-aware log sampler (feature-log-management-contract §Sampling Policy, D5/D8):
|
||||
@@ -71,4 +75,61 @@ class SamplingTurboFilterTest {
|
||||
assertThat(negative.getRate()).isEqualTo(1.0);
|
||||
assertThat(decide(negative, Level.INFO)).isEqualTo(FilterReply.NEUTRAL);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(
|
||||
strings = {"notification.audit", "dev.caskeleton.fileserver.audit", "notification.security"})
|
||||
@DisplayName("감사·보안 logger 의 INFO 는 표본 비율 0 에서도 버리지 않는다")
|
||||
void auditAndSecurityLoggersAreNeverSampled(String loggerName) {
|
||||
// given
|
||||
SamplingTurboFilter filter = started(0.0);
|
||||
LoggerContext context = new LoggerContext();
|
||||
|
||||
// when
|
||||
FilterReply reply =
|
||||
filter.decide(null, context.getLogger(loggerName), Level.INFO, "msg", null, null);
|
||||
|
||||
// then
|
||||
assertThat(reply).isEqualTo(FilterReply.NEUTRAL);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aLineMarkedAuditIsNeverSampled() {
|
||||
// given
|
||||
SamplingTurboFilter filter = started(0.0);
|
||||
LoggerContext context = new LoggerContext();
|
||||
|
||||
// when
|
||||
FilterReply reply =
|
||||
filter.decide(
|
||||
MarkerFactory.getMarker(SamplingTurboFilter.AUDIT_MARKER),
|
||||
context.getLogger("dev.caskeleton.feature.Orders"),
|
||||
Level.INFO,
|
||||
"msg",
|
||||
null,
|
||||
null);
|
||||
|
||||
// then
|
||||
assertThat(reply).isEqualTo(FilterReply.NEUTRAL);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(
|
||||
strings = {
|
||||
"dev.caskeleton.feature.Orders",
|
||||
"org.springframework.security.web.FilterChainProxy"
|
||||
})
|
||||
@DisplayName("마지막 마디가 audit·security 가 아닌 logger 는 표본 대상이다(라이브러리 security 패키지 포함)")
|
||||
void anOrdinaryLoggerIsStillSampled(String loggerName) {
|
||||
// given
|
||||
SamplingTurboFilter filter = started(0.0);
|
||||
|
||||
// when
|
||||
FilterReply reply =
|
||||
filter.decide(
|
||||
null, new LoggerContext().getLogger(loggerName), Level.INFO, "msg", null, null);
|
||||
|
||||
// then
|
||||
assertThat(reply).isEqualTo(FilterReply.DENY);
|
||||
}
|
||||
}
|
||||
|
||||
+35
-12
@@ -10,7 +10,12 @@ import dev.caskeleton.application.fileserver.recovery.FileReconciliationService;
|
||||
import dev.caskeleton.application.fileserver.recovery.ReconcileRecoverableFilesCommand;
|
||||
import dev.caskeleton.application.fileserver.recovery.ReconcileRecoverableFilesUseCase;
|
||||
import dev.caskeleton.application.fileserver.recovery.ReconciliationResult;
|
||||
import dev.caskeleton.shared.error.ApiErrorCarrier;
|
||||
import dev.caskeleton.shared.error.ApiErrorCode;
|
||||
import dev.caskeleton.shared.error.ServerFailureLog;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -82,13 +87,8 @@ public final class FileserverCleanupWorker {
|
||||
kv("reconcile.examined", results.size()));
|
||||
}
|
||||
} catch (RuntimeException failure) {
|
||||
LOG.warn(
|
||||
"fileserver.reconcile pass failed; the next run looks again",
|
||||
kv("capability", "fileserver"),
|
||||
kv("operation", "reconcile"),
|
||||
kv("warning.kind", "scheduled-task-failed"),
|
||||
kv("retry.disposition", "next-run"),
|
||||
failure);
|
||||
logPassFailure(
|
||||
"fileserver.reconcile pass failed; the next run looks again", "reconcile", failure);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,13 +113,36 @@ public final class FileserverCleanupWorker {
|
||||
kv("cleanup.reclaimed_bytes", result.reclaimedBytes()));
|
||||
}
|
||||
} catch (RuntimeException failure) {
|
||||
LOG.warn(
|
||||
logPassFailure(
|
||||
"fileserver.cleanup batch failed; the next run retries the same queue",
|
||||
kv("capability", "fileserver"),
|
||||
kv("operation", "cleanup"),
|
||||
kv("warning.kind", "scheduled-task-failed"),
|
||||
kv("retry.disposition", "next-run"),
|
||||
"cleanup",
|
||||
failure);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* One WARN line per failed pass. A classified failure is written as every transport writes it
|
||||
* ({@link ServerFailureLog}): a persistence failure by its classification only, because its cause
|
||||
* holds SQL, constraint names and values; any other carrier, client-side code included, with its
|
||||
* stack ({@link ServerFailureLog#forBackgroundTask}). The whole exception used to be attached
|
||||
* whatever it was (review 2026-11-12). An unclassified failure is a defect and keeps its stack.
|
||||
*/
|
||||
private static void logPassFailure(String message, String operation, RuntimeException failure) {
|
||||
Object[] fields = {
|
||||
kv("capability", "fileserver"),
|
||||
kv("operation", operation),
|
||||
kv("warning.kind", "scheduled-task-failed"),
|
||||
kv("retry.disposition", "next-run")
|
||||
};
|
||||
Optional<Throwable> carrier = ApiErrorCarrier.firstIn(failure);
|
||||
if (carrier.isPresent()) {
|
||||
ApiErrorCode code = ((ApiErrorCarrier) carrier.get()).errorCode();
|
||||
ServerFailureLog line = ServerFailureLog.forBackgroundTask(carrier.get(), code);
|
||||
LOG.warn(line.format("; " + message), line.arguments(fields));
|
||||
return;
|
||||
}
|
||||
Object[] withFailure = Arrays.copyOf(fields, fields.length + 1);
|
||||
withFailure[fields.length] = failure;
|
||||
LOG.warn(message, withFailure);
|
||||
}
|
||||
}
|
||||
|
||||
+45
@@ -1,14 +1,23 @@
|
||||
package dev.caskeleton.composition.fileserver;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.tuple;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.read.ListAppender;
|
||||
import dev.caskeleton.application.fileserver.api.FileId;
|
||||
import dev.caskeleton.application.fileserver.cleanup.CleanupBatchResult;
|
||||
import dev.caskeleton.application.fileserver.recovery.FileReconciliationService;
|
||||
import dev.caskeleton.application.fileserver.recovery.ReconciliationResult;
|
||||
import dev.caskeleton.shared.error.OperationalError;
|
||||
import dev.caskeleton.shared.error.PersistenceFailureException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
class FileserverCleanupWorkerTest {
|
||||
|
||||
@@ -61,4 +70,40 @@ class FileserverCleanupWorkerTest {
|
||||
// then
|
||||
assertThat(reconciled).containsExactly(25);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("분류된 persistence 실패는 분류만 WARN 으로 남기고 SQL·값을 담은 원인 사슬을 붙이지 않는다")
|
||||
void aClassifiedCleanupFailureIsLoggedWithoutItsCause() {
|
||||
// given
|
||||
FileserverCleanupWorker worker =
|
||||
new FileserverCleanupWorker(
|
||||
(maxItems, maxBytes) -> {
|
||||
throw new PersistenceFailureException(
|
||||
OperationalError.DB_UNAVAILABLE,
|
||||
"SECRET_SQL delete from file_metadata where id=7",
|
||||
new IllegalStateException("SECRET_SQL cause"));
|
||||
},
|
||||
reconciliation,
|
||||
25,
|
||||
1024);
|
||||
Logger logger = (Logger) LoggerFactory.getLogger(FileserverCleanupWorker.class);
|
||||
ListAppender<ILoggingEvent> appender = new ListAppender<>();
|
||||
appender.start();
|
||||
logger.addAppender(appender);
|
||||
|
||||
// when
|
||||
try {
|
||||
worker.runBatch();
|
||||
} finally {
|
||||
logger.detachAppender(appender);
|
||||
}
|
||||
|
||||
// then
|
||||
assertThat(appender.list)
|
||||
.extracting(
|
||||
ILoggingEvent::getLevel,
|
||||
event -> event.getFormattedMessage().contains("SECRET_SQL"),
|
||||
event -> event.getThrowableProxy() == null)
|
||||
.containsExactly(tuple(Level.WARN, false, true));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,9 @@ dependencies {
|
||||
implementation project(':application-core')
|
||||
// StrictSettingsBinder, the typed startup failures and the runtime warning fields.
|
||||
implementation project(':adapter:outbound:support')
|
||||
// ApiErrorCarrier and ServerFailureLog: a failed relay tick is logged the way every transport
|
||||
// logs a classified failure, without the SQL a persistence failure's cause holds.
|
||||
implementation project(':shared-contract')
|
||||
|
||||
implementation 'org.springframework.boot:spring-boot-autoconfigure'
|
||||
implementation 'org.springframework:spring-context'
|
||||
|
||||
+34
-7
@@ -5,6 +5,11 @@ import static net.logstash.logback.argument.StructuredArguments.kv;
|
||||
import dev.caskeleton.application.outbox.OutboxRelayResult;
|
||||
import dev.caskeleton.application.outbox.PublishPendingOutboxEventsCommand;
|
||||
import dev.caskeleton.application.outbox.PublishPendingOutboxEventsUseCase;
|
||||
import dev.caskeleton.shared.error.ApiErrorCarrier;
|
||||
import dev.caskeleton.shared.error.ApiErrorCode;
|
||||
import dev.caskeleton.shared.error.ServerFailureLog;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.SmartLifecycle;
|
||||
@@ -112,16 +117,38 @@ public class OutboxRelayScheduler implements SmartLifecycle {
|
||||
} while (!stopRequested && result.publishedAny() && cycles < MAX_CYCLES_PER_TICK);
|
||||
|
||||
} catch (Exception ex) {
|
||||
log.error(
|
||||
"outbox relay scheduler: unexpected error in relay cycle — "
|
||||
+ "relay will retry on the next tick",
|
||||
kv("capability", "outbox"),
|
||||
kv("operation", "relay"),
|
||||
kv("retry.disposition", "next-tick"),
|
||||
ex);
|
||||
logRelayFailure(ex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* One line per failed tick. A classified failure is written as every transport writes it ({@link
|
||||
* ServerFailureLog}): a persistence failure by its classification only, because its cause holds
|
||||
* SQL, constraint names and values; any other carrier, client-side code included, with its stack
|
||||
* ({@link ServerFailureLog#forBackgroundTask}). The whole exception used to be attached whatever
|
||||
* it was (review 2026-11-12). An unclassified failure is a defect and keeps its stack.
|
||||
*/
|
||||
private static void logRelayFailure(Exception failure) {
|
||||
Object[] fields = {
|
||||
kv("capability", "outbox"), kv("operation", "relay"), kv("retry.disposition", "next-tick")
|
||||
};
|
||||
Optional<Throwable> carrier = ApiErrorCarrier.firstIn(failure);
|
||||
if (carrier.isPresent()) {
|
||||
ApiErrorCode code = ((ApiErrorCarrier) carrier.get()).errorCode();
|
||||
ServerFailureLog line = ServerFailureLog.forBackgroundTask(carrier.get(), code);
|
||||
log.error(
|
||||
line.format(" in an outbox relay cycle; the relay retries on the next tick"),
|
||||
line.arguments(fields));
|
||||
return;
|
||||
}
|
||||
Object[] withFailure = Arrays.copyOf(fields, fields.length + 1);
|
||||
withFailure[fields.length] = failure;
|
||||
log.error(
|
||||
"outbox relay scheduler: unexpected error in relay cycle — "
|
||||
+ "relay will retry on the next tick",
|
||||
withFailure);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
synchronized (monitor) {
|
||||
|
||||
+76
@@ -12,8 +12,14 @@ import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.read.ListAppender;
|
||||
import dev.caskeleton.application.outbox.PublishPendingOutboxEventsCommand;
|
||||
import dev.caskeleton.application.outbox.PublishPendingOutboxEventsUseCase;
|
||||
import dev.caskeleton.shared.error.ApiErrorCarrier;
|
||||
import dev.caskeleton.shared.error.ApiErrorCode;
|
||||
import dev.caskeleton.shared.error.OperationalError;
|
||||
import dev.caskeleton.shared.error.PersistenceFailureException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -44,6 +50,63 @@ class OutboxRelaySchedulerLoggingTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("분류된 persistence 실패는 분류만 남기고 SQL·값을 담은 원인 사슬을 붙이지 않는다")
|
||||
void aPersistenceFailureIsLoggedByItsClassificationWithoutItsCause() {
|
||||
// given
|
||||
PublishPendingOutboxEventsUseCase relay = mock(PublishPendingOutboxEventsUseCase.class);
|
||||
when(relay.handle(any(PublishPendingOutboxEventsCommand.class)))
|
||||
.thenThrow(
|
||||
new PersistenceFailureException(
|
||||
OperationalError.DB_UNAVAILABLE,
|
||||
"SECRET_SQL select * from outbox where tenant='acme'",
|
||||
new IllegalStateException("SECRET_SQL cause")));
|
||||
OutboxRelayScheduler scheduler = new OutboxRelayScheduler(relay, mock(OutboxMetrics.class));
|
||||
scheduler.start();
|
||||
|
||||
// when
|
||||
ILoggingEvent event;
|
||||
try (CapturedLogger captured = capture(OutboxRelayScheduler.class)) {
|
||||
scheduler.relay();
|
||||
event = captured.singleEvent();
|
||||
}
|
||||
|
||||
// then
|
||||
assertThat(
|
||||
List.of(
|
||||
event.getFormattedMessage().contains("SECRET_SQL"),
|
||||
event.getThrowableProxy() == null,
|
||||
event
|
||||
.getFormattedMessage()
|
||||
.startsWith("persistence failure classified as DB_UNAVAILABLE")))
|
||||
.containsExactly(false, true, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("client 쪽 코드를 실은 실패도 스케줄러에서는 결함이라 stack 과 함께 분류해 남긴다")
|
||||
void aClientSideCarrierInARelayCycleKeepsItsStack() {
|
||||
// given
|
||||
PublishPendingOutboxEventsUseCase relay = mock(PublishPendingOutboxEventsUseCase.class);
|
||||
RuntimeException failure = new ClientSideCarrier();
|
||||
when(relay.handle(any(PublishPendingOutboxEventsCommand.class))).thenThrow(failure);
|
||||
OutboxRelayScheduler scheduler = new OutboxRelayScheduler(relay, mock(OutboxMetrics.class));
|
||||
scheduler.start();
|
||||
|
||||
// when
|
||||
ILoggingEvent event;
|
||||
try (CapturedLogger captured = capture(OutboxRelayScheduler.class)) {
|
||||
scheduler.relay();
|
||||
event = captured.singleEvent();
|
||||
}
|
||||
|
||||
// then
|
||||
assertThat(event)
|
||||
.extracting(
|
||||
e -> e.getFormattedMessage().startsWith("failure classified as BAD_PARAMETER"),
|
||||
e -> e.getThrowableProxy() == null ? null : e.getThrowableProxy().getClassName())
|
||||
.containsExactly(true, ClientSideCarrier.class.getName());
|
||||
}
|
||||
|
||||
private static CapturedLogger capture(Class<?> owner) {
|
||||
Logger logger = (Logger) LoggerFactory.getLogger(owner);
|
||||
ListAppender<ILoggingEvent> appender = new ListAppender<>();
|
||||
@@ -61,6 +124,19 @@ class OutboxRelaySchedulerLoggingTest {
|
||||
return Arrays.stream(arguments).map(String::valueOf).collect(Collectors.joining(" | "));
|
||||
}
|
||||
|
||||
private static final class ClientSideCarrier extends RuntimeException implements ApiErrorCarrier {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
ClientSideCarrier() {
|
||||
super("a client-side code raised by server code");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApiErrorCode errorCode() {
|
||||
return OperationalError.BAD_PARAMETER;
|
||||
}
|
||||
}
|
||||
|
||||
private record CapturedLogger(Logger logger, ListAppender<ILoggingEvent> appender)
|
||||
implements AutoCloseable {
|
||||
|
||||
|
||||
@@ -210,12 +210,10 @@ runtimeOnly project(':composition:service-runtime')
|
||||
`SimpleMeterRegistry`에 대해 직접 구동할 수 있게 하기 위함이다.
|
||||
|
||||
#### MetricsDistributionMeterFilter
|
||||
- **소유한(owned) 타이머 메트릭 5종에만 SLO 기반 히스토그램·백분위 설정을 적용한다.** 대상:
|
||||
`http.server.requests`, `http.server.requests.latency`, `dependency.client.requests`,
|
||||
`db.query.duration`, `jvm.gc.pause`. 이들은 `metrics.yaml`에서 이 브랜치가 소유하고
|
||||
`histogram_buckets: slo_driven`인 행이다. `resilience4j.circuitbreaker.calls`,
|
||||
`hikaricp.connections.acquire`처럼 소비만 하고 소유하지 않는(consumed-but-not-owned) 행은 다른
|
||||
브랜치 소유라 의도적으로 제외 — 덮어쓰면 안 된다.
|
||||
- **소유한(owned) 타이머 메트릭 둘에만 SLO 기반 히스토그램·백분위 설정을 적용한다.** 대상:
|
||||
`http.server.requests`, `jvm.gc.pause`. `metrics.yaml`에서 `histogram_buckets: slo_driven`인 행은 정확히 이 둘이다
|
||||
(`MetricsAlertingContractTest`가 두 집합을 대조한다). `hikaricp.connections.acquire`처럼 다른 라이브러리가 내는 timer는
|
||||
histogram을 켜지 않고, 그 행은 평균(sum/count)으로 경보를 적는다.
|
||||
- **히스토그램 전략은 두 축이다.**
|
||||
- *인스턴스 간 집계 가능한 진실의 원천*: `percentilesHistogram(true)`(= `publishPercentileHistogram`)가
|
||||
`_bucket` 시계열을 생성하고, Prometheus 에서 `histogram_quantile()`로 인스턴스 간 집계할 수 있다.
|
||||
|
||||
+15
-6
@@ -15,18 +15,27 @@ import java.util.Set;
|
||||
*/
|
||||
public final class MetricsDistributionMeterFilter implements MeterFilter {
|
||||
|
||||
/**
|
||||
* The owned timers some code registers. Three more names stood here — {@code
|
||||
* http.server.requests.latency}, {@code dependency.client.requests}, {@code db.query.duration} —
|
||||
* that nothing registered, so the filter configured meters that never existed (review
|
||||
* 2026-11-12). Latency alerts use {@code http.server.requests}.
|
||||
*/
|
||||
private static final Set<String> SLO_DRIVEN_TIMERS =
|
||||
Set.of(
|
||||
"http.server.requests",
|
||||
"http.server.requests.latency",
|
||||
"dependency.client.requests",
|
||||
"db.query.duration",
|
||||
"jvm.gc.pause");
|
||||
Set.of("http.server.requests", "jvm.gc.pause");
|
||||
|
||||
public MetricsDistributionMeterFilter() {
|
||||
// public no-arg constructor — required by MetricsContractConfig.install()
|
||||
}
|
||||
|
||||
/**
|
||||
* The timers this filter gives an SLO histogram; {@code docs/registries/metrics.yaml} marks
|
||||
* exactly these {@code slo_driven}.
|
||||
*/
|
||||
public static Set<String> sloDrivenTimers() {
|
||||
return SLO_DRIVEN_TIMERS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DistributionStatisticConfig configure(Meter.Id id, DistributionStatisticConfig config) {
|
||||
if (!SLO_DRIVEN_TIMERS.contains(id.getName())) {
|
||||
|
||||
+3
-3
@@ -113,9 +113,9 @@ class MetricsContractConfigTest {
|
||||
.isNull();
|
||||
|
||||
// Clean metric: registered (D9 applied for timers)
|
||||
registry.timer("db.query.duration", "operation", "select", "outcome", "SUCCESS");
|
||||
assertThat(registry.find("db.query.duration").timer())
|
||||
.as("db.query.duration timer with clean tags must be registered")
|
||||
registry.timer("http.server.requests", "method", "GET", "outcome", "SUCCESS");
|
||||
assertThat(registry.find("http.server.requests").timer())
|
||||
.as("http.server.requests timer with clean tags must be registered")
|
||||
.isNotNull();
|
||||
}
|
||||
}
|
||||
|
||||
+4
-32
@@ -26,14 +26,7 @@ class MetricsDistributionMeterFilterTest {
|
||||
// ----------------------------------------------------------------
|
||||
|
||||
@ParameterizedTest(name = "D9: configure() applies SLO histogram to owned timer ''{0}''")
|
||||
@ValueSource(
|
||||
strings = {
|
||||
"http.server.requests",
|
||||
"http.server.requests.latency",
|
||||
"dependency.client.requests",
|
||||
"db.query.duration",
|
||||
"jvm.gc.pause"
|
||||
})
|
||||
@ValueSource(strings = {"http.server.requests", "jvm.gc.pause"})
|
||||
void configureAppliesSloHistogramToOwnedTimers(String metricName) {
|
||||
Meter.Id id = new Meter.Id(metricName, Tags.empty(), null, null, Meter.Type.TIMER);
|
||||
DistributionStatisticConfig result = filter.configure(id, DistributionStatisticConfig.DEFAULT);
|
||||
@@ -44,14 +37,7 @@ class MetricsDistributionMeterFilterTest {
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "D9: configure() sets non-empty SLO boundaries for owned timer ''{0}''")
|
||||
@ValueSource(
|
||||
strings = {
|
||||
"http.server.requests",
|
||||
"http.server.requests.latency",
|
||||
"dependency.client.requests",
|
||||
"db.query.duration",
|
||||
"jvm.gc.pause"
|
||||
})
|
||||
@ValueSource(strings = {"http.server.requests", "jvm.gc.pause"})
|
||||
void configureSetsSloBoundariesForOwnedTimers(String metricName) {
|
||||
Meter.Id id = new Meter.Id(metricName, Tags.empty(), null, null, Meter.Type.TIMER);
|
||||
DistributionStatisticConfig result = filter.configure(id, DistributionStatisticConfig.DEFAULT);
|
||||
@@ -66,14 +52,7 @@ class MetricsDistributionMeterFilterTest {
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "D9: configure() sets client-side percentiles for owned timer ''{0}''")
|
||||
@ValueSource(
|
||||
strings = {
|
||||
"http.server.requests",
|
||||
"http.server.requests.latency",
|
||||
"dependency.client.requests",
|
||||
"db.query.duration",
|
||||
"jvm.gc.pause"
|
||||
})
|
||||
@ValueSource(strings = {"http.server.requests", "jvm.gc.pause"})
|
||||
void configureSetsPercentilesForOwnedTimers(String metricName) {
|
||||
Meter.Id id = new Meter.Id(metricName, Tags.empty(), null, null, Meter.Type.TIMER);
|
||||
DistributionStatisticConfig result = filter.configure(id, DistributionStatisticConfig.DEFAULT);
|
||||
@@ -86,14 +65,7 @@ class MetricsDistributionMeterFilterTest {
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "D9: configure() sets min/max expected value for owned timer ''{0}''")
|
||||
@ValueSource(
|
||||
strings = {
|
||||
"http.server.requests",
|
||||
"http.server.requests.latency",
|
||||
"dependency.client.requests",
|
||||
"db.query.duration",
|
||||
"jvm.gc.pause"
|
||||
})
|
||||
@ValueSource(strings = {"http.server.requests", "jvm.gc.pause"})
|
||||
void configureSetsMinMaxExpectedValueForOwnedTimers(String metricName) {
|
||||
Meter.Id id = new Meter.Id(metricName, Tags.empty(), null, null, Meter.Type.TIMER);
|
||||
DistributionStatisticConfig result = filter.configure(id, DistributionStatisticConfig.DEFAULT);
|
||||
|
||||
Reference in New Issue
Block a user