From 5b8a8dc37749e85828ccc97245a404ac4806554c Mon Sep 17 00:00:00 2001 From: Spencer Date: Thu, 13 Jun 2024 15:24:31 +0100 Subject: [PATCH 01/11] changed env variable prefix from NMS to NGINX_AGENT --- main.go | 4 ++++ src/core/config/config.go | 18 ++++++++++++++++-- src/core/config/config_test.go | 2 +- src/core/config/defaults.go | 3 ++- 4 files changed, 23 insertions(+), 4 deletions(-) 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..a12bac556 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 @@ -133,7 +136,7 @@ func deprecateFlags() { } func RegisterFlags() { - Viper.SetEnvPrefix(EnvPrefix) + Viper.SetEnvPrefix(NewEnvPrefix) Viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) Viper.AutomaticEnv() @@ -149,6 +152,17 @@ func RegisterFlags() { if err := Viper.BindPFlag(strings.ReplaceAll(flag.Name, "-", "_"), fs.Lookup(flag.Name)); err != nil { return } + + oldKey := strings.ToUpper(OldEnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) + newKey := strings.ToUpper(NewEnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) + + 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..aa2c31be2 100644 --- a/src/core/config/config_test.go +++ b/src/core/config/config_test.go @@ -480,7 +480,7 @@ func TestUpdateAgentConfig(t *testing.T) { } func setEnvVariable(t *testing.T, name string, value string) { - key := strings.ToUpper(EnvPrefix + agent_config.KeyDelimiter + name) + key := strings.ToUpper(NewEnvPrefix + agent_config.KeyDelimiter + name) err := os.Setenv(key, value) require.NoError(t, err) } diff --git a/src/core/config/defaults.go b/src/core/config/defaults.go index 731d6e48a..03ec8557e 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" + OldEnvPrefix = "nms" + NewEnvPrefix = "nginx_agent" ConfigPathKey = "path" DynamicConfigPathKey = "dynamic_config_path" From 1997ecd82551862b07cf3d3a048eeb0b94aa8995 Mon Sep 17 00:00:00 2001 From: Spencer Date: Thu, 13 Jun 2024 15:32:57 +0100 Subject: [PATCH 02/11] changed env variable prefix from NMS to NGINX_AGENT --- .../nginx/agent/v2/src/core/config/config.go | 18 ++++++++++++++++-- .../nginx/agent/v2/src/core/config/defaults.go | 3 ++- .../nginx/agent/v2/src/core/config/config.go | 18 ++++++++++++++++-- .../nginx/agent/v2/src/core/config/defaults.go | 3 ++- 4 files changed, 36 insertions(+), 6 deletions(-) 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..a12bac556 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 @@ -133,7 +136,7 @@ func deprecateFlags() { } func RegisterFlags() { - Viper.SetEnvPrefix(EnvPrefix) + Viper.SetEnvPrefix(NewEnvPrefix) Viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) Viper.AutomaticEnv() @@ -149,6 +152,17 @@ func RegisterFlags() { if err := Viper.BindPFlag(strings.ReplaceAll(flag.Name, "-", "_"), fs.Lookup(flag.Name)); err != nil { return } + + oldKey := strings.ToUpper(OldEnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) + newKey := strings.ToUpper(NewEnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) + + 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..03ec8557e 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" + OldEnvPrefix = "nms" + NewEnvPrefix = "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..a12bac556 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 @@ -133,7 +136,7 @@ func deprecateFlags() { } func RegisterFlags() { - Viper.SetEnvPrefix(EnvPrefix) + Viper.SetEnvPrefix(NewEnvPrefix) Viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) Viper.AutomaticEnv() @@ -149,6 +152,17 @@ func RegisterFlags() { if err := Viper.BindPFlag(strings.ReplaceAll(flag.Name, "-", "_"), fs.Lookup(flag.Name)); err != nil { return } + + oldKey := strings.ToUpper(OldEnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) + newKey := strings.ToUpper(NewEnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) + + 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..03ec8557e 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" + OldEnvPrefix = "nms" + NewEnvPrefix = "nginx_agent" ConfigPathKey = "path" DynamicConfigPathKey = "dynamic_config_path" From 3c5157c6704e79722efb2f295e62760920917181 Mon Sep 17 00:00:00 2001 From: Spencer Date: Mon, 17 Jun 2024 09:57:22 +0100 Subject: [PATCH 03/11] renamed env prefix constants --- src/core/config/config.go | 6 +++--- src/core/config/config_test.go | 2 +- src/core/config/defaults.go | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/core/config/config.go b/src/core/config/config.go index a12bac556..75b72a9e1 100644 --- a/src/core/config/config.go +++ b/src/core/config/config.go @@ -136,7 +136,7 @@ func deprecateFlags() { } func RegisterFlags() { - Viper.SetEnvPrefix(NewEnvPrefix) + Viper.SetEnvPrefix(EnvPrefix) Viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) Viper.AutomaticEnv() @@ -153,8 +153,8 @@ func RegisterFlags() { return } - oldKey := strings.ToUpper(OldEnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) - newKey := strings.ToUpper(NewEnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) + oldKey := strings.ToUpper(LegacyEnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) + newKey := strings.ToUpper(EnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) if os.Getenv(oldKey) != "" && os.Getenv(newKey) == "" { if err := os.Setenv(newKey, os.Getenv(oldKey)); err != nil { diff --git a/src/core/config/config_test.go b/src/core/config/config_test.go index aa2c31be2..44dc135f8 100644 --- a/src/core/config/config_test.go +++ b/src/core/config/config_test.go @@ -480,7 +480,7 @@ func TestUpdateAgentConfig(t *testing.T) { } func setEnvVariable(t *testing.T, name string, value string) { - key := strings.ToUpper(NewEnvPrefix + agent_config.KeyDelimiter + name) + key := strings.ToUpper(EnvPrefix + agent_config.KeyDelimiter + name) err := os.Setenv(key, value) require.NoError(t, err) } diff --git a/src/core/config/defaults.go b/src/core/config/defaults.go index 03ec8557e..9a39c9a52 100644 --- a/src/core/config/defaults.go +++ b/src/core/config/defaults.go @@ -103,8 +103,8 @@ const ( DynamicConfigFileAbsFreeBsdPath = "/var/db/nginx-agent/agent-dynamic.conf" ConfigFileName = "nginx-agent.conf" ConfigFileType = "yaml" - OldEnvPrefix = "nms" - NewEnvPrefix = "nginx_agent" + LegacyEnvPrefix = "nms" + EnvPrefix = "nginx_agent" ConfigPathKey = "path" DynamicConfigPathKey = "dynamic_config_path" From aa1a5eace7fec1b664a5977d36db61289c928be0 Mon Sep 17 00:00:00 2001 From: Spencer Date: Mon, 17 Jun 2024 09:58:42 +0100 Subject: [PATCH 04/11] renamed env prefix constants --- .../github.com/nginx/agent/v2/src/core/config/config.go | 6 +++--- .../github.com/nginx/agent/v2/src/core/config/defaults.go | 4 ++-- .../github.com/nginx/agent/v2/src/core/config/config.go | 6 +++--- .../github.com/nginx/agent/v2/src/core/config/defaults.go | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) 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 a12bac556..75b72a9e1 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 @@ -136,7 +136,7 @@ func deprecateFlags() { } func RegisterFlags() { - Viper.SetEnvPrefix(NewEnvPrefix) + Viper.SetEnvPrefix(EnvPrefix) Viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) Viper.AutomaticEnv() @@ -153,8 +153,8 @@ func RegisterFlags() { return } - oldKey := strings.ToUpper(OldEnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) - newKey := strings.ToUpper(NewEnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) + oldKey := strings.ToUpper(LegacyEnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) + newKey := strings.ToUpper(EnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) if os.Getenv(oldKey) != "" && os.Getenv(newKey) == "" { if err := os.Setenv(newKey, os.Getenv(oldKey)); err != nil { 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 03ec8557e..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,8 +103,8 @@ const ( DynamicConfigFileAbsFreeBsdPath = "/var/db/nginx-agent/agent-dynamic.conf" ConfigFileName = "nginx-agent.conf" ConfigFileType = "yaml" - OldEnvPrefix = "nms" - NewEnvPrefix = "nginx_agent" + 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 a12bac556..75b72a9e1 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 @@ -136,7 +136,7 @@ func deprecateFlags() { } func RegisterFlags() { - Viper.SetEnvPrefix(NewEnvPrefix) + Viper.SetEnvPrefix(EnvPrefix) Viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) Viper.AutomaticEnv() @@ -153,8 +153,8 @@ func RegisterFlags() { return } - oldKey := strings.ToUpper(OldEnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) - newKey := strings.ToUpper(NewEnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) + oldKey := strings.ToUpper(LegacyEnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) + newKey := strings.ToUpper(EnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) if os.Getenv(oldKey) != "" && os.Getenv(newKey) == "" { if err := os.Setenv(newKey, os.Getenv(oldKey)); err != nil { 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 03ec8557e..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,8 +103,8 @@ const ( DynamicConfigFileAbsFreeBsdPath = "/var/db/nginx-agent/agent-dynamic.conf" ConfigFileName = "nginx-agent.conf" ConfigFileType = "yaml" - OldEnvPrefix = "nms" - NewEnvPrefix = "nginx_agent" + LegacyEnvPrefix = "nms" + EnvPrefix = "nginx_agent" ConfigPathKey = "path" DynamicConfigPathKey = "dynamic_config_path" From 5ff0aa5b2c74d6843433879350443383b935154c Mon Sep 17 00:00:00 2001 From: Spencer Date: Mon, 17 Jun 2024 10:27:34 +0100 Subject: [PATCH 05/11] refactored string building of oldKey and newKey --- src/core/config/config.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/config/config.go b/src/core/config/config.go index 75b72a9e1..ab46af64a 100644 --- a/src/core/config/config.go +++ b/src/core/config/config.go @@ -153,8 +153,8 @@ func RegisterFlags() { return } - oldKey := strings.ToUpper(LegacyEnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) - newKey := strings.ToUpper(EnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) + oldKey := strings.ToUpper(fmt.Sprintf("%s%s%s", LegacyEnvPrefix, agent_config.KeyDelimiter, strings.ReplaceAll(flag.Name, "-", "_"))) + newKey := strings.ToUpper(fmt.Sprintf("%s%s%s", EnvPrefix, agent_config.KeyDelimiter, strings.ReplaceAll(flag.Name, "-", "_"))) if os.Getenv(oldKey) != "" && os.Getenv(newKey) == "" { if err := os.Setenv(newKey, os.Getenv(oldKey)); err != nil { From 8873427c18efb5d96c477f088abda3ea71d426ae Mon Sep 17 00:00:00 2001 From: Spencer Date: Mon, 17 Jun 2024 10:28:29 +0100 Subject: [PATCH 06/11] refactored string building of oldKey and newKey --- .../github.com/nginx/agent/v2/src/core/config/config.go | 4 ++-- .../github.com/nginx/agent/v2/src/core/config/config.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) 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 75b72a9e1..ab46af64a 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 @@ -153,8 +153,8 @@ func RegisterFlags() { return } - oldKey := strings.ToUpper(LegacyEnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) - newKey := strings.ToUpper(EnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) + oldKey := strings.ToUpper(fmt.Sprintf("%s%s%s", LegacyEnvPrefix, agent_config.KeyDelimiter, strings.ReplaceAll(flag.Name, "-", "_"))) + newKey := strings.ToUpper(fmt.Sprintf("%s%s%s", EnvPrefix, agent_config.KeyDelimiter, strings.ReplaceAll(flag.Name, "-", "_"))) if os.Getenv(oldKey) != "" && os.Getenv(newKey) == "" { if err := os.Setenv(newKey, os.Getenv(oldKey)); err != nil { 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 75b72a9e1..ab46af64a 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 @@ -153,8 +153,8 @@ func RegisterFlags() { return } - oldKey := strings.ToUpper(LegacyEnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) - newKey := strings.ToUpper(EnvPrefix + "_" + strings.ReplaceAll(flag.Name, "-", "_")) + oldKey := strings.ToUpper(fmt.Sprintf("%s%s%s", LegacyEnvPrefix, agent_config.KeyDelimiter, strings.ReplaceAll(flag.Name, "-", "_"))) + newKey := strings.ToUpper(fmt.Sprintf("%s%s%s", EnvPrefix, agent_config.KeyDelimiter, strings.ReplaceAll(flag.Name, "-", "_"))) if os.Getenv(oldKey) != "" && os.Getenv(newKey) == "" { if err := os.Setenv(newKey, os.Getenv(oldKey)); err != nil { From 596b76426fe9eed8cac4ca61b941c838aafccf8d Mon Sep 17 00:00:00 2001 From: Spencer Date: Mon, 17 Jun 2024 12:37:56 +0100 Subject: [PATCH 07/11] added test for env prefix migration --- src/core/config/config_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/core/config/config_test.go b/src/core/config/config_test.go index 44dc135f8..ce45b25f5 100644 --- a/src/core/config/config_test.go +++ b/src/core/config/config_test.go @@ -479,6 +479,18 @@ func TestUpdateAgentConfig(t *testing.T) { } } +func TestDeprecatedEnvPrefixMigration(t *testing.T){ + want := true + + t.Setenv("NMS_TLS_SKIP_VERIFY","true") + + SetDefaults() + RegisterFlags() + + got := MigratedEnv + 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) From f1d4a55272eddb90fadb878fa9f8fff6a0c7c66c Mon Sep 17 00:00:00 2001 From: Spencer Date: Mon, 17 Jun 2024 12:39:10 +0100 Subject: [PATCH 08/11] added test for env prefix migration --- src/core/config/config_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/core/config/config_test.go b/src/core/config/config_test.go index ce45b25f5..59ac39f2f 100644 --- a/src/core/config/config_test.go +++ b/src/core/config/config_test.go @@ -479,17 +479,17 @@ func TestUpdateAgentConfig(t *testing.T) { } } -func TestDeprecatedEnvPrefixMigration(t *testing.T){ +func TestDeprecatedEnvPrefixMigration(t *testing.T) { want := true - - t.Setenv("NMS_TLS_SKIP_VERIFY","true") + + t.Setenv("NMS_TLS_SKIP_VERIFY", "true") SetDefaults() RegisterFlags() got := MigratedEnv assert.Equal(t, want, got) -} +} func setEnvVariable(t *testing.T, name string, value string) { key := strings.ToUpper(EnvPrefix + agent_config.KeyDelimiter + name) From e4e8ca6316937a1d009f02385ba236a695fec54e Mon Sep 17 00:00:00 2001 From: Spencer Date: Mon, 17 Jun 2024 12:45:51 +0100 Subject: [PATCH 09/11] refactored string builing of oldKey and newKey --- src/core/config/config.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/config/config.go b/src/core/config/config.go index ab46af64a..5703bf109 100644 --- a/src/core/config/config.go +++ b/src/core/config/config.go @@ -153,8 +153,8 @@ func RegisterFlags() { return } - oldKey := strings.ToUpper(fmt.Sprintf("%s%s%s", LegacyEnvPrefix, agent_config.KeyDelimiter, strings.ReplaceAll(flag.Name, "-", "_"))) - newKey := strings.ToUpper(fmt.Sprintf("%s%s%s", EnvPrefix, agent_config.KeyDelimiter, strings.ReplaceAll(flag.Name, "-", "_"))) + 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 { From c3c01d32d147ee353ddb08e5f0d57857a7867387 Mon Sep 17 00:00:00 2001 From: Spencer Date: Mon, 17 Jun 2024 12:46:31 +0100 Subject: [PATCH 10/11] refactored string builing of oldKey and newKey --- .../github.com/nginx/agent/v2/src/core/config/config.go | 4 ++-- .../github.com/nginx/agent/v2/src/core/config/config.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) 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 ab46af64a..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 @@ -153,8 +153,8 @@ func RegisterFlags() { return } - oldKey := strings.ToUpper(fmt.Sprintf("%s%s%s", LegacyEnvPrefix, agent_config.KeyDelimiter, strings.ReplaceAll(flag.Name, "-", "_"))) - newKey := strings.ToUpper(fmt.Sprintf("%s%s%s", EnvPrefix, agent_config.KeyDelimiter, strings.ReplaceAll(flag.Name, "-", "_"))) + 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 { 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 ab46af64a..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 @@ -153,8 +153,8 @@ func RegisterFlags() { return } - oldKey := strings.ToUpper(fmt.Sprintf("%s%s%s", LegacyEnvPrefix, agent_config.KeyDelimiter, strings.ReplaceAll(flag.Name, "-", "_"))) - newKey := strings.ToUpper(fmt.Sprintf("%s%s%s", EnvPrefix, agent_config.KeyDelimiter, strings.ReplaceAll(flag.Name, "-", "_"))) + 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 { From a31da69cf3017ee52a9e1cc63fdd4dbacc4b13a8 Mon Sep 17 00:00:00 2001 From: Spencer Date: Mon, 17 Jun 2024 13:59:26 +0100 Subject: [PATCH 11/11] fixed deprecated env prefix migration test --- src/core/config/config_test.go | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/core/config/config_test.go b/src/core/config/config_test.go index 59ac39f2f..3e390a780 100644 --- a/src/core/config/config_test.go +++ b/src/core/config/config_test.go @@ -482,12 +482,30 @@ func TestUpdateAgentConfig(t *testing.T) { func TestDeprecatedEnvPrefixMigration(t *testing.T) { want := true - t.Setenv("NMS_TLS_SKIP_VERIFY", "true") + curDir, err := os.Getwd() + require.NoError(t, err) - SetDefaults() - RegisterFlags() + 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 := MigratedEnv + got := config.TLS.SkipVerify assert.Equal(t, want, got) }