This commit is contained in:
79
skills/google-cloud-iac-patterns/references/kms.md
Normal file
79
skills/google-cloud-iac-patterns/references/kms.md
Normal file
@@ -0,0 +1,79 @@
|
||||
# Cloud KMS
|
||||
|
||||
A key ring and a key for customer-managed encryption, and the grant that lets a product use it.
|
||||
|
||||
- The key ring must be in the same location as the resource it protects.
|
||||
- Key rings and keys cannot be deleted. `prevent_destroy` stops Terraform trying.
|
||||
- `rotation_period` is in seconds, such as `"7776000s"` for 90 days.
|
||||
- The product's service agent, not the workload's service account, needs `roles/cloudkms.cryptoKeyEncrypterDecrypter` on the key. The resource that uses the key must `depends_on` that grant.
|
||||
- Service agents: Pub/Sub is `service-NUMBER@gcp-sa-pubsub.iam.gserviceaccount.com`, Cloud Run is `service-NUMBER@serverless-robot-prod.iam.gserviceaccount.com`, Cloud Storage comes from the `google_storage_project_service_account` data source.
|
||||
|
||||
```hcl
|
||||
variable "project_id" {
|
||||
type = string
|
||||
description = "Project to deploy into."
|
||||
}
|
||||
|
||||
variable "region" {
|
||||
type = string
|
||||
description = "Region for regional resources."
|
||||
}
|
||||
|
||||
data "google_project" "this" {
|
||||
project_id = var.project_id
|
||||
}
|
||||
|
||||
data "google_storage_project_service_account" "this" {
|
||||
project = var.project_id
|
||||
}
|
||||
|
||||
resource "google_kms_key_ring" "workload" {
|
||||
project = var.project_id
|
||||
name = "workload"
|
||||
location = var.region
|
||||
}
|
||||
|
||||
resource "google_kms_crypto_key" "data" {
|
||||
name = "data"
|
||||
key_ring = google_kms_key_ring.workload.id
|
||||
purpose = "ENCRYPT_DECRYPT"
|
||||
rotation_period = "7776000s"
|
||||
|
||||
lifecycle {
|
||||
prevent_destroy = true
|
||||
}
|
||||
}
|
||||
|
||||
resource "google_kms_crypto_key_iam_member" "pubsub_uses_key" {
|
||||
crypto_key_id = google_kms_crypto_key.data.id
|
||||
role = "roles/cloudkms.cryptoKeyEncrypterDecrypter"
|
||||
member = "serviceAccount:service-${data.google_project.this.number}@gcp-sa-pubsub.iam.gserviceaccount.com"
|
||||
}
|
||||
|
||||
resource "google_kms_crypto_key_iam_member" "storage_uses_key" {
|
||||
crypto_key_id = google_kms_crypto_key.data.id
|
||||
role = "roles/cloudkms.cryptoKeyEncrypterDecrypter"
|
||||
member = "serviceAccount:${data.google_storage_project_service_account.this.email_address}"
|
||||
}
|
||||
|
||||
resource "google_pubsub_topic" "encrypted" {
|
||||
project = var.project_id
|
||||
name = "encrypted-events"
|
||||
kms_key_name = google_kms_crypto_key.data.id
|
||||
|
||||
depends_on = [google_kms_crypto_key_iam_member.pubsub_uses_key]
|
||||
}
|
||||
|
||||
resource "google_storage_bucket" "encrypted" {
|
||||
project = var.project_id
|
||||
name = "${var.project_id}-encrypted"
|
||||
location = var.region
|
||||
uniform_bucket_level_access = true
|
||||
|
||||
encryption {
|
||||
default_kms_key_name = google_kms_crypto_key.data.id
|
||||
}
|
||||
|
||||
depends_on = [google_kms_crypto_key_iam_member.storage_uses_key]
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user