Skip to content

Commit

Permalink
PMM-11844: fix setting auth props (#640)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritbl authored Apr 4, 2023
1 parent bed531b commit 83a9cdf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
6 changes: 5 additions & 1 deletion exporter/dsn_fix/dsn_fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import (
// ClientOptionsForDSN applies URI to Client.
func ClientOptionsForDSN(dsn string) (*options.ClientOptions, error) {
clientOptions := options.Client().ApplyURI(dsn)
if e := clientOptions.Validate(); e != nil {
return nil, e
}

// Workaround for PMM-9320
// if username or password is set, need to replace it with correctly parsed credentials.
Expand All @@ -36,7 +39,8 @@ func ClientOptionsForDSN(dsn string) (*options.ClientOptions, error) {
username := parsedDsn.User.Username()
password, _ := parsedDsn.User.Password()
if username != "" || password != "" {
clientOptions = clientOptions.SetAuth(options.Credential{Username: username, Password: password})
clientOptions.Auth.Username = username
clientOptions.Auth.Password = password
}

return clientOptions, nil
Expand Down
27 changes: 22 additions & 5 deletions exporter/dsn_fix/dsn_fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ import (
func TestClientOptionsForDSN(t *testing.T) {
tests := []struct {
name string
error string
dsn string
expectedUser string
expectedPassword string
}{
{
name: "Escape username",
dsn: (&url.URL{
Scheme: "mongo",
Scheme: "mongodb",
Host: "localhost",
Path: "/db",
User: url.UserPassword("user+", "pass"),
Expand All @@ -44,21 +45,37 @@ func TestClientOptionsForDSN(t *testing.T) {
{
name: "Escape password",
dsn: (&url.URL{
Scheme: "mongo",
Scheme: "mongodb",
Host: "localhost",
Path: "/db",
User: url.UserPassword("user", "pass+"),
}).String(),
expectedUser: "user",
expectedPassword: "pass+",
},
{
name: "Invalid URI",
dsn: (&url.URL{
Scheme: "<invalid>",
Host: "localhost",
Path: "/db",
User: url.UserPassword("user", "pass+"),
}).String(),
error: "error parsing uri: scheme must be \"mongodb\" or \"mongodb+srv\"",
expectedUser: "user",
expectedPassword: "pass+",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ClientOptionsForDSN(tt.dsn)
assert.Nil(t, err)
assert.Equal(t, got.Auth.Username, tt.expectedUser)
assert.Equal(t, got.Auth.Password, tt.expectedPassword)
if tt.error != "" {
assert.Equal(t, err.Error(), tt.error)
} else {
assert.Empty(t, err)
assert.Equal(t, got.Auth.Username, tt.expectedUser)
assert.Equal(t, got.Auth.Password, tt.expectedPassword)
}
})
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
github.com/shirou/gopsutil v3.21.8+incompatible // indirect
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.1
go.mongodb.org/mongo-driver v1.10.3
go.mongodb.org/mongo-driver v1.11.3
)

require (
Expand Down
5 changes: 3 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47e
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
Expand Down Expand Up @@ -257,8 +258,8 @@ github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7Jul
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go.mongodb.org/mongo-driver v1.10.3 h1:XDQEvmh6z1EUsXuIkXE9TaVeqHw6SwS1uf93jFs0HBA=
go.mongodb.org/mongo-driver v1.10.3/go.mod h1:z4XpeoU6w+9Vht+jAFyLgVrD+jGSQQe0+CBWFHNiHt8=
go.mongodb.org/mongo-driver v1.11.3 h1:Ql6K6qYHEzB6xvu4+AU0BoRoqf9vFPcc4o7MUIdPW8Y=
go.mongodb.org/mongo-driver v1.11.3/go.mod h1:PTSz5yu21bkT/wXpkS7WR5f0ddqw5quethTUn9WM+2g=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
Expand Down

0 comments on commit 83a9cdf

Please sign in to comment.