add support for reading password from secret/file (#22)

* add support for reading password from secret/file

Signed-off-by: mvadu <8618235+mvadu@users.noreply.github.com>

* avoid trimming

Signed-off-by: mvadu <8618235+mvadu@users.noreply.github.com>
This commit is contained in:
Adarsha 2022-02-12 03:46:45 -05:00 committed by GitHub
parent e75108942e
commit 23838bf07e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 18 deletions

View File

@ -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

View File

@ -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