From 174be3ded14c7d11af294b984751d05e6ff0ed57 Mon Sep 17 00:00:00 2001 From: spencerugbo <102359791+spencerugbo@users.noreply.github.com> Date: Mon, 17 Jun 2024 15:40:15 +0100 Subject: [PATCH] Change environment prefix from nms to nginx_agent (#706) * changed env variable prefix from NMS to NGINX_AGENT * changed env variable prefix from NMS to NGINX_AGENT * renamed env prefix constants * renamed env prefix constants * refactored string building of oldKey and newKey * refactored string building of oldKey and newKey * added test for env prefix migration * added test for env prefix migration * refactored string builing of oldKey and newKey * refactored string builing of oldKey and newKey * fixed deprecated env prefix migration test --- main.go | 4 +++ src/core/config/config.go | 16 +++++++++- src/core/config/config_test.go | 30 +++++++++++++++++++ src/core/config/defaults.go | 3 +- .../nginx/agent/v2/src/core/config/config.go | 16 +++++++++- .../agent/v2/src/core/config/defaults.go | 3 +- .../nginx/agent/v2/src/core/config/config.go | 16 +++++++++- .../agent/v2/src/core/config/defaults.go | 3 +- 8 files changed, 85 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 0fa995e5a2..12debc3d34 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 58c5ce0f36..5703bf109d 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 44dc135f8f..3e390a7802 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 731d6e48a1..9a39c9a52a 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 58c5ce0f36..5703bf109d 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 731d6e48a1..9a39c9a52a 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 58c5ce0f36..5703bf109d 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 731d6e48a1..9a39c9a52a 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"