Skip to content

Commit 21a6b9b

Browse files
GODRIVER-3502: Refactor and remove builder pattern for MongoCryptOptions (#2278)
1 parent d6ad234 commit 21a6b9b

File tree

6 files changed

+47
-180
lines changed

6 files changed

+47
-180
lines changed

mongo/client.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -651,15 +651,16 @@ func (c *Client) newMongoCrypt(opts *options.AutoEncryptionOptions) (*mongocrypt
651651
bypassAutoEncryption := opts.BypassAutoEncryption != nil && *opts.BypassAutoEncryption
652652
bypassQueryAnalysis := opts.BypassQueryAnalysis != nil && *opts.BypassQueryAnalysis
653653

654-
mc, err := mongocrypt.NewMongoCrypt(mcopts.MongoCrypt().
655-
SetKmsProviders(kmsProviders).
656-
SetLocalSchemaMap(cryptSchemaMap).
657-
SetBypassQueryAnalysis(bypassQueryAnalysis).
658-
SetEncryptedFieldsMap(cryptEncryptedFieldsMap).
659-
SetCryptSharedLibDisabled(cryptSharedLibDisabled || bypassAutoEncryption).
660-
SetCryptSharedLibOverridePath(cryptSharedLibPath).
661-
SetHTTPClient(opts.HTTPClient).
662-
SetKeyExpiration(opts.KeyExpiration))
654+
mc, err := mongocrypt.NewMongoCrypt(&mcopts.MongoCryptOptions{
655+
KmsProviders: kmsProviders,
656+
LocalSchemaMap: cryptSchemaMap,
657+
BypassQueryAnalysis: bypassQueryAnalysis,
658+
EncryptedFieldsMap: cryptEncryptedFieldsMap,
659+
CryptSharedLibDisabled: cryptSharedLibDisabled || bypassAutoEncryption,
660+
CryptSharedLibOverridePath: cryptSharedLibPath,
661+
HTTPClient: opts.HTTPClient,
662+
KeyExpiration: opts.KeyExpiration,
663+
})
663664
if err != nil {
664665
return nil, err
665666
}

mongo/client_encryption.go

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@ func NewClientEncryption(keyVaultClient *Client, opts ...options.Lister[options.
5353
return nil, fmt.Errorf("error creating KMS providers map: %w", err)
5454
}
5555

56-
mc, err := mongocrypt.NewMongoCrypt(mcopts.MongoCrypt().
57-
SetKmsProviders(kmsProviders).
56+
mc, err := mongocrypt.NewMongoCrypt(&mcopts.MongoCryptOptions{
57+
KmsProviders: kmsProviders,
5858
// Explicitly disable loading the crypt_shared library for the Crypt used for
5959
// ClientEncryption because it's only needed for AutoEncryption and we don't expect users to
6060
// have the crypt_shared library installed if they're using ClientEncryption.
61-
SetCryptSharedLibDisabled(true).
62-
SetHTTPClient(cea.HTTPClient).
63-
SetKeyExpiration(cea.KeyExpiration))
61+
CryptSharedLibDisabled: true,
62+
HTTPClient: cea.HTTPClient,
63+
KeyExpiration: cea.KeyExpiration,
64+
})
6465
if err != nil {
6566
return nil, err
6667
}
@@ -173,7 +174,10 @@ func (ce *ClientEncryption) CreateDataKey(
173174
return bson.Binary{}, fmt.Errorf("failed to construct options from builder: %w", err)
174175
}
175176

176-
co := mcopts.DataKey().SetKeyAltNames(args.KeyAltNames)
177+
co := &mcopts.DataKeyOptions{
178+
KeyAltNames: args.KeyAltNames,
179+
KeyMaterial: args.KeyMaterial,
180+
}
177181
if args.MasterKey != nil {
178182
keyDoc, err := marshal(
179183
args.MasterKey,
@@ -182,10 +186,7 @@ func (ce *ClientEncryption) CreateDataKey(
182186
if err != nil {
183187
return bson.Binary{}, err
184188
}
185-
co.SetMasterKey(keyDoc)
186-
}
187-
if args.KeyMaterial != nil {
188-
co.SetKeyMaterial(args.KeyMaterial)
189+
co.MasterKey = keyDoc
189190
}
190191

191192
// create data key document
@@ -208,18 +209,12 @@ func (ce *ClientEncryption) CreateDataKey(
208209
func transformExplicitEncryptionOptions(opts ...options.Lister[options.EncryptOptions]) *mcopts.ExplicitEncryptionOptions {
209210
args, _ := mongoutil.NewOptions[options.EncryptOptions](opts...)
210211

211-
transformed := mcopts.ExplicitEncryption()
212-
if args.KeyID != nil {
213-
transformed.SetKeyID(*args.KeyID)
214-
}
215-
if args.KeyAltName != nil {
216-
transformed.SetKeyAltName(*args.KeyAltName)
217-
}
218-
transformed.SetAlgorithm(args.Algorithm)
219-
transformed.SetQueryType(args.QueryType)
220-
221-
if args.ContentionFactor != nil {
222-
transformed.SetContentionFactor(*args.ContentionFactor)
212+
transformed := &mcopts.ExplicitEncryptionOptions{
213+
KeyID: args.KeyID,
214+
KeyAltName: args.KeyAltName,
215+
Algorithm: args.Algorithm,
216+
QueryType: args.QueryType,
217+
ContentionFactor: args.ContentionFactor,
223218
}
224219

225220
if args.RangeOptions != nil {
@@ -241,7 +236,7 @@ func transformExplicitEncryptionOptions(opts ...options.Lister[options.EncryptOp
241236
if rangeArgs.TrimFactor != nil {
242237
transformedRange.TrimFactor = rangeArgs.TrimFactor
243238
}
244-
transformed.SetRangeOptions(transformedRange)
239+
transformed.RangeOptions = &transformedRange
245240
}
246241
return transformed
247242
}
@@ -468,7 +463,9 @@ func (ce *ClientEncryption) RewrapManyDataKey(
468463
}
469464

470465
// Transfer rmdko options to /x/ package options to publish the mongocrypt feed.
471-
co := mcopts.RewrapManyDataKey()
466+
co := &mcopts.RewrapManyDataKeyOptions{
467+
Provider: args.Provider,
468+
}
472469
if args.MasterKey != nil {
473470
keyDoc, err := marshal(
474471
args.MasterKey,
@@ -477,10 +474,7 @@ func (ce *ClientEncryption) RewrapManyDataKey(
477474
if err != nil {
478475
return nil, err
479476
}
480-
co.SetMasterKey(keyDoc)
481-
}
482-
if args.Provider != nil {
483-
co.SetProvider(*args.Provider)
477+
co.MasterKey = keyDoc
484478
}
485479

486480
// Prepare the filters and rewrap the data key using mongocrypt.

x/mongo/driver/mongocrypt/binary_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func createEncryptedDocForBench(b *testing.B, crypt *MongoCrypt, iter int) bsonc
7070
Data: []byte("aaaaaaaaaaaaaaaa"),
7171
}
7272

73-
encryptOpts := options.ExplicitEncryption().SetAlgorithm(algorithm).SetKeyID(keyID)
73+
encryptOpts := &options.ExplicitEncryptionOptions{Algorithm: algorithm, KeyID: &keyID}
7474
doc := bsoncore.NewDocumentBuilder().AppendString("v", fmt.Sprintf("value %04v", iter)).Build()
7575

7676
encryptCtx, err := crypt.CreateExplicitEncryptionContext(doc, encryptOpts)
@@ -141,7 +141,9 @@ func newCryptForBench(b *testing.B) *MongoCrypt {
141141
AppendDocument("local", localProvider).
142142
Build()
143143

144-
cryptOpts := options.MongoCrypt().SetKmsProviders(kmsProviders)
144+
cryptOpts := &options.MongoCryptOptions{
145+
KmsProviders: kmsProviders,
146+
}
145147

146148
crypt, err := NewMongoCrypt(cryptOpts)
147149

x/mongo/driver/mongocrypt/mongocrypt_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ func createMongoCrypt(t *testing.T) *MongoCrypt {
6161
AppendDocument("local", localProvider).
6262
Build()
6363

64-
cryptOpts := options.MongoCrypt().SetKmsProviders(kmsProviders)
64+
cryptOpts := &options.MongoCryptOptions{
65+
KmsProviders: kmsProviders,
66+
}
6567
crypt, err := NewMongoCrypt(cryptOpts)
6668
noerr(t, err)
6769
if crypt == nil {
@@ -216,7 +218,10 @@ func TestMongoCrypt(t *testing.T) {
216218
AppendString("secretAccessKey", "example").
217219
FinishDocument().
218220
Build()
219-
cryptOpts := options.MongoCrypt().SetKmsProviders(kmsProviders).SetLocalSchemaMap(schemaMap)
221+
cryptOpts := &options.MongoCryptOptions{
222+
KmsProviders: kmsProviders,
223+
LocalSchemaMap: schemaMap,
224+
}
220225
crypt, err := NewMongoCrypt(cryptOpts)
221226
noerr(t, err)
222227
defer crypt.Close()
@@ -289,7 +294,7 @@ func TestMongoCrypt(t *testing.T) {
289294
masterKey, _ = bsoncore.AppendDocumentEnd(masterKey, midx)
290295

291296
// create data key context and check initial state
292-
dataKeyOpts := options.DataKey().SetMasterKey(masterKey)
297+
dataKeyOpts := &options.DataKeyOptions{MasterKey: masterKey}
293298
dataKeyCtx, err := crypt.CreateDataKeyContext("local", dataKeyOpts)
294299
noerr(t, err)
295300
defer dataKeyCtx.Close()
@@ -320,7 +325,7 @@ func TestMongoCrypt(t *testing.T) {
320325
Subtype: 0x04, // 0x04 is UUID subtype
321326
Data: []byte("aaaaaaaaaaaaaaaa"),
322327
}
323-
opts := options.ExplicitEncryption().SetKeyID(keyID).SetAlgorithm(algorithm)
328+
opts := &options.ExplicitEncryptionOptions{KeyID: &keyID, Algorithm: algorithm}
324329
encryptCtx, err := crypt.CreateExplicitEncryptionContext(originalDoc, opts)
325330
noerr(t, err)
326331
defer encryptCtx.Close()
@@ -353,7 +358,8 @@ func TestMongoCrypt(t *testing.T) {
353358
defer crypt.Close()
354359

355360
// create explicit encryption context and check initial state
356-
opts := options.ExplicitEncryption().SetKeyAltName("altKeyName").SetAlgorithm(algorithm)
361+
keyAltName := "altKeyName"
362+
opts := &options.ExplicitEncryptionOptions{KeyAltName: &keyAltName, Algorithm: algorithm}
357363
encryptCtx, err := crypt.CreateExplicitEncryptionContext(originalDoc, opts)
358364
noerr(t, err)
359365
defer encryptCtx.Close()

x/mongo/driver/mongocrypt/options/mongocrypt_context_options.go

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,6 @@ type DataKeyOptions struct {
1818
MasterKey bsoncore.Document
1919
}
2020

21-
// DataKey creates a new DataKeyOptions instance.
22-
func DataKey() *DataKeyOptions {
23-
return &DataKeyOptions{}
24-
}
25-
26-
// SetKeyAltNames specifies alternate key names.
27-
func (dko *DataKeyOptions) SetKeyAltNames(names []string) *DataKeyOptions {
28-
dko.KeyAltNames = names
29-
return dko
30-
}
31-
32-
// SetMasterKey specifies the master key.
33-
func (dko *DataKeyOptions) SetMasterKey(key bsoncore.Document) *DataKeyOptions {
34-
dko.MasterKey = key
35-
return dko
36-
}
37-
38-
// SetKeyMaterial specifies the key material.
39-
func (dko *DataKeyOptions) SetKeyMaterial(keyMaterial []byte) *DataKeyOptions {
40-
dko.KeyMaterial = keyMaterial
41-
return dko
42-
}
43-
4421
// QueryType describes the type of query the result of Encrypt is used for.
4522
type QueryType int
4623

@@ -68,47 +45,6 @@ type ExplicitRangeOptions struct {
6845
Precision *int32
6946
}
7047

71-
// ExplicitEncryption creates a new ExplicitEncryptionOptions instance.
72-
func ExplicitEncryption() *ExplicitEncryptionOptions {
73-
return &ExplicitEncryptionOptions{}
74-
}
75-
76-
// SetKeyID sets the key identifier.
77-
func (eeo *ExplicitEncryptionOptions) SetKeyID(keyID bson.Binary) *ExplicitEncryptionOptions {
78-
eeo.KeyID = &keyID
79-
return eeo
80-
}
81-
82-
// SetKeyAltName sets the key alternative name.
83-
func (eeo *ExplicitEncryptionOptions) SetKeyAltName(keyAltName string) *ExplicitEncryptionOptions {
84-
eeo.KeyAltName = &keyAltName
85-
return eeo
86-
}
87-
88-
// SetAlgorithm specifies an encryption algorithm.
89-
func (eeo *ExplicitEncryptionOptions) SetAlgorithm(algorithm string) *ExplicitEncryptionOptions {
90-
eeo.Algorithm = algorithm
91-
return eeo
92-
}
93-
94-
// SetQueryType specifies the query type.
95-
func (eeo *ExplicitEncryptionOptions) SetQueryType(queryType string) *ExplicitEncryptionOptions {
96-
eeo.QueryType = queryType
97-
return eeo
98-
}
99-
100-
// SetContentionFactor specifies the contention factor.
101-
func (eeo *ExplicitEncryptionOptions) SetContentionFactor(contentionFactor int64) *ExplicitEncryptionOptions {
102-
eeo.ContentionFactor = &contentionFactor
103-
return eeo
104-
}
105-
106-
// SetRangeOptions specifies the range options.
107-
func (eeo *ExplicitEncryptionOptions) SetRangeOptions(ro ExplicitRangeOptions) *ExplicitEncryptionOptions {
108-
eeo.RangeOptions = &ro
109-
return eeo
110-
}
111-
11248
// RewrapManyDataKeyOptions represents all possible options used to decrypt and encrypt all matching data keys with a
11349
// possibly new masterKey.
11450
type RewrapManyDataKeyOptions struct {
@@ -118,20 +54,3 @@ type RewrapManyDataKeyOptions struct {
11854
// MasterKey identifies the new masterKey. If omitted, rewraps with the current masterKey.
11955
MasterKey bsoncore.Document
12056
}
121-
122-
// RewrapManyDataKey creates a new RewrapManyDataKeyOptions instance.
123-
func RewrapManyDataKey() *RewrapManyDataKeyOptions {
124-
return new(RewrapManyDataKeyOptions)
125-
}
126-
127-
// SetProvider sets the value for the Provider field.
128-
func (rmdko *RewrapManyDataKeyOptions) SetProvider(provider string) *RewrapManyDataKeyOptions {
129-
rmdko.Provider = &provider
130-
return rmdko
131-
}
132-
133-
// SetMasterKey sets the value for the MasterKey field.
134-
func (rmdko *RewrapManyDataKeyOptions) SetMasterKey(masterKey bsoncore.Document) *RewrapManyDataKeyOptions {
135-
rmdko.MasterKey = masterKey
136-
return rmdko
137-
}

x/mongo/driver/mongocrypt/options/mongocrypt_options.go

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,58 +24,3 @@ type MongoCryptOptions struct {
2424
HTTPClient *http.Client
2525
KeyExpiration *time.Duration
2626
}
27-
28-
// MongoCrypt creates a new MongoCryptOptions instance.
29-
func MongoCrypt() *MongoCryptOptions {
30-
return &MongoCryptOptions{}
31-
}
32-
33-
// SetKmsProviders specifies the KMS providers map.
34-
func (mo *MongoCryptOptions) SetKmsProviders(kmsProviders bsoncore.Document) *MongoCryptOptions {
35-
mo.KmsProviders = kmsProviders
36-
return mo
37-
}
38-
39-
// SetLocalSchemaMap specifies the local schema map.
40-
func (mo *MongoCryptOptions) SetLocalSchemaMap(localSchemaMap map[string]bsoncore.Document) *MongoCryptOptions {
41-
mo.LocalSchemaMap = localSchemaMap
42-
return mo
43-
}
44-
45-
// SetBypassQueryAnalysis skips the NeedMongoMarkings state.
46-
func (mo *MongoCryptOptions) SetBypassQueryAnalysis(bypassQueryAnalysis bool) *MongoCryptOptions {
47-
mo.BypassQueryAnalysis = bypassQueryAnalysis
48-
return mo
49-
}
50-
51-
// SetEncryptedFieldsMap specifies the encrypted fields map.
52-
func (mo *MongoCryptOptions) SetEncryptedFieldsMap(efcMap map[string]bsoncore.Document) *MongoCryptOptions {
53-
mo.EncryptedFieldsMap = efcMap
54-
return mo
55-
}
56-
57-
// SetCryptSharedLibDisabled explicitly disables loading the crypt_shared library if set to true.
58-
func (mo *MongoCryptOptions) SetCryptSharedLibDisabled(disabled bool) *MongoCryptOptions {
59-
mo.CryptSharedLibDisabled = disabled
60-
return mo
61-
}
62-
63-
// SetCryptSharedLibOverridePath sets the override path to the crypt_shared library file. Setting
64-
// an override path disables the default operating system dynamic library search path.
65-
func (mo *MongoCryptOptions) SetCryptSharedLibOverridePath(path string) *MongoCryptOptions {
66-
mo.CryptSharedLibOverridePath = path
67-
return mo
68-
}
69-
70-
// SetHTTPClient sets the http client.
71-
func (mo *MongoCryptOptions) SetHTTPClient(httpClient *http.Client) *MongoCryptOptions {
72-
mo.HTTPClient = httpClient
73-
return mo
74-
}
75-
76-
// SetKeyExpiration sets the key expiration duration. 0 means "never expire".
77-
// The granularity is in milliseconds. Any sub-millisecond fraction will be rounded up.
78-
func (mo *MongoCryptOptions) SetKeyExpiration(expiration *time.Duration) *MongoCryptOptions {
79-
mo.KeyExpiration = expiration
80-
return mo
81-
}

0 commit comments

Comments
 (0)