@@ -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,30 +471,48 @@ func (c *Client) endSessions(ctx context.Context) {
471471}
472472
473473func (c * Client ) configureAutoEncryption (args * options.ClientOptions ) error {
474- c .encryptedFieldsMap = args .AutoEncryptionOptions .EncryptedFieldsMap
474+ aeOpts := args .AutoEncryptionOptions
475+ c .encryptedFieldsMap = aeOpts .EncryptedFieldsMap
475476 if err := c .configureKeyVaultClientFLE (args ); err != nil {
476477 return err
477478 }
478479
479- if err := c .configureMetadataClientFLE (args ); err != nil {
480- return err
481- }
482-
483- mc , err := c .newMongoCrypt (args .AutoEncryptionOptions )
480+ mc , err := c .newMongoCrypt (aeOpts )
484481 if err != nil {
485482 return err
486483 }
487484
488485 // If the crypt_shared library was not loaded, try to spawn and connect to mongocryptd.
489486 if mc .CryptSharedLibVersionString () == "" {
490- mongocryptdFLE , err : = newMongocryptdClient (args . AutoEncryptionOptions )
487+ c . mongocryptdFLE , err = newMongocryptdClient (aeOpts )
491488 if err != nil {
492489 return err
493490 }
494- c .mongocryptdFLE = mongocryptdFLE
495491 }
496492
497- c .configureCryptFLE (mc , args .AutoEncryptionOptions )
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+ })
498516 return nil
499517}
500518
@@ -537,24 +555,6 @@ func (c *Client) configureKeyVaultClientFLE(clientOpts *options.ClientOptions) e
537555 return nil
538556}
539557
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-
558558func (c * Client ) newMongoCrypt (opts * options.AutoEncryptionOptions ) (* mongocrypt.MongoCrypt , error ) {
559559 // convert schemas in SchemaMap to bsoncore documents
560560 cryptSchemaMap := make (map [string ]bsoncore.Document )
@@ -611,7 +611,8 @@ func (c *Client) newMongoCrypt(opts *options.AutoEncryptionOptions) (*mongocrypt
611611 SetEncryptedFieldsMap (cryptEncryptedFieldsMap ).
612612 SetCryptSharedLibDisabled (cryptSharedLibDisabled || bypassAutoEncryption ).
613613 SetCryptSharedLibOverridePath (cryptSharedLibPath ).
614- SetHTTPClient (opts .HTTPClient ))
614+ SetHTTPClient (opts .HTTPClient ).
615+ SetKeyExpiration (opts .KeyExpiration ))
615616 if err != nil {
616617 return nil , err
617618 }
@@ -637,28 +638,6 @@ func (c *Client) newMongoCrypt(opts *options.AutoEncryptionOptions) (*mongocrypt
637638 return mc , nil
638639}
639640
640- //nolint:unused // the unused linter thinks that this function is unreachable because "c.newMongoCrypt" always panics without the "cse" build tag set.
641- func (c * Client ) configureCryptFLE (mc * mongocrypt.MongoCrypt , opts * options.AutoEncryptionOptions ) {
642- bypass := opts .BypassAutoEncryption != nil && * opts .BypassAutoEncryption
643- kr := keyRetriever {coll : c .keyVaultCollFLE }
644- var cir collInfoRetriever
645- // If bypass is true, c.metadataClientFLE is nil and the collInfoRetriever
646- // will not be used. If bypass is false, to the parent client or the
647- // internal client.
648- if ! bypass {
649- cir = collInfoRetriever {client : c .metadataClientFLE }
650- }
651-
652- c .cryptFLE = driver .NewCrypt (& driver.CryptOptions {
653- MongoCrypt : mc ,
654- CollInfoFn : cir .cryptCollInfo ,
655- KeyFn : kr .cryptKeys ,
656- MarkFn : c .mongocryptdFLE .markCommand ,
657- TLSConfig : opts .TLSConfig ,
658- BypassAutoEncryption : bypass ,
659- })
660- }
661-
662641// validSession returns an error if the session doesn't belong to the client
663642func (c * Client ) validSession (sess * session.Client ) error {
664643 if sess != nil && sess .ClientID != c .id {
0 commit comments