-
Notifications
You must be signed in to change notification settings - Fork 21
PMM-9320: fix username/password incorrect escape #372
base: main
Are you sure you want to change the base?
Changes from 3 commits
e702a18
27a9c8a
8976716
9fabb3e
4c44b7f
9f69bb3
0a57917
1f022eb
1cc70c5
68f8084
d4ce64f
2b88a51
c189e5f
32a33c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package mongo_fix | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. called
ritbl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import ( | ||
"net/url" | ||
|
||
"github.com/pkg/errors" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
// ClientForDSN applies URI to Client | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚫 [golangci-lint] reported by reviewdog 🐶 |
||
func ClientForDSN(dsn string) (*options.ClientOptions, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably we should call it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, |
||
parsedDsn, err := url.Parse(dsn) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "cannot parse DSN") | ||
} | ||
|
||
clientOptions := options.Client().ApplyURI(dsn) | ||
|
||
// Workaround for PMM-9320 | ||
username := parsedDsn.User.Username() | ||
password, _ := parsedDsn.User.Password() | ||
if username != "" || password != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need to set |
||
clientOptions = clientOptions.SetAuth(options.Credential{Username: username, Password: password}) | ||
} | ||
|
||
return clientOptions, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,8 @@ import ( | |
"github.com/stretchr/testify/require" | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
|
||
"github.com/percona/pmm-agent/mongo_fix" | ||
) | ||
|
||
// GetTestMongoDBDSN returns DNS for MongoDB test database. | ||
|
@@ -104,7 +105,12 @@ func GetTestMongoDBReplicatedWithSSLDSN(tb testing.TB, pathToRoot string) (strin | |
func OpenTestMongoDB(tb testing.TB, dsn string) *mongo.Client { | ||
tb.Helper() | ||
|
||
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI(dsn)) | ||
opts, err := mongo_fix.ClientForDSN(dsn) | ||
if err != nil { | ||
panic(err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
client, err := mongo.Connect(context.Background(), opts) | ||
require.NoError(tb, err) | ||
|
||
require.NoError(tb, client.Ping(context.Background(), nil)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is test, so we can
panic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can, but should we?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, here we return error, so I think we can just propagate error.
but what about other cases, like in
func OpenTestMongoDB(tb testing.TB, dsn string) *mongo.Client {
we don't have error in the API, do you think we should refactor API or we can panic (in tests only)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can call
require.NoError(tb, err)
in that places. so it will stop test with failed resultThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, good idea