@@ -28,7 +28,6 @@ import (
2828 "go.mongodb.org/mongo-driver/v2/bson"
2929 "go.mongodb.org/mongo-driver/v2/event"
3030 "go.mongodb.org/mongo-driver/v2/internal/assert"
31- "go.mongodb.org/mongo-driver/v2/internal/credproviders"
3231 "go.mongodb.org/mongo-driver/v2/internal/handshake"
3332 "go.mongodb.org/mongo-driver/v2/internal/integration/mtest"
3433 "go.mongodb.org/mongo-driver/v2/internal/integtest"
@@ -3146,144 +3145,125 @@ func TestClientSideEncryptionProse(t *testing.T) {
31463145 })
31473146 }
31483147 })
3148+ }
31493149
3150- mt .RunOpts ("26. custom AWS credentials" , qeRunOpts22 , func (mt * mtest.T ) {
3151- mt .Run ("Case 1: ClientEncryption with credentialProviders and incorrect kmsProviders" , func (mt * mtest.T ) {
3152- opts := options .Client ().ApplyURI (mtest .ClusterURI ())
3153- integtest .AddTestServerAPIVersion (opts )
3154- keyVaultClient , err := mongo .Connect (opts )
3155- assert .NoErrorf (mt , err , "error on Connect: %v" , err )
3150+ func TestCustomAwsCredentialsProse (t * testing.T ) {
3151+ mt := mtest .New (t , mtest .NewOptions ().CreateClient (false ))
31563152
3157- ceo := options .ClientEncryption ().
3158- SetKeyVaultNamespace ("keyvault.datakeys" ).
3159- SetKmsProviders (map [string ]map [string ]any {
3160- "aws" : {
3161- "accessKeyId" : awsAccessKeyID ,
3162- "secretAccessKey" : awsSecretAccessKey ,
3163- },
3164- }).
3165- SetCredentialProviders (map [string ]options.CredentialsProvider {
3166- "aws" : func (ctx context.Context ) (options.Credentials , error ) {
3167- var cred options.Credentials
3168- provider := credproviders .NewEnvProvider ()
3169- c , err := provider .Retrieve (ctx )
3170- if err != nil {
3171- return cred , err
3172- }
3173- cred .AccessKeyID = c .AccessKeyID
3174- cred .SecretAccessKey = c .SecretAccessKey
3175- cred .SessionToken = c .SessionToken
3176- cred .ExpirationCallback = provider .IsExpired
3177- return cred , nil
3178- },
3179- })
3180- clientEncryption , err := mongo .NewClientEncryption (keyVaultClient , ceo )
3181- assert .NoErrorf (mt , err , "error on NewClientEncryption: %v" , err )
3153+ mt .Run ("Case 1: ClientEncryption with credentialProviders and incorrect kmsProviders" , func (mt * mtest.T ) {
3154+ opts := options .Client ().ApplyURI (mtest .ClusterURI ())
3155+ integtest .AddTestServerAPIVersion (opts )
3156+ keyVaultClient , err := mongo .Connect (opts )
3157+ assert .NoErrorf (mt , err , "error on Connect: %v" , err )
31823158
3183- dkOpts := options .DataKey ()
3184- _ , err = clientEncryption .CreateDataKey (context .Background (), "aws" , dkOpts )
3185- assert .Error (mt , err , "expected an error" )
3186- })
3187- mt .Run ("Case 2: ClientEncryption with credentialProviders works" , func (mt * mtest.T ) {
3188- opts := options .Client ().ApplyURI (mtest .ClusterURI ())
3189- integtest .AddTestServerAPIVersion (opts )
3190- keyVaultClient , err := mongo .Connect (opts )
3191- assert .NoErrorf (mt , err , "error on Connect: %v" , err )
3159+ ceo := options .ClientEncryption ().
3160+ SetKeyVaultNamespace ("keyvault.datakeys" ).
3161+ SetKmsProviders (map [string ]map [string ]any {
3162+ "aws" : {
3163+ "accessKeyId" : awsAccessKeyID ,
3164+ "secretAccessKey" : awsSecretAccessKey ,
3165+ },
3166+ }).
3167+ SetCredentialProviders (map [string ]options.CredentialsProvider {
3168+ "aws" : func (ctx context.Context ) (options.Credentials , error ) {
3169+ return options.Credentials {}, nil
3170+ },
3171+ })
3172+ _ , err = mongo .NewClientEncryption (keyVaultClient , ceo )
3173+ assert .ErrorContains (mt , err , "can only provide a custom AWS credential provider" ,
3174+ "unexpected error: %v" , err )
3175+ })
31923176
3193- var calledCount int
3194- ceo := options .ClientEncryption ().
3195- SetKeyVaultNamespace ("keyvault.datakeys" ).
3196- SetKmsProviders (map [string ]map [string ]any {
3197- "aws" : map [string ]any {},
3198- }).
3199- SetCredentialProviders (map [string ]options.CredentialsProvider {
3200- "aws" : func (_ context.Context ) (options.Credentials , error ) {
3201- calledCount ++
3202- return options.Credentials {
3203- AccessKeyID : awsAccessKeyID ,
3204- SecretAccessKey : awsSecretAccessKey ,
3205- ExpirationCallback : func () bool { return false },
3206- }, nil
3207- },
3208- })
3209- clientEncryption , err := mongo .NewClientEncryption (keyVaultClient , ceo )
3210- assert .NoErrorf (mt , err , "error on NewClientEncryption: %v" , err )
3177+ mt .Run ("Case 2: ClientEncryption with credentialProviders works" , func (mt * mtest.T ) {
3178+ opts := options .Client ().ApplyURI (mtest .ClusterURI ())
3179+ integtest .AddTestServerAPIVersion (opts )
3180+ keyVaultClient , err := mongo .Connect (opts )
3181+ assert .NoErrorf (mt , err , "error on Connect: %v" , err )
32113182
3212- dkOpts := options .DataKey ()
3213- _ , err = clientEncryption .CreateDataKey (context .Background (), "aws" , dkOpts )
3214- assert .NoErrorf (mt , err , "unexpected error %v" , err )
3215- assert .Equal (mt , 1 , calledCount , "expected credential provider to be called once" )
3216- })
3183+ var calledCount int
3184+ ceo := options .ClientEncryption ().
3185+ SetKeyVaultNamespace ("keyvault.datakeys" ).
3186+ SetKmsProviders (map [string ]map [string ]any {
3187+ "aws" : map [string ]any {},
3188+ }).
3189+ SetCredentialProviders (map [string ]options.CredentialsProvider {
3190+ "aws" : func (_ context.Context ) (options.Credentials , error ) {
3191+ calledCount ++
3192+ return options.Credentials {
3193+ AccessKeyID : awsAccessKeyID ,
3194+ SecretAccessKey : awsSecretAccessKey ,
3195+ ExpirationCallback : func () bool { return false },
3196+ }, nil
3197+ },
3198+ })
3199+ clientEncryption , err := mongo .NewClientEncryption (keyVaultClient , ceo )
3200+ assert .NoErrorf (mt , err , "error on NewClientEncryption: %v" , err )
32173201
3218- mt .Run ("Case 3: AutoEncryptionOpts with credentialProviders and incorrect kmsProviders" , func (mt * mtest.T ) {
3219- aeo := options .AutoEncryption ().
3220- SetKeyVaultNamespace ("keyvault.datakeys" ).
3221- SetKmsProviders (map [string ]map [string ]any {
3222- "aws" : {
3223- "accessKeyId" : awsAccessKeyID ,
3224- "secretAccessKey" : awsSecretAccessKey ,
3225- },
3226- }).
3227- SetCredentialProviders (map [string ]options.CredentialsProvider {
3228- "aws" : func (ctx context.Context ) (options.Credentials , error ) {
3229- var cred options.Credentials
3230- provider := credproviders .NewEnvProvider ()
3231- c , err := provider .Retrieve (ctx )
3232- if err != nil {
3233- return cred , err
3234- }
3235- cred .AccessKeyID = c .AccessKeyID
3236- cred .SecretAccessKey = c .SecretAccessKey
3237- cred .SessionToken = c .SessionToken
3238- cred .ExpirationCallback = provider .IsExpired
3239- return cred , nil
3240- },
3241- })
3242- co := options .Client ().SetAutoEncryptionOptions (aeo ).ApplyURI (mtest .ClusterURI ())
3243- integtest .AddTestServerAPIVersion (co )
3244- _ , err := mongo .Connect (co )
3245- assert .Error (mt , err , "expected an error" )
3202+ dkOpts := options .DataKey ().SetMasterKey (bson.D {
3203+ {"region" , "us-east-1" },
3204+ {"key" , "arn:aws:kms:us-east-1:579766882180:key/89fcc2c4-08b0-4bd9-9f25-e30687b580d0" },
32463205 })
3206+ _ , err = clientEncryption .CreateDataKey (context .Background (), "aws" , dkOpts )
3207+ assert .NoErrorf (mt , err , "unexpected error %v" , err )
3208+ assert .Equal (mt , 1 , calledCount , "expected credential provider to be called once" )
3209+ })
32473210
3248- mt .Run ("Case 4: ClientEncryption with credentialProviders and valid environment variables" , func (mt * mtest.T ) {
3249- mt .Setenv ("AWS_ACCESS_KEY_ID" , os .Getenv ("FLE_AWS_SECRET_ACCESS_KEY" ))
3250- mt .Setenv ("AWS_SECRET_ACCESS_KEY" , os .Getenv ("FLE_AWS_ACCESS_KEY_ID" ))
3211+ mt .Run ("Case 3: AutoEncryptionOpts with credentialProviders and incorrect kmsProviders" , func (mt * mtest.T ) {
3212+ aeo := options .AutoEncryption ().
3213+ SetKeyVaultNamespace ("keyvault.datakeys" ).
3214+ SetKmsProviders (map [string ]map [string ]any {
3215+ "aws" : {
3216+ "accessKeyId" : awsAccessKeyID ,
3217+ "secretAccessKey" : awsSecretAccessKey ,
3218+ },
3219+ }).
3220+ SetCredentialProviders (map [string ]options.CredentialsProvider {
3221+ "aws" : func (ctx context.Context ) (options.Credentials , error ) {
3222+ return options.Credentials {}, nil
3223+ },
3224+ })
3225+ co := options .Client ().SetAutoEncryptionOptions (aeo ).ApplyURI (mtest .ClusterURI ())
3226+ integtest .AddTestServerAPIVersion (co )
3227+ _ , err := mongo .Connect (co )
3228+ assert .ErrorContainsf (mt , err , "can only provide a custom AWS credential provider" ,
3229+ "unexpected error: %v" , err )
3230+ })
32513231
3252- opts := options .Client ().ApplyURI (mtest .ClusterURI ())
3253- integtest .AddTestServerAPIVersion (opts )
3254- keyVaultClient , err := mongo .Connect (opts )
3255- assert .NoErrorf (mt , err , "error on Connect: %v" , err )
3232+ mt .Run ("Case 4: ClientEncryption with credentialProviders and valid environment variables" , func (mt * mtest.T ) {
3233+ mt .Setenv ("AWS_ACCESS_KEY_ID" , os .Getenv ("FLE_AWS_SECRET_ACCESS_KEY" ))
3234+ mt .Setenv ("AWS_SECRET_ACCESS_KEY" , os .Getenv ("FLE_AWS_ACCESS_KEY_ID" ))
32563235
3257- ceo := options .ClientEncryption ().
3258- SetKeyVaultNamespace ("keyvault.datakeys" ).
3259- SetKmsProviders (map [string ]map [string ]any {
3260- "aws" : {
3261- "accessKeyId" : awsAccessKeyID ,
3262- "secretAccessKey" : awsSecretAccessKey ,
3263- },
3264- }).
3265- SetCredentialProviders (map [string ]options.CredentialsProvider {
3266- "aws" : func (ctx context.Context ) (options.Credentials , error ) {
3267- var cred options.Credentials
3268- provider := credproviders .NewEnvProvider ()
3269- c , err := provider .Retrieve (ctx )
3270- if err != nil {
3271- return cred , err
3272- }
3273- cred .AccessKeyID = c .AccessKeyID
3274- cred .SecretAccessKey = c .SecretAccessKey
3275- cred .SessionToken = c .SessionToken
3276- cred .ExpirationCallback = provider .IsExpired
3277- return cred , nil
3278- },
3279- })
3280- clientEncryption , err := mongo .NewClientEncryption (keyVaultClient , ceo )
3281- assert .NoErrorf (mt , err , "error on NewClientEncryption: %v" , err )
3236+ opts := options .Client ().ApplyURI (mtest .ClusterURI ())
3237+ integtest .AddTestServerAPIVersion (opts )
3238+ keyVaultClient , err := mongo .Connect (opts )
3239+ assert .NoErrorf (mt , err , "error on Connect: %v" , err )
32823240
3283- dkOpts := options .DataKey ()
3284- _ , err = clientEncryption .CreateDataKey (context .Background (), "aws" , dkOpts )
3285- assert .NoErrorf (mt , err , "unexpected error %v" , err )
3241+ var calledCount int
3242+ ceo := options .ClientEncryption ().
3243+ SetKeyVaultNamespace ("keyvault.datakeys" ).
3244+ SetKmsProviders (map [string ]map [string ]any {
3245+ "aws" : map [string ]any {},
3246+ }).
3247+ SetCredentialProviders (map [string ]options.CredentialsProvider {
3248+ "aws" : func (ctx context.Context ) (options.Credentials , error ) {
3249+ calledCount ++
3250+ return options.Credentials {
3251+ AccessKeyID : awsAccessKeyID ,
3252+ SecretAccessKey : awsSecretAccessKey ,
3253+ ExpirationCallback : func () bool { return false },
3254+ }, nil
3255+ },
3256+ })
3257+ clientEncryption , err := mongo .NewClientEncryption (keyVaultClient , ceo )
3258+ assert .NoErrorf (mt , err , "error on NewClientEncryption: %v" , err )
3259+
3260+ dkOpts := options .DataKey ().SetMasterKey (bson.D {
3261+ {"region" , "us-east-1" },
3262+ {"key" , "arn:aws:kms:us-east-1:579766882180:key/89fcc2c4-08b0-4bd9-9f25-e30687b580d0" },
32863263 })
3264+ _ , err = clientEncryption .CreateDataKey (context .Background (), "aws" , dkOpts )
3265+ assert .NoErrorf (mt , err , "unexpected error %v" , err )
3266+ assert .Equal (mt , 1 , calledCount , "expected credential provider to be called once" )
32873267 })
32883268}
32893269
0 commit comments