#!/usr/bin/env bash set -euo pipefail function env2ini::log() { # '%b' expands the \n escapes of the callers without treating the message itself as a format string printf '%b\n' "${1}" } function env2ini::read_config_to_env() { local section="${1}" local line="${2}" if [[ -z "${line}" ]]; then # skip empty line return fi # 'xargs echo -n' trims all leading/trailing whitespaces and a trailing new line local setting setting="$(awk -F '=' '{print $1}' <<< "${line}" | xargs echo -n)" if [[ -z "${setting}" ]]; then env2ini::log ' ! invalid setting' exit 1 fi local value='' local regex="^${setting}(\s*)=(\s*)(.*)" if [[ $line =~ $regex ]]; then value="${BASH_REMATCH[3]}" else env2ini::log ' ! invalid setting' exit 1 fi env2ini::log " + '${setting}'" local masked_setting="${setting//./_0X2E_}" masked_setting="${masked_setting//-/_0X2D_}" if [[ -z "${section}" ]]; then 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_}" 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 # skip empty line return fi # 'xargs echo -n' trims all leading/trailing whitespaces and a trailing new line local setting setting="$(awk -F '=' '{print $1}' <<< "${line}" | xargs echo -n)" if [[ -z "${setting}" ]]; then env2ini::log ' ! invalid setting' exit 1 fi local value='' local regex="^${setting}(\s*)=(\s*)(.*)" if [[ $line =~ $regex ]]; then value="${BASH_REMATCH[3]}" else env2ini::log ' ! invalid setting' exit 1 fi 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#*__}" env2ini::set_config_env "GITEA__${section^^}__${key}" "${value}" else env2ini::set_config_env "${setting}" "${value}" fi done < "$TMP_EXISTING_ENVS_FILE" rm "$TMP_EXISTING_ENVS_FILE" } function env2ini::process_config_file() { local config_file="${1}" local section section="$(basename "${config_file}")" if [[ $section == '_generals_' ]]; then env2ini::log " [ini root]" section='' else env2ini::log " ${section}" fi while read -r line; do env2ini::read_config_to_env "${section}" "${line}" done < <(awk 1 "${config_file}") # Helm .toYaml trims the trailing new line which breaks line processing; awk 1 ... adds it back while reading } function env2ini::load_config_sources() { local path="${1}" if [[ -d "${path}" ]]; then env2ini::log "Processing $(basename "${path}")..." while read -r -d '' configFile; do env2ini::process_config_file "${configFile}" done < <(find "${path}" -type l -not -name '..data' -print0) env2ini::log "\n" fi } function env2ini::generate_initial_secrets() { # These environment variables will either be # - overwritten with user defined values, # - initially used to set up Gitea # Anyway, they won't harm existing app.ini files GITEA__SECURITY__INTERNAL_TOKEN="$(gitea generate secret INTERNAL_TOKEN)" GITEA__SECURITY__SECRET_KEY="$(gitea generate secret SECRET_KEY)" GITEA__OAUTH2__JWT_SECRET="$(gitea generate secret JWT_SECRET)" GITEA__SERVER__LFS_JWT_SECRET="$(gitea generate secret LFS_JWT_SECRET)" export GITEA__SECURITY__INTERNAL_TOKEN \ GITEA__SECURITY__SECRET_KEY \ GITEA__OAUTH2__JWT_SECRET \ GITEA__SERVER__LFS_JWT_SECRET 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 env2ini::load_config_sources "$ENV_TO_INI_MOUNT_POINT/inlines/" env2ini::load_config_sources "$ENV_TO_INI_MOUNT_POINT/additionals/" # load existing envs to override auto generated envs env2ini::reload_preset_envs env2ini::log "=== All configuration sources loaded ===\n" # safety to prevent rewrite of secret keys if an app.ini already exists if [ -f "${GITEA_APP_INI}" ]; then env2ini::log 'An app.ini file already exists. To prevent overwriting secret keys, these settings are dropped and remain unchanged:' env2ini::log ' - security.INTERNAL_TOKEN' env2ini::log ' - security.SECRET_KEY' env2ini::log ' - oauth2.JWT_SECRET' env2ini::log ' - 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 keep_keys_file="$(mktemp)" trap 'rm -f "${keep_keys_file}"' EXIT printf '%s\n' '[security]' 'SECRET_KEY=' 'INTERNAL_TOKEN=' '[oauth2]' 'JWT_SECRET=' '[server]' 'LFS_JWT_SECRET=' > "${keep_keys_file}" gitea config edit-ini --apply-env --config "$GITEA_APP_INI" --config-keep-keys "${keep_keys_file}" --out "$GITEA_APP_INI" else gitea config edit-ini --apply-env --config "$GITEA_APP_INI" --out "$GITEA_APP_INI" fi