Skip to content

Commit

Permalink
Added function and tests to redact password from URL
Browse files Browse the repository at this point in the history
  • Loading branch information
xBlaz3kx committed Nov 26, 2024
1 parent cd74be0 commit 3983b96
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
10 changes: 9 additions & 1 deletion internal/connectionmanager/connection_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package connectionmanager
import (
"errors"
"fmt"
"net/url"
"sync"
"time"

Expand Down Expand Up @@ -42,12 +43,19 @@ func dial(log logger.Logger, resolver Resolver, conf amqp.Config) (*amqp.Connect
if err == nil {
return conn, err
}
log.Warnf("failed to connect to amqp server %s: %v", url, err)

// Log masked url
log.Warnf("failed to connect to amqp server %s: %v", maskPassword(url), err)
errs = append(errs, err)
}
return nil, errors.Join(errs...)
}

func maskPassword(urlToMask string) string {
parsedUrl, _ := url.Parse(urlToMask)
return parsedUrl.Redacted()
}

// NewConnectionManager creates a new connection manager
func NewConnectionManager(resolver Resolver, conf amqp.Config, log logger.Logger, reconnectInterval time.Duration) (*ConnectionManager, error) {
conn, err := dial(log, resolver, amqp.Config(conf))
Expand Down
40 changes: 40 additions & 0 deletions internal/connectionmanager/connection_manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package connectionmanager

import "testing"

func Test_maskUrl(t *testing.T) {
tests := []struct {
name string
url string
expected string
}{
{
name: "No username or password",
url: "amqp://localhost",
expected: "amqp://localhost",
},
{
name: "With username and password",
url: "amqp://user:password@localhost",
expected: "amqp://user:xxxxx@localhost",
},
{
name: "With username and password and query params",
url: "amqp://user:password@localhost?heartbeat=60",
expected: "amqp://user:xxxxx@localhost?heartbeat=60",
},
{
name: "Invalid URL",
url: "invalidUrl",
expected: "invalidUrl",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if maskPassword(tt.url) != tt.expected {
t.Errorf("masked password = %v, but wanted %v", maskPassword(tt.url), tt.expected)
}
})
}
}

0 comments on commit 3983b96

Please sign in to comment.