Files
helm-gitea/unittests/bash/tests/init-containers/config/config_environment.bats
T
volker.raschekandCopilot a5579c167e
Bash / bash-unittest (push) Successful in 19s
Bash / shellcheck (push) Successful in 26s
changelog / changelog (push) Successful in 12s
Helm / helm-lint (push) Successful in 16s
Helm / helm-unittest (push) Successful in 16s
Markdown linter / markdown-lint (push) Successful in 15s
Markdown linter / markdown-link-checker (push) Successful in 53s
[Close #1077] fix!: protect secrets and enforce config precedence
Equivalent environment variable encodings could overwrite existing
secrets or make configuration precedence depend on environment order.

BREAKING CHANGE: Duplicate preset environment variables targeting the
same setting now cause initialization to fail. Remove duplicates.

Co-authored-by: Copilot <copilot@github.com>
2026-12-04 22:37:36 +01:00

339 lines
9.8 KiB
Bash

#!/usr/bin/env bats
function setup() {
PROJECT_ROOT="$(git rev-parse --show-toplevel)"
TEST_ROOT="$PROJECT_ROOT/unittests/bash"
load "$TEST_ROOT/test_helper/common-setup"
common_setup
export GITEA_APP_INI="$BATS_TEST_TMPDIR/app.ini"
export TMP_EXISTING_ENVS_FILE="$BATS_TEST_TMPDIR/existing-envs"
export ENV_TO_INI_MOUNT_POINT="$BATS_TEST_TMPDIR/env-to-ini-mounts"
export GITEA_EDIT_INI_EXPECTED=0
export PATH="$BATS_TEST_TMPDIR/bin:$PATH"
mkdir -p "$BATS_TEST_TMPDIR/bin"
cat >"$BATS_TEST_TMPDIR/bin/gitea" <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
case "$*" in
'generate secret INTERNAL_TOKEN')
echo 'mocked-internal-token'
;;
'generate secret SECRET_KEY')
echo 'mocked-secret-key'
;;
'generate secret JWT_SECRET')
echo 'mocked-jwt-secret'
;;
'generate secret LFS_JWT_SECRET')
echo 'mocked-lfs-jwt-secret'
;;
"config edit-ini --apply-env --config $GITEA_APP_INI --config-keep-keys "*" --out $GITEA_APP_INI")
test "$GITEA_EDIT_INI_EXPECTED" -eq 1
cat "$7"
echo 'Stubbed gitea config edit-ini rebuild was called!'
;;
"config edit-ini --apply-env --config $GITEA_APP_INI --out $GITEA_APP_INI")
if [ "$GITEA_EDIT_INI_EXPECTED" -eq 1 ]; then
echo 'Stubbed gitea config edit-ini was called!'
exit 0
fi
echo 'Unexpected gitea config edit-ini invocation' >&2
exit 127
;;
*)
echo "Unexpected gitea invocation: $*" >&2
exit 127
;;
esac
EOF
chmod +x "$BATS_TEST_TMPDIR/bin/gitea"
}
function teardown() {
:
}
function expect_gitea_config_edit_ini_call() {
export GITEA_EDIT_INI_EXPECTED=1
}
function execute_test_script() {
currentEnvsBefore=$(env | sort)
source $PROJECT_ROOT/scripts/init-containers/config/config_environment.sh
local exitCode=$?
currentEnvsAfter=$(env | sort)
# diff as unified +/- output without context before/after
diff --unified=0 <(echo "$currentEnvsBefore") <(echo "$currentEnvsAfter")
exit $exitCode
}
function write_mounted_file() {
# either "inlines" or "additionals"
scope="${1}"
file="${2}"
content="${3}"
mkdir -p "$ENV_TO_INI_MOUNT_POINT/$scope/..data/"
echo "${content}" > "$ENV_TO_INI_MOUNT_POINT/$scope/..data/$file"
ln -sf "$ENV_TO_INI_MOUNT_POINT/$scope/..data/$file" "$ENV_TO_INI_MOUNT_POINT/$scope/$file"
}
@test "works as expected when nothing is configured" {
expect_gitea_config_edit_ini_call
run $PROJECT_ROOT/scripts/init-containers/config/config_environment.sh
assert_success
assert_line '...Initial secrets generated'
assert_line 'Reloading preset envs...'
assert_line '=== All configuration sources loaded ==='
assert_line 'Stubbed gitea config edit-ini was called!'
}
@test "exports initial secrets" {
expect_gitea_config_edit_ini_call
run execute_test_script
assert_success
assert_line '+GITEA__OAUTH2__JWT_SECRET=mocked-jwt-secret'
assert_line '+GITEA__SECURITY__INTERNAL_TOKEN=mocked-internal-token'
assert_line '+GITEA__SECURITY__SECRET_KEY=mocked-secret-key'
assert_line '+GITEA__SERVER__LFS_JWT_SECRET=mocked-lfs-jwt-secret'
}
@test "does NOT export initial secrets when app.ini already exists" {
expect_gitea_config_edit_ini_call
touch $GITEA_APP_INI
run execute_test_script
assert_success
assert_line --partial 'An app.ini file already exists.'
refute_line '+GITEA__OAUTH2__JWT_SECRET=mocked-jwt-secret'
refute_line '+GITEA__SECURITY__INTERNAL_TOKEN=mocked-internal-token'
refute_line '+GITEA__SECURITY__SECRET_KEY=mocked-secret-key'
refute_line '+GITEA__SERVER__LFS_JWT_SECRET=mocked-lfs-jwt-secret'
}
@test "protects existing secrets from file and encoded environment variables" {
expect_gitea_config_edit_ini_call
touch "$GITEA_APP_INI"
export GITEA__SECURITY__SECRET_KEY__FILE="/tmp/secret"
export GITEA__SEC_0X55_RITY__INTERNAL_TOKEN="override"
export GITEA__OAUTH2__JWT_0X5F_SECRET__FILE="/tmp/jwt"
export GITEA__SERVER__LFS_JWT_SECRET="override"
export GITEA__SECURITY__OTHER__FILE="/tmp/other"
export GITEA___0x7365637572697479____0x5345435245545f4b4559_="encoded"
export GITEA__SECURITY__secret_key="other-case"
for GITEA_REBUILD_APP_INI in false true; do
export GITEA_REBUILD_APP_INI
run execute_test_script
assert_success
assert_line '-GITEA__SECURITY__SECRET_KEY__FILE=/tmp/secret'
assert_line '-GITEA__SEC_0X55_RITY__INTERNAL_TOKEN=override'
assert_line '-GITEA__OAUTH2__JWT_0X5F_SECRET__FILE=/tmp/jwt'
assert_line '-GITEA__SERVER__LFS_JWT_SECRET=override'
assert_line '-GITEA___0x7365637572697479____0x5345435245545f4b4559_=encoded'
refute_line '+GITEA___0X7365637572697479____0x5345435245545f4b4559_=encoded'
refute_line '-GITEA__SECURITY__OTHER__FILE=/tmp/other'
refute_line '-GITEA__SECURITY__secret_key=other-case'
done
}
@test "rebuilds an existing app.ini while preserving secret keys" {
expect_gitea_config_edit_ini_call
export GITEA_REBUILD_APP_INI=true
touch "$GITEA_APP_INI"
run "$PROJECT_ROOT/scripts/init-containers/config/config_environment.sh"
assert_success
assert_line '[security]'
assert_line 'SECRET_KEY='
assert_line 'INTERNAL_TOKEN='
assert_line '[oauth2]'
assert_line 'JWT_SECRET='
assert_line '[server]'
assert_line 'LFS_JWT_SECRET='
assert_line 'Stubbed gitea config edit-ini rebuild was called!'
}
@test "uses normal initialization when rebuild is enabled for a new app.ini" {
expect_gitea_config_edit_ini_call
export GITEA_REBUILD_APP_INI=true
run "$PROJECT_ROOT/scripts/init-containers/config/config_environment.sh"
assert_success
assert_line 'Stubbed gitea config edit-ini was called!'
}
@test "ensures that preset environment variables take precedence over auto-generated ones" {
expect_gitea_config_edit_ini_call
export GITEA__OAUTH2__JWT_SECRET="pre-defined-jwt-secret"
run execute_test_script
assert_success
refute_line '+GITEA__OAUTH2__JWT_SECRET=mocked-jwt-secret'
}
@test "ensures that preset secrets with lowercase sections override generated secrets" {
expect_gitea_config_edit_ini_call
export GITEA__oauth2__JWT_SECRET="pre-defined-jwt-secret"
run execute_test_script
assert_success
assert_line '+GITEA__OAUTH2__JWT_SECRET=pre-defined-jwt-secret'
refute_line '+GITEA__OAUTH2__JWT_SECRET=mocked-jwt-secret'
}
@test "preset encoded file variables override generated and mounted settings" {
expect_gitea_config_edit_ini_call
export GITEA___0x6f6175746832___JWT_0x5f_SECRET__FILE="/tmp/jwt"
write_mounted_file "inlines" "oauth2" "JWT_SECRET=inline"
write_mounted_file "additionals" "oauth2" "JWT_SECRET=additional"
run execute_test_script
assert_success
refute_line '+GITEA__OAUTH2__JWT_SECRET=mocked-jwt-secret'
refute_line '+GITEA__OAUTH2__JWT_SECRET=inline'
refute_line '+GITEA__OAUTH2__JWT_SECRET=additional'
assert_line '+GITEA___0X6F6175746832___JWT_0x5f_SECRET__FILE=/tmp/jwt'
}
@test "rejects conflicting preset variables for the same setting" {
expect_gitea_config_edit_ini_call
export GITEA__OAUTH2__JWT_SECRET="first"
export GITEA__oauth2__JWT_0X5F_SECRET__FILE="/tmp/jwt"
run "$PROJECT_ROOT/scripts/init-containers/config/config_environment.sh"
assert_failure
assert_output --partial 'conflicting preset environment variables'
}
@test "ensures that preset environment variables take precedence over mounted ones" {
expect_gitea_config_edit_ini_call
export GITEA__OAUTH2__JWT_SECRET="pre-defined-jwt-secret"
write_mounted_file "inlines" "oauth2" "$(cat << EOF
JWT_SECRET=inline-jwt-secret
EOF
)"
run execute_test_script
assert_success
refute_line '+GITEA__OAUTH2__JWT_SECRET=mocked-jwt-secret'
refute_line '+GITEA__OAUTH2__JWT_SECRET=inline-jwt-secret'
}
@test "ensures that additionals take precedence over inlines" {
expect_gitea_config_edit_ini_call
write_mounted_file "inlines" "oauth2" "$(cat << EOF
JWT_SECRET=inline-jwt-secret
EOF
)"
write_mounted_file "additionals" "oauth2" "$(cat << EOF
JWT_SECRET=additional-jwt-secret
EOF
)"
run execute_test_script
assert_success
refute_line '+GITEA__OAUTH2__JWT_SECRET=mocked-jwt-secret'
refute_line '+GITEA__OAUTH2__JWT_SECRET=inline-jwt-secret'
assert_line '+GITEA__OAUTH2__JWT_SECRET=additional-jwt-secret'
}
@test "ensures that dotted/dashed sections are properly masked" {
expect_gitea_config_edit_ini_call
write_mounted_file "inlines" "repository.pull-request" "$(cat << EOF
WORK_IN_PROGRESS_PREFIXES=WIP:,[WIP]
EOF
)"
run execute_test_script
assert_success
assert_line '+GITEA__REPOSITORY_0X2E_PULL_0X2D_REQUEST__WORK_IN_PROGRESS_PREFIXES=WIP:,[WIP]'
}
@test "preserves the case of setting names and masks dots and dashes" {
expect_gitea_config_edit_ini_call
write_mounted_file "inlines" "log" "$(cat << EOF
logger.router.MODE=console-warn
Mixed-Case=value
EOF
)"
run execute_test_script
assert_success
assert_line '+GITEA__LOG__logger_0X2E_router_0X2E_MODE=console-warn'
assert_line '+GITEA__LOG__Mixed_0X2D_Case=value'
}
@test "preserves preset setting case and its precedence over mounted values" {
expect_gitea_config_edit_ini_call
export GITEA__log__MixedKey="preset"
write_mounted_file "inlines" "log" "$(cat << EOF
MixedKey=inline
EOF
)"
run execute_test_script
assert_success
assert_line '-GITEA__log__MixedKey=preset'
assert_line '+GITEA__LOG__MixedKey=preset'
refute_line '+GITEA__LOG__MixedKey=inline'
}
@test "treats top-level configuration as section-less" {
expect_gitea_config_edit_ini_call
write_mounted_file "inlines" "_generals_" "$(cat << EOF
APP_NAME=Hello top-level configuration
RUN_MODE=dev
EOF
)"
run execute_test_script
assert_success
assert_line '+GITEA____APP_NAME=Hello top-level configuration'
assert_line '+GITEA____RUN_MODE=dev'
}
@test "fails on invalid setting" {
write_mounted_file "inlines" "_generals_" "$(cat << EOF
some random invalid string
EOF
)"
run execute_test_script
assert_failure
}
@test "treats empty setting name as invalid setting" {
write_mounted_file "inlines" "_generals_" "$(cat << EOF
=value
EOF
)"
run execute_test_script
assert_failure
}