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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion x/mongo/driver/topology/polling_srv_records_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,21 @@ func compareHosts(t *testing.T, received []description.Server, expected []string
}

func TestPollingSRVRecordsSpec(t *testing.T) {
for _, uri := range []string{
"mongodb+srv://test1.test.build.10gen.cc/?heartbeatFrequencyMS=100",
// Test with user:pass as a regression test for GODRIVER-2620
"mongodb+srv://user:[email protected]/?heartbeatFrequencyMS=100",
qingyang-hu marked this conversation as resolved.
Show resolved Hide resolved
} {
t.Run(uri, func(t *testing.T) {
testPollingSRVRecordsSpec(t, uri)
})
}
}

func testPollingSRVRecordsSpec(t *testing.T, uri string) {
qingyang-hu marked this conversation as resolved.
Show resolved Hide resolved
t.Helper()
for _, tt := range srvPollingTests {
t.Run(tt.name, func(t *testing.T) {
uri := "mongodb+srv://test1.test.build.10gen.cc/?heartbeatFrequencyMS=100"
cfg, err := NewConfig(options.Client().ApplyURI(uri), nil)
require.NoError(t, err, "error constructing topology configs: %v", err)

Expand Down
27 changes: 18 additions & 9 deletions x/mongo/driver/topology/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"context"
"errors"
"fmt"
"net"
"net/url"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -234,7 +236,21 @@ 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).

if err != nil {
return err
}
// sanity check before passing the hostname to resolver
if parsedHosts := strings.Split(uri.Host, ","); len(parsedHosts) != 1 {
return fmt.Errorf("URI with SRV must include one and only one hostname")
}
_, _, err = net.SplitHostPort(uri.Host)
if err == nil {
// we were able to successfully extract a port from the host,
// but should not be able to when using SRV
return fmt.Errorf("URI with srv must not include a port number")
}
go t.pollSRVRecords(uri.Host)
t.pollingwg.Add(1)
}

Expand Down Expand Up @@ -556,7 +572,7 @@ func (t *Topology) selectServerFromDescription(desc description.Topology,
return suitable, nil
}

func (t *Topology) pollSRVRecords() {
func (t *Topology) pollSRVRecords(hosts string) {
defer t.pollingwg.Done()

serverConfig := newServerConfig(t.cfg.ServerOpts...)
Expand All @@ -573,13 +589,6 @@ func (t *Topology) pollSRVRecords() {
}
}()

// remove the scheme
uri := t.cfg.URI[14:]
hosts := uri
if idx := strings.IndexAny(uri, "/?@"); idx != -1 {
hosts = uri[:idx]
}

for {
select {
case <-pollTicker.C:
Expand Down