Files
clean-architecture-backend-…/scripts/verify-compose-profile-contracts.sh
DongHyeonka 2d4d3abfd3
pr / done (pull_request) Successful in 3m5s
fix(compose): 계약 판정기가 heap dump 경로·겹친 mount·ACL 줄을 실제처럼 읽는다
문제: compose 계약 판정기(scripts/compose-contract-verdict.py)가 입력을
실제로 쓰는 쪽과 다르게 읽어, 틀린 계약을 통과시키거나 맞는 계약을
떨어뜨린다.
- heap dump 경로 정규식이 줄바꿈을 넘어 잡는다. 주석이 옵션으로 끝나거나,
  따옴표 없는 ENV 줄 끝이거나, 빈 값이거나, 한 줄에 두 번이면 엉뚱한
  경로를 읽고, 진짜 경로의 tmpfs가 rc=0으로 지나간다.
- tmpfs 아래 더 깊은 volume·bind가 heap 경로를 잡아도 실패로 본다.
  겹친 bind에서는 목록에 먼저 나온 쪽 ACL 파일을 읽는다. Docker는 선언
  순서와 상관없이 더 깊은 mount를 위에 붙인다.
- 역할 목록·최상위 목록이 문자열·숫자·object면 빈 목록으로 읽는다.
- 같은 사용자가 두 줄이어도 둘 다 `on`일 때만 잡는다. Redis는 이름이
  겹치면 기동을 거절한다.
- ACL 줄을 str.split()으로 잘라, Redis가 거절하는 탭·U+00A0·공백 둘·
  lone CR·주석 줄을 통과시킨다. 줄 앞 공백·`on`이 이름 바로 뒤가 아닌
  줄처럼 Redis가 받는 것은 떨어뜨린다. 같은 줄의 `on` 뒤 `off`·`reset`은
  켜진 계정으로 본다.
- 꺼진 계정 조건과 lane id `fullmatch`를 약하게 바꿔도 시험이 통과한다.

고른 방법:
- Dockerfile은 LF에서만 자르고 주석 줄을 뺀 뒤 한 줄씩 값을 읽는다.
  값이 여럿이면 JVM이 어느 것을 받을지 명령마다 달라, 모두 판정한다.
  마지막 값이 비면 78이다.
- mount는 target을 한 번 정규화해 `Mount`로 두고, 경로를 품은 가장 깊은
  mount를 고르는 함수 하나를 heap dump와 ACL 파일이 같이 쓴다. 중복
  target도 정규화한 값으로 비교한다.
- ACL은 redis-server --aclfile처럼 읽는 함수 하나로 줄 번호와 낱말을
  만든다. `user <name>`으로 시작하지 않는 줄, 겹친 이름, 마지막
  on/off/reset으로 정한 켜짐을 그 결과로 판정한다.
- 선택 목록은 없음·null·false 또는 이름의 목록만 받는다.
- 버린 안: 카드의 첫 줄 MULTILINE 검색(이어진 줄에 두 번이면 앞 값을
  읽는다), 마지막 값 하나만 판정(뒤 명령의 다른 값이 앞의 tmpfs를
  가린다), 건마다 카드(같은 두 파일에서 충돌한다).

전제(C2): desktop Docker 29.6.1·compose 5.3.0·redis:7.4로 각 항목을
실제로 돌렸고 모두 위 동작과 맞았다. 틀린 전제는 없어 미적용 항목이
없다.

위험·한계:
- 값이 여럿인 Dockerfile은 JVM이 실제로 받지 않는 값이 tmpfs에 놓여도
  실패한다. 게이트가 막는 쪽으로 틀린다.
- ACL은 Redis 문법을 다 검사하지 않는다. 판정이 보는 낱말(이름, on·off·
  reset, >비밀번호, ~키 패턴)과 줄 머리만 Redis처럼 읽는다.
- `user default off`는 그 낱말 셋인 줄만 받는다(base와 같다).

검증(노트북): python3 -m unittest discover -s scripts/tests 371개 OK.
새 시험을 base 판정기로 돌리면 실패하고, 지키는 줄을 하나씩 약하게
바꾼 변이 38개가 모두 시험을 실패시킨다. desktop pr 단계는 이 커밋
트리로 돌린다.

Refs: REV-BE-0053
2026-11-23 10:32:13 +09:00

