Skip to content

Commit 1ef3676

Browse files
modular-magicianRustem Bekmukhametov
and
Rustem Bekmukhametov
authored
CMEK support for Cloud Functions (#5973) (#11627)
* CMEK support for Cloud Functions (docker_repository and kms_key_name fields) * separate tests for CMEK and AR; added role binding for the CMEK test * Remaining IAM configs * Formatting * Granting additional permissions to the service accounts * Another attempts at fixing the IAM (potentially a race) issue * Accounting for feedback: create kms key via bootstraping util * documentation update * Remove refernces to beta in the docs (feedback) Co-authored-by: Rustem Bekmukhametov <[email protected]> Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Rustem Bekmukhametov <[email protected]>
1 parent 9ee005d commit 1ef3676

File tree

5 files changed

+47
-16
lines changed

5 files changed

+47
-16
lines changed

.changelog/5973.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
cloudfunctions: CMEK support for Cloud Functions
3+
```

.goreleaser.yml

+2-9
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,11 @@ builds:
3232
- -s -w -X internal/provider.Version={{.Version}}
3333
mod_timestamp: '{{ .CommitTimestamp }}'
3434
checksum:
35-
extra_files:
36-
- glob: 'terraform-registry-manifest.json'
37-
name_template: '{{ .ProjectName }}_{{ .Version }}_manifest.json'
3835
name_template: '{{ .ProjectName }}_{{ .Version }}_SHA256SUMS'
3936
algorithm: sha256
4037
publishers:
4138
- name: hc-releases
4239
checksum: true
43-
extra_files:
44-
- glob: 'terraform-registry-manifest.json'
45-
name_template: '{{ .ProjectName }}_{{ .Version }}_manifest.json'
4640
signature: true
4741
cmd: hc-releases upload-file -header="x-terraform-protocol-version={{ .Env.PROTOCOL_VERSION }}" -header="x-terraform-protocol-versions={{ .Env.PROTOCOL_VERSIONS }}" {{ abs .ArtifactPath }}
4842
env:
@@ -52,9 +46,6 @@ publishers:
5246
- AWS_SECRET_ACCESS_KEY={{ .Env.AWS_SECRET_ACCESS_KEY }}
5347
- AWS_SESSION_TOKEN={{ .Env.AWS_SESSION_TOKEN }}
5448
release:
55-
extra_files:
56-
- glob: 'terraform-registry-manifest.json'
57-
name_template: '{{ .ProjectName }}_{{ .Version }}_manifest.json'
5849
ids:
5950
- none
6051
signs:
@@ -67,6 +58,7 @@ signs:
6758
sign
6859
--dearmor
6960
--file ${artifact}
61+
--signer {{ .Env.SIGNER }}
7062
--out ${signature}
7163
artifacts: checksum
7264
# Signature file with GPG Public Key ID in filename (i.e. terraform-provider-awscc_VERSION_SHA256SUMS.7685B676.sig)
@@ -80,6 +72,7 @@ signs:
8072
sign
8173
--dearmor
8274
--file ${artifact}
75+
--signer {{ .Env.SIGNER }}
8376
--out ${signature}
8477
artifacts: checksum
8578
snapshot:

google/resource_cloudfunctions_function.go

+36
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,18 @@ func resourceCloudFunctionsFunction() *schema.Resource {
146146
},
147147
},
148148

149+
"docker_repository": {
150+
Type: schema.TypeString,
151+
Optional: true,
152+
Description: `User managed repository created in Artifact Registry optionally with a customer managed encryption key. If specified, deployments will use Artifact Registry for storing images built with Cloud Build.`,
153+
},
154+
155+
"kms_key_name": {
156+
Type: schema.TypeString,
157+
Optional: true,
158+
Description: `Resource name of a KMS crypto key (managed by the user) used to encrypt/decrypt function resources.`,
159+
},
160+
149161
"description": {
150162
Type: schema.TypeString,
151163
Optional: true,
@@ -498,6 +510,14 @@ func resourceCloudFunctionsCreate(d *schema.ResourceData, meta interface{}) erro
498510
function.VpcConnectorEgressSettings = v.(string)
499511
}
500512

513+
if v, ok := d.GetOk("docker_repository"); ok {
514+
function.DockerRepository = v.(string)
515+
}
516+
517+
if v, ok := d.GetOk("kms_key_name"); ok {
518+
function.KmsKeyName = v.(string)
519+
}
520+
501521
if v, ok := d.GetOk("max_instances"); ok {
502522
function.MaxInstances = int64(v.(int))
503523
}
@@ -629,6 +649,12 @@ func resourceCloudFunctionsRead(d *schema.ResourceData, meta interface{}) error
629649
if err := d.Set("event_trigger", flattenEventTrigger(function.EventTrigger)); err != nil {
630650
return fmt.Errorf("Error setting event_trigger: %s", err)
631651
}
652+
if err := d.Set("docker_repository", function.DockerRepository); err != nil {
653+
return fmt.Errorf("Error setting docker_repository: %s", err)
654+
}
655+
if err := d.Set("kms_key_name", function.KmsKeyName); err != nil {
656+
return fmt.Errorf("Error setting kms_key_name: %s", err)
657+
}
632658
if err := d.Set("max_instances", function.MaxInstances); err != nil {
633659
return fmt.Errorf("Error setting max_instances: %s", err)
634660
}
@@ -754,6 +780,16 @@ func resourceCloudFunctionsUpdate(d *schema.ResourceData, meta interface{}) erro
754780
updateMaskArr = append(updateMaskArr, "eventTrigger", "eventTrigger.failurePolicy.retry")
755781
}
756782

783+
if d.HasChange("docker_repository") {
784+
function.Runtime = d.Get("docker_repository").(string)
785+
updateMaskArr = append(updateMaskArr, "dockerRepository")
786+
}
787+
788+
if d.HasChange("kms_key_name") {
789+
function.Runtime = d.Get("docker_repository").(string)
790+
updateMaskArr = append(updateMaskArr, "kmsKeyName")
791+
}
792+
757793
if d.HasChange("max_instances") {
758794
function.MaxInstances = int64(d.Get("max_instances").(int))
759795
updateMaskArr = append(updateMaskArr, "maxInstances")

terraform-registry-manifest.json

-6
This file was deleted.

website/docs/r/cloudfunctions_function.html.markdown

+6-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,12 @@ Eg. `"nodejs10"`, `"nodejs12"`, `"nodejs14"`, `"python37"`, `"python38"`, `"pyth
146146
* `source_archive_object` - (Optional) The source archive object (file) in archive bucket.
147147

148148
* `source_repository` - (Optional) Represents parameters related to source repository where a function is hosted.
149-
Cannot be set alongside `source_archive_bucket` or `source_archive_object`. Structure is [documented below](#nested_source_repository).
149+
Cannot be set alongside `source_archive_bucket` or `source_archive_object`. Structure is [documented below](#nested_source_repository). It must match the pattern `projects/{project}/locations/{location}/repositories/{repository}`.*
150+
151+
* `docker_repository` - (Optional) User managed repository created in Artifact Registry optionally with a customer managed encryption key. If specified, deployments will use Artifact Registry. This is the repository to which the function docker image will be pushed after it is built by Cloud Build. If unspecified, Container Registry will be used by default, unless specified otherwise by other means.
152+
153+
* `kms_key_name` - (Optional) Resource name of a KMS crypto key (managed by the user) used to encrypt/decrypt function resources. It must match the pattern `projects/{project}/locations/{location}/keyRings/{key_ring}/cryptoKeys/{crypto_key}`.
154+
If specified, you must also provide an artifact registry repository using the `docker_repository` field that was created with the same KMS crypto key. Before deploying, please complete all pre-requisites described in https://cloud.google.com/functions/docs/securing/cmek#granting_service_accounts_access_to_the_key
150155

151156
* `max_instances` - (Optional) The limit on the maximum number of function instances that may coexist at a given time.
152157

0 commit comments

Comments
 (0)