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 1 commit
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 equivalent docker secret, exit program if not found
DennisGaida marked this conversation as resolved.
Show resolved Hide resolved
*/
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. Exiting", envVarFileName)
Copy link
Owner

Choose a reason for hiding this comment

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

First, nice error message ! That would help the user.
Second, to go further, also send the "root" error back to the user. That would make debugging easier : was it a miss-configuration and file is not present? Or app can't read the file because of permission ? etc etc

Copy link
Author

Choose a reason for hiding this comment

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

Also outputting the original error in DennisGaida@fba0274

Please note that ReadFile doesn't guarantee any specific behavior besides no-error == nil. I hope that using %v is the right thing to do, but seeing other error checking code, usually specific errors are caught, like "file not found" or "path invalid".

}
return string(envVarFromFile)
}
return envVar
}
Expand Down