Skip to content

Commit 14f28bb

Browse files
rileykarsonmodular-magician
authored andcommitted
Add support for protection_level to google_kms_crypto_key
1 parent 963f81f commit 14f28bb

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

google/resource_kms_crypto_key.go

+61-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package google
22

33
import (
44
"fmt"
5+
"github.com/hashicorp/terraform/helper/validation"
56
"log"
67
"regexp"
78
"strconv"
@@ -39,6 +40,27 @@ func resourceKmsCryptoKey() *schema.Resource {
3940
Optional: true,
4041
ValidateFunc: validateKmsCryptoKeyRotationPeriod,
4142
},
43+
"version_template": {
44+
Type: schema.TypeList,
45+
MaxItems: 1,
46+
Optional: true,
47+
Computed: true,
48+
Elem: &schema.Resource{
49+
Schema: map[string]*schema.Schema{
50+
"algorithm": {
51+
Type: schema.TypeString,
52+
Required: true,
53+
},
54+
"protection_level": {
55+
Type: schema.TypeString,
56+
Optional: true,
57+
ForceNew: true,
58+
Default: "SOFTWARE",
59+
ValidateFunc: validation.StringInSlice([]string{"SOFTWARE", "HSM", ""}, false),
60+
},
61+
},
62+
},
63+
},
4264
"self_link": {
4365
Type: schema.TypeString,
4466
Computed: true,
@@ -84,7 +106,10 @@ func resourceKmsCryptoKeyCreate(d *schema.ResourceData, meta interface{}) error
84106
Name: d.Get("name").(string),
85107
}
86108

87-
key := cloudkms.CryptoKey{Purpose: "ENCRYPT_DECRYPT"}
109+
key := cloudkms.CryptoKey{
110+
Purpose: "ENCRYPT_DECRYPT",
111+
VersionTemplate: expandVersionTemplate(d.Get("version_template").([]interface{})),
112+
}
88113

89114
if d.Get("rotation_period") != "" {
90115
rotationPeriod := d.Get("rotation_period").(string)
@@ -133,6 +158,10 @@ func resourceKmsCryptoKeyUpdate(d *schema.ResourceData, meta interface{}) error
133158
key.RotationPeriod = rotationPeriod
134159
}
135160

161+
if d.HasChange("version_template") {
162+
key.VersionTemplate = expandVersionTemplate(d.Get("version_template").([]interface{}))
163+
}
164+
136165
cryptoKey, err := config.clientKms.Projects.Locations.KeyRings.CryptoKeys.Patch(cryptoKeyId.cryptoKeyId(), &key).UpdateMask("rotation_period,next_rotation_time").Do()
137166

138167
if err != nil {
@@ -165,6 +194,10 @@ func resourceKmsCryptoKeyRead(d *schema.ResourceData, meta interface{}) error {
165194
d.Set("rotation_period", cryptoKey.RotationPeriod)
166195
d.Set("self_link", cryptoKey.Name)
167196

197+
if err = d.Set("version_template", flattenVersionTemplate(cryptoKey.VersionTemplate)); err != nil {
198+
return fmt.Errorf("Error setting version_tempalte in state: %s", err.Error())
199+
}
200+
168201
d.SetId(cryptoKeyId.cryptoKeyId())
169202

170203
return nil
@@ -219,6 +252,33 @@ and all its CryptoKeyVersions will be destroyed, but it will still be present on
219252
return nil
220253
}
221254

255+
func expandVersionTemplate(configured []interface{}) *cloudkms.CryptoKeyVersionTemplate {
256+
if configured == nil || len(configured) == 0 {
257+
return nil
258+
}
259+
260+
data := configured[0].(map[string]interface{})
261+
return &cloudkms.CryptoKeyVersionTemplate{
262+
Algorithm: data["algorithm"].(string),
263+
ProtectionLevel: data["protection_level"].(string),
264+
}
265+
}
266+
267+
func flattenVersionTemplate(versionTemplate *cloudkms.CryptoKeyVersionTemplate) []map[string]interface{} {
268+
if versionTemplate == nil {
269+
return nil
270+
}
271+
272+
versionTemplateSchema := make([]map[string]interface{}, 0, 1)
273+
data := map[string]interface{}{
274+
"algorithm": versionTemplate.Algorithm,
275+
"protection_level": versionTemplate.ProtectionLevel,
276+
}
277+
278+
versionTemplateSchema = append(versionTemplateSchema, data)
279+
return versionTemplateSchema
280+
}
281+
222282
func validateKmsCryptoKeyRotationPeriod(value interface{}, _ string) (ws []string, errors []error) {
223283
period := value.(string)
224284
pattern := regexp.MustCompile("^([0-9.]*\\d)s$")

google/resource_kms_crypto_key_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,10 @@ resource "google_kms_crypto_key" "crypto_key" {
270270
name = "%s"
271271
key_ring = "${google_kms_key_ring.key_ring.self_link}"
272272
rotation_period = "1000000s"
273+
version_template {
274+
algorithm = "GOOGLE_SYMMETRIC_ENCRYPTION"
275+
protection_level = "SOFTWARE"
276+
}
273277
}
274278
`, projectId, projectId, projectOrg, projectBillingAccount, keyRingName, cryptoKeyName)
275279
}

website/docs/r/google_kms_crypto_key.html.markdown

+13
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ The following arguments are supported:
5959
the primary. The first rotation will take place after the specified period. The rotation period has the format
6060
of a decimal number with up to 9 fractional digits, followed by the letter s (seconds). It must be greater than
6161
a day (ie, 86400).
62+
63+
* `version_template` - (Optional) A template describing settings for new crypto key versions. Structure is documented below.
64+
65+
---
66+
67+
The `version_template` block supports:
68+
69+
* `algorithm` - (Required) The algorithm to use when creating a version based on this template.
70+
See the [algorithm reference](https://cloud.google.com/kms/docs/reference/rest/v1/CryptoKeyVersionAlgorithm)
71+
for possible inputs.
72+
73+
* `protection_level` - (Optional) The protection level to use when creating a version based on this template.
74+
One of `SOFTWARE`, or `HSM`.
6275

6376
## Attributes Reference
6477

0 commit comments

Comments
 (0)