Skip to content

Commit

Permalink
fix(inputs.mysql): Use tls=tlsid instead of tls=custom
Browse files Browse the repository at this point in the history
Before mysql.ParseDSN(), replace "tls=custom" by "tls={tlsid}" in the DSN string so it matches the TLS Config name that was created earlier.
In order to replace it, first find the last "/" (slash) symbol and split the string in two slices, the second one being the "query part". Then, on the query part, find and replace "tls=custom" by "tls={tlsid}".
It is important to first split the string by the last "/" so if a username or password are "tls=custom", they are not replaced.
  • Loading branch information
asayans authored Jun 12, 2024
1 parent d097a91 commit 6c29066
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions plugins/inputs/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ func (m *Mysql) Init() error {
}
dsn := dsnSecret.String()
dsnSecret.Destroy()
// We need to replace "tls=custom" in the DSN to use the tlsid generated instead.
// If not replaced, mysql.ParseDSN would look for a TLS Config named "custom" and
// would crash as the TLS Config needs to be present before parsing the DSN.
lastSlashIndex := strings.LastIndex(dsn, "/")
if lastSlashIndex != -1 {
dsnBase := dsn[:lastSlashIndex]
dsnQuery := dsn[lastSlashIndex:]
// Replace "tls=custom" with "tls={tlsid}" in the query part
dsnQuery = strings.Replace(dsnQuery, "tls=custom", "tls="+tlsid, 1)
// Join the parts back together
dsn = dsnBase + dsnQuery
}
conf, err := mysql.ParseDSN(dsn)
if err != nil {
return fmt.Errorf("parsing %q failed: %w", dsn, err)
Expand Down

0 comments on commit 6c29066

Please sign in to comment.