Skip to content

Commit c8744d5

Browse files
committed
fix: allow read-only Django secret files
Kubernetes Secret mounts are read-only, so enforcing mode 600 can prevent startup. Preserve readable read-only secrets and accept them in split services while retaining permission repair for writable files. Fixes #4877
1 parent d058556 commit c8744d5

4 files changed

Lines changed: 84 additions & 2 deletions

File tree

‎start‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,19 @@ ensure_django_secret() {
233233
fi
234234

235235
# Repair secrets created by older container versions or copied into the volume.
236-
chmod 600 "$secret_file"
236+
# Mounted secrets can be read-only, with permissions managed externally.
237+
if ! chmod 600 "$secret_file" 2> /dev/null; then
238+
if [[ ! -s $secret_file || ! -r $secret_file || -w $secret_file ]]; then
239+
echo "Failed to secure Django secret permissions: $secret_file" >&2
240+
exit 1
241+
fi
242+
fi
237243
)
238244
}
239245

240246
django_secret_ready() {
241247
[[ -s /app/data/secret && -r /app/data/secret ]] || return 1
242-
[[ $(stat -c %a /app/data/secret 2> /dev/null) == "600" ]]
248+
[[ ! -w /app/data/secret || $(stat -c %a /app/data/secret 2> /dev/null) == "600" ]]
243249
}
244250

245251
wait_for_django_secret() {

‎tests/integration/test-runtime-secrets‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ MODE=$(secret_mode)
3333
[ "$MODE" = "600" ] || fail "Django secret mode was not repaired: $MODE"
3434
[ "$(secret_checksum)" = "$CHECKSUM" ] || fail "Django secret changed while repairing permissions"
3535

36+
# Kubernetes secrets are mounted read-only with permissions managed externally.
37+
SECRET_FILE=$(mktemp)
38+
trap 'rm -f "$SECRET_FILE"' EXIT HUP INT TERM
39+
printf 'read-only-mounted-test-secret\n' > "$SECRET_FILE"
40+
chmod 444 "$SECRET_FILE"
41+
docker compose run --rm --no-deps \
42+
--volume "$SECRET_FILE:/app/data/secret:ro" \
43+
"$MIGRATION_SERVICE" list_versions > /dev/null
44+
# Also exercise the readiness check used by services that do not run migrations.
45+
docker compose run --rm --no-deps \
46+
--env WEBLATE_SERVICE=web \
47+
--volume "$SECRET_FILE:/app/data/secret:ro" \
48+
weblate list_versions > /dev/null
49+
[ "$(cat "$SECRET_FILE")" = read-only-mounted-test-secret ] || fail "Mounted Django secret changed"
50+
3651
if [ "$1" = "split" ]; then
3752
GENERATIONS=$(docker compose logs --no-color 2>&1 | grep -c "Generating Django secret..." || true)
3853
[ "$GENERATIONS" -eq 1 ] || fail "Expected one Django secret generation, found $GENERATIONS"

‎tests/run‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ echo 'Running image signing tests'
1717

1818
echo 'Running custom CA certificate tests'
1919
./tests/test-custom-ca-certificates
20+
21+
echo 'Running Django secret tests'
22+
bash tests/test-django-secret

‎tests/test-django-secret‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/bin/bash
2+
# Exercise the secret startup functions against a temporary data directory.
3+
set -eu
4+
5+
# Root bypasses permission bits; run these cases as an unprivileged user.
6+
if ((EUID == 0)); then
7+
test_root=$(mktemp -d)
8+
trap 'rm -rf "$test_root"' EXIT HUP INT TERM
9+
mkdir "$test_root/tests"
10+
cp start "$test_root/start"
11+
cp "$0" "$test_root/tests/test-django-secret"
12+
chmod -R a+rX "$test_root"
13+
setpriv --reuid=65534 --regid=65534 --clear-groups \
14+
bash -c 'cd "$1"; bash tests/test-django-secret' bash "$test_root"
15+
exit 0
16+
fi
17+
18+
test_dir=$(mktemp -d)
19+
trap 'rm -rf "$test_dir"' EXIT HUP INT TERM
20+
21+
awk '/^ensure_django_secret\(\) \{/,/^\}/; /^django_secret_ready\(\) \{/,/^\}/; /^wait_for_django_secret\(\) \{/,/^\}/' start \
22+
| sed "s|/app/data|$test_dir|g" > "$test_dir/functions.sh"
23+
# shellcheck source=/dev/null
24+
. "$test_dir/functions.sh"
25+
26+
if django_secret_ready; then
27+
echo 'Missing secret was considered ready' >&2
28+
exit 1
29+
fi
30+
31+
touch "$test_dir/secret"
32+
if django_secret_ready; then
33+
echo 'Empty secret was considered ready' >&2
34+
exit 1
35+
fi
36+
37+
printf 'existing-secret\n' > "$test_dir/secret"
38+
chmod 644 "$test_dir/secret"
39+
if django_secret_ready; then
40+
echo 'Writable secret needed permission repair but was considered ready' >&2
41+
exit 1
42+
fi
43+
ensure_django_secret
44+
[[ $(stat -c %a "$test_dir/secret") == 600 ]]
45+
django_secret_ready
46+
47+
# Owners can repair permissions even when the file is not writable.
48+
for mode in 444 000 040; do
49+
chmod "$mode" "$test_dir/secret"
50+
if [[ $mode != 444 ]] && django_secret_ready; then
51+
echo 'Unreadable secret was considered ready' >&2
52+
exit 1
53+
fi
54+
ensure_django_secret
55+
[[ $(stat -c %a "$test_dir/secret") == 600 ]]
56+
[[ $(cat "$test_dir/secret") == existing-secret ]]
57+
wait_for_django_secret
58+
done

0 commit comments

Comments
 (0)