Skip to content

Commit c8749e5

Browse files
authored
Merge pull request #195 from xBlaz3kx/fix/redact-password
Fix: Redact password from URL
2 parents cd74be0 + c9fe7d9 commit c8749e5

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

internal/connectionmanager/connection_manager.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package connectionmanager
33
import (
44
"errors"
55
"fmt"
6+
"net/url"
67
"sync"
78
"time"
89

@@ -42,12 +43,17 @@ func dial(log logger.Logger, resolver Resolver, conf amqp.Config) (*amqp.Connect
4243
if err == nil {
4344
return conn, err
4445
}
45-
log.Warnf("failed to connect to amqp server %s: %v", url, err)
46+
log.Warnf("failed to connect to amqp server %s: %v", maskPassword(url), err)
4647
errs = append(errs, err)
4748
}
4849
return nil, errors.Join(errs...)
4950
}
5051

52+
func maskPassword(urlToMask string) string {
53+
parsedUrl, _ := url.Parse(urlToMask)
54+
return parsedUrl.Redacted()
55+
}
56+
5157
// NewConnectionManager creates a new connection manager
5258
func NewConnectionManager(resolver Resolver, conf amqp.Config, log logger.Logger, reconnectInterval time.Duration) (*ConnectionManager, error) {
5359
conn, err := dial(log, resolver, amqp.Config(conf))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package connectionmanager
2+
3+
import "testing"
4+
5+
func Test_maskUrl(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
url string
9+
expected string
10+
}{
11+
{
12+
name: "No username or password",
13+
url: "amqp://localhost",
14+
expected: "amqp://localhost",
15+
},
16+
{
17+
name: "With username and password",
18+
url: "amqp://user:password@localhost",
19+
expected: "amqp://user:xxxxx@localhost",
20+
},
21+
{
22+
name: "With username and password and query params",
23+
url: "amqp://user:password@localhost?heartbeat=60",
24+
expected: "amqp://user:xxxxx@localhost?heartbeat=60",
25+
},
26+
{
27+
name: "Invalid URL",
28+
url: "invalidUrl",
29+
expected: "invalidUrl",
30+
},
31+
}
32+
33+
for _, tt := range tests {
34+
t.Run(tt.name, func(t *testing.T) {
35+
if maskPassword(tt.url) != tt.expected {
36+
t.Errorf("masked password = %v, but wanted %v", maskPassword(tt.url), tt.expected)
37+
}
38+
})
39+
}
40+
}

0 commit comments

Comments
 (0)