Skip to content

Commit b191759

Browse files
authored
Check for deprecated/unknown environment variables (#1342)
* added a check for deprecated variables
1 parent 5d148d9 commit b191759

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

internal/config/config.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,40 @@ func Execute(ctx context.Context) error {
6464
func Init(version, commit string) {
6565
setVersion(version, commit)
6666
registerFlags()
67+
checkDeprecatedEnvVars()
68+
}
69+
70+
func checkDeprecatedEnvVars() {
71+
allViperKeys := make(map[string]struct{})
72+
for _, key := range viperInstance.AllKeys() {
73+
allViperKeys[key] = struct{}{}
74+
}
75+
76+
const v3Prefix = EnvPrefix + KeyDelimiter
77+
78+
for _, env := range os.Environ() {
79+
parts := strings.SplitN(env, "=", KeyValueNumber)
80+
if len(parts) != KeyValueNumber {
81+
continue
82+
}
83+
envKey := parts[0]
84+
85+
if !strings.HasPrefix(envKey, v3Prefix) {
86+
continue
87+
}
88+
89+
viperKey := strings.TrimPrefix(envKey, v3Prefix)
90+
91+
viperKey = strings.ToLower(viperKey)
92+
93+
if _, exists := allViperKeys[viperKey]; !exists {
94+
slog.Warn("Detected deprecated or unknown environment variables. "+
95+
"Please update to use the latest environment variables. For more information, visit "+
96+
"https://docs.nginx.com/nginx-one/agent/configure-instances/configuration-overview/.",
97+
"deprecated_env_var", envKey,
98+
)
99+
}
100+
}
67101
}
68102

69103
func RegisterConfigFile() error {

internal/config/config_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
package config
66

77
import (
8+
"bytes"
89
_ "embed"
910
"errors"
11+
"log/slog"
1012
"os"
1113
"path"
1214
"sort"
@@ -164,6 +166,100 @@ func TestNormalizeFunc(t *testing.T) {
164166
assert.Equal(t, expected, result)
165167
}
166168

169+
type deprecatedEnvVarsTest struct {
170+
name string
171+
expectedLogContent string
172+
unexpectedLogContent string
173+
envVars map[string]string
174+
viperKeys []string
175+
expectWarning bool
176+
}
177+
178+
func TestCheckDeprecatedEnvVars(t *testing.T) {
179+
tests := []deprecatedEnvVarsTest{
180+
{
181+
name: "Test 1: should log warning for deprecated env var",
182+
envVars: map[string]string{
183+
"NGINX_AGENT_SERVER_HOST": "value",
184+
},
185+
viperKeys: []string{"some_other_key"},
186+
expectedLogContent: "NGINX_AGENT_SERVER_HOST",
187+
expectWarning: true,
188+
},
189+
{
190+
name: "Test 2: should not log warning for valid env var",
191+
envVars: map[string]string{
192+
"NGINX_AGENT_LOG_LEVEL": "info",
193+
},
194+
viperKeys: []string{"log_level"},
195+
unexpectedLogContent: "NGINX_AGENT_LOG_LEVEL",
196+
expectWarning: false,
197+
},
198+
{
199+
name: "Test 3: should handle mixed valid and deprecated env vars",
200+
envVars: map[string]string{
201+
"NGINX_AGENT_LOG_LEVEL": "info",
202+
"NGINX_AGENT_DEPRECATED_VAR": "value",
203+
},
204+
viperKeys: []string{"log_level"},
205+
expectedLogContent: "NGINX_AGENT_DEPRECATED_VAR",
206+
unexpectedLogContent: "NGINX_AGENT_LOG_LEVEL",
207+
expectWarning: true,
208+
},
209+
{
210+
name: "Test 4: should ignore non-agent env vars",
211+
envVars: map[string]string{
212+
"NGINX_LICENSE": "value",
213+
},
214+
viperKeys: []string{},
215+
expectWarning: false,
216+
},
217+
}
218+
219+
for _, tc := range tests {
220+
t.Run(tc.name, func(t *testing.T) {
221+
runDeprecatedEnvVarsTest(t, tc)
222+
})
223+
}
224+
}
225+
226+
func runDeprecatedEnvVarsTest(t *testing.T, tc deprecatedEnvVarsTest) {
227+
t.Helper()
228+
229+
originalViper := viperInstance
230+
viperInstance = viper.NewWithOptions(viper.KeyDelimiter(KeyDelimiter))
231+
defer func() { viperInstance = originalViper }()
232+
233+
for key, value := range tc.envVars {
234+
t.Setenv(key, value)
235+
}
236+
237+
for _, key := range tc.viperKeys {
238+
viperInstance.Set(key, "any-value")
239+
}
240+
241+
var logBuffer bytes.Buffer
242+
handler := slog.NewTextHandler(&logBuffer, nil)
243+
slog.SetDefault(slog.New(handler))
244+
245+
checkDeprecatedEnvVars()
246+
247+
logOutput := logBuffer.String()
248+
249+
if tc.expectWarning {
250+
require.NotEmpty(t, logOutput, "Expected a warning log, but got none")
251+
assert.Contains(t, logOutput, "Detected deprecated or unknown environment variables")
252+
if tc.expectedLogContent != "" {
253+
assert.Contains(t, logOutput, tc.expectedLogContent)
254+
}
255+
if tc.unexpectedLogContent != "" {
256+
assert.NotContains(t, logOutput, tc.unexpectedLogContent)
257+
}
258+
} else {
259+
assert.Empty(t, logOutput, "Expected no warning logs")
260+
}
261+
}
262+
167263
func TestResolveAllowedDirectories(t *testing.T) {
168264
tests := []struct {
169265
name string

0 commit comments

Comments
 (0)