Add support for file-based environment variables in environment-to-ini#19857
Add support for file-based environment variables in environment-to-ini#19857aminosbh wants to merge 2 commits intogo-gitea:mainfrom
Conversation
|
Fixes #10311 |
|
Any reason why this PR was discontinued? This change would be extremely useful imho. |
|
I agree, this is a key functionality for a lot of production environments. Having it embedded in the code is far better than implementing potentially unsafe workarounds. |
|
Maybe another prefix like |
I found suffixed version to be quite standard with some other Docker containers, but I don't have a strong opinion. Could you land this change for us, @lunny ? It looks like you are a member. Then, I was writing my own wrapper now, based a previous issue that was discussed here, which works as a workaround for me for now. Here's the code for completion (I modified the original prosposal to be compliant with # This file has been copied from the original Docker container, as follows:
# docker run gitea/gitea:1.18.0
# docker exec <container_name>> cat /usr/bin/entrypoint > gitea.entrypoint.sh
# The patch starts here. It's a rewritten form of what was posted here
# https://github.com/go-gitea/gitea/issues/10311.
# >>> SNIP
export_secret_as_env_var()
{
secret=$1
envFile="${secret}_FILE"
envFileName="$(printenv "${envFile}")"
if [ -n "${envFileName}" ]; then
if [ -f "${envFileName}" ]; then
val=$(cat "${envFileName}")
export "${secret}"="$val"
echo "${secret} environment variable was set via secret ${envFileName}"
else
>&2 echo "Error: Secret ${secret} cannot be set via secret ${envFileName}. Not a file"
fi
else
echo "Warn: ${secret} environment variable ist not defined in secret"
fi
}
# Set environment variables by their respective secrets
export_secret_as_env_var "GITEA__database__PASSWD"
export_secret_as_env_var "GITEA__database__USER"
export_secret_as_env_var "GITEA__mailer__USER"
export_secret_as_env_var "GITEA__mailer__PASSWD"
# <<< SNAPIt seemed though, that the Cherrs and thanks for reading this far :) |
dynamicat
left a comment
There was a problem hiding this comment.
Can someone review approve and merge this?
"GITEA__FILE__foo__..." conflicts with "[FILE].foo", the "FILE" part might be parsed as the "section_name", according to existing rule "GITEA__section_name__KEY_NAME". |
| if isFileBased { | ||
| isFile, err := util.IsFile(value) | ||
| if err != nil { | ||
| log.Fatal("Unable to check if %s is a file. Error: %v", value, err) | ||
| } | ||
| if isFile { | ||
| if content, err := os.ReadFile(value); err == nil { | ||
| value = string(content) | ||
| } else { | ||
| log.Fatal("Failed to load value from file '%s': %v", value, err) | ||
| } | ||
| } else { | ||
| log.Fatal("File '%s' not found", value) | ||
| } | ||
| } |
There was a problem hiding this comment.
| if isFileBased { | |
| isFile, err := util.IsFile(value) | |
| if err != nil { | |
| log.Fatal("Unable to check if %s is a file. Error: %v", value, err) | |
| } | |
| if isFile { | |
| if content, err := os.ReadFile(value); err == nil { | |
| value = string(content) | |
| } else { | |
| log.Fatal("Failed to load value from file '%s': %v", value, err) | |
| } | |
| } else { | |
| log.Fatal("File '%s' not found", value) | |
| } | |
| } | |
| if isFileBased { | |
| if content, err := os.ReadFile(value); err == nil { | |
| value = string(content) | |
| } else { | |
| log.Fatal("Failed to load value from file %q: %v", value, err) | |
| } | |
| } |
I guess this is enough?
|
Although there are some conflicts, I think this PR could be fine-tuned and merged. Could maintainers with writer permission do some helps? |
|
Generally I think that what If someone wants to use env vars, they should be able to. This goes for systemd deployments, gitpod and more. |
|
Replaced by Make environment-to-ini support loading key value from file #24832 |
Improve
environment-to-inito allow for file content to be set as the value of an environment variable.Useful when using
docker secretand were the secret is mounted as a file in/run/secrets/<SECRET_NAME>.Any settings in
app.inican be set or overridden with the content of a file by defining an environment variable of the form:GITEA__section_name__KEY_NAME__FILEthat points to a file.Fixes #19856