From 5ebfbea5fffc6faa3afbe269a8934c078290617d Mon Sep 17 00:00:00 2001 From: Akshay Chawla Date: Wed, 15 Oct 2025 15:52:53 +0100 Subject: [PATCH 1/4] added a check for deprecated variables --- internal/config/config.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/internal/config/config.go b/internal/config/config.go index cc72af59c7..9394843183 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -63,6 +63,40 @@ func Execute(ctx context.Context) error { func Init(version, commit string) { setVersion(version, commit) registerFlags() + checkDeprecatedEnvVars() +} + +func checkDeprecatedEnvVars() { + allViperKeys := make(map[string]struct{}) + for _, key := range viperInstance.AllKeys() { + allViperKeys[key] = struct{}{} + } + + const v3Prefix = EnvPrefix + KeyDelimiter + + for _, env := range os.Environ() { + parts := strings.SplitN(env, "=", KeyValueNumber) + if len(parts) != KeyValueNumber { + continue + } + envKey := parts[0] + + if !strings.HasPrefix(envKey, v3Prefix) { + continue + } + + viperKey := strings.TrimPrefix(envKey, v3Prefix) + + viperKey = strings.ToLower(viperKey) + + if _, exists := allViperKeys[viperKey]; !exists { + slog.Warn("Detected deprecated environment variables. "+ + "Please update to use the latest environment variables. For more information, visit "+ + "https://docs.nginx.com/nginx-one/agent/configure-instances/configuration-overview/.", + "deprecated_env_var", envKey, + ) + } + } } func RegisterConfigFile() error { From 40c63cdc55a13e4494db393a2f70e7687fe50821 Mon Sep 17 00:00:00 2001 From: Akshay Chawla Date: Thu, 16 Oct 2025 10:52:54 +0100 Subject: [PATCH 2/4] added unit test --- internal/config/config.go | 2 +- internal/config/config_test.go | 96 ++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) diff --git a/internal/config/config.go b/internal/config/config.go index 9394843183..bce48003cb 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -90,7 +90,7 @@ func checkDeprecatedEnvVars() { viperKey = strings.ToLower(viperKey) if _, exists := allViperKeys[viperKey]; !exists { - slog.Warn("Detected deprecated environment variables. "+ + slog.Warn("Detected deprecated or unknown environment variables. "+ "Please update to use the latest environment variables. For more information, visit "+ "https://docs.nginx.com/nginx-one/agent/configure-instances/configuration-overview/.", "deprecated_env_var", envKey, diff --git a/internal/config/config_test.go b/internal/config/config_test.go index f55afee23e..a45dce333a 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -5,8 +5,10 @@ package config import ( + "bytes" _ "embed" "errors" + "log/slog" "os" "path" "sort" @@ -163,6 +165,100 @@ func TestNormalizeFunc(t *testing.T) { assert.Equal(t, expected, result) } +type deprecatedEnvVarsTest struct { + name string + expectedLogContent string + unexpectedLogContent string + envVars map[string]string + viperKeys []string + expectWarning bool +} + +func TestCheckDeprecatedEnvVars(t *testing.T) { + tests := []deprecatedEnvVarsTest{ + { + name: "Test 1: should log warning for deprecated env var", + envVars: map[string]string{ + "NGINX_AGENT_SERVER_HOST": "value", + }, + viperKeys: []string{"some_other_key"}, + expectedLogContent: "NGINX_AGENT_SERVER_HOST", + expectWarning: true, + }, + { + name: "Test 2: should not log warning for valid env var", + envVars: map[string]string{ + "NGINX_AGENT_LOG_LEVEL": "info", + }, + viperKeys: []string{"log_level"}, + unexpectedLogContent: "NGINX_AGENT_LOG_LEVEL", + expectWarning: false, + }, + { + name: "Test 3: should handle mixed valid and deprecated env vars", + envVars: map[string]string{ + "NGINX_AGENT_LOG_LEVEL": "info", + "NGINX_AGENT_DEPRECATED_VAR": "value", + }, + viperKeys: []string{"log_level"}, + expectedLogContent: "NGINX_AGENT_DEPRECATED_VAR", + unexpectedLogContent: "NGINX_AGENT_LOG_LEVEL", + expectWarning: true, + }, + { + name: "Test 4: should ignore non-agent env vars", + envVars: map[string]string{ + "NGINX_LICENSE": "value", + }, + viperKeys: []string{}, + expectWarning: false, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + runDeprecatedEnvVarsTest(t, tc) + }) + } +} + +func runDeprecatedEnvVarsTest(t *testing.T, tc deprecatedEnvVarsTest) { + t.Helper() + + originalViper := viperInstance + viperInstance = viper.NewWithOptions(viper.KeyDelimiter(KeyDelimiter)) + defer func() { viperInstance = originalViper }() + + for key, value := range tc.envVars { + t.Setenv(key, value) + } + + for _, key := range tc.viperKeys { + viperInstance.Set(key, "any-value") + } + + var logBuffer bytes.Buffer + handler := slog.NewTextHandler(&logBuffer, nil) + slog.SetDefault(slog.New(handler)) + + checkDeprecatedEnvVars() + + logOutput := logBuffer.String() + + if tc.expectWarning { + require.NotEmpty(t, logOutput, "Expected a warning log, but got none") + assert.Contains(t, logOutput, "Detected deprecated or unknown environment variables") + if tc.expectedLogContent != "" { + assert.Contains(t, logOutput, tc.expectedLogContent) + } + if tc.unexpectedLogContent != "" { + assert.NotContains(t, logOutput, tc.unexpectedLogContent) + } + } else { + assert.Empty(t, logOutput, "Expected no warning logs") + } +} + func TestResolveAllowedDirectories(t *testing.T) { tests := []struct { name string From 011d5c800d361a1bbb070515156a5d99b2790e89 Mon Sep 17 00:00:00 2001 From: Aphral Griffin Date: Thu, 16 Oct 2025 15:54:09 +0100 Subject: [PATCH 3/4] fix pipeline --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b21cbe935d..2a270ad8d2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,7 +20,7 @@ permissions: env: NFPM_VERSION: 'v2.35.3' - GOPROXY: "direct" + GOPROXY: "https://${{ secrets.ARTIFACTORY_USER }}:${{ secrets.ARTIFACTORY_TOKEN }}@azr.artifactory.f5net.com/artifactory/api/go/f5-nginx-go-dev" jobs: proxy-sanity-check: From 71c2fdd388ad81aec9e261bd4392a4ed89c6f8e4 Mon Sep 17 00:00:00 2001 From: Akshay Chawla Date: Mon, 3 Nov 2025 10:27:14 +0000 Subject: [PATCH 4/4] reverted the fix done for git pipeline issue --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2a270ad8d2..b21cbe935d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,7 +20,7 @@ permissions: env: NFPM_VERSION: 'v2.35.3' - GOPROXY: "https://${{ secrets.ARTIFACTORY_USER }}:${{ secrets.ARTIFACTORY_TOKEN }}@azr.artifactory.f5net.com/artifactory/api/go/f5-nginx-go-dev" + GOPROXY: "direct" jobs: proxy-sanity-check: