Skip to content

Commit aa59d8f

Browse files
committed
Warn if configuration file not found, fail if missing auths or modules
Fixes #1456 Signed-off-by: Brian Candler <[email protected]>
1 parent 2fc7f4f commit aa59d8f

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

config/config.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package config
1616
import (
1717
"errors"
1818
"fmt"
19+
"log/slog"
1920
"os"
2021
"path/filepath"
2122
"regexp"
@@ -25,13 +26,16 @@ import (
2526
"gopkg.in/yaml.v2"
2627
)
2728

28-
func LoadFile(paths []string, expandEnvVars bool) (*Config, error) {
29+
func LoadFile(paths []string, expandEnvVars bool, validate bool, logger *slog.Logger) (*Config, error) {
2930
cfg := &Config{}
3031
for _, p := range paths {
3132
files, err := filepath.Glob(p)
3233
if err != nil {
3334
return nil, err
3435
}
36+
if validate && len(files) == 0 && logger != nil {
37+
logger.Warn("No file found matching pattern", "file", p)
38+
}
3539
for _, f := range files {
3640
content, err := os.ReadFile(f)
3741
if err != nil {
@@ -44,6 +48,13 @@ func LoadFile(paths []string, expandEnvVars bool) (*Config, error) {
4448
}
4549
}
4650

51+
if validate && len(cfg.Auths) == 0 {
52+
return nil, errors.New("No auths provided")
53+
}
54+
if validate && len(cfg.Modules) == 0 {
55+
return nil, errors.New("No modules provided")
56+
}
57+
4758
if expandEnvVars {
4859
var err error
4960
for i, auth := range cfg.Auths {

config_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
func TestHideConfigSecrets(t *testing.T) {
2424
sc := &SafeConfig{}
25-
err := sc.ReloadConfig([]string{"testdata/snmp-auth.yml"}, false)
25+
err := sc.ReloadConfig([]string{"testdata/snmp-auth.yml"}, false, false, nil)
2626
if err != nil {
2727
t.Errorf("Error loading config %v: %v", "testdata/snmp-auth.yml", err)
2828
}
@@ -41,7 +41,7 @@ func TestHideConfigSecrets(t *testing.T) {
4141

4242
func TestLoadConfigWithOverrides(t *testing.T) {
4343
sc := &SafeConfig{}
44-
err := sc.ReloadConfig([]string{"testdata/snmp-with-overrides.yml"}, false)
44+
err := sc.ReloadConfig([]string{"testdata/snmp-with-overrides.yml"}, false, false, nil)
4545
if err != nil {
4646
t.Errorf("Error loading config %v: %v", "testdata/snmp-with-overrides.yml", err)
4747
}
@@ -56,7 +56,7 @@ func TestLoadConfigWithOverrides(t *testing.T) {
5656
func TestLoadMultipleConfigs(t *testing.T) {
5757
sc := &SafeConfig{}
5858
configs := []string{"testdata/snmp-auth.yml", "testdata/snmp-with-overrides.yml"}
59-
err := sc.ReloadConfig(configs, false)
59+
err := sc.ReloadConfig(configs, false, false, nil)
6060
if err != nil {
6161
t.Errorf("Error loading configs %v: %v", configs, err)
6262
}
@@ -75,7 +75,7 @@ func TestEnvSecrets(t *testing.T) {
7575
t.Setenv("ENV_PRIV_PASSWORD", "snmp_priv_password")
7676

7777
sc := &SafeConfig{}
78-
err := sc.ReloadConfig([]string{"testdata/snmp-auth-envvars.yml"}, true)
78+
err := sc.ReloadConfig([]string{"testdata/snmp-auth-envvars.yml"}, true, false, nil)
7979
if err != nil {
8080
t.Errorf("Error loading config %v: %v", "testdata/snmp-auth-envvars.yml", err)
8181
}
@@ -106,7 +106,7 @@ func TestEnvSecretsMissing(t *testing.T) {
106106
t.Setenv("ENV_PRIV_PASSWORD", "snmp_priv_password")
107107

108108
sc := &SafeConfig{}
109-
err := sc.ReloadConfig([]string{"testdata/snmp-auth-envvars.yml"}, true)
109+
err := sc.ReloadConfig([]string{"testdata/snmp-auth-envvars.yml"}, true, false, nil)
110110
if err != nil {
111111
// we check the error message pattern to determine the error
112112
if strings.Contains(err.Error(), "environment variable not found") {
@@ -120,7 +120,7 @@ func TestEnvSecretsMissing(t *testing.T) {
120120
// When SNMPv2 was specified without credentials
121121
func TestEnvSecretsNotSpecified(t *testing.T) {
122122
sc := &SafeConfig{}
123-
err := sc.ReloadConfig([]string{"testdata/snmp-auth-v2nocreds.yml"}, true)
123+
err := sc.ReloadConfig([]string{"testdata/snmp-auth-v2nocreds.yml"}, true, false, nil)
124124
if err != nil {
125125
t.Errorf("Error loading config %v: %v", "testdata/snmp-auth-v2nocreds.yml", err)
126126
}

main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ type SafeConfig struct {
182182
C *config.Config
183183
}
184184

185-
func (sc *SafeConfig) ReloadConfig(configFile []string, expandEnvVars bool) (err error) {
186-
conf, err := config.LoadFile(configFile, expandEnvVars)
185+
func (sc *SafeConfig) ReloadConfig(configFile []string, expandEnvVars bool, validate bool, logger *slog.Logger) (err error) {
186+
conf, err := config.LoadFile(configFile, expandEnvVars, validate, logger)
187187
if err != nil {
188188
return err
189189
}
@@ -214,7 +214,7 @@ func main() {
214214
prometheus.MustRegister(versioncollector.NewCollector("snmp_exporter"))
215215

216216
// Bail early if the config is bad.
217-
err := sc.ReloadConfig(*configFile, *expandEnvVars)
217+
err := sc.ReloadConfig(*configFile, *expandEnvVars, true, logger)
218218
if err != nil {
219219
logger.Error("Error parsing config file", "err", err)
220220
logger.Error("Possible version missmatch between generator and snmp_exporter. Make sure generator and snmp_exporter are the same version.")
@@ -235,13 +235,13 @@ func main() {
235235
for {
236236
select {
237237
case <-hup:
238-
if err := sc.ReloadConfig(*configFile, *expandEnvVars); err != nil {
238+
if err := sc.ReloadConfig(*configFile, *expandEnvVars, true, logger); err != nil {
239239
logger.Error("Error reloading config", "err", err)
240240
} else {
241241
logger.Info("Loaded config file")
242242
}
243243
case rc := <-reloadCh:
244-
if err := sc.ReloadConfig(*configFile, *expandEnvVars); err != nil {
244+
if err := sc.ReloadConfig(*configFile, *expandEnvVars, true, logger); err != nil {
245245
logger.Error("Error reloading config", "err", err)
246246
rc <- err
247247
} else {

0 commit comments

Comments
 (0)