From 7ba427ad443fa7dcf413935ada2994fe661dccde Mon Sep 17 00:00:00 2001 From: ngqile Date: Mon, 30 Nov 2026 10:15:29 +0800 Subject: [PATCH] fix: keep secrets out of the service images Each Java module's build context now holds only its jar, and local/image-check.sh fails an image that carries a key file, a private key under any name, or a secret setting with a built-in default. It found one: service-email defaulted SMSS360_APIKEY to a real-looking key, now removed (the key is in git history and must be rotated). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM --- local/image-check.sh | 60 +++++++++++++++++++ .../changes/add-release-ci-packaging/tasks.md | 5 +- solvway-api-gateway/.dockerignore | 3 + solvway-mobile-api/.dockerignore | 3 + solvway-services-auth/.dockerignore | 3 + solvway-services-counter/.dockerignore | 3 + solvway-services-email/.dockerignore | 3 + .../src/main/resources/application.yml | 2 +- solvway-services-log/.dockerignore | 3 + solvway-services-member/.dockerignore | 3 + solvway-services-system/.dockerignore | 3 + 11 files changed, 88 insertions(+), 3 deletions(-) create mode 100755 local/image-check.sh create mode 100644 solvway-api-gateway/.dockerignore create mode 100644 solvway-mobile-api/.dockerignore create mode 100644 solvway-services-auth/.dockerignore create mode 100644 solvway-services-counter/.dockerignore create mode 100644 solvway-services-email/.dockerignore create mode 100644 solvway-services-log/.dockerignore create mode 100644 solvway-services-member/.dockerignore create mode 100644 solvway-services-system/.dockerignore diff --git a/local/image-check.sh b/local/image-check.sh new file mode 100755 index 0000000..ebe102f --- /dev/null +++ b/local/image-check.sh @@ -0,0 +1,60 @@ +#!/bin/sh +# Fails when an image carries secrets (openspec spec release-packaging): +# - key or env files by name (appkeys.json, .env*, *.pem, *.p12, *.jks) anywhere outside the system folders; +# - any file holding "private_key" (a Google service-account key, whatever it is named), loose in the image or +# inside app.jar or the project's own solvway-* library jars; +# - a secret-named Spring placeholder (key, secret, password, pwd, token) with a built-in default, e.g. ${SMS_APIKEY:abc}. +# Usage: local/image-check.sh +set -eu +image="$1" +work=$(mktemp -d) +cid="" +trap '[ -n "$cid" ] && docker rm -f "$cid" >/dev/null 2>&1; rm -rf "$work"' EXIT +command -v unzip >/dev/null || { echo "FAIL $image: unzip is required to inspect app.jar"; exit 2; } +cid=$(docker create "$image") + +# ponytail: the base image's system folders are skipped (our Dockerfiles only add /app.jar or /app); a key planted under +# /usr or /etc would pass. Upgrade: scan everything and allow-list the base image's own files. +SYSTEM='^(bin|boot|dev|etc|lib|lib64|opt/java|proc|sbin|sys|usr|var)/' +fail=0 +mkdir "$work/fs" +# --anchored: only the top-level dev/proc/sys are skipped, never a folder of that name deeper down. +docker export "$cid" | tar -x -C "$work/fs" --anchored --exclude=dev --exclude=proc --exclude=sys +(cd "$work/fs" && find . -type f | sed 's|^\./||') | grep -vE "$SYSTEM" > "$work/files" || true + +if grep -E '(^|/)(appkeys\.json|\.env(\.[^/]*)?|[^/]*\.pem|[^/]*\.p12|[^/]*\.jks)$' "$work/files"; then + echo "FAIL $image: key or env file in the image (listed above)" + fail=1 +fi + +# Every non-system file, whatever its name or extension, is searched in place for a private key. +# Test grep's output, not its exit code: xargs exits 123 when any one batch finds nothing. +found=$(cd "$work/fs" && tr '\n' '\0' < "$work/files" | xargs -0 -r grep -lE '"private_key"' || true) +if [ -n "$found" ]; then + echo "$found" + echo "FAIL $image: a file holding a private key (listed above)" + fail=1 +fi + +scan="$work/scan" +mkdir "$scan" +if [ -f "$work/fs/app.jar" ]; then + unzip -q "$work/fs/app.jar" -d "$scan/jar" + for lib in "$scan"/jar/BOOT-INF/lib/solvway-*.jar; do + [ -e "$lib" ] || continue + unzip -q "$lib" -d "$lib.d" + done +fi +if grep -rlE '"private_key"' "$scan"; then + echo "FAIL $image: a file inside app.jar holding a private key (listed above)" + fail=1 +fi +hits=$(grep -rhoiE '\$\{[a-z0-9_.-]*(key|secret|password|pwd|token)[a-z0-9_.-]*:[^}]+\}' \ + --include='*.yml' --include='*.yaml' --include='*.properties' "$scan" | sed -E 's/^\$\{([^:]+):.*/\1/' | sort -u || true) +if [ -n "$hits" ]; then + echo "FAIL $image: secret settings with a built-in default value: $hits" + fail=1 +fi + +[ "$fail" -eq 0 ] && echo "OK $image" +exit "$fail" diff --git a/openspec/changes/add-release-ci-packaging/tasks.md b/openspec/changes/add-release-ci-packaging/tasks.md index 86b4c5e..0586a85 100644 --- a/openspec/changes/add-release-ci-packaging/tasks.md +++ b/openspec/changes/add-release-ci-packaging/tasks.md @@ -4,13 +4,14 @@ Design: `design.md`. All tasks run serially: each touches a build file, CI or SQ ## 1. Images and database package -- [ ] 1.1 Make the images production-safe (design decisions 4 and 5). +- [x] 1.1 Make the images production-safe (design decisions 4 and 5). - Add a `.dockerignore` to each of the nine modules that lacks one. - Check each Dockerfile for baked-in config or secrets. - Write `local/image-check.sh `. It fails when an image holds `appkeys.json`, `.env`, `*.pem`, `*.p12` or a Google service-account JSON. Tests first: build one image with a planted `appkeys.json` in its module folder and show the check fails (red). Add the `.dockerignore` and show it passes (green). Then build all nine images locally, and run the check on each. - - Scope: `solvway-*/Dockerfile`, `solvway-*/.dockerignore`, `local/image-check.sh` + - The check also fails on a secret-named setting (KEY, SECRET, PASSWORD, PWD, TOKEN) that has a built-in default in the jar's config. It found one: `SMSS360_APIKEY` in service-email carried a real-looking key as its default, which is now removed. That key is in git history, so it must be rotated outside this change. + - Scope: `solvway-*/Dockerfile`, `solvway-*/.dockerignore`, `local/image-check.sh`, `solvway-services-email/src/main/resources/application.yml` (the key default only) - Verify: all nine `docker build` commands succeed, and `local/image-check.sh` passes on each image. - [ ] 1.2 Write the production database package (design decision 6): `sql/00` to `04` with source headers. diff --git a/solvway-api-gateway/.dockerignore b/solvway-api-gateway/.dockerignore new file mode 100644 index 0000000..8881e96 --- /dev/null +++ b/solvway-api-gateway/.dockerignore @@ -0,0 +1,3 @@ +# The image needs only the built jar; nothing else from the module enters the build context. +* +!target/*.jar diff --git a/solvway-mobile-api/.dockerignore b/solvway-mobile-api/.dockerignore new file mode 100644 index 0000000..8881e96 --- /dev/null +++ b/solvway-mobile-api/.dockerignore @@ -0,0 +1,3 @@ +# The image needs only the built jar; nothing else from the module enters the build context. +* +!target/*.jar diff --git a/solvway-services-auth/.dockerignore b/solvway-services-auth/.dockerignore new file mode 100644 index 0000000..8881e96 --- /dev/null +++ b/solvway-services-auth/.dockerignore @@ -0,0 +1,3 @@ +# The image needs only the built jar; nothing else from the module enters the build context. +* +!target/*.jar diff --git a/solvway-services-counter/.dockerignore b/solvway-services-counter/.dockerignore new file mode 100644 index 0000000..8881e96 --- /dev/null +++ b/solvway-services-counter/.dockerignore @@ -0,0 +1,3 @@ +# The image needs only the built jar; nothing else from the module enters the build context. +* +!target/*.jar diff --git a/solvway-services-email/.dockerignore b/solvway-services-email/.dockerignore new file mode 100644 index 0000000..8881e96 --- /dev/null +++ b/solvway-services-email/.dockerignore @@ -0,0 +1,3 @@ +# The image needs only the built jar; nothing else from the module enters the build context. +* +!target/*.jar diff --git a/solvway-services-email/src/main/resources/application.yml b/solvway-services-email/src/main/resources/application.yml index af98e5d..5cfcd4b 100644 --- a/solvway-services-email/src/main/resources/application.yml +++ b/solvway-services-email/src/main/resources/application.yml @@ -81,4 +81,4 @@ mailjet: sms: smss360: email: ${SMSS360_EMAIL:marketing.tastefullymy@gmail.com} - api-key: ${SMSS360_APIKEY:306496f1390f352e0cbad097e1927138} \ No newline at end of file + api-key: ${SMSS360_APIKEY:} \ No newline at end of file diff --git a/solvway-services-log/.dockerignore b/solvway-services-log/.dockerignore new file mode 100644 index 0000000..8881e96 --- /dev/null +++ b/solvway-services-log/.dockerignore @@ -0,0 +1,3 @@ +# The image needs only the built jar; nothing else from the module enters the build context. +* +!target/*.jar diff --git a/solvway-services-member/.dockerignore b/solvway-services-member/.dockerignore new file mode 100644 index 0000000..8881e96 --- /dev/null +++ b/solvway-services-member/.dockerignore @@ -0,0 +1,3 @@ +# The image needs only the built jar; nothing else from the module enters the build context. +* +!target/*.jar diff --git a/solvway-services-system/.dockerignore b/solvway-services-system/.dockerignore new file mode 100644 index 0000000..8881e96 --- /dev/null +++ b/solvway-services-system/.dockerignore @@ -0,0 +1,3 @@ +# The image needs only the built jar; nothing else from the module enters the build context. +* +!target/*.jar