Skip to content

Modern Docker Management, Designed for Everyone

fix(settings): honour *_FILE on the environment-managed settings path (#4203)

Kenneth Trelborg Vestergaard013611964071
diff --git a/backend/internal/config/config.go b/backend/internal/config/config.go
index 57c03385c..211e3af68 100644
--- a/backend/internal/config/config.go
+++ b/backend/internal/config/config.go
@@ -218,25 +218,34 @@ func loadFromEnv(cfg *Config) {
return
}
- defaultValue := fieldType.Tag.Get("default")
-
- // Get the environment value directly first
- envValue := utils.TrimQuotes(os.Getenv(envTag))
- if envValue == "" {
- envValue = defaultValue
+ envValue := os.Getenv(envTag)
+ var fromFile bool
+ if hasOptionInternal(fieldType, "file") {
+ envValue, _, fromFile = utils.LookupEnvOrFile(envTag)
+ }
+ if !fromFile {
+ envValue = utils.TrimQuotes(envValue)
+ if envValue == "" {
+ envValue = fieldType.Tag.Get("default")
+ }
}
setFieldValueInternal(field, fieldType, envValue)
})
}
+// hasOptionInternal reports whether the field's `options` tag lists option.
+func hasOptionInternal(fieldType reflect.StructField, option string) bool {
+ return slices.Contains(strings.Split(fieldType.Tag.Get("options"), ","), option)
+}
+
// DeprecatedEnvVarsSet returns the env names of fields tagged deprecated that
// still carry a value, so bootstrap can warn once logging is configured.
func (c *Config) DeprecatedEnvVarsSet() []string {
var envNames []string
v := reflect.ValueOf(c).Elem()
visitConfigFields(v, func(field reflect.Value, fieldType reflect.StructField) {
- if !slices.Contains(strings.Split(fieldType.Tag.Get("options"), ","), "deprecated") {
+ if !hasOptionInternal(fieldType, "deprecated") {
return
}
if field.Kind() == reflect.String && field.String() == "" {
@@ -259,8 +268,6 @@ func applyOptions(cfg *Config) {
options := strings.SplitSeq(optionsTag, ",")
for option := range options {
switch strings.TrimSpace(option) {
- case "file":
- resolveFileBasedEnvVariable(field, fieldType)
case "toLower":
if field.Kind() == reflect.String {
field.SetString(strings.ToLower(field.String()))
@@ -312,57 +319,6 @@ func visitConfigFields(v reflect.Value, fn func(reflect.Value, reflect.StructFie
}
}
-// resolveFileBasedEnvVariable checks if an environment variable with the suffix "_FILE" is set,
-// reads the content of the file specified by that variable, and sets the corresponding field's value.
-func resolveFileBasedEnvVariable(field reflect.Value, fieldType reflect.StructField) {
- // Only process string and []byte fields
- isString := field.Kind() == reflect.String
- isByteSlice := field.Kind() == reflect.Slice && field.Type().Elem().Kind() == reflect.Uint8
- if !isString && !isByteSlice {
- return
- }
-
- // Only process fields with the "env" tag
- envTag := fieldType.Tag.Get("env")
- if envTag == "" {
- return
- }
-
- // Check both double underscore (__FILE) and single underscore (_FILE) variants
- // Double underscore takes precedence
- var filePath string
- for _, suffix := range []string{"__FILE", "_FILE"} {
- if fp := os.Getenv(envTag + suffix); fp != "" {
- filePath = fp
- break
- }
- }
-
- if filePath == "" {
- return
- }
-
- // os.* rather than acfs: *_FILE Docker secret paths are arbitrary absolute
- // paths on the host, so no confinement root exists for them.
- fileContent, err := os.ReadFile(filePath) //nolint:gosec // file path intentionally comes from *_FILE env vars for Docker secrets
- if err != nil {
- slog.Warn("Failed to read secret from file, falling back to direct env var",
- "error", err)
- return
- }
-
- // Log when file value overrides a direct env var
- if os.Getenv(envTag) != "" {
- slog.Debug("Using secret from file, overriding direct env var")
- }
-
- if isString {
- field.SetString(strings.TrimSpace(string(fileContent)))
- } else {
- field.SetBytes(fileContent)
- }
-}
-
// setFieldValueInternal sets a reflect.Value from a string based on the field's type.
func setFieldValueInternal(field reflect.Value, fieldType reflect.StructField, value string) {
if !field.CanSet() {
diff --git a/backend/internal/settings/service.go b/backend/internal/settings/service.go
index a3c76e8b6..0cd93393a 100644
--- a/backend/internal/settings/service.go
+++ b/backend/internal/settings/service.go
@@ -379,7 +379,7 @@ func (s *SettingsService) loadDatabaseConfigFromEnv(ctx context.Context, db *dat
envVarName := strings.ToUpper(utils.CamelCaseToSnakeCase(key))
// debug: log each env name checked and whether a value exists
- if val, ok := os.LookupEnv(envVarName); ok {
+ if val, ok, _ := utils.LookupEnvOrFile(envVarName); ok {
mask := "<empty>"
if len(val) > 0 {
mask = fmt.Sprintf("%d chars", len(val))
@@ -440,7 +440,7 @@ func resolveSettingsEnvOverridesInternal() []settingsEnvOverride {
}
envVarName := strings.ToUpper(utils.CamelCaseToSnakeCase(key))
- if val, ok := os.LookupEnv(envVarName); ok && val != "" {
+ if val, ok, _ := utils.LookupEnvOrFile(envVarName); ok && val != "" {
overrides = append(overrides, settingsEnvOverride{
fieldIndex: i,
key: key,
@@ -874,7 +874,7 @@ func (s *SettingsService) processEnvField(ctx context.Context, tx *gorm.DB, fiel
}
envVarName := strings.ToUpper(utils.CamelCaseToSnakeCase(key))
- envVal, ok := os.LookupEnv(envVarName)
+ envVal, ok, _ := utils.LookupEnvOrFile(envVarName)
if !ok {
return nil
}
@@ -1183,7 +1183,7 @@ func (s *SettingsService) NormalizeProjectsDirectory(ctx context.Context, projec
func (s *SettingsService) NormalizeBuildsDirectory(ctx context.Context) error {
const buildsKey = "buildsDirectory"
envVarName := strings.ToUpper(utils.CamelCaseToSnakeCase(buildsKey))
- if envVal, ok := os.LookupEnv(envVarName); ok && strings.TrimSpace(envVal) != "" {
+ if envVal, ok, _ := utils.LookupEnvOrFile(envVarName); ok && strings.TrimSpace(envVal) != "" {
slog.DebugContext(ctx, "BUILDS_DIRECTORY environment variable is set, skipping normalization", "value", envVal)
return nil
}
diff --git a/backend/internal/settings/service_test.go b/backend/internal/settings/service_test.go
index a9a1b99e8..7e32d8f81 100644
--- a/backend/internal/settings/service_test.go
+++ b/backend/internal/settings/service_test.go
@@ -3,6 +3,7 @@ package settings
import (
"context"
"encoding/base64"
+ "os"
"path/filepath"
"strings"
"sync/atomic"
@@ -855,7 +856,10 @@ func TestSettingsService_LoadDatabaseSettings_UIConfigurationDisabled_Env(t *tes
// Set env + disable flag BEFORE service init
t.Setenv("UI_CONFIGURATION_DISABLED", "true")
t.Setenv("PROJECTS_DIRECTORY", "env/projects")
- t.Setenv("BASE_SERVER_URL", "https://env.example")
+ // One value as a variable, one as a Docker secret file.
+ baseURLFile := filepath.Join(t.TempDir(), "base-server-url")
+ require.NoError(t, os.WriteFile(baseURLFile, []byte("https://env.example\n"), 0o600))
+ t.Setenv("BASE_SERVER_URL_FILE", baseURLFile)
c := config.Load()
c.UIConfigurationDisabled = true
diff --git a/backend/pkg/utils/env_util.go b/backend/pkg/utils/env_util.go
new file mode 100644
index 000000000..546c49b68
--- /dev/null
+++ b/backend/pkg/utils/env_util.go
@@ -0,0 +1,32 @@
+package utils
+
+import (
+ "log/slog"
+ "os"
+ "strings"
+)
+
+// LookupEnvOrFile is os.LookupEnv with Docker secret support: when NAME__FILE
+// or NAME_FILE (checked in that order) names a readable file, its trimmed
+// contents stand in for NAME. An unreadable file falls back to NAME.
+// The booleans report whether a value was found and whether it came from a file.
+func LookupEnvOrFile(name string) (value string, found bool, fromFile bool) {
+ for _, suffix := range []string{"__FILE", "_FILE"} {
+ filePath := os.Getenv(name + suffix)
+ if filePath == "" {
+ continue
+ }
+
+ // Secret paths are arbitrary host paths, so there is no acfs root to confine them to.
+ content, err := os.ReadFile(filePath) //nolint:gosec // path intentionally comes from a *_FILE env var
+ if err != nil {
+ slog.Warn("Failed to read secret file, falling back to direct env var", "env", name+suffix, "error", err)
+ break
+ }
+
+ return strings.TrimSpace(string(content)), true, true
+ }
+
+ value, found = os.LookupEnv(name)
+ return value, found, false
+}
diff --git a/backend/pkg/utils/env_util_test.go b/backend/pkg/utils/env_util_test.go
new file mode 100644
index 000000000..324e9b2b9
--- /dev/null
+++ b/backend/pkg/utils/env_util_test.go
@@ -0,0 +1,74 @@
+package utils
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestLookupEnvOrFile(t *testing.T) {
+ const name = "ARCANE_TEST_SECRET"
+ writeFile := func(t *testing.T, content string) string {
+ t.Helper()
+ path := filepath.Join(t.TempDir(), "secret")
+ require.NoError(t, os.WriteFile(path, []byte(content), 0o600))
+ return path
+ }
+
+ tests := []struct {
+ name string
+ setup func(t *testing.T)
+ want string
+ found bool
+ }{
+ {
+ name: "file wins over the variable and is trimmed",
+ setup: func(t *testing.T) {
+ t.Setenv(name+"_FILE", writeFile(t, "from-file\n"))
+ t.Setenv(name, "from-env")
+ },
+ want: "from-file",
+ found: true,
+ },
+ {
+ name: "double underscore wins over single",
+ setup: func(t *testing.T) {
+ t.Setenv(name+"__FILE", writeFile(t, "double"))
+ t.Setenv(name+"_FILE", writeFile(t, "single"))
+ },
+ want: "double",
+ found: true,
+ },
+ {
+ name: "unreadable file falls back to the variable",
+ setup: func(t *testing.T) {
+ t.Setenv(name+"_FILE", filepath.Join(t.TempDir(), "missing"))
+ t.Setenv(name, "from-env")
+ },
+ want: "from-env",
+ found: true,
+ },
+ {
+ name: "variable only",
+ setup: func(t *testing.T) { t.Setenv(name, "from-env") },
+ want: "from-env",
+ found: true,
+ },
+ {
+ name: "unset",
+ setup: func(t *testing.T) {},
+ found: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ tt.setup(t)
+ got, ok, _ := LookupEnvOrFile(name)
+ require.Equal(t, tt.found, ok)
+ require.Equal(t, tt.want, got)
+ })
+ }
+}