From 1123dac0b1dba5e2260f2c3cc7d08c8529c750de Mon Sep 17 00:00:00 2001 From: Matt Dale <9760375+matthewdale@users.noreply.github.com> Date: Wed, 2 Nov 2022 14:34:29 -0700 Subject: [PATCH] GODRIVER-1530 GODRIVER-1213 Fix the enterprise auth test runner. (#1118) Co-authored-by: Benjamin Rewis <32186188+benjirewis@users.noreply.github.com> --- .evergreen/config.yml | 14 +- Makefile | 8 +- cmd/testentauth/main.go | 51 ++++++ x/mongo/driver/connstring/connstring.go | 2 +- x/mongo/driver/examples/count/main.go | 59 ------- .../driver/examples/server_monitoring/main.go | 40 ----- x/mongo/driver/examples/workload/main.go | 163 ------------------ 7 files changed, 66 insertions(+), 271 deletions(-) create mode 100644 cmd/testentauth/main.go delete mode 100644 x/mongo/driver/examples/count/main.go delete mode 100644 x/mongo/driver/examples/server_monitoring/main.go delete mode 100644 x/mongo/driver/examples/workload/main.go diff --git a/.evergreen/config.yml b/.evergreen/config.yml index e3402ebaba..7e3b30bc47 100644 --- a/.evergreen/config.yml +++ b/.evergreen/config.yml @@ -335,7 +335,6 @@ functions: - command: shell.exec type: test params: - silent: true working_dir: src/go.mongodb.org/mongo-driver script: | # DO NOT ECHO WITH XTRACE @@ -350,7 +349,10 @@ functions: export GOROOT="${GO_DIST}" export GOCACHE="$GOCACHE" export PATH="${GCC_PATH}:${GO_DIST}/bin:$PATH" - MONGODB_URI="${MONGODB_URI}" MONGO_GO_DRIVER_COMPRESSOR="${MONGO_GO_DRIVER_COMPRESSOR}" make -s evg-test-auth + export MONGODB_URI="${MONGODB_URI}" + export MONGO_GO_DRIVER_COMPRESSOR="${MONGO_GO_DRIVER_COMPRESSOR}" + + make -s evg-test-enterprise-auth run-enterprise-gssapi-auth-tests: - command: shell.exec @@ -377,7 +379,9 @@ functions: export GOROOT="${GO_DIST}" export GOCACHE="$GOCACHE" export PATH="${GCC_PATH}:${GO_DIST}/bin:$PATH" - MONGO_GO_DRIVER_COMPRESSOR="${MONGO_GO_DRIVER_COMPRESSOR}" make -s evg-test-auth + export MONGO_GO_DRIVER_COMPRESSOR="${MONGO_GO_DRIVER_COMPRESSOR}" + + make -s evg-test-enterprise-auth run-enterprise-gssapi-service-host-auth-tests: - command: shell.exec @@ -404,7 +408,9 @@ functions: export GOROOT="${GO_DIST}" export GOCACHE="$GOCACHE" export PATH="${GCC_PATH}:${GO_DIST}/bin:$PATH" - MONGO_GO_DRIVER_COMPRESSOR="${MONGO_GO_DRIVER_COMPRESSOR}" make -s evg-test-auth + export MONGO_GO_DRIVER_COMPRESSOR="${MONGO_GO_DRIVER_COMPRESSOR}" + + make -s evg-test-enterprise-auth run-atlas-test: - command: shell.exec diff --git a/Makefile b/Makefile index a04e99bf54..6d2b8be9e3 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ build: .PHONY: build-examples build-examples: - go build $(BUILD_TAGS) ./examples/... ./x/mongo/driver/examples/... + go build $(BUILD_TAGS) ./examples/... .PHONY: build-no-tags build-no-tags: @@ -128,9 +128,9 @@ evg-test-atlas-data-lake: ATLAS_DATA_LAKE_INTEGRATION_TEST=true go test -v ./mongo/integration -run TestUnifiedSpecs/atlas-data-lake-testing >> spec_test.suite ATLAS_DATA_LAKE_INTEGRATION_TEST=true go test -v ./mongo/integration -run TestAtlasDataLake >> spec_test.suite -.PHONY: evg-test-auth -evg-test-auth: - go run -tags gssapi ./x/mongo/driver/examples/count/main.go -uri $(MONGODB_URI) +.PHONY: evg-test-enterprise-auth +evg-test-enterprise-auth: + go run -tags gssapi ./cmd/testentauth/main.go .PHONY: evg-test-kmip evg-test-kmip: diff --git a/cmd/testentauth/main.go b/cmd/testentauth/main.go new file mode 100644 index 0000000000..9bcea33294 --- /dev/null +++ b/cmd/testentauth/main.go @@ -0,0 +1,51 @@ +// Copyright (C) MongoDB, Inc. 2022-present. +// +// 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 + +package main + +import ( + "context" + "log" + "os" + "time" + + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" + "go.mongodb.org/mongo-driver/x/mongo/driver/connstring" +) + +func main() { + uri := os.Getenv("MONGODB_URI") + compressor := os.Getenv("MONGO_GO_DRIVER_COMPRESSOR") + + client, err := mongo.Connect( + context.Background(), + options.Client().ApplyURI(uri).SetCompressors([]string{compressor})) + if err != nil { + log.Fatalf("Error connecting client: %v", err) + } + + // Use the defaultauthdb (i.e. the database name after the "/") specified in the connection + // string to run the count operation. + cs, err := connstring.Parse(uri) + if err != nil { + log.Fatalf("Error parsing connection string: %v", err) + } + if cs.Database == "" { + log.Fatal("Connection string must contain a defaultauthdb.") + } + + coll := client.Database(cs.Database).Collection("test") + + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + + count, err := coll.EstimatedDocumentCount(ctx) + if err != nil { + log.Fatalf("failed executing count command: %v", err) + } + log.Println("Count of test collection:", count) +} diff --git a/x/mongo/driver/connstring/connstring.go b/x/mongo/driver/connstring/connstring.go index ac6a33c8fd..6f03a58577 100644 --- a/x/mongo/driver/connstring/connstring.go +++ b/x/mongo/driver/connstring/connstring.go @@ -494,7 +494,7 @@ func (p *parser) validateAuth() error { return fmt.Errorf("username required for GSSAPI") } for k := range p.AuthMechanismProperties { - if k != "SERVICE_NAME" && k != "CANONICALIZE_HOST_NAME" && k != "SERVICE_REALM" { + if k != "SERVICE_NAME" && k != "CANONICALIZE_HOST_NAME" && k != "SERVICE_REALM" && k != "SERVICE_HOST" { return fmt.Errorf("invalid auth property for GSSAPI") } } diff --git a/x/mongo/driver/examples/count/main.go b/x/mongo/driver/examples/count/main.go deleted file mode 100644 index 54fb536324..0000000000 --- a/x/mongo/driver/examples/count/main.go +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright (C) MongoDB, Inc. 2017-present. -// -// 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 - -package main - -import ( - "context" - "log" - "time" - - "flag" - - "go.mongodb.org/mongo-driver/mongo/description" - "go.mongodb.org/mongo-driver/mongo/options" - "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" - "go.mongodb.org/mongo-driver/x/mongo/driver/operation" - "go.mongodb.org/mongo-driver/x/mongo/driver/topology" -) - -var uri = flag.String("uri", "mongodb://localhost:27017", "the mongodb uri to use") -var col = flag.String("c", "test", "the collection name to use") - -func main() { - - flag.Parse() - - if *uri == "" { - log.Fatalf("uri flag must have a value") - } - - cfg, err := topology.NewConfig(options.Client().ApplyURI(*uri), nil) - if err != nil { - log.Fatal(err) - } - - t, err := topology.New(cfg) - if err != nil { - log.Fatal(err) - } - err = t.Connect() - if err != nil { - log.Fatal(err) - } - - ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) - defer cancel() - - op := operation.NewCommand(bsoncore.BuildDocument(nil, bsoncore.AppendStringElement(nil, "count", *col))). - Deployment(t).Database("test").ServerSelector(description.WriteSelector()) - err = op.Execute(ctx) - if err != nil { - log.Fatalf("failed executing count command: %v", err) - } - rdr := op.Result() - log.Println(rdr) -} diff --git a/x/mongo/driver/examples/server_monitoring/main.go b/x/mongo/driver/examples/server_monitoring/main.go deleted file mode 100644 index 5c5adef8fc..0000000000 --- a/x/mongo/driver/examples/server_monitoring/main.go +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (C) MongoDB, Inc. 2017-present. -// -// 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 - -package main - -import ( - "log" - "time" - - "github.com/kr/pretty" - "go.mongodb.org/mongo-driver/bson/primitive" - "go.mongodb.org/mongo-driver/mongo/address" - "go.mongodb.org/mongo-driver/x/mongo/driver/topology" -) - -// TODO(GODRIVER-2527): convert to a Go doc example. - -func main() { - s, err := topology.ConnectServer( - address.Address("localhost:27017"), - nil, - primitive.NewObjectID(), - topology.WithHeartbeatInterval(func(time.Duration) time.Duration { return 2 * time.Second }), - ) - if err != nil { - log.Fatalf("could not start server: %v", err) - } - - sub, err := s.Subscribe() - if err != nil { - log.Fatalf("could not subscribe to server: %v", err) - } - - for desc := range sub.C { - log.Printf("%# v", pretty.Formatter(desc)) - } -} diff --git a/x/mongo/driver/examples/workload/main.go b/x/mongo/driver/examples/workload/main.go deleted file mode 100644 index 4f5cf822d8..0000000000 --- a/x/mongo/driver/examples/workload/main.go +++ /dev/null @@ -1,163 +0,0 @@ -// Copyright (C) MongoDB, Inc. 2017-present. -// -// 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 - -package main - -import ( - "context" - "flag" - "fmt" - "log" - "math/rand" - "net/http" - _ "net/http/pprof" - "os" - "os/signal" - "strings" - "time" - - "go.mongodb.org/mongo-driver/mongo/description" - "go.mongodb.org/mongo-driver/mongo/options" - "go.mongodb.org/mongo-driver/mongo/readpref" - "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" - "go.mongodb.org/mongo-driver/x/mongo/driver" - "go.mongodb.org/mongo-driver/x/mongo/driver/operation" - "go.mongodb.org/mongo-driver/x/mongo/driver/topology" -) - -var concurrency = flag.Int("concurrency", 24, "how much concurrency should be used") -var ns = flag.String("namespace", "test.foo", "the namespace to use for test data") - -// TODO(GODRIVER-2527): convert to a Go doc example. -func main() { - - go func() { - log.Println(http.ListenAndServe("localhost:6060", nil)) - }() - - sig := make(chan os.Signal, 1) - signal.Notify(sig, os.Interrupt) - - cfg, err := topology.NewConfig(options.Client().ApplyURI("mongodb://localhost:27017/"), nil) - if err != nil { - log.Fatalf("error constructing topology config: %v", err) - } - - c, err := topology.New(cfg) - if err != nil { - log.Fatalf("unable to create topology: %s", err) - } - - err = c.Connect() - if err != nil { - log.Fatalf("unable to connect topology: %s", err) - } - - done := make(chan struct{}) - ctx, cancel := context.WithCancel(context.Background()) - go func() { - <-sig - cancel() - close(done) - }() - - log.Println("prepping") - err = prep(ctx, c) - if err != nil { - log.Fatalf("unable to prep: %s", err) - } - log.Println("done prepping") - - log.Println("working") - for i := 0; i < *concurrency; i++ { - go work(ctx, i, c) - } - - <-done - log.Println("interrupt received: shutting down") - _ = c.Disconnect(ctx) - log.Println("finished") -} - -func prep(ctx context.Context, c *topology.Topology) error { - - var docs = make([]bsoncore.Document, 0, 1000) - for i := 0; i < 1000; i++ { - docs = append(docs, bsoncore.BuildDocument(nil, bsoncore.AppendInt32Element(nil, "_id", int32(i)))) - } - - collection, database, err := parseNamespace(*ns) - if err != nil { - return err - } - - deletes := bsoncore.BuildDocument(nil, - bsoncore.BuildDocumentElement(nil, "q"), - bsoncore.AppendInt32Element(nil, "limit", 0), - ) - err = operation.NewDelete(deletes).Collection(collection).Database(database). - Deployment(c).ServerSelector(description.WriteSelector()).Execute(ctx) - if err != nil { - return err - } - - err = operation.NewInsert(docs...).Collection(collection).Database(database).Deployment(c). - ServerSelector(description.WriteSelector()).Execute(ctx) - return err -} - -func work(ctx context.Context, idx int, c *topology.Topology) { - r := rand.New(rand.NewSource(time.Now().Unix())) - collection, database, err := parseNamespace(*ns) - if err != nil { - panic(fmt.Errorf("failed to parse namespace: %v", err)) - } - rp := readpref.Nearest() - for { - select { - case <-ctx.Done(): - default: - - limit := r.Intn(999) + 1 - - pipeline := bsoncore.BuildArray(nil, bsoncore.BuildDocumentValue(bsoncore.AppendInt32Element(nil, "$limit", int32(limit)))) - - op := operation.NewAggregate(pipeline).Collection(collection).Database(database).Deployment(c).BatchSize(200). - ServerSelector(description.ReadPrefSelector(rp)) - err := op.Execute(ctx) - if err != nil { - log.Printf("%d-failed executing aggregate: %s", idx, err) - continue - } - - cursor, err := op.Result(driver.CursorOptions{BatchSize: 200}) - if err != nil { - log.Printf("%d-failed to create cursor: %v", idx, err) - continue - } - count := 0 - for cursor.Next(ctx) { - count += cursor.Batch().DocumentCount() - } - if cursor.Err() != nil { - _ = cursor.Close(ctx) - log.Printf("%d-failed iterating aggregate results: %s", idx, cursor.Err()) - return - } - _ = cursor.Close(ctx) - - log.Printf("%d-iterated %d docs", idx, count) - } - } -} - -func parseNamespace(ns string) (collection, database string, err error) { - idx := strings.Index(ns, ".") - if idx == -1 { - return "", "", fmt.Errorf("invalid namespace: %s", ns) - } - return ns[idx+1:], ns[:idx], nil -}