Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

fix(mysql): hide password when logging DSN #903

Merged
merged 3 commits into from
Oct 6, 2022
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
4 changes: 2 additions & 2 deletions modules/mysql/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (m *MySQL) collect() (map[string]int64, error) {
func (m *MySQL) openConnection() error {
db, err := sql.Open("mysql", m.DSN)
if err != nil {
return fmt.Errorf("error on opening a connection with the mysql database [%s]: %v", m.DSN, err)
return fmt.Errorf("error on opening a connection with the mysql database [%s]: %v", m.safeDSN, err)
}

db.SetConnMaxLifetime(10 * time.Minute)
Expand All @@ -100,7 +100,7 @@ func (m *MySQL) openConnection() error {

if err := db.PingContext(ctx); err != nil {
_ = db.Close()
return fmt.Errorf("error on pinging the mysql database [%s]: %v", m.DSN, err)
return fmt.Errorf("error on pinging the mysql database [%s]: %v", m.safeDSN, err)
}

m.db = db
Expand Down
14 changes: 13 additions & 1 deletion modules/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ package mysql

import (
"database/sql"
"strings"
"sync"
"time"

"github.com/blang/semver/v4"
"github.com/go-sql-driver/mysql"
_ "github.com/go-sql-driver/mysql"

"github.com/netdata/go.d.plugin/agent/module"
Expand Down Expand Up @@ -55,6 +57,7 @@ type MySQL struct {
Config `yaml:",inline"`

db *sql.DB
safeDSN string
version *semver.Version
isMariaDB bool
isPercona bool
Expand Down Expand Up @@ -96,6 +99,15 @@ func (m *MySQL) Init() bool {
return false
}

cfg, err := mysql.ParseDSN(m.DSN)
if err != nil {
m.Errorf("error on parsing DSN: %v", err)
return false
}

cfg.Passwd = strings.Repeat("*", len(cfg.Passwd))
m.safeDSN = cfg.FormatDSN()

m.Debugf("using DSN [%s]", m.DSN)
return true
}
Expand Down Expand Up @@ -125,7 +137,7 @@ func (m *MySQL) Cleanup() {
return
}
if err := m.db.Close(); err != nil {
m.Errorf("cleanup: error on closing the mysql database [%s]: %v", m.DSN, err)
m.Errorf("cleanup: error on closing the mysql database [%s]: %v", m.safeDSN, err)
}
m.db = nil
}