Skip to content

Commit

Permalink
Hide passwords in error logs (#205)
Browse files Browse the repository at this point in the history
* Hide postgres login:password string in error log

* Hide login:password string in error log of mysql, rabbitmq and redis

* Go format code

* Improve regular expressions to hide passwords
  • Loading branch information
mossroy authored Jan 23, 2024
1 parent 7ed908e commit 165dd26
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
5 changes: 4 additions & 1 deletion checker/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ import (
"wait4x.dev/v2/checker"
// Needed for the MySQL driver
_ "github.com/go-sql-driver/mysql"
"regexp"
)

var hidePasswordRegexp = regexp.MustCompile(`^([^:]+):[^:@]+@`)

// MySQL represents MySQL checker
type MySQL struct {
dsn string
Expand Down Expand Up @@ -66,7 +69,7 @@ func (m *MySQL) Check(ctx context.Context) (err error) {
if checker.IsConnectionRefused(err) {
return checker.NewExpectedError(
"failed to establish a connection to the mysql server", err,
"dsn", m.dsn,
"dsn", hidePasswordRegexp.ReplaceAllString(m.dsn, `$1:***@`),
)
}

Expand Down
5 changes: 4 additions & 1 deletion checker/postgresql/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ import (
"wait4x.dev/v2/checker"
// Needed for the PostgreSQL driver
_ "github.com/lib/pq"
"regexp"
)

var hidePasswordRegexp = regexp.MustCompile(`^(postgres://[^/:]+):[^:@]+@`)

// PostgreSQL represents PostgreSQL checker
type PostgreSQL struct {
dsn string
Expand Down Expand Up @@ -66,7 +69,7 @@ func (p *PostgreSQL) Check(ctx context.Context) (err error) {
if checker.IsConnectionRefused(err) {
return checker.NewExpectedError(
"failed to establish a connection to the postgresql server", err,
"dsn", p.dsn,
"dsn", hidePasswordRegexp.ReplaceAllString(p.dsn, `$1:***@`),
)
}

Expand Down
5 changes: 4 additions & 1 deletion checker/rabbitmq/rabbitmq.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ import (
"fmt"
"github.com/streadway/amqp"
"net"
"regexp"
"time"
"wait4x.dev/v2/checker"
)

var hidePasswordRegexp = regexp.MustCompile(`(amqp://[^/:]+):[^:@]+@`)

// Option configures a RabbitMQ.
type Option func(r *RabbitMQ)

Expand Down Expand Up @@ -118,7 +121,7 @@ func (r *RabbitMQ) Check(ctx context.Context) (err error) {
if checker.IsConnectionRefused(err) {
return checker.NewExpectedError(
"failed to establish a connection to the rabbitmq server", err,
"dsn", r.dsn,
"dsn", hidePasswordRegexp.ReplaceAllString(r.dsn, `$1:***@`),
)
}

Expand Down
4 changes: 3 additions & 1 deletion checker/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"wait4x.dev/v2/checker"
)

var hidePasswordRegexp = regexp.MustCompile(`([^/]+//[^/:]+):[^:@]+@`)

// Option configures a Redis.
type Option func(r *Redis)

Expand Down Expand Up @@ -94,7 +96,7 @@ func (r *Redis) Check(ctx context.Context) error {
if checker.IsConnectionRefused(err) {
return checker.NewExpectedError(
"failed to establish a connection to the redis server", err,
"dsn", r.address,
"dsn", hidePasswordRegexp.ReplaceAllString(r.address, `$1:***@`),
)
}

Expand Down

0 comments on commit 165dd26

Please sign in to comment.