Skip to content

Commit

Permalink
Feat: Encrypt bucket with CMEK (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
ps-occrp authored Jul 9, 2024
1 parent 0856434 commit 07e3a4e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 8 deletions.
3 changes: 2 additions & 1 deletion examples/simple_bucket/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@ module "bucket" {
member = "group:[email protected]"
}]

autoclass = true
autoclass = true
encryption = { default_kms_key_name = null }
}
2 changes: 1 addition & 1 deletion modules/simple_bucket/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Functional examples are included in the
| bucket\_policy\_only | Enables Bucket Policy Only access to a bucket. | `bool` | `true` | no |
| cors | Configuration of CORS for bucket with structure as defined in https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_bucket#cors. | `any` | `[]` | no |
| custom\_placement\_config | Configuration of the bucket's custom location in a dual-region bucket setup. If the bucket is designated a single or multi-region, the variable are null. | <pre>object({<br> data_locations = list(string)<br> })</pre> | `null` | no |
| encryption | A Cloud KMS key that will be used to encrypt objects inserted into this bucket | <pre>object({<br> default_kms_key_name = string<br> })</pre> | `null` | no |
| encryption | A Cloud KMS key that will be used to encrypt objects inserted into this bucket. If default\_kms\_key\_name is set to 'null' a new keyring and key pair will be created and used to encrypt bucket using CMEK. | <pre>object({<br> default_kms_key_name = string<br> })</pre> | `null` | no |
| force\_destroy | When deleting a bucket, this boolean option will delete all contained objects. If false, Terraform will fail to delete buckets which contain objects. | `bool` | `false` | no |
| iam\_members | The list of IAM members to grant permissions on the bucket. | <pre>list(object({<br> role = string<br> member = string<br> }))</pre> | `[]` | no |
| labels | A set of key/value label pairs to assign to the bucket. | `map(string)` | `null` | no |
Expand Down
21 changes: 20 additions & 1 deletion modules/simple_bucket/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ resource "google_storage_bucket" "bucket" {
dynamic "encryption" {
for_each = var.encryption == null ? [] : [var.encryption]
content {
default_kms_key_name = var.encryption.default_kms_key_name
default_kms_key_name = var.encryption.default_kms_key_name != null ? var.encryption.default_kms_key_name : module.encryption_key[0].keys[var.name]
}
}

Expand Down Expand Up @@ -120,3 +120,22 @@ resource "google_storage_bucket_iam_member" "members" {
role = each.value.role
member = each.value.member
}

data "google_storage_project_service_account" "gcs_account" {
project = var.project_id
}

module "encryption_key" {
count = var.encryption == null ? 0 : (var.encryption.default_kms_key_name == null ? 1 : 0)
source = "terraform-google-modules/kms/google"
version = "~> 2.0"
project_id = var.project_id
location = var.location
keyring = var.name
prevent_destroy = false
keys = [var.name]
set_decrypters_for = [var.name]
set_encrypters_for = [var.name]
decrypters = ["serviceAccount:${data.google_storage_project_service_account.gcs_account.email_address}"]
encrypters = ["serviceAccount:${data.google_storage_project_service_account.gcs_account.email_address}"]
}
2 changes: 1 addition & 1 deletion modules/simple_bucket/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ variable "cors" {
}

variable "encryption" {
description = "A Cloud KMS key that will be used to encrypt objects inserted into this bucket"
description = "A Cloud KMS key that will be used to encrypt objects inserted into this bucket. If default_kms_key_name is set to 'null' a new keyring and key pair will be created and used to encrypt bucket using CMEK."
type = object({
default_kms_key_name = string
})
Expand Down
3 changes: 2 additions & 1 deletion test/setup/iam.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

locals {
int_required_roles = [
"roles/storage.admin",
"roles/cloudkms.admin",
"roles/iam.serviceAccountUser",
"roles/storage.admin",
]
}

Expand Down
7 changes: 4 additions & 3 deletions test/setup/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ module "project" {
billing_account = var.billing_account

activate_apis = [
"iam.googleapis.com",
"storage-api.googleapis.com",
"cloudkms.googleapis.com",
"cloudresourcemanager.googleapis.com",
"compute.googleapis.com",
"serviceusage.googleapis.com"
"iam.googleapis.com",
"serviceusage.googleapis.com",
"storage-api.googleapis.com",
]
}

0 comments on commit 07e3a4e

Please sign in to comment.