@@ -16,6 +16,7 @@ import (
16
16
"runtime"
17
17
"strings"
18
18
"testing"
19
+ "time"
19
20
20
21
"go.mongodb.org/mongo-driver/bson"
21
22
"go.mongodb.org/mongo-driver/bson/primitive"
@@ -630,6 +631,83 @@ func TestClientSideEncryptionProse(t *testing.T) {
630
631
})
631
632
}
632
633
})
634
+ mt .RunOpts ("bypass mongocryptd spawning" , noClientOpts , func (mt * mtest.T ) {
635
+ kmsProviders := map [string ]map [string ]interface {}{
636
+ "local" : {
637
+ "key" : localMasterKey ,
638
+ },
639
+ }
640
+ schemaMap := map [string ]interface {}{
641
+ "db.coll" : readJSONFile (mt , "external-schema.json" ),
642
+ }
643
+
644
+ // All mongocryptd options use port 27021 instead of the default 27020 to avoid interference with mongocryptd
645
+ // instances spawned by previous tests.
646
+ mongocryptdBypassSpawnTrue := map [string ]interface {}{
647
+ "mongocryptdBypassSpawn" : true ,
648
+ "mongocryptdURI" : "mongodb://localhost:27021/db?serverSelectionTimeoutMS=1000" ,
649
+ "mongocryptdSpawnArgs" : []string {"--pidfilepath=bypass-spawning-mongocryptd.pid" , "--port=27021" },
650
+ }
651
+ mongocryptdBypassSpawnFalse := map [string ]interface {}{
652
+ "mongocryptdBypassSpawn" : false ,
653
+ "mongocryptdSpawnArgs" : []string {"--pidfilepath=bypass-spawning-mongocryptd.pid" , "--port=27021" },
654
+ }
655
+ mongocryptdBypassSpawnNotSet := map [string ]interface {}{
656
+ "mongocryptdSpawnArgs" : []string {"--pidfilepath=bypass-spawning-mongocryptd.pid" , "--port=27021" },
657
+ }
658
+
659
+ testCases := []struct {
660
+ name string
661
+ mongocryptdOpts map [string ]interface {}
662
+ setBypassAutoEncryption bool
663
+ bypassAutoEncryption bool
664
+ }{
665
+ {"mongocryptdBypassSpawn only" , mongocryptdBypassSpawnTrue , false , false },
666
+ {"bypassAutoEncryption only" , mongocryptdBypassSpawnNotSet , true , true },
667
+ {"mongocryptdBypassSpawn false, bypassAutoEncryption true" , mongocryptdBypassSpawnFalse , true , true },
668
+ {"mongocryptdBypassSpawn true, bypassAutoEncryption false" , mongocryptdBypassSpawnTrue , true , false },
669
+ }
670
+ for _ , tc := range testCases {
671
+ mt .Run (tc .name , func (mt * mtest.T ) {
672
+ aeo := options .AutoEncryption ().
673
+ SetKmsProviders (kmsProviders ).
674
+ SetKeyVaultNamespace (kvNamespace ).
675
+ SetSchemaMap (schemaMap ).
676
+ SetExtraOptions (tc .mongocryptdOpts )
677
+ if tc .setBypassAutoEncryption {
678
+ aeo .SetBypassAutoEncryption (tc .bypassAutoEncryption )
679
+ }
680
+ cpt := setup (mt , aeo , nil , nil )
681
+ defer cpt .teardown (mt )
682
+
683
+ _ , err := cpt .cseColl .InsertOne (mtest .Background , bson.D {{"unencrypted" , "test" }})
684
+
685
+ // Check for mongocryptd server selection error if auto encryption was not bypassed.
686
+ if ! (tc .setBypassAutoEncryption && tc .bypassAutoEncryption ) {
687
+ assert .NotNil (mt , err , "expected InsertOne error, got nil" )
688
+ mcryptErr , ok := err .(mongo.MongocryptdError )
689
+ assert .True (mt , ok , "expected error type %T, got %v of type %T" , mongo.MongocryptdError {}, err , err )
690
+ assert .True (mt , strings .Contains (mcryptErr .Error (), "server selection error" ),
691
+ "expected mongocryptd server selection error, got %v" , err )
692
+ return
693
+ }
694
+
695
+ // If auto encryption is bypassed, the command should succeed. Create a new client to connect to
696
+ // mongocryptd and verify it is not running.
697
+ assert .Nil (mt , err , "InsertOne error: %v" , err )
698
+
699
+ mcryptOpts := options .Client ().ApplyURI ("mongodb://localhost:27021" ).
700
+ SetServerSelectionTimeout (1 * time .Second )
701
+ mcryptClient , err := mongo .Connect (mtest .Background , mcryptOpts )
702
+ assert .Nil (mt , err , "mongocryptd Connect error: %v" , err )
703
+
704
+ err = mcryptClient .Database ("admin" ).RunCommand (mtest .Background , bson.D {{"ismaster" , 1 }}).Err ()
705
+ assert .NotNil (mt , err , "expected mongocryptd ismaster error, got nil" )
706
+ assert .True (mt , strings .Contains (err .Error (), "server selection error" ),
707
+ "expected mongocryptd server selection error, got %v" , err )
708
+ })
709
+ }
710
+ })
633
711
}
634
712
635
713
type cseProseTest struct {
0 commit comments