87 lines
5.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# The static half of the Compose contract: every lane renders, and renders exactly what it claims.
#
# Called by: the pull-request stage `prCheck` (`verifyComposeProfiles`, root build.gradle) and
# scripts/run-compose-runtime-smoke.sh before it starts the lanes.
#
# This file keeps the docker calls and nothing else. Which lanes to render and with which files and
# profiles, and every judgement below, are scripts/compose-contract-verdict.py, whose cases in
# scripts/tests try each judgement on broken input without Docker. Each judgement used to be jq here,
# and a jq branch could not be tried on a broken input: an empty lane list passed as "all 0 lanes
# match", and so did a JPA lane with no expected vendor and a heap dump path under a tmpfs.
#
# Ten things are checked, and each exists because assuming it cost something:
#
# 1. the Compose version, because `!override` is what stops the dev overlay's tmpfs merging with
# the base's and colliding with a bind mount, and it needs 2.24.4;
# 2. the exact service set per lane — exact, not a superset, because a lane that quietly gains a
# service is a lane whose evidence describes a different stack than the one that ran;
# 3. the rendered SPRING_PROFILES_ACTIVE, because a Compose profile selects services and says
# nothing about which environment the application thinks it is in;
# 4. mount-target uniqueness in the merged model, because that collision is exactly what made the
# dev stack unrenderable and "the syntax looks right" is not the same as "the targets are
# distinct";
# 5. the service-role partition, because a service the lane renders and files under no role is
# started by nothing and checked by nothing, and a one-shot filed as a --wait target hangs the
# lane for its full timeout on a container that was built to exit;
# 6. the expected application report (expectedCapabilitiesOn, expectedPersistenceVendor) names
# only catalogued capabilities and a vendor that agrees with persistence-jpa, because an
# expectation the application cannot report fails every run of the lane;
# 7. a lane that switches Redis on authenticates as an account its Redis provisions, because the
# lanes ran Redis open and the application anonymous, and the two startup warnings that trade
# prints failed the "startup is silent" check of every such lane (r3-J). Lowering that warning
# would hide the same trade in production; the lane has to look like a deployment instead.
# 8. a lane's appMemoryLimit, when it has one, is a Compose memory size, because the smoke wrapper
# writes it into the application's mem_limit and a malformed one fails only at `up`.
# 9. the application's heap dump path (-XX:HeapDumpPath in the Dockerfile) is not on a tmpfs: the
# deepest mount at that path or at a directory above it is not one, because a tmpfs is charged to the
# container's memory limit and is gone once the container exits: the dump written at an
# OutOfMemoryError could only push the container into an OOM kill and was never there to read
# (2026-11-12 review).
# 10. the Keycloak realm artifact carries no key Keycloak refuses to deserialize, because the import
# fails on one and the container exits before the lane reaches what it was testing.
#
# Static only. Nothing starts here; scripts/run-compose-runtime-smoke.sh owns that.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
CONTRACTS="${REPO_ROOT}/src/bootstrap/app/compose-lanes/compose-profile-contracts.json"
VERDICT="${REPO_ROOT}/scripts/compose-contract-verdict.py"
command -v python3 >/dev/null 2>&1 || { echo "python3 is required" >&2; exit 78; }
command -v docker >/dev/null 2>&1 || { echo "docker is required" >&2; exit 78; }
[[ -r "${CONTRACTS}" ]] || { echo "missing ${CONTRACTS}" >&2; exit 78; }
cd "${REPO_ROOT}"
RENDERED="$(mktemp -d "${TMPDIR:-/tmp}/compose-contract.XXXXXX")"
trap 'rm -rf "${RENDERED}"' EXIT
# 1. The version is judged before anything renders: an older Compose fails the dev overlay's render
# with a merge error that says nothing about the version.
compose_version="$(docker compose version --short)"
python3 "${VERDICT}" version "${CONTRACTS}" "${compose_version}"
# Every lane the contract lets the verdict render, as its id and its `docker compose` arguments. A
# lane it cannot render (no files, an unknown file) is not listed; `judge` reports it.
python3 "${VERDICT}" lanes "${CONTRACTS}" >"${RENDERED}/lanes.tsv"
# Each call's output and exit status go to files, and the status is read outside any condition so a
# failed render is recorded as itself. stderr of `config --services` goes to its own file rather than
# into the list: merged with `2>&1`, Compose's warning about an unset interpolation variable arrived
# as an extra "service" and twelve lanes failed with a log line in place of a container name.
# stdin is /dev/null: the loop reads the lane list from stdin, and a call that read it would leave
# the lanes after it without a record.
while IFS=$'\t' read -r -a fields; do
lane="${RENDERED}/${fields[0]}"
args=("${fields[@]:1}")
status=0
docker compose "${args[@]}" config --services </dev/null >"${lane}.services" 2>"${lane}.services.err" || status=$?
echo "${status}" >"${lane}.services.rc"
status=0
docker compose "${args[@]}" config --format json </dev/null >"${lane}.json" 2>"${lane}.json.err" || status=$?
echo "${status}" >"${lane}.json.rc"
done <"${RENDERED}/lanes.tsv"
# 2-10, and the shape of every lane.
python3 "${VERDICT}" judge "${CONTRACTS}" "${RENDERED}"