|
| 1 | +package google |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "crypto/rsa" |
| 6 | + "crypto/sha256" |
| 7 | + "crypto/x509" |
| 8 | + "encoding/base64" |
| 9 | + "encoding/pem" |
| 10 | + "fmt" |
| 11 | + "hash/crc32" |
| 12 | + "log" |
| 13 | + "testing" |
| 14 | + |
| 15 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 16 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 17 | +) |
| 18 | + |
| 19 | +func TestAccKmsSecretAsymmetricBasic(t *testing.T) { |
| 20 | + // Nested tests confuse VCR |
| 21 | + skipIfVcr(t) |
| 22 | + t.Parallel() |
| 23 | + |
| 24 | + projectOrg := getTestOrgFromEnv(t) |
| 25 | + projectBillingAccount := getTestBillingAccountFromEnv(t) |
| 26 | + |
| 27 | + projectID := "terraform-" + randString(t, 10) |
| 28 | + keyRingName := fmt.Sprintf("tf-test-%s", randString(t, 10)) |
| 29 | + cryptoKeyName := fmt.Sprintf("tf-test-%s", randString(t, 10)) |
| 30 | + |
| 31 | + plaintext := fmt.Sprintf("secret-%s", randString(t, 10)) |
| 32 | + |
| 33 | + // The first test creates resources needed to encrypt plaintext and produce ciphertext |
| 34 | + vcrTest(t, resource.TestCase{ |
| 35 | + PreCheck: func() { testAccPreCheck(t) }, |
| 36 | + Providers: testAccProviders, |
| 37 | + Steps: []resource.TestStep{ |
| 38 | + { |
| 39 | + Config: kmsCryptoKeyAsymmetricDecryptBasic(projectID, projectOrg, projectBillingAccount, keyRingName, cryptoKeyName), |
| 40 | + Check: func(s *terraform.State) error { |
| 41 | + ciphertext, cryptoKeyVersionID, crc, err := testAccEncryptSecretDataAsymmetricWithPublicKey(t, s, "data.google_kms_crypto_key_version.crypto_key", plaintext) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + |
| 46 | + // The second test asserts that the data source has the correct plaintext, given the created ciphertext |
| 47 | + vcrTest(t, resource.TestCase{ |
| 48 | + PreCheck: func() { testAccPreCheck(t) }, |
| 49 | + Providers: testAccProviders, |
| 50 | + Steps: []resource.TestStep{ |
| 51 | + { |
| 52 | + Config: googleKmsSecretAsymmetricDatasource(cryptoKeyVersionID, ciphertext), |
| 53 | + Check: resource.TestCheckResourceAttr("data.google_kms_secret_asymmetric.acceptance", "plaintext", plaintext), |
| 54 | + }, |
| 55 | + { |
| 56 | + Config: googleKmsSecretAsymmetricDatasourceWithCrc(cryptoKeyVersionID, ciphertext, crc), |
| 57 | + Check: resource.TestCheckResourceAttr("data.google_kms_secret_asymmetric.acceptance_with_crc", "plaintext", plaintext), |
| 58 | + }, |
| 59 | + }, |
| 60 | + }) |
| 61 | + |
| 62 | + return nil |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +func testAccEncryptSecretDataAsymmetricWithPublicKey(t *testing.T, s *terraform.State, cryptoKeyResourceName, plaintext string) (string, string, uint32, error) { |
| 70 | + rs, ok := s.RootModule().Resources[cryptoKeyResourceName] |
| 71 | + if !ok { |
| 72 | + return "", "", 0, fmt.Errorf("resource not found: %s", cryptoKeyResourceName) |
| 73 | + } |
| 74 | + |
| 75 | + cryptoKeyVersionID := rs.Primary.Attributes["id"] |
| 76 | + |
| 77 | + block, _ := pem.Decode([]byte(rs.Primary.Attributes["public_key.0.pem"])) |
| 78 | + publicKey, err := x509.ParsePKIXPublicKey(block.Bytes) |
| 79 | + if err != nil { |
| 80 | + return "", "", 0, fmt.Errorf("failed to parse public key: %v", err) |
| 81 | + } |
| 82 | + rsaKey, ok := publicKey.(*rsa.PublicKey) |
| 83 | + if !ok { |
| 84 | + return "", "", 0, fmt.Errorf("public key is not rsa") |
| 85 | + } |
| 86 | + |
| 87 | + ciphertext, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, rsaKey, []byte(plaintext), nil) |
| 88 | + if err != nil { |
| 89 | + return "", "", 0, fmt.Errorf("rsa.EncryptOAEP: %v", err) |
| 90 | + } |
| 91 | + |
| 92 | + crc := crc32.Checksum(ciphertext, crc32.MakeTable(crc32.Castagnoli)) |
| 93 | + |
| 94 | + result := base64.StdEncoding.EncodeToString(ciphertext) |
| 95 | + log.Printf("[INFO] Successfully encrypted plaintext and got ciphertext: %s", result) |
| 96 | + |
| 97 | + return result, cryptoKeyVersionID, crc, nil |
| 98 | +} |
| 99 | + |
| 100 | +func googleKmsSecretAsymmetricDatasource(cryptoKeyTerraformID, ciphertext string) string { |
| 101 | + return fmt.Sprintf(` |
| 102 | +data "google_kms_secret_asymmetric" "acceptance" { |
| 103 | + crypto_key_version = "%s" |
| 104 | + ciphertext = "%s" |
| 105 | +} |
| 106 | +`, cryptoKeyTerraformID, ciphertext) |
| 107 | +} |
| 108 | + |
| 109 | +func googleKmsSecretAsymmetricDatasourceWithCrc(cryptoKeyTerraformID, ciphertext string, crc uint32) string { |
| 110 | + return fmt.Sprintf(` |
| 111 | +data "google_kms_secret_asymmetric" "acceptance_with_crc" { |
| 112 | + crypto_key_version = "%s" |
| 113 | + ciphertext = "%s" |
| 114 | + crc32 = "%x" |
| 115 | +} |
| 116 | +`, cryptoKeyTerraformID, ciphertext, crc) |
| 117 | +} |
| 118 | + |
| 119 | +func kmsCryptoKeyAsymmetricDecryptBasic(projectID, projectOrg, projectBillingAccount, keyRingName, cryptoKeyName string) string { |
| 120 | + return fmt.Sprintf(` |
| 121 | +resource "google_project" "acceptance" { |
| 122 | + name = "%s" |
| 123 | + project_id = "%s" |
| 124 | + org_id = "%s" |
| 125 | + billing_account = "%s" |
| 126 | +} |
| 127 | +
|
| 128 | +resource "google_project_service" "acceptance" { |
| 129 | + project = google_project.acceptance.project_id |
| 130 | + service = "cloudkms.googleapis.com" |
| 131 | +} |
| 132 | +
|
| 133 | +resource "google_kms_key_ring" "key_ring" { |
| 134 | + project = google_project_service.acceptance.project |
| 135 | + name = "%s" |
| 136 | + location = "us-central1" |
| 137 | + depends_on = [google_project_service.acceptance] |
| 138 | +} |
| 139 | +
|
| 140 | +resource "google_kms_crypto_key" "crypto_key" { |
| 141 | + name = "%s" |
| 142 | + key_ring = google_kms_key_ring.key_ring.self_link |
| 143 | + purpose = "ASYMMETRIC_DECRYPT" |
| 144 | + version_template { |
| 145 | + algorithm = "RSA_DECRYPT_OAEP_4096_SHA256" |
| 146 | + } |
| 147 | +} |
| 148 | +
|
| 149 | +data "google_kms_crypto_key_version" "crypto_key" { |
| 150 | + crypto_key = google_kms_crypto_key.crypto_key.id |
| 151 | +} |
| 152 | +`, projectID, projectID, projectOrg, projectBillingAccount, keyRingName, cryptoKeyName) |
| 153 | +} |
0 commit comments