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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
1 parent
3c093068a3
commit
7ba427ad44
11 files changed
+88
-3
No files matched your search
Executable
+60
@@ -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 <image>
|
||||||
|
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"
|
||||||
@@ -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. 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.
|
- Add a `.dockerignore` to each of the nine modules that lacks one.
|
||||||
- Check each Dockerfile for baked-in config or secrets.
|
- Check each Dockerfile for baked-in config or secrets.
|
||||||
- Write `local/image-check.sh <image>`. It fails when an image holds `appkeys.json`, `.env`, `*.pem`, `*.p12` or a Google service-account JSON.
|
- Write `local/image-check.sh <image>`. 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.
|
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.
|
- 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.
|
- [ ] 1.2 Write the production database package (design decision 6): `sql/00` to `04` with source headers.
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# The image needs only the built jar; nothing else from the module enters the build context.
|
||||||
|
*
|
||||||
|
!target/*.jar
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# The image needs only the built jar; nothing else from the module enters the build context.
|
||||||
|
*
|
||||||
|
!target/*.jar
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# The image needs only the built jar; nothing else from the module enters the build context.
|
||||||
|
*
|
||||||
|
!target/*.jar
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# The image needs only the built jar; nothing else from the module enters the build context.
|
||||||
|
*
|
||||||
|
!target/*.jar
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# The image needs only the built jar; nothing else from the module enters the build context.
|
||||||
|
*
|
||||||
|
!target/*.jar
|
||||||
@@ -81,4 +81,4 @@ mailjet:
|
|||||||
sms:
|
sms:
|
||||||
smss360:
|
smss360:
|
||||||
email: ${SMSS360_EMAIL:marketing.tastefullymy@gmail.com}
|
email: ${SMSS360_EMAIL:marketing.tastefullymy@gmail.com}
|
||||||
api-key: ${SMSS360_APIKEY:306496f1390f352e0cbad097e1927138}
|
api-key: ${SMSS360_APIKEY:}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# The image needs only the built jar; nothing else from the module enters the build context.
|
||||||
|
*
|
||||||
|
!target/*.jar
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# The image needs only the built jar; nothing else from the module enters the build context.
|
||||||
|
*
|
||||||
|
!target/*.jar
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
# The image needs only the built jar; nothing else from the module enters the build context.
|
||||||
|
*
|
||||||
|
!target/*.jar
|
||||||
Reference in new issue
Block a user