Skip to content

Commit 33b5df2

Browse files
arvenilSuperQ
authored andcommitted
Ignore boolean keys in my.cnf. (#283)
Below is valid MySQL config however mysqld_exporter fails to read it, because of the boolean keys. Config: ``` [client] user = root password = abc123 [mysql] skip-auto-rehash ``` Signed-off-by: Kamil Dziedzic <[email protected]>
1 parent d560e62 commit 33b5df2

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

mysqld_exporter.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ var scrapers = map[collector.Scraper]bool{
6565

6666
func parseMycnf(config interface{}) (string, error) {
6767
var dsn string
68-
cfg, err := ini.Load(config)
68+
opts := ini.LoadOptions{
69+
// MySQL ini file can have boolean keys.
70+
AllowBooleanKeys: true,
71+
}
72+
cfg, err := ini.LoadSources(opts, config)
6973
if err != nil {
7074
return dsn, fmt.Errorf("failed reading ini file: %s", err)
7175
}

mysqld_exporter_test.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ func TestParseMycnf(t *testing.T) {
5454
host = 1.2.3.4
5555
port = 3307
5656
`
57+
ignoreBooleanKeys = `
58+
[client]
59+
user = root
60+
password = abc123
61+
62+
[mysql]
63+
skip-auto-rehash
64+
`
5765
badConfig = `
5866
[client]
5967
user = root
@@ -67,10 +75,7 @@ func TestParseMycnf(t *testing.T) {
6775
[hello]
6876
world = ismine
6977
`
70-
badConfig4 = `
71-
[hello]
72-
world
73-
`
78+
badConfig4 = `[hello`
7479
)
7580
convey.Convey("Various .my.cnf configurations", t, func() {
7681
convey.Convey("Local tcp connection", func() {
@@ -93,21 +98,25 @@ func TestParseMycnf(t *testing.T) {
9398
dsn, _ := parseMycnf([]byte(remoteConfig))
9499
convey.So(dsn, convey.ShouldEqual, "dude:nopassword@tcp(1.2.3.4:3307)/")
95100
})
101+
convey.Convey("Ignore boolean keys", func() {
102+
dsn, _ := parseMycnf([]byte(ignoreBooleanKeys))
103+
convey.So(dsn, convey.ShouldEqual, "root:abc123@tcp(localhost:3306)/")
104+
})
96105
convey.Convey("Missed user", func() {
97106
_, err := parseMycnf([]byte(badConfig))
98-
convey.So(err, convey.ShouldNotBeNil)
107+
convey.So(err, convey.ShouldBeError, fmt.Errorf("no user or password specified under [client] in %s", badConfig))
99108
})
100109
convey.Convey("Missed password", func() {
101110
_, err := parseMycnf([]byte(badConfig2))
102-
convey.So(err, convey.ShouldNotBeNil)
111+
convey.So(err, convey.ShouldBeError, fmt.Errorf("no user or password specified under [client] in %s", badConfig2))
103112
})
104113
convey.Convey("No [client] section", func() {
105114
_, err := parseMycnf([]byte(badConfig3))
106-
convey.So(err, convey.ShouldNotBeNil)
115+
convey.So(err, convey.ShouldBeError, fmt.Errorf("no user or password specified under [client] in %s", badConfig3))
107116
})
108117
convey.Convey("Invalid config", func() {
109118
_, err := parseMycnf([]byte(badConfig4))
110-
convey.So(err, convey.ShouldNotBeNil)
119+
convey.So(err, convey.ShouldBeError, fmt.Errorf("failed reading ini file: unclosed section: %s", badConfig4))
111120
})
112121
})
113122
}

0 commit comments

Comments
 (0)