# key-baseline / gcp / t1 "Standard" # # A hardware-protected key with a 30-day wait before any destroyed version is # really gone, key managers and key users kept apart, and a record every time # the key is used. locals { labels = merge({ owner = var.owner environment = var.environment data_classification = "restricted" managed_by = "terraform" }, var.extra_labels) } resource "google_project_service" "kms" { project = var.project_id service = "cloudkms.googleapis.com" disable_on_destroy = false } resource "random_id" "ring" { byte_length = 3 } resource "google_kms_key_ring" "this" { name = "${var.name}-${random_id.ring.hex}" location = var.location project = var.project_id depends_on = [google_project_service.kms] } resource "google_kms_crypto_key" "this" { name = var.name key_ring = google_kms_key_ring.this.id rotation_period = "7776000s" # 90 days # Destroying a key makes everything encrypted with it unreadable, for good. # A destroy that reaches this key stops with an error. lifecycle { prevent_destroy = true } labels = local.labels # A destroyed version waits this long before the material is really gone, and # can be restored until then. Thirty days leaves time to notice a mistake. destroy_scheduled_duration = "2592000s" version_template { algorithm = "GOOGLE_SYMMETRIC_ENCRYPTION" protection_level = "HSM" } } # Managers on the ring, users on the key. Nobody needs both. resource "google_kms_key_ring_iam_member" "admins" { for_each = toset(var.key_admin_members) key_ring_id = google_kms_key_ring.this.id role = "roles/cloudkms.admin" member = each.value } resource "google_kms_crypto_key_iam_member" "users" { for_each = toset(var.key_user_members) crypto_key_id = google_kms_crypto_key.this.id role = "roles/cloudkms.cryptoKeyEncrypterDecrypter" member = each.value } # Encrypt and decrypt calls are Data Access logs, which are off by default. Until # they are on, nobody can say who used the key or when. # # This resource owns the audit settings for Cloud KMS in the whole project. If # something else already manages them, merge the two first. resource "google_project_iam_audit_config" "kms" { project = var.project_id service = "cloudkms.googleapis.com" audit_log_config { log_type = "DATA_READ" } audit_log_config { log_type = "DATA_WRITE" } }