Merge remote-tracking branch 'origin/claude/t3-error-codes-loglevel-fcf427' into claude/integration-r7
This commit is contained in:
@@ -115,6 +115,10 @@ commit-aware response wrapper 계약을 구현한다. 또한 이 모듈은 HTML
|
||||
코드의 일반 `client_safe_message` 만 담고, 원시 예외 텍스트·`Authorization` 헤더·issuer·audience 는
|
||||
절대 포함하지 않는다. 로그 라인엔 code/category/요청 path 만 기록하고 bearer token 은 절대 로깅하지
|
||||
않는다(leak 테스트가 강제하는 계약). 전체 로그 마스킹 필터는 별도 log-management 영역에서 다룬다.
|
||||
- **로그 레벨.** 4xx(토큰 누락·만료·서명·issuer·audience·kid 실패, 권한 부족)는 호출자 쪽 실패라 WARN이다.
|
||||
5xx(`AUTH_JWKS_UNAVAILABLE` 503, `INTERNAL_AUTH_MISCONFIGURATION` 500)는 서버 쪽 고장이라 ERROR다. 다른
|
||||
경로가 5xx를 ERROR로 남기는 규칙(shared-contract `ServerFailureLog`)과 같다. 레벨은 오류 코드 레지스트리의
|
||||
칸이 아니라 이 코드가 정한다(설계 메모 `docs/superpowers/specs/2026-11-06-error-code-log-level-design.md`).
|
||||
- **WWW-Authenticate(RFC 9110 §15.5.2).** 401 응답은 반드시 WWW-Authenticate 헤더를 갖되, `error_description`
|
||||
으로 issuer/token 세부가 새지 않도록 최소한으로 유지한다.
|
||||
|
||||
|
||||
+11
-7
@@ -8,6 +8,7 @@ import jakarta.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.slf4j.event.Level;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import tools.jackson.databind.ObjectMapper;
|
||||
|
||||
@@ -28,13 +29,16 @@ class AuthErrorResponseWriter {
|
||||
|
||||
void write(HttpServletRequest request, HttpServletResponse response, OperationalError code)
|
||||
throws IOException {
|
||||
// Log only safe metadata — never the token or the raw failure message.
|
||||
log.warn(
|
||||
"security failure: code={} category={} method={} path={}",
|
||||
code.code(),
|
||||
code.category(),
|
||||
request.getMethod(),
|
||||
request.getRequestURI());
|
||||
// Log only safe metadata — never the token or the raw failure message. A 5xx is the server's
|
||||
// own failure (the key service is down, the decoder is misconfigured) and is an ERROR, as a 5xx
|
||||
// is on every other path (ServerFailureLog); a 4xx is the caller's and stays a WARN.
|
||||
log.atLevel(code.httpStatus() >= 500 ? Level.ERROR : Level.WARN)
|
||||
.log(
|
||||
"security failure: code={} category={} method={} path={}",
|
||||
code.code(),
|
||||
code.category(),
|
||||
request.getMethod(),
|
||||
request.getRequestURI());
|
||||
|
||||
// A 401 response carries a minimal WWW-Authenticate header (no issuer / token detail).
|
||||
if (code.httpStatus() == 401) {
|
||||
|
||||
+86
@@ -2,17 +2,23 @@ package dev.caskeleton.adapter.inbound.web.auth;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.read.ListAppender;
|
||||
import dev.caskeleton.shared.error.Category;
|
||||
import dev.caskeleton.shared.error.ClientErrorMessages;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.test.system.CapturedOutput;
|
||||
import org.springframework.boot.test.system.OutputCaptureExtension;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.security.authentication.InsufficientAuthenticationException;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.oauth2.core.OAuth2Error;
|
||||
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
|
||||
import org.springframework.security.oauth2.jwt.BadJwtException;
|
||||
@@ -106,6 +112,86 @@ class EnvelopeAuthenticationEntryPointTest {
|
||||
.isEqualTo("AUTH_JWKS_UNAVAILABLE");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKeyServiceOutageIsLoggedAtError() throws Exception {
|
||||
// given
|
||||
AuthenticationException outage =
|
||||
new InvalidBearerTokenException(
|
||||
"invalid",
|
||||
new JwtDecoderConfig.AuthenticationKeyServiceUnavailableException(
|
||||
new IOException("connect timed out")));
|
||||
|
||||
// when
|
||||
List<ILoggingEvent> lines = writerLinesFor(outage);
|
||||
|
||||
// then
|
||||
assertThat(lines)
|
||||
.singleElement()
|
||||
.extracting(event -> event.getLevel().toString(), ILoggingEvent::getFormattedMessage)
|
||||
.containsExactly(
|
||||
"ERROR",
|
||||
"security failure: code=AUTH_JWKS_UNAVAILABLE category=TRANSIENT_DEPENDENCY"
|
||||
+ " method=GET path=/v1/things");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aDecoderMisconfigurationIsLoggedAtError() throws Exception {
|
||||
// given
|
||||
AuthenticationException misconfiguration =
|
||||
new InvalidBearerTokenException(
|
||||
"invalid",
|
||||
new JwtDecoderConfig.AuthenticationDecoderMisconfigurationException(
|
||||
new IllegalStateException("issuer metadata does not match")));
|
||||
|
||||
// when
|
||||
List<ILoggingEvent> lines = writerLinesFor(misconfiguration);
|
||||
|
||||
// then
|
||||
assertThat(lines)
|
||||
.singleElement()
|
||||
.extracting(event -> event.getLevel().toString(), ILoggingEvent::getFormattedMessage)
|
||||
.containsExactly(
|
||||
"ERROR",
|
||||
"security failure: code=INTERNAL_AUTH_MISCONFIGURATION category=INTERNAL"
|
||||
+ " method=GET path=/v1/things");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRejectedSignatureStaysAtWarn() throws Exception {
|
||||
// given
|
||||
AuthenticationException rejected =
|
||||
new InvalidBearerTokenException(
|
||||
"invalid", new BadJwtException("Signed JWT rejected: Invalid signature"));
|
||||
|
||||
// when
|
||||
List<ILoggingEvent> lines = writerLinesFor(rejected);
|
||||
|
||||
// then
|
||||
assertThat(lines)
|
||||
.singleElement()
|
||||
.extracting(event -> event.getLevel().toString(), ILoggingEvent::getFormattedMessage)
|
||||
.containsExactly(
|
||||
"WARN",
|
||||
"security failure: code=AUTH_TOKEN_INVALID_SIGNATURE category=AUTH"
|
||||
+ " method=GET path=/v1/things");
|
||||
}
|
||||
|
||||
/** The lines the security writer logs while the entry point answers {@code failure}. */
|
||||
private List<ILoggingEvent> writerLinesFor(AuthenticationException failure) throws Exception {
|
||||
Logger logger = (Logger) LoggerFactory.getLogger(AuthErrorResponseWriter.class);
|
||||
ListAppender<ILoggingEvent> appender = new ListAppender<>();
|
||||
appender.start();
|
||||
logger.addAppender(appender);
|
||||
try {
|
||||
entryPoint.commence(
|
||||
new MockHttpServletRequest("GET", "/v1/things"), new MockHttpServletResponse(), failure);
|
||||
} finally {
|
||||
logger.detachAppender(appender);
|
||||
appender.stop();
|
||||
}
|
||||
return appender.list;
|
||||
}
|
||||
|
||||
@Test
|
||||
void tokenValueNeverLeaksIntoResponseBodyOrLogs(CapturedOutput output) throws Exception {
|
||||
// §테스트 계약: a JWT value must never appear in the response or the log output.
|
||||
|
||||
+19
@@ -98,6 +98,25 @@ class OperationalErrorRegistryContractTest {
|
||||
assertThat(actual).isNotEmpty().isEqualTo(expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* The log level is not a registry column. The failure's kind and path decide it — {@code
|
||||
* ServerFailureLog} on every transport, {@code AuthErrorResponseWriter} in the security filter —
|
||||
* not the code, so a per-row value can only drift from what is logged, as the column that was
|
||||
* removed did (docs/superpowers/specs/2026-11-06-error-code-log-level-design.md).
|
||||
*/
|
||||
@Test
|
||||
void noRegistryRowCarriesALogLevel() {
|
||||
List<String> rowsWithALogLevel =
|
||||
registryRows().stream()
|
||||
.filter(row -> row.containsKey("log_level"))
|
||||
.map(row -> String.valueOf(row.get("code")))
|
||||
.toList();
|
||||
|
||||
assertThat(rowsWithALogLevel)
|
||||
.as("%s rows with a log_level; the code decides the level, not the registry", REGISTRY)
|
||||
.isEmpty();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static List<Map<String, Object>> registryRows() {
|
||||
Object loaded = new Yaml(new SafeConstructor(new LoaderOptions())).load(read(REGISTRY));
|
||||
|
||||
Reference in New Issue
Block a user