Skip to content

Fix use of TOOLS_TESTING_MONGOD with development server builds #772

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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: 11 additions & 3 deletions common/db/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (sp *SessionProvider) ServerVersion() (string, error) {
func (sp *SessionProvider) ServerVersionArray() (Version, error) {
var version Version
out := struct {
VersionArray []int32 `bson:"versionArray"`
VersionArray []int `bson:"versionArray"`
}{}
err := sp.RunString("buildInfo", &out, "admin")
if err != nil {
Expand All @@ -113,9 +113,17 @@ func (sp *SessionProvider) ServerVersionArray() (Version, error) {
if len(out.VersionArray) < 3 {
return version, fmt.Errorf("buildInfo.versionArray had fewer than 3 elements")
}
for i := 0; i <= 2; i++ {
version[i] = int(out.VersionArray[i])

copy(version[:], out.VersionArray[:len(version)])

// In development server builds `versionArray`’s 4th member is negative, and
// `versionArray`’s patch version exceeds `version`’s by 1. Since we have
// logic that compares this method’s output to `version` we need to subtract
// one from the patch value.
if len(out.VersionArray) > 3 && out.VersionArray[3] < 0 {
version[2]--
}

return version, nil
}

Expand Down
8 changes: 6 additions & 2 deletions common/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,6 @@ func configureClient(opts options.ToolOptions) (*mongo.Client, error) {
clientopt := mopt.Client()
cs := opts.URI.ParsedConnString()

clientopt.Hosts = cs.Hosts

if opts.RetryWrites != nil {
clientopt.SetRetryWrites(*opts.RetryWrites)
}
Expand Down Expand Up @@ -506,6 +504,12 @@ func configureClient(opts options.ToolOptions) (*mongo.Client, error) {
clientopt.SetAuth(cred)
}

if opts.Kerberos != nil && opts.Kerberos.ServiceHost != "" {
clientopt.Hosts = cs.Hosts
} else {
clientopt.ApplyURI(cs.String())
}

if opts.SSL != nil && opts.UseSSL {
// Error on unsupported features
if opts.SSLFipsMode {
Expand Down
31 changes: 31 additions & 0 deletions common/util/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
package util

import (
"context"
"fmt"
"strings"

"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)

const (
Expand Down Expand Up @@ -228,3 +233,29 @@ func ValidateCollectionGrammar(collection string) error {
// collection name is valid
return nil
}

func IsConnectionAuthenticated(ctx context.Context, conn *mongo.Client) (bool, error) {
res := conn.Database("admin").RunCommand(
ctx,
bson.D{{"connectionStatus", 1}},
)

if res.Err() != nil {
return false, errors.Wrap(res.Err(), "failed to query for connection information")
}

body := struct {
AuthInfo struct {
AuthenticatedUsers []bson.D `bson:"authenticatedUsers"`
} `bson:"authInfo"`
}{}

err := res.Decode(&body)
if err != nil {
raw, _ := res.Raw()

return false, errors.Wrapf(err, "failed to decode connection information (%+v)", raw)
}

return len(body.AuthInfo.AuthenticatedUsers) > 0, nil
}
22 changes: 19 additions & 3 deletions mongorestore/mongorestore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/mongodb/mongo-tools/common/options"
"github.com/mongodb/mongo-tools/common/testtype"
"github.com/mongodb/mongo-tools/common/testutil"
"github.com/mongodb/mongo-tools/common/util"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -1833,7 +1834,8 @@ func TestCreateIndexes(t *testing.T) {
"RestoreOplog() should convert commitIndexBuild op to createIndexes cmd and build index",
func() {
destColl := session.Database("create_indexes").Collection("test")
indexes, _ := destColl.Indexes().List(context.Background())
indexes, err := destColl.Indexes().List(context.Background())
So(err, ShouldBeNil)

type indexSpec struct {
Name, NS string
Expand Down Expand Up @@ -2202,7 +2204,14 @@ func TestRestoreTimeseriesCollections(t *testing.T) {

session, err := sessionProvider.GetSession()
if err != nil {
t.Fatalf("No client available")
t.Fatalf("No client available: %v", err)
}

isAuthn, err := util.IsConnectionAuthenticated(ctx, session)
require.NoError(t, err, "should query for authentication state")

if isAuthn {
t.Skip("This test requires a non-authenticated connection.")
}

fcv := testutil.GetFCV(session)
Expand Down Expand Up @@ -3424,12 +3433,19 @@ func TestDumpAndRestoreConfigDB(t *testing.T) {

testtype.SkipUnlessTestType(t, testtype.IntegrationTestType)

_, err := testutil.GetBareSession()
client, err := testutil.GetBareSession()
require.NoError(err, "can connect to server")

isAuthn, err := util.IsConnectionAuthenticated(context.Background(), client)
require.NoError(err, "should query for authentication state")

t.Run(
"test dump and restore only config db includes all config collections",
func(t *testing.T) {
if isAuthn {
t.Skip("This test requires a non-authenticated connection.")
}

testDumpAndRestoreConfigDBIncludesAllCollections(t)
},
)
Expand Down