Skip to content

Commit

Permalink
GODRIVER-3038 Apply CSE to IndexView operations (#1636)
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvasquez authored May 21, 2024
1 parent 8452cb3 commit a18e483
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 20 deletions.
74 changes: 72 additions & 2 deletions internal/integration/client_side_encryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,12 +660,82 @@ func TestFLE2CreateCollectionWithAutoEncryption(t *testing.T) {
encryptedClient.Database("db").CreateCollection(context.Background(), "coll", options.CreateCollection().SetEncryptedFields(encryptedFields))

// Check resulting events sent.
assert.Equal(mt, startedCommands, []string{
assert.Equal(mt, []string{
"create", // Create ESC collection.
"create", // Create ECOC collection.
"create", // Create 'coll' collection.
"listCollections", // Run listCollections when processing `createIndexes` command for automatic encryption.
"createIndexes",
})
}, startedCommands)
})
}

func TestFLEIndexView(t *testing.T) {
verifyClientSideEncryptionVarsSet(t)

mt := mtest.New(t, mtest.NewOptions().MinServerVersion("4.2").Enterprise(true).CreateClient(false))

opts := options.Client().ApplyURI(mtest.ClusterURI()).SetWriteConcern(mtest.MajorityWc).
SetReadPreference(mtest.PrimaryRp)

cc := &customCrypt{}
opts.Crypt = cc

integtest.AddTestServerAPIVersion(opts)

client, err := mongo.Connect(opts)
assert.NoError(mt, err)

mt.Cleanup(func() { client.Disconnect(context.Background()) })

coll := client.Database("db").Collection("coll")

err = coll.Drop(context.Background())
assert.Nil(mt, err, "Drop error: %v", err)

mt.Run("create many", func(mt *mtest.T) {
createIndexes(mt, coll, 2)

assert.Equal(mt, cc.numEncryptCalls, 2, "expected 2 calls to Encrypt, got %v", cc.numEncryptCalls)
})

mt.Run("list", func(mt *mtest.T) {
cc.numEncryptCalls = 0 // Reset Encrypt calls from createIndexes

_, err := coll.Indexes().List(context.Background(), options.ListIndexes().SetBatchSize(2))
assert.NoError(mt, err, "error creating list cursor: %v", err)

assert.Equal(mt, cc.numEncryptCalls, 1, "expected 1 call to Encrypt, got %v", cc.numEncryptCalls)
})

mt.Run("list specifications", func(mt *mtest.T) {
cc.numEncryptCalls = 0 // Reset Encrypt calls from createIndexes

_, err := coll.Indexes().ListSpecifications(context.Background())
assert.NoError(mt, err, "error listing specifications : %v", err)

assert.Equal(mt, cc.numEncryptCalls, 1, "expected 1 call to Encrypt, got %v", cc.numEncryptCalls)
})

mt.Run("drop one", func(mt *mtest.T) {
createIndexes(mt, coll, 1)

cc.numEncryptCalls = 0 // Reset Encrypt calls from createIndexes

_, err := coll.Indexes().DropOne(context.Background(), "a_1")
assert.NoError(mt, err, "error dropping one index: %v", err)

assert.Equal(mt, cc.numEncryptCalls, 1, "expected 1 call to Encrypt, got %v", cc.numEncryptCalls)
})

mt.Run("drop all", func(mt *mtest.T) {
createIndexes(mt, coll, 2)

cc.numEncryptCalls = 0 // Reset Encrypt calls from createIndexes

err := coll.Indexes().DropAll(context.Background())
assert.NoError(mt, err, "error dropping all indexes: %v", err)

assert.Equal(mt, cc.numEncryptCalls, 1, "expected 1 call to Encrypt, got %v", cc.numEncryptCalls)
})
}
32 changes: 16 additions & 16 deletions internal/integration/index_view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,6 @@ func TestIndexView(t *testing.T) {
var pint32 = func(i int32) *int32 { return &i }

mt.Run("list", func(mt *mtest.T) {
createIndexes := func(mt *mtest.T, numIndexes int) {
mt.Helper()

models := make([]mongo.IndexModel, 0, numIndexes)
for i, key := 0, 'a'; i < numIndexes; i, key = i+1, key+1 {
models = append(models, mongo.IndexModel{
Keys: bson.M{string(key): 1},
})
}

_, err := mt.Coll.Indexes().CreateMany(context.Background(), models)
assert.Nil(mt, err, "CreateMany error: %v", err)
}

// For server versions below 3.0, we internally execute List() as a legacy OP_QUERY against the system.indexes
// collection. Command monitoring upconversions translate this to a "find" command rather than "listIndexes".
cmdName := "listIndexes"
Expand All @@ -62,13 +48,13 @@ func TestIndexView(t *testing.T) {
})
})
mt.Run("getMore commands are monitored", func(mt *mtest.T) {
createIndexes(mt, 2)
createIndexes(mt, mt.Coll, 2)
assertGetMoreCommandsAreMonitored(mt, cmdName, func() (*mongo.Cursor, error) {
return mt.Coll.Indexes().List(context.Background(), options.ListIndexes().SetBatchSize(2))
})
})
mt.Run("killCursors commands are monitored", func(mt *mtest.T) {
createIndexes(mt, 2)
createIndexes(mt, mt.Coll, 2)
assertKillCursorsCommandsAreMonitored(mt, cmdName, func() (*mongo.Cursor, error) {
return mt.Coll.Indexes().List(context.Background(), options.ListIndexes().SetBatchSize(2))
})
Expand Down Expand Up @@ -723,3 +709,17 @@ func verifyIndexExists(mt *mtest.T, iv mongo.IndexView, expected index) {
assert.Nil(mt, cursor.Err(), "cursor error: %v", err)
assert.True(mt, found, "expected to find index %v but was not found", expected.Name)
}

func createIndexes(mt *mtest.T, coll *mongo.Collection, numIndexes int) {
mt.Helper()

models := make([]mongo.IndexModel, 0, numIndexes)
for i, key := 0, 'a'; i < numIndexes; i, key = i+1, key+1 {
models = append(models, mongo.IndexModel{
Keys: bson.M{string(key): 1},
})
}

_, err := coll.Indexes().CreateMany(context.Background(), models)
assert.Nil(mt, err, "CreateMany error: %v", err)
}
4 changes: 2 additions & 2 deletions mongo/index_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (iv IndexView) List(ctx context.Context, opts ...*options.ListIndexesOption
ServerSelector(selector).ClusterClock(iv.coll.client.clock).
Database(iv.coll.db.name).Collection(iv.coll.name).
Deployment(iv.coll.client.deployment).ServerAPI(iv.coll.client.serverAPI).
Timeout(iv.coll.client.timeout)
Timeout(iv.coll.client.timeout).Crypt(iv.coll.client.cryptFLE)

cursorOpts := iv.coll.client.createBaseCursorOptions()

Expand Down Expand Up @@ -418,7 +418,7 @@ func (iv IndexView) drop(ctx context.Context, name string, opts ...*options.Drop
ServerSelector(selector).ClusterClock(iv.coll.client.clock).
Database(iv.coll.db.name).Collection(iv.coll.name).
Deployment(iv.coll.client.deployment).ServerAPI(iv.coll.client.serverAPI).
Timeout(iv.coll.client.timeout).MaxTime(dio.MaxTime)
Timeout(iv.coll.client.timeout).MaxTime(dio.MaxTime).Crypt(iv.coll.client.cryptFLE)

err = op.Execute(ctx)
if err != nil {
Expand Down

0 comments on commit a18e483

Please sign in to comment.