-
-
Notifications
You must be signed in to change notification settings - Fork 97
feat: add TLS config for MySQL driver #907
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
Merged
+185
−0
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
75cab2b
feat: add TLS config for MySQL driver
burningalchemist aef4c8c
fix: add explicitly minimal TLS version
burningalchemist cf466e8
fix: strip tls params after tls config init
burningalchemist b7d3168
chore: cleanup
burningalchemist 5fd7b88
fix: register TLSConfig once
burningalchemist f4a7b1f
fix: minor fixes
burningalchemist 64eaa16
refactor: change tag to nomysql
burningalchemist 55e0802
build: add build-nomysql task
burningalchemist 3bb7264
refactor: naming, improve logs, add comments
burningalchemist caa5acc
fix: support multiple tls configs for jobs mode
burningalchemist 3d8e175
Merge branch 'master' into fix/mysql-tls
burningalchemist b56ffc0
fix: update func comments, formatting
burningalchemist ce7f1bc
docs: add MySQL Custom TLS section
burningalchemist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| //go:build !nomysql | ||
|
|
||
| package sql_exporter | ||
|
|
||
| import ( | ||
| "crypto/tls" | ||
| "crypto/x509" | ||
| "errors" | ||
| "fmt" | ||
| "log/slog" | ||
| "net/url" | ||
| "os" | ||
| "sync" | ||
|
|
||
| "github.com/go-sql-driver/mysql" | ||
| ) | ||
|
|
||
| const ( | ||
| mysqlTLSParamCACert = "tls-ca" | ||
| mysqlTLSParamClientCert = "tls-cert" | ||
| mysqlTLSParamClientKey = "tls-key" | ||
| ) | ||
|
|
||
| // mysqlTLSParams is a list of TLS parameters that can be used in MySQL DSNs. It is used to identify and strip TLS | ||
| // parameters from the DSN after registering the TLS configuration, as these parameters are not recognized by the MySQL | ||
| // driver and would cause connection failure if left in the DSN. | ||
| var ( | ||
| mysqlTLSParams = []string{mysqlTLSParamCACert, mysqlTLSParamClientCert, mysqlTLSParamClientKey} | ||
|
|
||
| onceMap sync.Map | ||
| ) | ||
|
|
||
| // handleMySQLTLSConfig wraps the registration of a MySQL TLS configuration in a thread-safe manner. It uses a | ||
| // sync.Once to ensure that the TLS configuration for a given config name is registered only once, even if multiple | ||
| // goroutines attempt to register it concurrently. | ||
| func handleMySQLTLSConfig(configName string, params url.Values) error { | ||
| onceConn, _ := onceMap.LoadOrStore(configName, &sync.Once{}) | ||
| once := onceConn.(*sync.Once) | ||
| var err error | ||
| once.Do(func() { | ||
| err = registerMySQLTLSConfig(configName, params) | ||
| if err != nil { | ||
| slog.Error("Failed to register MySQL TLS config", "error", err) | ||
| } | ||
| }) | ||
| return err | ||
| } | ||
|
|
||
| // registerMySQLTLSConfig registers a custom TLS configuration for MySQL with the given config name and parameters. | ||
| func registerMySQLTLSConfig(configName string, params url.Values) error { | ||
| caCert := params.Get(mysqlTLSParamCACert) | ||
| clientCert := params.Get(mysqlTLSParamClientCert) | ||
| clientKey := params.Get(mysqlTLSParamClientKey) | ||
|
|
||
| slog.Debug("MySQL TLS config", "configName", configName, mysqlTLSParamCACert, caCert, | ||
| mysqlTLSParamClientCert, clientCert, mysqlTLSParamClientKey, clientKey) | ||
|
|
||
| var rootCertPool *x509.CertPool | ||
| if caCert != "" { | ||
| rootCertPool = x509.NewCertPool() | ||
| pem, err := os.ReadFile(caCert) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read CA certificate: %w", err) | ||
| } | ||
| if ok := rootCertPool.AppendCertsFromPEM(pem); !ok { | ||
| return errors.New("failed to append PEM") | ||
| } | ||
| } | ||
|
|
||
| var certs []tls.Certificate | ||
| if clientCert != "" || clientKey != "" { | ||
| if clientCert == "" || clientKey == "" { | ||
| return errors.New("both tls-cert and tls-key must be provided for client authentication") | ||
| } | ||
| cert, err := tls.LoadX509KeyPair(clientCert, clientKey) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to load client certificate and key: %w", err) | ||
| } | ||
| certs = append(certs, cert) | ||
| } | ||
|
|
||
| tlsConfig := &tls.Config{ | ||
| RootCAs: rootCertPool, | ||
| Certificates: certs, | ||
| MinVersion: tls.VersionTLS12, | ||
| } | ||
|
|
||
| return mysql.RegisterTLSConfig(configName, tlsConfig) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| //go:build nomysql | ||
|
|
||
| package sql_exporter | ||
|
|
||
| import ( | ||
| "errors" | ||
| "net/url" | ||
| ) | ||
|
|
||
| // There are no TLS parameters to strip when MySQL support is disabled, but we need to define the variable to avoid compilation errors in sql.go. | ||
| var mysqlTLSParams = []string{} | ||
|
|
||
| // registerMySQLTLSConfig is a stub function that returns an error indicating that MySQL TLS support is disabled when the "nomysql" build tag is used. | ||
| func handleMySQLTLSConfig(_ url.Values) error { | ||
| return errors.New("MySQL TLS support disabled (built with -tags nomysql)") | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.