...

Source file src/go.mongodb.org/mongo-driver/mongo/integration/csot_cse_prose_test.go

Documentation: go.mongodb.org/mongo-driver/mongo/integration

     1  // Copyright (C) MongoDB, Inc. 2022-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  //go:build cse
     8  // +build cse
     9  
    10  package integration
    11  
    12  import (
    13  	"context"
    14  	"testing"
    15  
    16  	"go.mongodb.org/mongo-driver/bson"
    17  	"go.mongodb.org/mongo-driver/event"
    18  	"go.mongodb.org/mongo-driver/internal/assert"
    19  	"go.mongodb.org/mongo-driver/internal/integtest"
    20  	"go.mongodb.org/mongo-driver/mongo"
    21  	"go.mongodb.org/mongo-driver/mongo/integration/mtest"
    22  	"go.mongodb.org/mongo-driver/mongo/options"
    23  )
    24  
    25  // CSOT prose tests that require the 'cse' Go build tag.
    26  func TestCSOTClientSideEncryptionProse(t *testing.T) {
    27  	verifyClientSideEncryptionVarsSet(t)
    28  	mt := mtest.New(t, mtest.NewOptions().MinServerVersion("4.2").CreateClient(false))
    29  
    30  	mt.RunOpts("2. maxTimeMS is not set for commands sent to mongocryptd",
    31  		noClientOpts, func(mt *mtest.T) {
    32  			kmsProviders := map[string]map[string]interface{}{
    33  				"local": {
    34  					"key": localMasterKey,
    35  				},
    36  			}
    37  			mongocryptdSpawnArgs := map[string]interface{}{
    38  				// Pass a custom pidfilepath to ensure a new mongocryptd process is spawned.
    39  				"mongocryptdSpawnArgs": []string{"--port=23000", "--pidfilepath=TestCSOTClientSideEncryptionProse_1.pid"},
    40  				"mongocryptdURI":       "mongodb://localhost:23000",
    41  				// Do not use the shared library to ensure mongocryptd is spawned.
    42  				"__cryptSharedLibDisabledForTestOnly": true,
    43  			}
    44  
    45  			// Setup encrypted client to cause spawning of mongocryptd on port 23000.
    46  			aeo := options.AutoEncryption().SetKmsProviders(kmsProviders).
    47  				SetExtraOptions(mongocryptdSpawnArgs)
    48  			cliOpts := options.Client().ApplyURI(mtest.ClusterURI()).SetAutoEncryptionOptions(aeo)
    49  			integtest.AddTestServerAPIVersion(cliOpts)
    50  			encClient, err := mongo.Connect(context.Background(), cliOpts)
    51  			assert.Nil(mt, err, "Connect error: %v", err)
    52  			defer func() {
    53  				err = encClient.Disconnect(context.Background())
    54  				assert.Nil(mt, err, "encrypted client Disconnect error: %v", err)
    55  			}()
    56  
    57  			// Run a Find through the encrypted client to make sure mongocryptd is started ('find' uses the
    58  			// mongocryptd and will wait for it to be active).
    59  			_, err = encClient.Database("test").Collection("test").Find(context.Background(), bson.D{})
    60  			assert.Nil(mt, err, "Find error: %v", err)
    61  
    62  			// Use a new Client to connect to 23000 where mongocryptd should be running. Use a custom
    63  			// command monitor to examine the eventual 'ping'.
    64  			var started *event.CommandStartedEvent
    65  			mcryptMonitor := &event.CommandMonitor{
    66  				Started: func(_ context.Context, evt *event.CommandStartedEvent) {
    67  					started = evt
    68  				},
    69  			}
    70  			mcryptOpts := options.Client().SetMonitor(mcryptMonitor).
    71  				ApplyURI("mongodb://localhost:23000/?timeoutMS=1000")
    72  			integtest.AddTestServerAPIVersion(mcryptOpts)
    73  			mcryptClient, err := mongo.Connect(context.Background(), mcryptOpts)
    74  			assert.Nil(mt, err, "mongocryptd Connect error: %v", err)
    75  			defer func() {
    76  				err = mcryptClient.Disconnect(context.Background())
    77  				assert.Nil(mt, err, "mongocryptd Disconnect error: %v", err)
    78  			}()
    79  
    80  			// Run Ping and assert that sent command does not contain 'maxTimeMS'. The 'ping' command
    81  			// does not exist on mongocryptd, so ignore the CommandNotFound error.
    82  			_ = mcryptClient.Ping(context.Background(), nil)
    83  			assert.NotNil(mt, started, "expected a CommandStartedEvent, got nil")
    84  			assert.Equal(mt, started.CommandName, "ping", "expected 'ping', got %q", started.CommandName)
    85  			commandElems, err := started.Command.Elements()
    86  			assert.Nil(mt, err, "Elements error: %v", err)
    87  			for _, elem := range commandElems {
    88  				assert.NotEqual(mt, elem.Key(), "maxTimeMS",
    89  					"expected no 'maxTimeMS' field in ping to mongocryptd")
    90  			}
    91  		})
    92  }
    93  

View as plain text