|
| 1 | +package google |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform/helper/acctest" |
| 9 | + "github.com/hashicorp/terraform/helper/resource" |
| 10 | + "github.com/hashicorp/terraform/terraform" |
| 11 | + "google.golang.org/api/cloudkms/v1" |
| 12 | + "log" |
| 13 | +) |
| 14 | + |
| 15 | +func TestAccGoogleKmsSecret_basic(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + |
| 18 | + projectOrg := getTestOrgFromEnv(t) |
| 19 | + projectBillingAccount := getTestBillingAccountFromEnv(t) |
| 20 | + |
| 21 | + projectId := "terraform-" + acctest.RandString(10) |
| 22 | + keyRingName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) |
| 23 | + cryptoKeyName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) |
| 24 | + |
| 25 | + plaintext := fmt.Sprintf("secret-%s", acctest.RandString(10)) |
| 26 | + |
| 27 | + // The first test creates resources needed to encrypt plaintext and produce ciphertext |
| 28 | + resource.Test(t, resource.TestCase{ |
| 29 | + PreCheck: func() { testAccPreCheck(t) }, |
| 30 | + Providers: testAccProviders, |
| 31 | + Steps: []resource.TestStep{ |
| 32 | + resource.TestStep{ |
| 33 | + Config: testGoogleKmsCryptoKey_basic(projectId, projectOrg, projectBillingAccount, keyRingName, cryptoKeyName), |
| 34 | + Check: func(s *terraform.State) error { |
| 35 | + ciphertext, cryptoKeyId, err := testAccEncryptSecretDataWithCryptoKey(s, "google_kms_crypto_key.crypto_key", plaintext) |
| 36 | + |
| 37 | + if err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + |
| 41 | + // The second test asserts that the data source has the correct plaintext, given the created ciphertext |
| 42 | + resource.Test(t, resource.TestCase{ |
| 43 | + PreCheck: func() { testAccPreCheck(t) }, |
| 44 | + Providers: testAccProviders, |
| 45 | + Steps: []resource.TestStep{ |
| 46 | + resource.TestStep{ |
| 47 | + Config: testGoogleKmsSecret_datasource(cryptoKeyId.terraformId(), ciphertext), |
| 48 | + Check: resource.TestCheckResourceAttr("data.google_kms_secret.acceptance", "plaintext", plaintext), |
| 49 | + }, |
| 50 | + }, |
| 51 | + }) |
| 52 | + |
| 53 | + return nil |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }) |
| 58 | +} |
| 59 | + |
| 60 | +func testAccEncryptSecretDataWithCryptoKey(s *terraform.State, cryptoKeyResourceName, plaintext string) (string, *kmsCryptoKeyId, error) { |
| 61 | + config := testAccProvider.Meta().(*Config) |
| 62 | + |
| 63 | + rs, ok := s.RootModule().Resources[cryptoKeyResourceName] |
| 64 | + if !ok { |
| 65 | + return "", nil, fmt.Errorf("Resource not found: %s", cryptoKeyResourceName) |
| 66 | + } |
| 67 | + |
| 68 | + cryptoKeyId, err := parseKmsCryptoKeyId(rs.Primary.Attributes["id"], config) |
| 69 | + |
| 70 | + if err != nil { |
| 71 | + return "", nil, err |
| 72 | + } |
| 73 | + |
| 74 | + kmsEncryptRequest := &cloudkms.EncryptRequest{ |
| 75 | + Plaintext: base64.StdEncoding.EncodeToString([]byte(plaintext)), |
| 76 | + } |
| 77 | + |
| 78 | + encryptResponse, err := config.clientKms.Projects.Locations.KeyRings.CryptoKeys.Encrypt(cryptoKeyId.cryptoKeyId(), kmsEncryptRequest).Do() |
| 79 | + |
| 80 | + if err != nil { |
| 81 | + return "", nil, fmt.Errorf("Error encrypting plaintext: %s", err) |
| 82 | + } |
| 83 | + |
| 84 | + log.Printf("[INFO] Successfully encrypted plaintext and got ciphertext: %s", encryptResponse.Ciphertext) |
| 85 | + |
| 86 | + return encryptResponse.Ciphertext, cryptoKeyId, nil |
| 87 | +} |
| 88 | + |
| 89 | +func testGoogleKmsSecret_datasource(cryptoKeyTerraformId, ciphertext string) string { |
| 90 | + return fmt.Sprintf(` |
| 91 | +data "google_kms_secret" "acceptance" { |
| 92 | + crypto_key = "%s" |
| 93 | + ciphertext = "%s" |
| 94 | +} |
| 95 | + `, cryptoKeyTerraformId, ciphertext) |
| 96 | +} |
0 commit comments