Skip to content

Commit ed0c8bd

Browse files
authored
Merge pull request #118 from mvadu/add-secret-support
add support for reading mqtt password from file
2 parents db6213e + 76aaeff commit ed0c8bd

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

Readme.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ Usage of ./mqtt2prometheus:
129129
show the builds version, date and commit
130130
-web-config-file string
131131
[EXPERIMENTAL] Path to configuration file that can enable TLS or authentication for metric scraping.
132+
-treat-mqtt-password-as-file-name bool (default: false)
133+
treat MQTT2PROM_MQTT_PASSWORD environment variable as a secret file path e.g. /var/run/secrets/mqtt-credential. Useful when docker secret or external credential management agents handle the secret file.
132134
```
133135
The logging is implemented via [zap](https://github.com/uber-go/zap). The logs are printed to `stderr` and valid log levels are
134136
those supported by zap.
@@ -267,6 +269,26 @@ Then load that file into the environment before starting the container:
267269
ghcr.io/hikhvar/mqtt2prometheus:latest
268270
```
269271

272+
#### Example use with Docker secret (in swarm)
273+
274+
Create a docker secret to store the password(`mqtt-credential` in the example below), and pass the optional `treat-mqtt-password-as-file-name` command line argument.
275+
```docker
276+
mqtt_exporter_tasmota:
277+
image: ghcr.io/hikhvar/mqtt2prometheus:latest
278+
secrets:
279+
- mqtt-credential
280+
environment:
281+
- MQTT2PROM_MQTT_USER=mqtt
282+
- MQTT2PROM_MQTT_PASSWORD=/var/run/secrets/mqtt-credential
283+
entrypoint:
284+
- /mqtt2prometheus
285+
- -log-level=debug
286+
- -treat-mqtt-password-as-file-name=true
287+
volumes:
288+
- config-tasmota.yml:/config.yaml:ro
289+
```
290+
291+
270292

271293
## Frequently Asked Questions
272294

cmd/mqtt2prometheus.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ var (
6464
"",
6565
"[EXPERIMENTAL] Path to configuration file that can enable TLS or authentication for metric scraping.",
6666
)
67+
usePasswordFromFile = flag.Bool(
68+
"treat-mqtt-password-as-file-name",
69+
false,
70+
"treat MQTT2PROM_MQTT_PASSWORD as a secret file path e.g. /var/run/secrets/mqtt-credential",
71+
)
6772
)
6873

6974
func main() {
@@ -81,13 +86,24 @@ func main() {
8186
}
8287

8388
mqtt_user := os.Getenv("MQTT2PROM_MQTT_USER")
84-
mqtt_password := os.Getenv("MQTT2PROM_MQTT_PASSWORD")
85-
8689
if mqtt_user != "" {
8790
cfg.MQTT.User = mqtt_user
8891
}
89-
if mqtt_password != "" {
90-
cfg.MQTT.Password = mqtt_password
92+
93+
mqtt_password := os.Getenv("MQTT2PROM_MQTT_PASSWORD")
94+
if *usePasswordFromFile {
95+
if mqtt_password == "" {
96+
logger.Fatal("MQTT2PROM_MQTT_PASSWORD is required")
97+
}
98+
secret, err := ioutil.ReadFile(mqtt_password)
99+
if err != nil {
100+
logger.Fatal("unable to read mqtt password from secret file", zap.Error(err))
101+
}
102+
cfg.MQTT.Password = string(secret)
103+
} else {
104+
if mqtt_password != "" {
105+
cfg.MQTT.Password = mqtt_password
106+
}
91107
}
92108

93109
mqttClientOptions := mqtt.NewClientOptions()

0 commit comments

Comments
 (0)