Skip to content

Commit

Permalink
Provide a support for rsaEncryptedkey in compute_disk (#7026) (#1…
Browse files Browse the repository at this point in the history
…3685)

* provide a support for rsaEncryptedkey in compute disk

* minor changes

* added test for RSA_Encrypted_Key

* fixing the test failure

* reverting ruby version

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Feb 8, 2023
1 parent b49bf66 commit d34e24f
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .changelog/7026.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```release-note:enhancement
compute: added field `rsaEncryptedKey` to resource `disk`

```
27 changes: 27 additions & 0 deletions google/resource_compute_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,15 @@ If absent, the Compute Engine Service Agent service account is used.`,
RFC 4648 base64 to either encrypt or decrypt this resource.`,
Sensitive: true,
},
"rsa_encrypted_key": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `Specifies an RFC 4648 base64 encoded, RSA-wrapped 2048-bit
customer-supplied encryption key to either encrypt or decrypt
this resource. You can provide either the rawKey or the rsaEncryptedKey.`,
Sensitive: true,
},
"sha256": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -1274,6 +1283,8 @@ func flattenComputeDiskDiskEncryptionKey(v interface{}, d *schema.ResourceData,
transformed := make(map[string]interface{})
transformed["raw_key"] =
flattenComputeDiskDiskEncryptionKeyRawKey(original["rawKey"], d, config)
transformed["rsa_encrypted_key"] =
flattenComputeDiskDiskEncryptionKeyRsaEncryptedKey(original["rsaEncryptedKey"], d, config)
transformed["sha256"] =
flattenComputeDiskDiskEncryptionKeySha256(original["sha256"], d, config)
transformed["kms_key_self_link"] =
Expand All @@ -1286,6 +1297,10 @@ func flattenComputeDiskDiskEncryptionKeyRawKey(v interface{}, d *schema.Resource
return v
}

func flattenComputeDiskDiskEncryptionKeyRsaEncryptedKey(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func flattenComputeDiskDiskEncryptionKeySha256(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}
Expand Down Expand Up @@ -1475,6 +1490,13 @@ func expandComputeDiskDiskEncryptionKey(v interface{}, d TerraformResourceData,
transformed["rawKey"] = transformedRawKey
}

transformedRsaEncryptedKey, err := expandComputeDiskDiskEncryptionKeyRsaEncryptedKey(original["rsa_encrypted_key"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedRsaEncryptedKey); val.IsValid() && !isEmptyValue(val) {
transformed["rsaEncryptedKey"] = transformedRsaEncryptedKey
}

transformedSha256, err := expandComputeDiskDiskEncryptionKeySha256(original["sha256"], d, config)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1503,6 +1525,10 @@ func expandComputeDiskDiskEncryptionKeyRawKey(v interface{}, d TerraformResource
return v, nil
}

func expandComputeDiskDiskEncryptionKeyRsaEncryptedKey(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandComputeDiskDiskEncryptionKeySha256(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -1626,6 +1652,7 @@ func resourceComputeDiskDecoder(d *schema.ResourceData, meta interface{}, res ma
transformed := make(map[string]interface{})
// The raw key won't be returned, so we need to use the original.
transformed["rawKey"] = d.Get("disk_encryption_key.0.raw_key")
transformed["rsaEncryptedKey"] = d.Get("disk_encryption_key.0.rsa_encrypted_key")
transformed["sha256"] = original["sha256"]

if kmsKeyName, ok := original["kmsKeyName"]; ok {
Expand Down
44 changes: 44 additions & 0 deletions google/resource_compute_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -922,3 +922,47 @@ func testAccComputeDisk_diskClone(diskName, refSelector string) string {
}
`, diskName, diskName+"-clone", refSelector)
}

func TestAccComputeDisk_encryptionWithRSAEncryptedKey(t *testing.T) {
t.Parallel()

diskName := fmt.Sprintf("tf-test-%s", randString(t, 10))
var disk compute.Disk

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeDiskDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeDisk_encryptionWithRSAEncryptedKey(diskName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeDiskExists(
t, "google_compute_disk.foobar-1", getTestProjectFromEnv(), &disk),
testAccCheckEncryptionKey(
t, "google_compute_disk.foobar-1", &disk),
),
},
},
})
}

func testAccComputeDisk_encryptionWithRSAEncryptedKey(diskName string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-11"
project = "debian-cloud"
}
resource "google_compute_disk" "foobar-1" {
name = "%s"
image = data.google_compute_image.my_image.self_link
size = 50
type = "pd-ssd"
zone = "us-central1-a"
disk_encryption_key {
rsa_encrypted_key = "fB6BS8tJGhGVDZDjGt1pwUo2wyNbkzNxgH1avfOtiwB9X6oPG94gWgenygitnsYJyKjdOJ7DyXLmxwQOSmnCYCUBWdKCSssyLV5907HL2mb5TfqmgHk5JcArI/t6QADZWiuGtR+XVXqiLa5B9usxFT2BTmbHvSKfkpJ7McCNc/3U0PQR8euFRZ9i75o/w+pLHFMJ05IX3JB0zHbXMV173PjObiV3ItSJm2j3mp5XKabRGSA5rmfMnHIAMz6stGhcuom6+bMri2u/axmPsdxmC6MeWkCkCmPjaKsVz1+uQUNCJkAnzesluhoD+R6VjFDm4WI7yYabu4MOOAOTaQXdEg=="
}
}
`, diskName)
}
1 change: 1 addition & 0 deletions google/resource_compute_region_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,7 @@ func resourceComputeRegionDiskDecoder(d *schema.ResourceData, meta interface{},
transformed := make(map[string]interface{})
// The raw key won't be returned, so we need to use the original.
transformed["rawKey"] = d.Get("disk_encryption_key.0.raw_key")
transformed["rsaEncryptedKey"] = d.Get("disk_encryption_key.0.rsa_encrypted_key")
transformed["sha256"] = original["sha256"]

if kmsKeyName, ok := original["kmsKeyName"]; ok {
Expand Down
9 changes: 8 additions & 1 deletion website/docs/r/compute_disk.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ To get more information about Disk, see:
* How-to Guides
* [Adding a persistent disk](https://cloud.google.com/compute/docs/disks/add-persistent-disk)

~> **Warning:** All arguments including `disk_encryption_key.raw_key` will be stored in the raw
~> **Warning:** All arguments including `disk_encryption_key.raw_key` and `disk_encryption_key.rsa_encrypted_key` will be stored in the raw
state as plain-text. [Read more about sensitive data in state](https://www.terraform.io/language/state/sensitive-data).

<div class = "oics-button" style="float: right; margin: 0 0 -15px">
Expand Down Expand Up @@ -243,6 +243,13 @@ The following arguments are supported:
RFC 4648 base64 to either encrypt or decrypt this resource.
**Note**: This property is sensitive and will not be displayed in the plan.

* `rsa_encrypted_key` -
(Optional)
Specifies an RFC 4648 base64 encoded, RSA-wrapped 2048-bit
customer-supplied encryption key to either encrypt or decrypt
this resource. You can provide either the rawKey or the rsaEncryptedKey.
**Note**: This property is sensitive and will not be displayed in the plan.

* `sha256` -
The RFC 4648 base64 encoded SHA-256 hash of the customer-supplied
encryption key that protects this resource.
Expand Down

0 comments on commit d34e24f

Please sign in to comment.