diff --git a/main.go b/main.go index 0fa995e5a..12debc3d3 100644 --- a/main.go +++ b/main.go @@ -59,6 +59,10 @@ func main() { defer logFile.Close() } + if config.MigratedEnv { + log.Warnf("The environment variable prefix 'NMS' is deprecated. Prefix has been migrated to 'NGINX_AGENT'. Please update your configuration to use the new prefix.") + } + log.Tracef("Config loaded from disk, %v", loadedConfig) if loadedConfig.DisplayName == "" { diff --git a/src/core/config/config.go b/src/core/config/config.go index 58c5ce0f3..5703bf109 100644 --- a/src/core/config/config.go +++ b/src/core/config/config.go @@ -47,7 +47,10 @@ const ( ` ) -var Viper = viper.NewWithOptions(viper.KeyDelimiter(agent_config.KeyDelimiter)) +var ( + Viper = viper.NewWithOptions(viper.KeyDelimiter(agent_config.KeyDelimiter)) + MigratedEnv = false +) func SetVersion(version, commit string) { ROOT_COMMAND.Version = version + "-" + commit @@ -149,6 +152,17 @@ func RegisterFlags() { if err := Viper.BindPFlag(strings.ReplaceAll(flag.Name, "-", "_"), fs.Lookup(flag.Name)); err != nil { return } + + oldKey := strings.ToUpper(LegacyEnvPrefix + agent_config.KeyDelimiter + strings.ReplaceAll(flag.Name, "-", agent_config.KeyDelimiter)) + newKey := strings.ToUpper(EnvPrefix + agent_config.KeyDelimiter + strings.ReplaceAll(flag.Name, "-", agent_config.KeyDelimiter)) + + if os.Getenv(oldKey) != "" && os.Getenv(newKey) == "" { + if err := os.Setenv(newKey, os.Getenv(oldKey)); err != nil { + log.Warnf("Failed to set environment variable %s: %v", newKey, err) + } + MigratedEnv = true + } + err := Viper.BindEnv(flag.Name) if err != nil { log.Warnf("Error occurred binding env %s: %v", flag.Name, err) diff --git a/src/core/config/config_test.go b/src/core/config/config_test.go index 44dc135f8..3e390a780 100644 --- a/src/core/config/config_test.go +++ b/src/core/config/config_test.go @@ -479,6 +479,36 @@ func TestUpdateAgentConfig(t *testing.T) { } } +func TestDeprecatedEnvPrefixMigration(t *testing.T) { + want := true + + curDir, err := os.Getwd() + require.NoError(t, err) + + tempConfDeleteFunc, err := sysutils.CopyFile(fmt.Sprintf("%s/%s", testCfgDir, emptyConfigFile), tempCfgFile) + defer func() { + err := tempConfDeleteFunc() + require.NoError(t, err, "deletion of temp config file failed") + }() + require.NoError(t, err) + + tempDynamicDeleteFunc, err := sysutils.CopyFile(fmt.Sprintf("%s/%s", testCfgDir, emptyConfigFile), tempDynamicCfgFile) + defer func() { + err := tempDynamicDeleteFunc() + require.NoError(t, err, "deletion of temp dynamic config file failed") + }() + require.NoError(t, err) + + cleanEnv(t, tempCfgFile, fmt.Sprintf("%s/%s", curDir, tempDynamicCfgFile)) + setEnvVariable(t, "tls_skip_verify", "true") + + config, err := GetConfig("1234") + require.NoError(t, err) + + got := config.TLS.SkipVerify + assert.Equal(t, want, got) +} + func setEnvVariable(t *testing.T, name string, value string) { key := strings.ToUpper(EnvPrefix + agent_config.KeyDelimiter + name) err := os.Setenv(key, value) diff --git a/src/core/config/defaults.go b/src/core/config/defaults.go index 731d6e48a..9a39c9a52 100644 --- a/src/core/config/defaults.go +++ b/src/core/config/defaults.go @@ -103,7 +103,8 @@ const ( DynamicConfigFileAbsFreeBsdPath = "/var/db/nginx-agent/agent-dynamic.conf" ConfigFileName = "nginx-agent.conf" ConfigFileType = "yaml" - EnvPrefix = "nms" + LegacyEnvPrefix = "nms" + EnvPrefix = "nginx_agent" ConfigPathKey = "path" DynamicConfigPathKey = "dynamic_config_path" diff --git a/test/integration/vendor/github.com/nginx/agent/v2/src/core/config/config.go b/test/integration/vendor/github.com/nginx/agent/v2/src/core/config/config.go index 58c5ce0f3..5703bf109 100644 --- a/test/integration/vendor/github.com/nginx/agent/v2/src/core/config/config.go +++ b/test/integration/vendor/github.com/nginx/agent/v2/src/core/config/config.go @@ -47,7 +47,10 @@ const ( ` ) -var Viper = viper.NewWithOptions(viper.KeyDelimiter(agent_config.KeyDelimiter)) +var ( + Viper = viper.NewWithOptions(viper.KeyDelimiter(agent_config.KeyDelimiter)) + MigratedEnv = false +) func SetVersion(version, commit string) { ROOT_COMMAND.Version = version + "-" + commit @@ -149,6 +152,17 @@ func RegisterFlags() { if err := Viper.BindPFlag(strings.ReplaceAll(flag.Name, "-", "_"), fs.Lookup(flag.Name)); err != nil { return } + + oldKey := strings.ToUpper(LegacyEnvPrefix + agent_config.KeyDelimiter + strings.ReplaceAll(flag.Name, "-", agent_config.KeyDelimiter)) + newKey := strings.ToUpper(EnvPrefix + agent_config.KeyDelimiter + strings.ReplaceAll(flag.Name, "-", agent_config.KeyDelimiter)) + + if os.Getenv(oldKey) != "" && os.Getenv(newKey) == "" { + if err := os.Setenv(newKey, os.Getenv(oldKey)); err != nil { + log.Warnf("Failed to set environment variable %s: %v", newKey, err) + } + MigratedEnv = true + } + err := Viper.BindEnv(flag.Name) if err != nil { log.Warnf("Error occurred binding env %s: %v", flag.Name, err) diff --git a/test/integration/vendor/github.com/nginx/agent/v2/src/core/config/defaults.go b/test/integration/vendor/github.com/nginx/agent/v2/src/core/config/defaults.go index 731d6e48a..9a39c9a52 100644 --- a/test/integration/vendor/github.com/nginx/agent/v2/src/core/config/defaults.go +++ b/test/integration/vendor/github.com/nginx/agent/v2/src/core/config/defaults.go @@ -103,7 +103,8 @@ const ( DynamicConfigFileAbsFreeBsdPath = "/var/db/nginx-agent/agent-dynamic.conf" ConfigFileName = "nginx-agent.conf" ConfigFileType = "yaml" - EnvPrefix = "nms" + LegacyEnvPrefix = "nms" + EnvPrefix = "nginx_agent" ConfigPathKey = "path" DynamicConfigPathKey = "dynamic_config_path" diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/config.go b/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/config.go index 58c5ce0f3..5703bf109 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/config.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/config.go @@ -47,7 +47,10 @@ const ( ` ) -var Viper = viper.NewWithOptions(viper.KeyDelimiter(agent_config.KeyDelimiter)) +var ( + Viper = viper.NewWithOptions(viper.KeyDelimiter(agent_config.KeyDelimiter)) + MigratedEnv = false +) func SetVersion(version, commit string) { ROOT_COMMAND.Version = version + "-" + commit @@ -149,6 +152,17 @@ func RegisterFlags() { if err := Viper.BindPFlag(strings.ReplaceAll(flag.Name, "-", "_"), fs.Lookup(flag.Name)); err != nil { return } + + oldKey := strings.ToUpper(LegacyEnvPrefix + agent_config.KeyDelimiter + strings.ReplaceAll(flag.Name, "-", agent_config.KeyDelimiter)) + newKey := strings.ToUpper(EnvPrefix + agent_config.KeyDelimiter + strings.ReplaceAll(flag.Name, "-", agent_config.KeyDelimiter)) + + if os.Getenv(oldKey) != "" && os.Getenv(newKey) == "" { + if err := os.Setenv(newKey, os.Getenv(oldKey)); err != nil { + log.Warnf("Failed to set environment variable %s: %v", newKey, err) + } + MigratedEnv = true + } + err := Viper.BindEnv(flag.Name) if err != nil { log.Warnf("Error occurred binding env %s: %v", flag.Name, err) diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/defaults.go b/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/defaults.go index 731d6e48a..9a39c9a52 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/defaults.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/core/config/defaults.go @@ -103,7 +103,8 @@ const ( DynamicConfigFileAbsFreeBsdPath = "/var/db/nginx-agent/agent-dynamic.conf" ConfigFileName = "nginx-agent.conf" ConfigFileType = "yaml" - EnvPrefix = "nms" + LegacyEnvPrefix = "nms" + EnvPrefix = "nginx_agent" ConfigPathKey = "path" DynamicConfigPathKey = "dynamic_config_path"