diff --git a/exporter/dsn_fix/dsn_fix.go b/exporter/dsn_fix/dsn_fix.go index 96dbda14f..88e878e7d 100644 --- a/exporter/dsn_fix/dsn_fix.go +++ b/exporter/dsn_fix/dsn_fix.go @@ -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. @@ -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 diff --git a/exporter/dsn_fix/dsn_fix_test.go b/exporter/dsn_fix/dsn_fix_test.go index 8ab175c26..7a3321b66 100644 --- a/exporter/dsn_fix/dsn_fix_test.go +++ b/exporter/dsn_fix/dsn_fix_test.go @@ -26,6 +26,7 @@ import ( func TestClientOptionsForDSN(t *testing.T) { tests := []struct { name string + error string dsn string expectedUser string expectedPassword string @@ -33,7 +34,7 @@ func TestClientOptionsForDSN(t *testing.T) { { name: "Escape username", dsn: (&url.URL{ - Scheme: "mongo", + Scheme: "mongodb", Host: "localhost", Path: "/db", User: url.UserPassword("user+", "pass"), @@ -44,7 +45,7 @@ func TestClientOptionsForDSN(t *testing.T) { { name: "Escape password", dsn: (&url.URL{ - Scheme: "mongo", + Scheme: "mongodb", Host: "localhost", Path: "/db", User: url.UserPassword("user", "pass+"), @@ -52,13 +53,29 @@ func TestClientOptionsForDSN(t *testing.T) { expectedUser: "user", expectedPassword: "pass+", }, + { + name: "Invalid URI", + dsn: (&url.URL{ + Scheme: "", + 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) + } }) } } diff --git a/go.mod b/go.mod index 76adea83d..f7b3c8d61 100644 --- a/go.mod +++ b/go.mod @@ -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 ( diff --git a/go.sum b/go.sum index ded8f1a51..e39e8c569 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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=