Skip to content

Commit 4429aad

Browse files
Add new encryption_spec field (#15068) (#24328)
[upstream:a71440718aca830a9504c23ef92e87e21e34e290] Signed-off-by: Modular Magician <[email protected]>
1 parent 597e0d6 commit 4429aad

File tree

5 files changed

+125
-2
lines changed

5 files changed

+125
-2
lines changed

.changelog/15068.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
privateca: added `encryption_spec` field to `google_privateca_ca_pool` resource
3+
```

google/services/privateca/resource_privateca_ca_pool.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,26 @@ running 'gcloud privateca locations list'.`,
7878
ValidateFunc: verify.ValidateEnum([]string{"ENTERPRISE", "DEVOPS"}),
7979
Description: `The Tier of this CaPool. Possible values: ["ENTERPRISE", "DEVOPS"]`,
8080
},
81+
"encryption_spec": {
82+
Type: schema.TypeList,
83+
Optional: true,
84+
ForceNew: true,
85+
Description: `Used when customer would like to encrypt data at rest. The customer-provided key will be used
86+
to encrypt the Subject, SubjectAltNames and PEM-encoded certificate fields. When unspecified,
87+
customer data will remain unencrypted.`,
88+
MaxItems: 1,
89+
Elem: &schema.Resource{
90+
Schema: map[string]*schema.Schema{
91+
"cloud_kms_key": {
92+
Type: schema.TypeString,
93+
Optional: true,
94+
ForceNew: true,
95+
Description: `The resource name for an existing Cloud KMS key in the format
96+
'projects/*/locations/*/keyRings/*/cryptoKeys/*'.`,
97+
},
98+
},
99+
},
100+
},
81101
"issuance_policy": {
82102
Type: schema.TypeList,
83103
Optional: true,
@@ -659,6 +679,12 @@ func resourcePrivatecaCaPoolCreate(d *schema.ResourceData, meta interface{}) err
659679
} else if v, ok := d.GetOkExists("publishing_options"); !tpgresource.IsEmptyValue(reflect.ValueOf(publishingOptionsProp)) && (ok || !reflect.DeepEqual(v, publishingOptionsProp)) {
660680
obj["publishingOptions"] = publishingOptionsProp
661681
}
682+
encryptionSpecProp, err := expandPrivatecaCaPoolEncryptionSpec(d.Get("encryption_spec"), d, config)
683+
if err != nil {
684+
return err
685+
} else if v, ok := d.GetOkExists("encryption_spec"); !tpgresource.IsEmptyValue(reflect.ValueOf(encryptionSpecProp)) && (ok || !reflect.DeepEqual(v, encryptionSpecProp)) {
686+
obj["encryptionSpec"] = encryptionSpecProp
687+
}
662688
effectiveLabelsProp, err := expandPrivatecaCaPoolEffectiveLabels(d.Get("effective_labels"), d, config)
663689
if err != nil {
664690
return err
@@ -776,6 +802,9 @@ func resourcePrivatecaCaPoolRead(d *schema.ResourceData, meta interface{}) error
776802
if err := d.Set("labels", flattenPrivatecaCaPoolLabels(res["labels"], d, config)); err != nil {
777803
return fmt.Errorf("Error reading CaPool: %s", err)
778804
}
805+
if err := d.Set("encryption_spec", flattenPrivatecaCaPoolEncryptionSpec(res["encryptionSpec"], d, config)); err != nil {
806+
return fmt.Errorf("Error reading CaPool: %s", err)
807+
}
779808
if err := d.Set("terraform_labels", flattenPrivatecaCaPoolTerraformLabels(res["labels"], d, config)); err != nil {
780809
return fmt.Errorf("Error reading CaPool: %s", err)
781810
}
@@ -1201,6 +1230,23 @@ func flattenPrivatecaCaPoolLabels(v interface{}, d *schema.ResourceData, config
12011230
return transformed
12021231
}
12031232

1233+
func flattenPrivatecaCaPoolEncryptionSpec(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1234+
if v == nil {
1235+
return nil
1236+
}
1237+
original := v.(map[string]interface{})
1238+
if len(original) == 0 {
1239+
return nil
1240+
}
1241+
transformed := make(map[string]interface{})
1242+
transformed["cloud_kms_key"] =
1243+
flattenPrivatecaCaPoolEncryptionSpecCloudKmsKey(original["cloudKmsKey"], d, config)
1244+
return []interface{}{transformed}
1245+
}
1246+
func flattenPrivatecaCaPoolEncryptionSpecCloudKmsKey(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1247+
return v
1248+
}
1249+
12041250
func flattenPrivatecaCaPoolTerraformLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
12051251
if v == nil {
12061252
return v
@@ -1601,6 +1647,29 @@ func expandPrivatecaCaPoolPublishingOptionsEncodingFormat(v interface{}, d tpgre
16011647
return v, nil
16021648
}
16031649

1650+
func expandPrivatecaCaPoolEncryptionSpec(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
1651+
l := v.([]interface{})
1652+
if len(l) == 0 || l[0] == nil {
1653+
return nil, nil
1654+
}
1655+
raw := l[0]
1656+
original := raw.(map[string]interface{})
1657+
transformed := make(map[string]interface{})
1658+
1659+
transformedCloudKmsKey, err := expandPrivatecaCaPoolEncryptionSpecCloudKmsKey(original["cloud_kms_key"], d, config)
1660+
if err != nil {
1661+
return nil, err
1662+
} else if val := reflect.ValueOf(transformedCloudKmsKey); val.IsValid() && !tpgresource.IsEmptyValue(val) {
1663+
transformed["cloudKmsKey"] = transformedCloudKmsKey
1664+
}
1665+
1666+
return transformed, nil
1667+
}
1668+
1669+
func expandPrivatecaCaPoolEncryptionSpecCloudKmsKey(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
1670+
return v, nil
1671+
}
1672+
16041673
func expandPrivatecaCaPoolEffectiveLabels(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
16051674
if v == nil {
16061675
return map[string]string{}, nil

google/services/privateca/resource_privateca_ca_pool_generated_meta.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ api_resource_type_kind: 'CaPool'
77
fields:
88
- field: 'effective_labels'
99
provider_only: true
10+
- field: 'encryption_spec.cloud_kms_key'
1011
- field: 'issuance_policy.allowed_issuance_modes.allow_config_based_issuance'
1112
- field: 'issuance_policy.allowed_issuance_modes.allow_csr_based_issuance'
1213
- field: 'issuance_policy.allowed_key_types.elliptic_curve.signature_algorithm'

google/services/privateca/resource_privateca_ca_pool_generated_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ func TestAccPrivatecaCaPool_privatecaCapoolAllFieldsExample(t *testing.T) {
7676
t.Parallel()
7777

7878
context := map[string]interface{}{
79+
"cloud_kms_key": acctest.BootstrapKMSKeyWithPurposeInLocation(t, "ENCRYPT_DECRYPT", "asia-east1").CryptoKey.Name,
80+
"pool_location": "asia-east1",
7981
"random_suffix": acctest.RandString(t, 10),
8082
}
8183

@@ -99,9 +101,19 @@ func TestAccPrivatecaCaPool_privatecaCapoolAllFieldsExample(t *testing.T) {
99101

100102
func testAccPrivatecaCaPool_privatecaCapoolAllFieldsExample(context map[string]interface{}) string {
101103
return acctest.Nprintf(`
104+
resource "google_project_service_identity" "privateca_sa" {
105+
service = "privateca.googleapis.com"
106+
}
107+
108+
resource "google_kms_crypto_key_iam_member" "privateca_sa_keyuser_encrypterdecrypter" {
109+
crypto_key_id = "%{cloud_kms_key}"
110+
role = "roles/cloudkms.cryptoKeyEncrypterDecrypter"
111+
member = google_project_service_identity.privateca_sa.member
112+
}
113+
102114
resource "google_privateca_ca_pool" "default" {
103115
name = "tf-test-my-pool%{random_suffix}"
104-
location = "us-central1"
116+
location = "%{pool_location}"
105117
tier = "ENTERPRISE"
106118
publishing_options {
107119
publish_ca_cert = false
@@ -111,6 +123,9 @@ resource "google_privateca_ca_pool" "default" {
111123
labels = {
112124
foo = "bar"
113125
}
126+
encryption_spec {
127+
cloud_kms_key = "%{cloud_kms_key}"
128+
}
114129
issuance_policy {
115130
allowed_key_types {
116131
elliptic_curve {
@@ -188,6 +203,10 @@ resource "google_privateca_ca_pool" "default" {
188203
}
189204
}
190205
}
206+
207+
depends_on = [
208+
google_kms_crypto_key_iam_member.privateca_sa_keyuser_encrypterdecrypter,
209+
]
191210
}
192211
`, context)
193212
}

website/docs/r/privateca_ca_pool.html.markdown

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,19 @@ resource "google_privateca_ca_pool" "default" {
6363

6464

6565
```hcl
66+
resource "google_project_service_identity" "privateca_sa" {
67+
service = "privateca.googleapis.com"
68+
}
69+
70+
resource "google_kms_crypto_key_iam_member" "privateca_sa_keyuser_encrypterdecrypter" {
71+
crypto_key_id = "projects/keys-project/locations/asia-east1/keyRings/key-ring/cryptoKeys/crypto-key"
72+
role = "roles/cloudkms.cryptoKeyEncrypterDecrypter"
73+
member = google_project_service_identity.privateca_sa.member
74+
}
75+
6676
resource "google_privateca_ca_pool" "default" {
6777
name = "my-pool"
68-
location = "us-central1"
78+
location = "asia-east1"
6979
tier = "ENTERPRISE"
7080
publishing_options {
7181
publish_ca_cert = false
@@ -75,6 +85,9 @@ resource "google_privateca_ca_pool" "default" {
7585
labels = {
7686
foo = "bar"
7787
}
88+
encryption_spec {
89+
cloud_kms_key = "projects/keys-project/locations/asia-east1/keyRings/key-ring/cryptoKeys/crypto-key"
90+
}
7891
issuance_policy {
7992
allowed_key_types {
8093
elliptic_curve {
@@ -152,6 +165,10 @@ resource "google_privateca_ca_pool" "default" {
152165
}
153166
}
154167
}
168+
169+
depends_on = [
170+
google_kms_crypto_key_iam_member.privateca_sa_keyuser_encrypterdecrypter,
171+
]
155172
}
156173
```
157174

@@ -194,6 +211,13 @@ The following arguments are supported:
194211
**Note**: This field is non-authoritative, and will only manage the labels present in your configuration.
195212
Please refer to the field `effective_labels` for all of the labels present on the resource.
196213

214+
* `encryption_spec` -
215+
(Optional)
216+
Used when customer would like to encrypt data at rest. The customer-provided key will be used
217+
to encrypt the Subject, SubjectAltNames and PEM-encoded certificate fields. When unspecified,
218+
customer data will remain unencrypted.
219+
Structure is [documented below](#nested_encryption_spec).
220+
197221
* `project` - (Optional) The ID of the project in which the resource belongs.
198222
If it is not provided, the provider project is used.
199223

@@ -576,6 +600,13 @@ The following arguments are supported:
576600
will be published in PEM.
577601
Possible values are: `PEM`, `DER`.
578602

603+
<a name="nested_encryption_spec"></a>The `encryption_spec` block supports:
604+
605+
* `cloud_kms_key` -
606+
(Optional)
607+
The resource name for an existing Cloud KMS key in the format
608+
`projects/*/locations/*/keyRings/*/cryptoKeys/*`.
609+
579610
## Attributes Reference
580611

581612
In addition to the arguments listed above, the following computed attributes are exported:

0 commit comments

Comments
 (0)