Skip to content

Commit 8109533

Browse files
committed
fix(config): also error when env var in config is prefixed or suffixed
Signed-off-by: Jonas Kuske <[email protected]>
1 parent 1cc3d8a commit 8109533

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ scrape_configs:
174174
```
175175

176176
You could pass `username`, `password` & `priv_password` via environment variables of your choice in below format.
177-
If the variables exist in the environment, they are resolved on the fly otherwise the string in the config file is passed as-is.
177+
If the variables exist in the environment, they are resolved on the fly, otherwise `snmp_exporter` will error while loading the config.
178178

179179
This requires the `--config.expand-environment-variables` flag be set.
180180

config/config.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,16 @@ func (re *Regexp) UnmarshalYAML(unmarshal func(interface{}) error) error {
361361
}
362362

363363
func substituteEnvVariables(value string) (string, error) {
364+
var missingEnv = ""
364365
result := os.Expand(value, func(s string) string {
365-
return os.Getenv(s)
366+
v, found := os.LookupEnv(s)
367+
if (v == "" || !found) && missingEnv == "" {
368+
missingEnv = s
369+
}
370+
return v
366371
})
367-
if result == "" {
368-
return "", errors.New(value + " environment variable not found")
372+
if missingEnv != "" {
373+
return "", errors.New(missingEnv + " environment variable not found")
369374
}
370375
return result, nil
371376
}

config_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestLoadMultipleConfigs(t *testing.T) {
7373

7474
// When all environment variables are present
7575
func TestEnvSecrets(t *testing.T) {
76-
t.Setenv("ENV_USERNAME", "snmp_username")
76+
t.Setenv("ENV_USERNAME", "username") // snmp_ prefix is set in config file
7777
t.Setenv("ENV_PASSWORD", "snmp_password")
7878
t.Setenv("ENV_PRIV_PASSWORD", "snmp_priv_password")
7979

@@ -110,6 +110,9 @@ func TestEnvSecretsMissing(t *testing.T) {
110110

111111
sc := &SafeConfig{}
112112
err := sc.ReloadConfig(nopLogger, []string{"testdata/snmp-auth-envvars.yml"}, true)
113+
if err == nil {
114+
t.Fatal("no error despite missing env var")
115+
}
113116
if err != nil {
114117
// we check the error message pattern to determine the error
115118
if strings.Contains(err.Error(), "environment variable not found") {

testdata/snmp-auth-envvars.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ auths:
22
with_secret:
33
community: mysecret
44
security_level: SomethingReadOnly
5-
username: ${ENV_USERNAME}
5+
username: snmp_${ENV_USERNAME}
66
password: ${ENV_PASSWORD}
77
auth_protocol: SHA256
88
priv_protocol: AES

0 commit comments

Comments
 (0)