Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,22 @@ func (ch *MySqlConfigHandler) ReloadConfig(filename string, mysqldAddress string
}
}

cfg.ValueMapper = os.ExpandEnv
cfg.ValueMapper = func(val string) string {
i := 0
for i < len(val) && val[i] == '$' {
i++
}

if i == 0 {
return val
}

if i%2 == 0 {
return strings.Repeat("$", i/2) + val[i:]
}

return os.ExpandEnv(val[i-1:])
}
config := &Config{}
m := make(map[string]MySqlConfig)
for _, sec := range cfg.Sections() {
Expand Down
51 changes: 51 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,57 @@ func TestValidateConfig(t *testing.T) {
convey.So(section.Password, convey.ShouldEqual, "foo")
convey.So(section.EnableCleartextPlugin, convey.ShouldBeTrue)
})

convey.Convey("Expand variables", t, func() {
c := MySqlConfigHandler{
Config: &Config{},
}
os.Setenv("MYSQLD_EXPORTER_PASSWORD", "supersecretpassword")
if err := c.ReloadConfig("testdata/expand_variables.cnf", "localhost:3306", "", true, promslog.NewNopLogger()); err != nil {
t.Error(err)
}

cfg := c.GetConfig()
section := cfg.Sections["client.server1"]
convey.So(section.User, convey.ShouldEqual, "test")
convey.So(section.Password, convey.ShouldEqual, "foo")

section = cfg.Sections["client.env"]
convey.So(section.User, convey.ShouldEqual, "test2")
convey.So(section.Password, convey.ShouldEqual, "supersecretpassword")

section = cfg.Sections["client.notExpandEnv"]
convey.So(section.User, convey.ShouldEqual, "mysql_exporter")
convey.So(section.Password, convey.ShouldEqual, "SECRET_PA$SWORD")

section = cfg.Sections["client.envNotExists"]
convey.So(section.User, convey.ShouldEqual, "test")
convey.So(section.Password, convey.ShouldEqual, "")

section = cfg.Sections["client.envEscaped"]
convey.So(section.User, convey.ShouldEqual, "test")
convey.So(section.Password, convey.ShouldEqual, "$MYSQLD_EXPORTER_PASSWORD")

section = cfg.Sections["client.password$$Escaped"]
convey.So(section.User, convey.ShouldEqual, "test")
convey.So(section.Password, convey.ShouldEqual, "$$MYSQLD_EXPORTER_PASSWORD")

section = cfg.Sections["client.password$$$Env"]
convey.So(section.User, convey.ShouldEqual, "test")
convey.So(section.Password, convey.ShouldEqual, "supersecretpassword")

section = cfg.Sections["client.password$$$Escaped"]
convey.So(section.User, convey.ShouldEqual, "test")
convey.So(section.Password, convey.ShouldEqual, "$$$MYSQLD_EXPORTER_PASSWORD")

section = cfg.Sections["client.password$$$$Escaped"]
convey.So(section.User, convey.ShouldEqual, "test")
convey.So(section.Password, convey.ShouldEqual, "$$$$MYSQLD_EXPORTER_PASSWORD")

section = cfg.Sections["client.password$$$$$Env"]
convey.So(section.User, convey.ShouldEqual, "test")
convey.So(section.Password, convey.ShouldEqual, "supersecretpassword")
})
}

func TestFormDSN(t *testing.T) {
Expand Down
30 changes: 30 additions & 0 deletions config/testdata/expand_variables.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[client.server1]
user = test
password = foo
[client.env]
user = test2
password = $MYSQLD_EXPORTER_PASSWORD
[client.notExpandEnv]
user = mysql_exporter
password = SECRET_PA$SWORD
[client.envNotExists]
user = test
password = $ENV_NOT_EXISTS
[client.envEscaped]
user = test
password = $$MYSQLD_EXPORTER_PASSWORD
[client.password$$Escaped]
user = test
password = $$$$MYSQLD_EXPORTER_PASSWORD
[client.password$$$Env]
user = test
password = $$$MYSQLD_EXPORTER_PASSWORD
[client.password$$$Escaped]
user = test
password = $$$$$$MYSQLD_EXPORTER_PASSWORD
[client.password$$$$Escaped]
user = test
password = $$$$$$$$MYSQLD_EXPORTER_PASSWORD
[client.password$$$$$Env]
user = test
password = $$$$$MYSQLD_EXPORTER_PASSWORD