Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for docker secrets #29

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@ func OptionalEnv(varName string, optional string) string {
}

/*
Check for an environment variable value, exit program if not found
Check for an environment variable value or the value of a file, exit program if not found
*/
func RequiredEnv(varName string) string {
envVar := os.Getenv(varName)
if envVar == "" {
envVarFileName := os.Getenv(varName + "_FILE")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding a "_FILE" suffix, then you have a value, could you detect if the environment value is a valid file path ? For example with the function os.Stat(path).
Like that, you have one less nest in the if statement, so it's more streamlined while still being able to get different logs for direct value vs file value.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could do that and you are the maintainer.

I use a lot of containers and the de facto standard is to use different named environment variables for your secrets. Linuxserver.io (which provide a loooot of container) use the FILE__<env-var> nomenclature (see e.g. https://hub.docker.com/r/linuxserver/mariadb) for their s6-overlay. Many other containers I know use the <env-var>_FILE nomenclature.

I'm not very opinionated and can also go with the os.Stat(path) if you tell me to do so.

if envVar == "" && envVarFileName == "" {
log.Fatalf("The required env var %s is not provided. Exiting", varName)
} else if envVar == "" && envVarFileName != "" {
envVarFromFile, err := os.ReadFile(envVarFileName)
if err != nil {
log.Fatalf("Could not read env var from file %s (Error: %v). Exiting", envVarFileName, err)
}
return string(envVarFromFile)
}
return envVar
}
Expand Down