[Close #1077] fix!: protect secrets and enforce config precedence
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
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
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>
This commit is contained in:
3 files changed
+163
-11
No files matched your search
@@ -10,6 +10,7 @@
|
||||
- [Limit resources](#limit-resources)
|
||||
- [Configuration](#configuration)
|
||||
- [Default Configuration](#default-configuration)
|
||||
- [Rebuilding app.ini](#rebuilding-appini)
|
||||
- [Database defaults](#database-defaults)
|
||||
- [Server defaults](#server-defaults)
|
||||
- [Metrics defaults](#metrics-defaults)
|
||||
@@ -56,8 +57,8 @@
|
||||
- [LivenessProbe](#livenessprobe)
|
||||
- [ReadinessProbe](#readinessprobe)
|
||||
- [StartupProbe](#startupprobe)
|
||||
- [valkey](#valkey)
|
||||
- [CloudNativePG](#cloudnativepg)
|
||||
- [valkey](#valkey)
|
||||
- [Advanced](#advanced)
|
||||
- [Contributing](#contributing)
|
||||
- [Upgrading](#upgrading)
|
||||
@@ -481,9 +482,20 @@ gitea:
|
||||
|
||||
Priority (highest to lowest) for defining app.ini variables:
|
||||
|
||||
1. Environment variables prefixed with `GITEA`
|
||||
1. Environment variables prefixed with `GITEA__`
|
||||
1. Additional config sources
|
||||
1. Values defined in `gitea.config`
|
||||
1. Automatically generated initial secrets
|
||||
|
||||
Setting names are case-sensitive. Encode `.` as `_0X2E_` and `-` as `_0X2D_` in environment variable names.
|
||||
Append `__FILE` to read a setting value from a file, as supported by Gitea.
|
||||
Section names are case-insensitive, and differently encoded names can refer to the same setting.
|
||||
If multiple predefined environment variables refer to the same setting, the init container fails with a conflict error;
|
||||
remove the duplicate variables before restarting it.
|
||||
|
||||
When `app.ini` already exists, the chart preserves `security.SECRET_KEY`, `security.INTERNAL_TOKEN`,
|
||||
`oauth2.JWT_SECRET`, and `server.LFS_JWT_SECRET` instead of overwriting them from any configuration source.
|
||||
This also applies to environment variables using `__FILE` or encoded names.
|
||||
|
||||
### External Database
|
||||
|
||||
|
||||
@@ -39,18 +39,19 @@ function env2ini::read_config_to_env() {
|
||||
masked_setting="${masked_setting//-/_0X2D_}"
|
||||
|
||||
if [[ -z "${section}" ]]; then
|
||||
export "GITEA____${masked_setting}=${value}"
|
||||
env2ini::set_config_env "GITEA____${masked_setting}" "${value}"
|
||||
return
|
||||
fi
|
||||
|
||||
local masked_section="${section//./_0X2E_}" # '//' instructs to replace all matches
|
||||
masked_section="${masked_section//-/_0X2D_}"
|
||||
|
||||
export "GITEA__${masked_section^^}__${masked_setting}=${value}"
|
||||
env2ini::set_config_env "GITEA__${masked_section^^}__${masked_setting}" "${value}"
|
||||
}
|
||||
|
||||
function env2ini::reload_preset_envs() {
|
||||
env2ini::log "Reloading preset envs..."
|
||||
local -A preset_names=()
|
||||
|
||||
while read -r line; do
|
||||
if [[ -z "${line}" ]]; then
|
||||
@@ -78,14 +79,22 @@ function env2ini::reload_preset_envs() {
|
||||
|
||||
env2ini::log " + '${setting}'"
|
||||
|
||||
if env2ini::decode_config_env "${setting}"; then
|
||||
local identity="${#gitea_decoded_section}:${gitea_decoded_section}${gitea_decoded_key}"
|
||||
if [[ -v 'preset_names["${identity}"]' ]]; then
|
||||
env2ini::log " ! conflicting preset environment variables: ${preset_names["${identity}"]} and ${setting}"
|
||||
return 1
|
||||
fi
|
||||
preset_names["${identity}"]="${setting}"
|
||||
fi
|
||||
|
||||
local section_and_key="${setting#GITEA__}"
|
||||
if [[ "${section_and_key}" == *'__'* ]]; then
|
||||
local section="${section_and_key%%__*}"
|
||||
local key="${section_and_key#*__}"
|
||||
unset "${setting}"
|
||||
export "GITEA__${section^^}__${key}=${value}"
|
||||
env2ini::set_config_env "GITEA__${section^^}__${key}" "${value}"
|
||||
else
|
||||
export "${setting}=${value}"
|
||||
env2ini::set_config_env "${setting}" "${value}"
|
||||
fi
|
||||
done < "$TMP_EXISTING_ENVS_FILE"
|
||||
|
||||
@@ -141,8 +150,85 @@ function env2ini::generate_initial_secrets() {
|
||||
env2ini::log "...Initial secrets generated\n"
|
||||
}
|
||||
|
||||
function env2ini::decode_config_env() {
|
||||
local encoded="${1#GITEA__}"
|
||||
encoded="${encoded%__FILE}"
|
||||
local escape_regex='_0[xX]([0-9a-fA-F][0-9a-fA-F])+_'
|
||||
local section='' key='' before escape hex byte
|
||||
local in_key=false
|
||||
|
||||
while [[ "${encoded}" =~ ${escape_regex} ]]; do
|
||||
escape="${BASH_REMATCH[0]}"
|
||||
before="${encoded%%"${escape}"*}"
|
||||
if [[ "${in_key}" == false && "${before}" == *'__'* ]]; then
|
||||
section+="${before%%__*}"
|
||||
key+="${before#*__}"
|
||||
in_key=true
|
||||
elif [[ "${in_key}" == false ]]; then
|
||||
section+="${before}"
|
||||
else
|
||||
key+="${before}"
|
||||
fi
|
||||
|
||||
hex="${escape:3:${#escape}-4}"
|
||||
while [[ -n "${hex}" ]]; do
|
||||
if [[ "${hex:0:2}" == 00 ]]; then
|
||||
return 1
|
||||
fi
|
||||
printf -v byte '%b' "\\x${hex:0:2}"
|
||||
if [[ "${in_key}" == false ]]; then
|
||||
section+="${byte}"
|
||||
else
|
||||
key+="${byte}"
|
||||
fi
|
||||
hex="${hex:2}"
|
||||
done
|
||||
encoded="${encoded#*"${escape}"}"
|
||||
done
|
||||
|
||||
if [[ "${in_key}" == false && "${encoded}" == *'__'* ]]; then
|
||||
section+="${encoded%%__*}"
|
||||
key+="${encoded#*__}"
|
||||
elif [[ "${in_key}" == true ]]; then
|
||||
key+="${encoded}"
|
||||
fi
|
||||
|
||||
[[ -n "${key}" ]] || return 1
|
||||
gitea_decoded_section="${section,,}"
|
||||
gitea_decoded_key="${key}"
|
||||
}
|
||||
|
||||
function env2ini::set_config_env() {
|
||||
local name="${1}" value="${2}" existing
|
||||
if env2ini::decode_config_env "${name}"; then
|
||||
local target_section="${gitea_decoded_section}" target_key="${gitea_decoded_key}"
|
||||
while IFS= read -r existing; do
|
||||
if env2ini::decode_config_env "${existing}" &&
|
||||
[[ "${gitea_decoded_section}" == "${target_section}" && "${gitea_decoded_key}" == "${target_key}" ]]; then
|
||||
unset "${existing}"
|
||||
fi
|
||||
done < <(compgen -e | grep '^GITEA__' || true)
|
||||
fi
|
||||
export "${name}=${value}"
|
||||
}
|
||||
|
||||
function env2ini::is_protected_secret() {
|
||||
env2ini::decode_config_env "${1}" || return 1
|
||||
case "${gitea_decoded_section}__${gitea_decoded_key}" in
|
||||
security__INTERNAL_TOKEN|security__SECRET_KEY|oauth2__JWT_SECRET|server__LFS_JWT_SECRET)
|
||||
return 0
|
||||
;;
|
||||
*)
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# save existing envs prior to script execution. Necessary to keep order of preexisting and custom envs
|
||||
env | (grep -e '^GITEA__' || [[ $? == 1 ]]) > "$TMP_EXISTING_ENVS_FILE"
|
||||
while IFS= read -r setting; do
|
||||
unset "${setting}"
|
||||
done < <(compgen -e | grep '^GITEA__' || true)
|
||||
|
||||
# MUST BE CALLED BEFORE OTHER CONFIGURATION
|
||||
env2ini::generate_initial_secrets
|
||||
@@ -163,10 +249,11 @@ if [ -f "${GITEA_APP_INI}" ]; then
|
||||
env2ini::log ' - oauth2.JWT_SECRET'
|
||||
env2ini::log ' - server.LFS_JWT_SECRET'
|
||||
|
||||
unset GITEA__SECURITY__INTERNAL_TOKEN
|
||||
unset GITEA__SECURITY__SECRET_KEY
|
||||
unset GITEA__OAUTH2__JWT_SECRET
|
||||
unset GITEA__SERVER__LFS_JWT_SECRET
|
||||
while IFS= read -r setting; do
|
||||
if env2ini::is_protected_secret "${setting}"; then
|
||||
unset "${setting}"
|
||||
fi
|
||||
done < <(compgen -e | grep '^GITEA__' || true)
|
||||
fi
|
||||
|
||||
if [[ "${GITEA_REBUILD_APP_INI:-false}" == "true" && -f "${GITEA_APP_INI}" ]]; then
|
||||
|
||||
@@ -120,6 +120,33 @@ function write_mounted_file() {
|
||||
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
|
||||
@@ -169,6 +196,32 @@ function write_mounted_file() {
|
||||
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"
|
||||
|
||||
Reference in new issue
Block a user