Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MySQL TLS configurations #718

Merged
merged 1 commit into from
Mar 16, 2023
Merged
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
18 changes: 13 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type MySqlConfig struct {
SslCert string `ini:"ssl-cert"`
SslKey string `ini:"ssl-key"`
TlsInsecureSkipVerify bool `ini:"ssl-skip-verfication"`
Tls string `ini:"tls"`
}

type MySqlConfigHandler struct {
Expand Down Expand Up @@ -132,6 +133,8 @@ func (ch *MySqlConfigHandler) ReloadConfig(filename string, mysqldAddress string
mysqlcfg := &MySqlConfig{
TlsInsecureSkipVerify: tlsInsecureSkipVerify,
}

// FIXME: this error check seems orphaned
if err != nil {
level.Error(logger).Log("msg", "failed to load config", "section", sectionName, "err", err)
continue
Expand Down Expand Up @@ -197,12 +200,17 @@ func (m MySqlConfig) FormDSN(target string) (string, error) {
config.Addr = target
}

if m.SslCa != "" {
if err := m.CustomizeTLS(); err != nil {
err = fmt.Errorf("failed to register a custom TLS configuration for mysql dsn: %w", err)
return "", err
if m.TlsInsecureSkipVerify {
config.TLSConfig = "skip-verify"
} else {
config.TLSConfig = m.Tls
if m.SslCa != "" {
if err := m.CustomizeTLS(); err != nil {
err = fmt.Errorf("failed to register a custom TLS configuration for mysql dsn: %w", err)
return "", err
}
config.TLSConfig = "custom"
}
config.TLSConfig = "custom"
}

return config.FormatDSN(), nil
Expand Down
81 changes: 78 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,24 +149,99 @@ 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)
}
convey.So(dsn, convey.ShouldEqual, "root:abc@tcp(server2:3306)/")
})
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)
}
convey.So(dsn, convey.ShouldEqual, "test:foo@tcp(server1:5000)/")
})
})
}

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")
})

})
}
18 changes: 18 additions & 0 deletions config/testdata/client_custom_tls.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[client_tls_true]
host = server2
port = 3306
user = usr
password = pwd
tls=true
[client_tls_preferred]
host = server3
port = 3306
user = usr
password = pwd
tls=preferred
[client_tls_skip_verify]
host = server3
port = 3306
user = usr
password = pwd
tls=skip-verify