Skip to content

Commit 0f99e4f

Browse files
Added feature to get password from Vault
To use the feature we need to set the following options in config section 'client': use_vault = true vault_address = http://<IP_ADDRESS:<IP_PORT> vault_role_id = <ROLE_ID> vault_secret_id = <SECRET_ID> vault_secret_path = <deployments/<UNIT>/dev/user/passwords_yml> vault_secret_mount_path = <secret_v2> credential_name_in_vault_secret = <prometheus_mysql_exporter_database_password> prometheus#883 Signed-off-by: Mitya_Eremeev <[email protected]>
1 parent 04268cc commit 0f99e4f

File tree

4 files changed

+100
-6
lines changed

4 files changed

+100
-6
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@
99
.idea
1010
*.iml
1111
/vendor
12+
13+
.my.cnf

config/config.go

+70-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package config
1515

1616
import (
17+
"context"
1718
"crypto/tls"
1819
"crypto/x509"
1920
"fmt"
@@ -27,6 +28,9 @@ import (
2728
"github.com/go-sql-driver/mysql"
2829
"github.com/prometheus/client_golang/prometheus"
2930

31+
"github.com/hashicorp/vault-client-go"
32+
"github.com/hashicorp/vault-client-go/schema"
33+
3034
"gopkg.in/ini.v1"
3135
)
3236

@@ -93,13 +97,9 @@ func (ch *MySqlConfigHandler) ReloadConfig(filename string, mysqldAddress string
9397
}
9498
}()
9599

96-
cfg, err := ini.LoadSources(
97-
opts,
98-
[]byte("[client]\npassword = ${MYSQLD_EXPORTER_PASSWORD}\n"),
99-
filename,
100-
)
100+
cfg, err := PutPasswordInConfig(filename, logger)
101101
if err != nil {
102-
return fmt.Errorf("failed to load config from %s: %w", filename, err)
102+
return fmt.Errorf("failed to put password in config file: %w", err)
103103
}
104104

