1
0
mirror of https://github.com/prometheus/mysqld_exporter.git synced 2025-07-30 06:43:05 +03:00

Add MySQL TLS configurations (#718)

This PR is a modified version of the #674 to match the FormDSN supported by the #708.

Fixes: https://github.com/prometheus/mysqld_exporter/issues/673

Signed-off-by: Nico Braun <rainbowstack@gmail.com>
Signed-off-by: Yasushi MIYAZAKI <MIYAZAKI.Yasushi@gmail.com>
Co-authored-by: Nico Braun <rainbowstack@gmail.com>
This commit is contained in:
Yasushi MIYAZAKI
2023-03-16 17:23:43 +09:00
committed by GitHub
parent c0d5150878
commit ac1c2d604f
3 changed files with 109 additions and 8 deletions

View File

@ -149,12 +149,12 @@ func TestFormDSN(t *testing.T) {
)
convey.Convey("Host exporter dsn", t, func() {
if err := c.ReloadConfig("testdata/client.cnf", "localhost:3306", "", true, log.NewNopLogger()); err != nil {
if err := c.ReloadConfig("testdata/client.cnf", "localhost:3306", "", false, log.NewNopLogger()); err != nil {
t.Error(err)
}
convey.Convey("Default Client", func() {
cfg := c.GetConfig()
section, _ := cfg.Sections["client"]
section := cfg.Sections["client"]
if dsn, err = section.FormDSN(""); err != nil {
t.Error(err)
}
@ -162,7 +162,7 @@ func TestFormDSN(t *testing.T) {
})
convey.Convey("Target specific with explicit port", func() {
cfg := c.GetConfig()
section, _ := cfg.Sections["client.server1"]
section := cfg.Sections["client.server1"]
if dsn, err = section.FormDSN("server1:5000"); err != nil {
t.Error(err)
}
@ -170,3 +170,78 @@ func TestFormDSN(t *testing.T) {
})
})
}
func TestFormDSNWithSslSkipVerify(t *testing.T) {
var (
c = MySqlConfigHandler{
Config: &Config{},
}
err error
dsn string
)
convey.Convey("Host exporter dsn with tls skip verify", t, func() {
if err := c.ReloadConfig("testdata/client.cnf", "localhost:3306", "", true, log.NewNopLogger()); err != nil {
t.Error(err)
}
convey.Convey("Default Client", func() {
cfg := c.GetConfig()
section := cfg.Sections["client"]
if dsn, err = section.FormDSN(""); err != nil {
t.Error(err)
}
convey.So(dsn, convey.ShouldEqual, "root:abc@tcp(server2:3306)/?tls=skip-verify")
})
convey.Convey("Target specific with explicit port", func() {
cfg := c.GetConfig()
section := cfg.Sections["client.server1"]
if dsn, err = section.FormDSN("server1:5000"); err != nil {
t.Error(err)
}
convey.So(dsn, convey.ShouldEqual, "test:foo@tcp(server1:5000)/?tls=skip-verify")
})
})
}
func TestFormDSNWithCustomTls(t *testing.T) {
var (
c = MySqlConfigHandler{
Config: &Config{},
}
err error
dsn string
)
convey.Convey("Host exporter dsn with custom tls", t, func() {
if err := c.ReloadConfig("testdata/client_custom_tls.cnf", "localhost:3306", "", false, log.NewNopLogger()); err != nil {
t.Error(err)
}
convey.Convey("Target tls enabled", func() {
cfg := c.GetConfig()
section := cfg.Sections["client_tls_true"]
if dsn, err = section.FormDSN(""); err != nil {
t.Error(err)
}
convey.So(dsn, convey.ShouldEqual, "usr:pwd@tcp(server2:3306)/?tls=true")
})
convey.Convey("Target tls preferred", func() {
cfg := c.GetConfig()
section := cfg.Sections["client_tls_preferred"]
if dsn, err = section.FormDSN(""); err != nil {
t.Error(err)
}
convey.So(dsn, convey.ShouldEqual, "usr:pwd@tcp(server3:3306)/?tls=preferred")
})
convey.Convey("Target tls skip-verify", func() {
cfg := c.GetConfig()
section := cfg.Sections["client_tls_skip_verify"]
if dsn, err = section.FormDSN(""); err != nil {
t.Error(err)
}
convey.So(dsn, convey.ShouldEqual, "usr:pwd@tcp(server3:3306)/?tls=skip-verify")
})
})
}