@@ -21,6 +21,9 @@ import (
2121 "github.com/aws/aws-sdk-go-v2/config"
2222 "github.com/aws/aws-sdk-go-v2/credentials"
2323 "github.com/aws/aws-sdk-go-v2/service/kms"
24+ awsv1 "github.com/aws/aws-sdk-go/aws"
25+ sessionv1 "github.com/aws/aws-sdk-go/aws/session"
26+ kmsv1 "github.com/aws/aws-sdk-go/service/kms"
2427 . "github.com/onsi/gomega"
2528 "github.com/ory/dockertest"
2629)
@@ -135,14 +138,24 @@ func TestMasterKey_Encrypt_SOPS_Compat(t *testing.T) {
135138 dataKey := []byte ("encrypt-compat" )
136139 g .Expect (encryptKey .Encrypt (dataKey )).To (Succeed ())
137140
138- decryptKey := createTestMasterKey (testKMSARN )
139- decryptKey .credentialsProvider = nil
140- decryptKey .EncryptedKey = encryptKey .EncryptedKey
141+ // This is the core decryption logic of `sopskms.MasterKey.Decrypt()`.
142+ // We don't call `sops.MasterKey.Decrypt()` directly to avoid issues with
143+ // session and config setup.
144+ config := awsv1.Config {
145+ Region : awsv1 .String ("us-west-2" ),
146+ Endpoint : & testKMSServerURL ,
147+ }
141148 t .Setenv ("AWS_ACCESS_KEY_ID" , "id" )
142149 t .Setenv ("AWS_SECRET_ACCESS_KEY" , "secret" )
143- dec , err := decryptKey . Decrypt ( )
150+ k , err := base64 . StdEncoding . DecodeString ( encryptKey . EncryptedKey )
144151 g .Expect (err ).ToNot (HaveOccurred ())
145- g .Expect (dec ).To (Equal (dataKey ))
152+ sess , err := sessionv1 .NewSessionWithOptions (sessionv1.Options {
153+ Config : config ,
154+ })
155+ kmsSvc := kmsv1 .New (sess )
156+ decrypted , err := kmsSvc .Decrypt (& kmsv1.DecryptInput {CiphertextBlob : k })
157+ g .Expect (err ).ToNot (HaveOccurred ())
158+ g .Expect (decrypted .Plaintext ).To (Equal (dataKey ))
146159}
147160
148161func TestMasterKey_EncryptIfNeeded (t * testing.T ) {
@@ -187,17 +200,25 @@ func TestMasterKey_Decrypt(t *testing.T) {
187200func TestMasterKey_Decrypt_SOPS_Compat (t * testing.T ) {
188201 g := NewWithT (t )
189202
203+ // This is the core encryption logic of `sopskms.MasterKey.Encrypt()`.
204+ // We don't call `sops.MasterKey.Encrypt()` directly to avoid issues with
205+ // session and config setup.
190206 dataKey := []byte ("decrypt-compat" )
191-
192- encryptKey := createTestMasterKey (testKMSARN )
193- encryptKey .credentialsProvider = nil
207+ config := awsv1.Config {
208+ Region : awsv1 .String ("us-west-2" ),
209+ Endpoint : & testKMSServerURL ,
210+ }
194211 t .Setenv ("AWS_ACCESS_KEY_ID" , "id" )
195212 t .Setenv ("AWS_SECRET_ACCESS_KEY" , "secret" )
196-
197- g .Expect (encryptKey .Encrypt (dataKey )).To (Succeed ())
213+ sess , err := sessionv1 .NewSessionWithOptions (sessionv1.Options {
214+ Config : config ,
215+ })
216+ kmsSvc := kmsv1 .New (sess )
217+ encrypted , err := kmsSvc .Encrypt (& kmsv1.EncryptInput {Plaintext : dataKey , KeyId : & testKMSARN })
218+ g .Expect (err ).ToNot (HaveOccurred ())
198219
199220 decryptKey := createTestMasterKey (testKMSARN )
200- decryptKey .EncryptedKey = encryptKey . EncryptedKey
221+ decryptKey .EncryptedKey = base64 . StdEncoding . EncodeToString ( encrypted . CiphertextBlob )
201222 dec , err := decryptKey .Decrypt ()
202223 g .Expect (err ).ToNot (HaveOccurred ())
203224 g .Expect (dec ).To (Equal (dataKey ))
0 commit comments