From 61d42e7621dc29f878d67ebf4139c3f1b86c9790 Mon Sep 17 00:00:00 2001 From: james h Date: Mon, 30 Jul 2018 20:13:06 +0200 Subject: [PATCH] Support _FILE postfixes for environment variables (#1142) * Support _FILE postfixes for environment variables to be loaded from files * fix gofmt error --- api/server/init.go | 17 +++++++++++++++++ docs/operating/options.md | 8 ++++++++ 2 files changed, 25 insertions(+) diff --git a/api/server/init.go b/api/server/init.go index d09104110..77b8597c3 100644 --- a/api/server/init.go +++ b/api/server/init.go @@ -2,10 +2,12 @@ package server import ( "context" + "io/ioutil" "os" "os/signal" "runtime" "strconv" + "strings" "syscall" "github.com/fnproject/fn/api/common" @@ -21,6 +23,11 @@ func init() { func getEnv(key, fallback string) string { if value, ok := os.LookupEnv(key); ok { return value + } else if value, ok := os.LookupEnv(key + "_FILE"); ok { + dat, err := ioutil.ReadFile(value) + if err == nil { + return string(dat) + } } return fallback } @@ -34,6 +41,16 @@ func getEnvInt(key string, fallback int) int { panic(err) // not sure how to handle this } return i + } else if value, ok := os.LookupEnv(key + "_FILE"); ok { + dat, err := ioutil.ReadFile(value) + if err == nil { + var err error + var i int + if i, err = strconv.Atoi(strings.TrimSpace(string(dat))); err != nil { + panic(err) // not sure how to handle this + } + return i + } } return fallback } diff --git a/docs/operating/options.md b/docs/operating/options.md index a66d6cb6d..4ef126c9b 100644 --- a/docs/operating/options.md +++ b/docs/operating/options.md @@ -19,6 +19,14 @@ docker run. For example: docker run -e VAR_NAME=VALUE ... ``` +Additionally you may desire to load the environment variables from files, for example if you are using Docker Secrets, in that case you may append +`_FILE` to the end of any variable name and specify the file location. The variable will be filled with the file's contents. For example: + +```sh +docker run -e VAR_NAME_FILE=/path/to/secret ... +``` + + | Env Variables | Description | Default values | | --------------|-------------|----------------| | `FN_DB_URL` | The database URL to use in URL format. See [Databases](databases/README.md) for more information. | sqlite3:///app/data/fn.db |