105105
if host, port, err = net.SplitHostPort(mysqldAddress); err != nil {
@@ -234,3 +234,67 @@ func (m MySqlConfig) CustomizeTLS() error {
234234
mysql.RegisterTLSConfig("custom", &tlsCfg)
235235
return nil
236236
}
237+
238+
func PutPasswordInConfig(filename string, logger *slog.Logger) (cfg *ini.File, err error) {
239+
cfg, err = ini.LoadSources(opts, filename)
240+
if err != nil {
241+
return nil, fmt.Errorf("failed to load config file %s: %w", filename, err)
242+
}
243+
244+
clientSection := cfg.Section("client")
245+
if clientSection == nil {
246+
logger.Error("msg", "no section 'client' in config", "err", err)
247+
return nil, fmt.Errorf("error: %w", err)
248+
}
249+
useVault, err := clientSection.Key("use_vault").Bool()
250+
if err != nil {
251+
logger.Error("msg", "failed to get 'use_vault'", "err", err)
252+
return nil, fmt.Errorf("error: %w", err)
253+
}
254+
255+
password := "${MYSQLD_EXPORTER_PASSWORD}"
256+
if useVault {
257+
client, err := vault.New(vault.WithAddress(clientSection.Key("vault_address").String()))
258+
if err != nil {
259+
logger.Error("msg", "failed to create vault client", "err", err)
260+
return nil, fmt.Errorf("error: %w", err)
261+
}
262+
ctx := context.Background()
263+
resp, err := client.Auth.AppRoleLogin(
264+
ctx,
265+
schema.AppRoleLoginRequest{
266+
RoleId: clientSection.Key("vault_role_id").String(),
267+
SecretId: clientSection.Key("vault_secret_id").String(),
268+
},
269+
)
270+
if err != nil {
271+
logger.Error("msg", "failed to login to vault", "err", err)
272+
return nil, fmt.Errorf("error: %w", err)
273+
}
274+
if err := client.SetToken(resp.Auth.ClientToken); err != nil {
275+
logger.Error("msg", "failed to set vault token", "err", err)
276+
return nil, fmt.Errorf("error: %w", err)
277+
}
278+
data, err := client.Secrets.KvV2Read(
279+
ctx,
280+
clientSection.Key("vault_secret_path").String(),
281+
vault.WithMountPath(clientSection.Key("vault_secret_mount_path").String()),
282+
)
283+
if err != nil {
284+
logger.Error("msg", "failed to get data", "err", err)
285+
return nil, fmt.Errorf("error: %w", err)
286+
}
287+
288+
password = data.Data.Data[clientSection.Key("credential_name_in_vault_secret").String()].(string)
289+
}
290+
291+
cfg, err = ini.LoadSources(
292+
opts,
293+
[]byte("[client]\npassword = "+password+"\n"),
294+
filename,
295+
)
296+
if err != nil {
297+
return nil, fmt.Errorf("failed to load %s: %w", filename, err)
298+
}
299+
return cfg, nil
300+
}

go.mod

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/go-sql-driver/mysql v1.8.1
1010
github.com/google/go-cmp v0.6.0
1111
github.com/google/uuid v1.6.0
12+
github.com/hashicorp/vault-client-go v0.4.3
1213
github.com/prometheus/client_golang v1.20.4
1314
github.com/prometheus/client_model v0.6.1
1415
github.com/prometheus/common v0.60.0
@@ -24,14 +25,20 @@ require (
2425
github.com/cespare/xxhash/v2 v2.3.0 // indirect
2526
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
2627
github.com/gopherjs/gopherjs v1.17.2 // indirect
28+
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
29+
github.com/hashicorp/go-retryablehttp v0.7.1 // indirect
30+
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
31+
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 // indirect
2732
github.com/jpillora/backoff v1.0.0 // indirect
2833
github.com/jtolds/gls v4.20.0+incompatible // indirect
2934
github.com/klauspost/compress v1.17.9 // indirect
3035
github.com/mdlayher/socket v0.4.1 // indirect
3136
github.com/mdlayher/vsock v1.2.1 // indirect
37+
github.com/mitchellh/go-homedir v1.1.0 // indirect
3238
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
3339
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
3440
github.com/prometheus/procfs v0.15.1 // indirect
41+
github.com/ryanuber/go-glob v1.0.0 // indirect
3542
github.com/smarty/assertions v1.15.0 // indirect
3643
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
3744
golang.org/x/crypto v0.27.0 // indirect
@@ -40,6 +47,7 @@ require (
4047
golang.org/x/sync v0.8.0 // indirect
4148
golang.org/x/sys v0.25.0 // indirect
4249
golang.org/x/text v0.18.0 // indirect
50+
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af // indirect
4351
google.golang.org/protobuf v1.34.2 // indirect
4452
gopkg.in/yaml.v2 v2.4.0 // indirect
4553
)

go.sum

+20
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
2626
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
2727
github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g=
2828
github.com/gopherjs/gopherjs v1.17.2/go.mod h1:pRRIvn/QzFLrKfvEz3qUuEhtE/zLCWfreZ6J5gM2i+k=
29+
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
30+
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
31+
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
32+
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
33+
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
34+
github.com/hashicorp/go-retryablehttp v0.7.1 h1:sUiuQAnLlbvmExtFQs72iFW/HXeUn8Z1aJLQ4LJJbTQ=
35+
github.com/hashicorp/go-retryablehttp v0.7.1/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
36+
github.com/hashicorp/go-rootcerts v1.0.2 h1:jzhAVGtqPKbwpyCPELlgNWhE1znq+qwJtW5Oi2viEzc=
37+
github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8=
38+
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 h1:kes8mmyCpxJsI7FTwtzRqEy9CdjCtrXrXGuOpxEA7Ts=
39+
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2/go.mod h1:Gou2R9+il93BqX25LAKCLuM+y9U2T4hlwvT1yprcna4=
40+
github.com/hashicorp/vault-client-go v0.4.3 h1:zG7STGVgn/VK6rnZc0k8PGbfv2x/sJExRKHSUg3ljWc=
41+
github.com/hashicorp/vault-client-go v0.4.3/go.mod h1:4tDw7Uhq5XOxS1fO+oMtotHL7j4sB9cp0T7U6m4FzDY=
2942
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
3043
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
3144
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
@@ -43,6 +56,8 @@ github.com/mdlayher/socket v0.4.1 h1:eM9y2/jlbs1M615oshPQOHZzj6R6wMT7bX5NPiQvn2U
4356
github.com/mdlayher/socket v0.4.1/go.mod h1:cAqeGjoufqdxWkD7DkpyS+wcefOtmu5OQ8KuoJGIReA=
4457
github.com/mdlayher/vsock v1.2.1 h1:pC1mTJTvjo1r9n9fbm7S1j04rCgCzhCOS5DY0zqHlnQ=
4558
github.com/mdlayher/vsock v1.2.1/go.mod h1:NRfCibel++DgeMD8z/hP+PPTjlNJsdPOmxcnENvE+SE=
59+
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
60+
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
4661
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
4762
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
4863
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU=
@@ -61,11 +76,14 @@ github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0leargg
6176
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
6277
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
6378
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
79+
github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk=
80+
github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
6481
github.com/smarty/assertions v1.15.0 h1:cR//PqUBUiQRakZWqBiFFQ9wb8emQGDb0HeGdqGByCY=
6582
github.com/smarty/assertions v1.15.0/go.mod h1:yABtdzeQs6l1brC900WlRNwj6ZR55d7B+E8C6HtKdec=
6683
github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sSznIX1xY=
6784
github.com/smartystreets/goconvey v1.8.1/go.mod h1:+/u4qLyY6x1jReYOp7GOM2FSt8aP9CzCZL03bI28W60=
6885
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
86+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
6987
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
7088
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
7189
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
@@ -83,6 +101,8 @@ golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
83101
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
84102
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
85103
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
104+
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af h1:Yx9k8YCG3dvF87UAn2tu2HQLf2dt/eR1bXxpLMWeH+Y=
105+
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
86106
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
87107
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
88108
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

0 commit comments

Comments
 (0)