Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GODRIVER-2620 Fix hostname parsing for SRV polling. #1112

Merged
merged 6 commits into from
Oct 28, 2022

Conversation

qingyang-hu
Copy link
Collaborator

GODRIVER-2620

Summary

Fix hostname parsing for SRV polling.

Background & Motivation

The original code parses the SRV hostname incorrectly for any URI with a username and password. This PR extracts the hostname from a URI using the net/url package instead of manual parsing and finishes all sanity checks before passing the hostname.
However, I couldn't find a decent way to pass the errors from inside the polling out.

@@ -127,7 +127,7 @@ func compareHosts(t *testing.T, received []description.Server, expected []string
func TestPollingSRVRecordsSpec(t *testing.T) {
for _, tt := range srvPollingTests {
t.Run(tt.name, func(t *testing.T) {
uri := "mongodb+srv://test1.test.build.10gen.cc/?heartbeatFrequencyMS=100"
uri := "mongodb+srv://user:pass@test1.test.build.10gen.cc/?heartbeatFrequencyMS=100"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only testing with a URI containing a username/password may cause the same lack of test coverage as there is currently, but for URIs with no username/password.

Is there a way we can test with multiple URI formats? Could we include the URI in the test struct and add test cases for both with and without username/password?

@@ -234,7 +236,22 @@ func (t *Topology) Connect() error {

t.serversLock.Unlock()
if t.pollingRequired {
go t.pollSRVRecords()
uri, err := url.Parse(t.cfg.URI)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned we don't have enough test coverage of MongoDB connection string formats in the SRV polling tests to assert that it's safe to use url.Parse.

Is every mongodb+srv:// connection string parseable with url.Parse?

Copy link
Collaborator Author

@qingyang-hu qingyang-hu Oct 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The package doc says url.Parse parses [scheme:][//[userinfo@]host][/]path[?query][#fragment].
It has been proven that mongodb+srv is parseable as a legal scheme, and covered in the test cases.
Mongo host in the connection string may be different from ordinary hostnames, e.g. a comma-separated host list. However, it is illegal for SRV, and should be sanity checked by topology.NewConfig before being passed in Connect().
Also, we don't need to parse the path string in this case so it doesn't impact us even though we may have special cases in the query parameters.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. I also tested out using url.Parse to parse a bunch of connection string formats and they all worked correctly (see here).

// but should not be able to when using SRV
return fmt.Errorf("URI with srv must not include a port number")
}
go t.pollSRVRecords(parsedHosts[0])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This refactor is a great idea!

Copy link
Contributor

@benjirewis benjirewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM % Matt's comment. We could just add a separate TestPollingSRVRecordsUserPass test and keep the existing test as-is. Thanks! 🧑‍🔧

Copy link
Collaborator

@prestonvasquez prestonvasquez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

if len(parsedHosts) != 1 {
return fmt.Errorf("URI with SRV must include one and only one hostname")
}
_, _, err = net.SplitHostPort(parsedHosts[0])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_, _, err = net.SplitHostPort(parsedHosts[0])
_, _, err = net.SplitHostPort(uri.Host)

[optional]: IMO this code reads better by using the uri to get the host, not the slice used in the sanity check.

// but should not be able to when using SRV
return fmt.Errorf("URI with srv must not include a port number")
}
go t.pollSRVRecords(parsedHosts[0])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
go t.pollSRVRecords(parsedHosts[0])
go t.pollSRVRecords(uri.Host)

[optional]: IMO this code reads better by using the uri to get the host, not the slice used in the sanity check.

Copy link
Contributor

@kevinAlbs kevinAlbs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, great find!

Copy link
Collaborator

@matthewdale matthewdale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍

@@ -234,7 +236,22 @@ func (t *Topology) Connect() error {

t.serversLock.Unlock()
if t.pollingRequired {
go t.pollSRVRecords()
uri, err := url.Parse(t.cfg.URI)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. I also tested out using url.Parse to parse a bunch of connection string formats and they all worked correctly (see here).

@qingyang-hu qingyang-hu merged commit e4853fb into mongodb:master Oct 28, 2022
qingyang-hu added a commit that referenced this pull request Nov 1, 2022
* GODRIVER-2620 Fix hostname parsing for SRV polling.

Co-authored-by: Preston Vasquez <[email protected]>
Co-authored-by: Kevin Albertson <[email protected]>
qingyang-hu added a commit that referenced this pull request Nov 1, 2022
* GODRIVER-2620 Fix hostname parsing for SRV polling.

Co-authored-by: Preston Vasquez <[email protected]>
Co-authored-by: Kevin Albertson <[email protected]>
qingyang-hu added a commit that referenced this pull request Nov 1, 2022
* GODRIVER-2620 Fix hostname parsing for SRV polling.

Co-authored-by: Preston Vasquez <[email protected]>
Co-authored-by: Kevin Albertson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants