Skip to content

Commit 9add837

Browse files
committed
revert refactor in mongo/client.go
1 parent c7a6219 commit 9add837

File tree

1 file changed

+50
-28
lines changed

1 file changed

+50
-28
lines changed

mongo/client.go

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func newClient(opts ...*options.ClientOptions) (*Client, error) {
193193
}
194194
// AutoEncryptionOptions
195195
if clientOpts.AutoEncryptionOptions != nil {
196-
if err = client.configureAutoEncryption(clientOpts); err != nil {
196+
if err := client.configureAutoEncryption(clientOpts); err != nil {
197197
return nil, err
198198
}
199199
} else {
@@ -471,48 +471,30 @@ func (c *Client) endSessions(ctx context.Context) {
471471
}
472472

473473
func (c *Client) configureAutoEncryption(args *options.ClientOptions) error {
474-
aeOpts := args.AutoEncryptionOptions
475-
c.encryptedFieldsMap = aeOpts.EncryptedFieldsMap
474+
c.encryptedFieldsMap = args.AutoEncryptionOptions.EncryptedFieldsMap
476475
if err := c.configureKeyVaultClientFLE(args); err != nil {
477476
return err
478477
}
479478

480-
mc, err := c.newMongoCrypt(aeOpts)
479+
if err := c.configureMetadataClientFLE(args); err != nil {
480+
return err
481+
}
482+
483+
mc, err := c.newMongoCrypt(args.AutoEncryptionOptions)
481484
if err != nil {
482485
return err
483486
}
484487

485488
// If the crypt_shared library was not loaded, try to spawn and connect to mongocryptd.
486489
if mc.CryptSharedLibVersionString() == "" {
487-
c.mongocryptdFLE, err = newMongocryptdClient(aeOpts)
490+
mongocryptdFLE, err := newMongocryptdClient(args.AutoEncryptionOptions)
488491
if err != nil {
489492
return err
490493
}
494+
c.mongocryptdFLE = mongocryptdFLE
491495
}
492496

493-
kr := keyRetriever{coll: c.keyVaultCollFLE}
494-
var cir collInfoRetriever
495-
bypass := aeOpts.BypassAutoEncryption != nil && *aeOpts.BypassAutoEncryption
496-
if !bypass {
497-
if args.MaxPoolSize != nil && *args.MaxPoolSize == 0 {
498-
c.metadataClientFLE = c
499-
} else {
500-
c.metadataClientFLE, err = c.getOrCreateInternalClient(args)
501-
if err != nil {
502-
return err
503-
}
504-
}
505-
cir.client = c.metadataClientFLE
506-
}
507-
508-
c.cryptFLE = driver.NewCrypt(&driver.CryptOptions{
509-
MongoCrypt: mc,
510-
CollInfoFn: cir.cryptCollInfo,
511-
KeyFn: kr.cryptKeys,
512-
MarkFn: c.mongocryptdFLE.markCommand,
513-
TLSConfig: aeOpts.TLSConfig,
514-
BypassAutoEncryption: bypass,
515-
})
497+
c.configureCryptFLE(mc, args.AutoEncryptionOptions)
516498
return nil
517499
}
518500

@@ -555,6 +537,24 @@ func (c *Client) configureKeyVaultClientFLE(clientOpts *options.ClientOptions) e
555537
return nil
556538
}
557539

540+
func (c *Client) configureMetadataClientFLE(clientOpts *options.ClientOptions) error {
541+
aeOpts := clientOpts.AutoEncryptionOptions
542+
543+
if aeOpts.BypassAutoEncryption != nil && *aeOpts.BypassAutoEncryption {
544+
// no need for a metadata client.
545+
return nil
546+
}
547+
if clientOpts.MaxPoolSize != nil && *clientOpts.MaxPoolSize == 0 {
548+
c.metadataClientFLE = c
549+
return nil
550+
}
551+
552+
var err error
553+
c.metadataClientFLE, err = c.getOrCreateInternalClient(clientOpts)
554+
555+
return err
556+
}
557+
558558
func (c *Client) newMongoCrypt(opts *options.AutoEncryptionOptions) (*mongocrypt.MongoCrypt, error) {
559559
// convert schemas in SchemaMap to bsoncore documents
560560
cryptSchemaMap := make(map[string]bsoncore.Document)
@@ -638,6 +638,28 @@ func (c *Client) newMongoCrypt(opts *options.AutoEncryptionOptions) (*mongocrypt
638638
return mc, nil
639639
}
640640

641+
//nolint:unused // the unused linter thinks that this function is unreachable because "c.newMongoCrypt" always panics without the "cse" build tag set.
642+
func (c *Client) configureCryptFLE(mc *mongocrypt.MongoCrypt, opts *options.AutoEncryptionOptions) {
643+
bypass := opts.BypassAutoEncryption != nil && *opts.BypassAutoEncryption
644+
kr := keyRetriever{coll: c.keyVaultCollFLE}
645+
var cir collInfoRetriever
646+
// If bypass is true, c.metadataClientFLE is nil and the collInfoRetriever
647+
// will not be used. If bypass is false, to the parent client or the
648+
// internal client.
649+
if !bypass {
650+
cir = collInfoRetriever{client: c.metadataClientFLE}
651+
}
652+
653+
c.cryptFLE = driver.NewCrypt(&driver.CryptOptions{
654+
MongoCrypt: mc,
655+
CollInfoFn: cir.cryptCollInfo,
656+
KeyFn: kr.cryptKeys,
657+
MarkFn: c.mongocryptdFLE.markCommand,
658+
TLSConfig: opts.TLSConfig,
659+
BypassAutoEncryption: bypass,
660+
})
661+
}
662+
641663
// validSession returns an error if the session doesn't belong to the client
642664
func (c *Client) validSession(sess *session.Client) error {
643665
if sess != nil && sess.ClientID != c.id {

0 commit comments

Comments
 (0)