diff --git a/README.md b/README.md index be148ac..24e0000 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ services: - server_port=9617 - interval=10s - log_limit=10000 + - password_from_file=true ``` ### Swarm mode (docker swarm init) @@ -159,6 +160,7 @@ services: - server_port=9617 - interval=10s - log_limit=10000 + - password_from_file=true ``` ## Usage diff --git a/config/configuration.go b/config/configuration.go index 96855e2..e8137b0 100644 --- a/config/configuration.go +++ b/config/configuration.go @@ -3,6 +3,7 @@ package config import ( "context" "fmt" + "io/ioutil" "log" "os" "reflect" @@ -16,28 +17,30 @@ import ( // Config is the exporter CLI configuration. type Config struct { - AdguardProtocol string `config:"adguard_protocol"` - AdguardHostname string `config:"adguard_hostname"` - AdguardUsername string `config:"adguard_username"` - AdguardPassword string `config:"adguard_password"` - AdguardPort string `config:"adguard_port"` - ServerPort string `config:"server_port"` - Interval time.Duration `config:"interval"` - LogLimit string `config:"log_limit"` - RDnsEnabled bool `config:"rdns_enabled"` + AdguardProtocol string `config:"adguard_protocol"` + AdguardHostname string `config:"adguard_hostname"` + AdguardUsername string `config:"adguard_username"` + AdguardPassword string `config:"adguard_password"` + AdguardPort string `config:"adguard_port"` + ServerPort string `config:"server_port"` + Interval time.Duration `config:"interval"` + LogLimit string `config:"log_limit"` + RDnsEnabled bool `config:"rdns_enabled"` + PasswordFromFile bool `config:"password_from_file"` } func getDefaultConfig() *Config { return &Config{ - AdguardProtocol: "http", - AdguardHostname: "127.0.0.1", - AdguardUsername: "", - AdguardPassword: "", - AdguardPort: "80", - ServerPort: "9617", - Interval: 10 * time.Second, - LogLimit: "1000", - RDnsEnabled: true, + AdguardProtocol: "http", + AdguardHostname: "127.0.0.1", + AdguardUsername: "", + AdguardPassword: "", + AdguardPort: "80", + ServerPort: "9617", + Interval: 10 * time.Second, + LogLimit: "1000", + RDnsEnabled: true, + PasswordFromFile: false, } } @@ -69,6 +72,17 @@ func Load() *Config { } } + //Set the adguard password based on the input configuration + if cfg.PasswordFromFile { + secret, err := ioutil.ReadFile(cfg.AdguardPassword) + if err != nil { + log.Printf("unable to read AdguardPassword from %s due to %s", cfg.AdguardPassword, err) + os.Exit(1) + } + + cfg.AdguardPassword = string(secret) + } + cfg.show() return cfg