Read credentials from env variable if not set

Allows for secrets to be provided to exporter outside of main
configuration. E.G. via Kubernetes Secrets.
Specified configuration files or flags take precedence over environment
variables.
This commit is contained in:
Ben Reedy 2020-05-27 20:59:15 +10:00
parent d3285ba301
commit c8a29f8423
No known key found for this signature in database
GPG Key ID: 235C15B6086C9D7E
2 changed files with 15 additions and 0 deletions

View File

@ -35,6 +35,14 @@ where `address` is the address of your router. `device` is the label name for th
in the metrics output to prometheus. The `user` and `password` are the ones you
created for the exporter to use to access the API.
User and password flags can be set with the `MIKROTIK_USER` and `MIKROTIK_PASSWORD` environment variables, respectively.
```
MIKROTIK_USER=prometheus
MIKROTIK_PASSWORD=changeme
./mikrotik-exporter -address 10.10.0.1 -device my_router
```
#### Config File
`./mikrotik-exporter -config-file config.yml`

View File

@ -115,6 +115,13 @@ func loadConfigFromFile() (*config.Config, error) {
}
func loadConfigFromFlags() (*config.Config, error) {
// Attempt to read credentials from env if not already defined
if *user == "" {
*user = os.Getenv("MIKROTIK_USER")
}
if *password == "" {
*password = os.Getenv("MIKROTIK_PASSWORD")
}
if *device == "" || *address == "" || *user == "" || *password == "" {
return nil, fmt.Errorf("missing required param for single device configuration")
}