feat: accept ND_-prefixed env var names in config files (#5258)
* feat: add toPascalCase helper for config key display Adds a toPascalCase helper that converts dotted lowercase Viper config keys (e.g. 'scanner.schedule') to PascalCase (e.g. 'Scanner.Schedule') for use in user-facing warning messages. Includes export_test.go binding and a full Ginkgo DescribeTable test suite covering simple, dotted, multi-segment, already-capitalized, and empty-string cases. * feat: remap ND_-prefixed env var names found in config files Detect when users mistakenly use environment variable names (like ND_ADDRESS) in config files, remap them to canonical keys, and warn. Fatal error if both ND_ and canonical versions of the same key exist. Closes #5242
This commit is contained in:
@@ -258,6 +258,13 @@ type searchOptions struct {
|
||||
FullString bool
|
||||
}
|
||||
|
||||
// fatalFunc is called for fatal config errors. Defaults to printing + os.Exit(1).
|
||||
// Overridden in tests to allow testing fatal paths.
|
||||
var fatalFunc = func(msg string) {
|
||||
_, _ = fmt.Fprintln(os.Stderr, "FATAL:", msg)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var (
|
||||
Server = &configOptions{}
|
||||
hooks []func()
|
||||
@@ -275,6 +282,7 @@ func LoadFromFile(confFile string) {
|
||||
|
||||
func Load(noConfigDump bool) {
|
||||
parseIniFileConfiguration()
|
||||
remapEnvVarKeysFromConfig()
|
||||
|
||||
// Map deprecated options to their new names for backwards compatibility
|
||||
mapDeprecatedOption("ReverseProxyWhitelist", "ExtAuth.TrustedSources")
|
||||
@@ -466,6 +474,35 @@ func logRemovedOptions(options ...string) {
|
||||
}
|
||||
}
|
||||
|
||||
// remapEnvVarKeysFromConfig detects ND_-prefixed keys in the config file (users mistakenly
|
||||
// using environment variable names) and remaps them to canonical Viper keys with a warning.
|
||||
func remapEnvVarKeysFromConfig() {
|
||||
for _, key := range viper.AllKeys() {
|
||||
if !strings.HasPrefix(key, "nd_") || !viper.InConfig(key) {
|
||||
continue
|
||||
}
|
||||
stripped := strings.TrimPrefix(key, "nd_")
|
||||
canonicalKey := strings.ReplaceAll(stripped, "_", ".")
|
||||
displayNDKey := "ND_" + strings.ToUpper(stripped)
|
||||
displayCanonical := toPascalCase(canonicalKey)
|
||||
|
||||
if viper.InConfig(canonicalKey) {
|
||||
fatalFunc(fmt.Sprintf(
|
||||
"Config file contains both '%s' and '%s'. Remove the ND_-prefixed version. "+
|
||||
"The 'ND_' prefix is only needed for environment variables, not config file keys.",
|
||||
displayNDKey, displayCanonical,
|
||||
))
|
||||
return
|
||||
}
|
||||
|
||||
viper.Set(canonicalKey, viper.Get(key))
|
||||
_, _ = fmt.Fprintf(os.Stderr, "WARNING: Config key '%s' uses environment variable naming. Use '%s' instead. "+
|
||||
"The 'ND_' prefix is only needed for environment variables.\n",
|
||||
displayNDKey, displayCanonical,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// mapDeprecatedOption is used to provide backwards compatibility for deprecated options. It should be called after
|
||||
// the config has been read by viper, but before unmarshalling it into the Config struct.
|
||||
func mapDeprecatedOption(legacyName, newName string) {
|
||||
@@ -617,6 +654,21 @@ func normalizeSearchBackend(value string) string {
|
||||
}
|
||||
}
|
||||
|
||||
// toPascalCase converts a dotted lowercase config key to PascalCase for display.
|
||||
// Example: "scanner.schedule" → "Scanner.Schedule"
|
||||
func toPascalCase(key string) string {
|
||||
if key == "" {
|
||||
return ""
|
||||
}
|
||||
parts := strings.Split(key, ".")
|
||||
for i, part := range parts {
|
||||
if len(part) > 0 {
|
||||
parts[i] = strings.ToUpper(part[:1]) + part[1:]
|
||||
}
|
||||
}
|
||||
return strings.Join(parts, ".")
|
||||
}
|
||||
|
||||
// AddHook is used to register initialization code that should run as soon as the config is loaded
|
||||
func AddHook(hook func()) {
|
||||
hooks = append(hooks, hook)
|
||||
|
||||
Reference in New Issue
Block a user