This repository has been archived by the owner on Aug 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
PMM-9320: fix username/password incorrect escape #372
Open
ritbl
wants to merge
14
commits into
main
Choose a base branch
from
PMM-9320-fix-mongo-creds-escape
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e702a18
fix username/password incorrect escape
ritbl 27a9c8a
fix empty password
ritbl 8976716
style
ritbl 9fabb3e
add copyright header
ritbl 4c44b7f
style
ritbl 9f69bb3
style
ritbl 0a57917
add test
ritbl 1f022eb
style
ritbl 1cc70c5
style
ritbl 68f8084
style
ritbl d4ce64f
Merge branch 'main' into PMM-9320-fix-mongo-creds-escape
ritbl 2b88a51
add comment
ritbl c189e5f
Merge remote-tracking branch 'origin/PMM-9320-fix-mongo-creds-escape'…
ritbl 32a33c0
Merge branch 'main' into PMM-9320-fix-mongo-creds-escape
YaroslavPodorvanov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// pmm-agent | ||
// Copyright 2019 Percona LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package mongo_fix | ||
ritbl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import ( | ||
"net/url" | ||
|
||
"github.com/pkg/errors" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
// ClientOptionsForDSN applies URI to Client. | ||
func ClientOptionsForDSN(dsn string) (*options.ClientOptions, error) { | ||
clientOptions := options.Client().ApplyURI(dsn) | ||
|
||
// Workaround for PMM-9320 | ||
parsedDsn, err := url.Parse(dsn) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "cannot parse DSN") | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package mongo_fix | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
func TestClientOptionsForDSN(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
dsn string | ||
expectedUser string | ||
expectedPassword string | ||
}{ | ||
{ | ||
name: "Escape username", | ||
dsn: (&url.URL{ | ||
Scheme: "mongo", | ||
Host: "localhost", | ||
Path: "/db", | ||
User: url.UserPassword("user+", "pass"), | ||
}).String(), | ||
expectedUser: "user+", | ||
expectedPassword: "pass", | ||
}, | ||
{ | ||
name: "Escape password", | ||
dsn: (&url.URL{ | ||
Scheme: "mongo", | ||
Host: "localhost", | ||
Path: "/db", | ||
User: url.UserPassword("user", "pass+"), | ||
}).String(), | ||
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
called
mongo_fix
not to clash with mongo driver