Skip to content

Commit eadc7dd

Browse files
committed
GODRIVER-2584 Error if RewrapManyDataKey is called with MasterKey and without Provider (#1270)
* add failing prose test * return error if MasterKey is set, but Provider is nil
1 parent 604cc07 commit eadc7dd

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

mongo/integration/client_side_encryption_prose_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,6 +1988,30 @@ func TestClientSideEncryptionProse(t *testing.T) {
19881988
}
19891989
}
19901990
})
1991+
1992+
mt.Run("Case 2: RewrapManyDataKeyOpts.provider is not optional", func(mt *mtest.T) {
1993+
var err error
1994+
var clientEncryption *mongo.ClientEncryption
1995+
{
1996+
var keyVaultClient *mongo.Client
1997+
{
1998+
co := options.Client().ApplyURI(mtest.ClusterURI())
1999+
keyVaultClient, err = mongo.Connect(context.Background(), co)
2000+
defer keyVaultClient.Disconnect(context.Background())
2001+
testutil.AddTestServerAPIVersion(co)
2002+
assert.Nil(mt, err, "error on Connect: %v", err)
2003+
}
2004+
ceOpts := options.ClientEncryption().
2005+
SetKeyVaultNamespace("keyvault.datakeys").
2006+
SetKmsProviders(fullKmsProvidersMap)
2007+
clientEncryption, err = mongo.NewClientEncryption(keyVaultClient, ceOpts)
2008+
assert.Nil(mt, err, "error in NewClientEncryption: %v", err)
2009+
defer clientEncryption.Close(context.Background())
2010+
}
2011+
2012+
_, err = clientEncryption.RewrapManyDataKey(context.Background(), bson.D{}, options.RewrapManyDataKey().SetMasterKey(bson.D{}))
2013+
assert.True(mt, strings.Contains(err.Error(), "expected 'Provider' to be set to identify type of 'MasterKey'"), "unexpected error message: %v", err)
2014+
})
19912015
})
19922016
}
19932017

x/mongo/driver/mongocrypt/mongocrypt.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,11 @@ func (m *MongoCrypt) RewrapDataKeyContext(filter []byte, opts *options.RewrapMan
321321
return nil, m.createErrorFromStatus()
322322
}
323323

324+
if opts.MasterKey != nil && opts.Provider == nil {
325+
// Provider is nil, but MasterKey is set. This is an error.
326+
return nil, fmt.Errorf("expected 'Provider' to be set to identify type of 'MasterKey'")
327+
}
328+
324329
if opts.Provider != nil {
325330
// If a provider has been specified, create an encryption key document for creating a data key or for rewrapping
326331
// datakeys. If a new provider is not specified, then the filter portion of this logic returns the data as it

0 commit comments

Comments
 (0)