@@ -23,6 +23,7 @@ func TestAccKmsSecret_basic(t *testing.T) {
2323 cryptoKeyName := fmt .Sprintf ("tf-test-%s" , acctest .RandString (10 ))
2424
2525 plaintext := fmt .Sprintf ("secret-%s" , acctest .RandString (10 ))
26+ aad := "plainaad"
2627
2728 // The first test creates resources needed to encrypt plaintext and produce ciphertext
2829 resource .Test (t , resource.TestCase {
@@ -32,7 +33,7 @@ func TestAccKmsSecret_basic(t *testing.T) {
3233 {
3334 Config : testGoogleKmsCryptoKey_basic (projectId , projectOrg , projectBillingAccount , keyRingName , cryptoKeyName ),
3435 Check : func (s * terraform.State ) error {
35- ciphertext , cryptoKeyId , err := testAccEncryptSecretDataWithCryptoKey (s , "google_kms_crypto_key.crypto_key" , plaintext )
36+ ciphertext , cryptoKeyId , err := testAccEncryptSecretDataWithCryptoKey (s , "google_kms_crypto_key.crypto_key" , plaintext , "" )
3637
3738 if err != nil {
3839 return err
@@ -50,14 +51,39 @@ func TestAccKmsSecret_basic(t *testing.T) {
5051 },
5152 })
5253
54+ return nil
55+ },
56+ },
57+ // With AAD
58+ {
59+ Config : testGoogleKmsCryptoKey_basic (projectId , projectOrg , projectBillingAccount , keyRingName , cryptoKeyName ),
60+ Check : func (s * terraform.State ) error {
61+ ciphertext , cryptoKeyId , err := testAccEncryptSecretDataWithCryptoKey (s , "google_kms_crypto_key.crypto_key" , plaintext , aad )
62+
63+ if err != nil {
64+ return err
65+ }
66+
67+ // The second test asserts that the data source has the correct plaintext, given the created ciphertext
68+ resource .Test (t , resource.TestCase {
69+ PreCheck : func () { testAccPreCheck (t ) },
70+ Providers : testAccProviders ,
71+ Steps : []resource.TestStep {
72+ {
73+ Config : testGoogleKmsSecret_aadDatasource (cryptoKeyId .terraformId (), ciphertext , base64 .StdEncoding .EncodeToString ([]byte (aad ))),
74+ Check : resource .TestCheckResourceAttr ("data.google_kms_secret.acceptance" , "plaintext" , plaintext ),
75+ },
76+ },
77+ })
78+
5379 return nil
5480 },
5581 },
5682 },
5783 })
5884}
5985
60- func testAccEncryptSecretDataWithCryptoKey (s * terraform.State , cryptoKeyResourceName , plaintext string ) (string , * kmsCryptoKeyId , error ) {
86+ func testAccEncryptSecretDataWithCryptoKey (s * terraform.State , cryptoKeyResourceName , plaintext , aad string ) (string , * kmsCryptoKeyId , error ) {
6187 config := testAccProvider .Meta ().(* Config )
6288
6389 rs , ok := s .RootModule ().Resources [cryptoKeyResourceName ]
@@ -75,6 +101,10 @@ func testAccEncryptSecretDataWithCryptoKey(s *terraform.State, cryptoKeyResource
75101 Plaintext : base64 .StdEncoding .EncodeToString ([]byte (plaintext )),
76102 }
77103
104+ if aad != "" {
105+ kmsEncryptRequest .AdditionalAuthenticatedData = base64 .StdEncoding .EncodeToString ([]byte (aad ))
106+ }
107+
78108 encryptResponse , err := config .clientKms .Projects .Locations .KeyRings .CryptoKeys .Encrypt (cryptoKeyId .cryptoKeyId (), kmsEncryptRequest ).Do ()
79109
80110 if err != nil {
@@ -94,3 +124,13 @@ data "google_kms_secret" "acceptance" {
94124}
95125` , cryptoKeyTerraformId , ciphertext )
96126}
127+
128+ func testGoogleKmsSecret_aadDatasource (cryptoKeyTerraformId , ciphertext , aad string ) string {
129+ return fmt .Sprintf (`
130+ data "google_kms_secret" "acceptance" {
131+ crypto_key = "%s"
132+ ciphertext = "%s"
133+ additional_authenticated_data = "%s"
134+ }
135+ ` , cryptoKeyTerraformId , ciphertext , aad )
136+ }
0 commit comments