Skip to content

Commit b4b6491

Browse files
authored
feat: The COS KMS auth policy created by the DA is now scoped to the exact KMS key. NOTE: When upgrading from an old version, the auth policy will be re-created, however it will be non disruptive as it will create the new policy before removing the old one (#206)
1 parent 2218c24 commit b4b6491

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

.secrets.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"files": "go.sum|^.secrets.baseline$",
44
"lines": null
55
},
6-
"generated_at": "2023-12-11T06:39:44Z",
6+
"generated_at": "2023-12-12T06:39:44Z",
77
"plugins_used": [
88
{
99
"name": "AWSKeyDetector"

solutions/instances/main.tf

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ locals {
3838
kms_region = ((length(coalesce(local.buckets_config, [])) != 0) ?
3939
(var.existing_cos_kms_key_crn == null ? module.kms_instance_crn_parser[0].region : null) : null)
4040

41-
cos_kms_key_crn = var.existing_cos_kms_key_crn != null ? var.existing_cos_kms_key_crn : length(coalesce(local.buckets_config, [])) != 0 ? module.kms[0].keys[format("%s.%s", local.cos_key_ring_name, local.cos_key_name)].crn : null
41+
cos_kms_key_crn = var.existing_cos_kms_key_crn != null ? var.existing_cos_kms_key_crn : length(coalesce(local.buckets_config, [])) != 0 ? module.kms[0].keys[format("%s.%s", local.cos_key_ring_name, local.cos_key_name)].crn : null
42+
parsed_kms_key_crn = local.cos_kms_key_crn != null ? split(":", local.cos_kms_key_crn) : []
43+
cos_kms_key_id = length(local.parsed_kms_key_crn) > 0 ? local.parsed_kms_key_crn[9] : null
44+
cos_kms_scope = length(local.parsed_kms_key_crn) > 0 ? local.parsed_kms_key_crn[6] : null
45+
kms_account_id = length(local.parsed_kms_key_crn) > 0 ? split("/", local.cos_kms_scope)[1] : null
4246

4347
cos_target_bucket_name = var.existing_at_cos_target_bucket_name != null ? var.existing_at_cos_target_bucket_name : var.enable_at_event_routing_to_cos_bucket ? module.cos_bucket[0].buckets[local.at_cos_target_bucket_name].bucket_name : null
4448
cos_resource_group_id = var.cos_resource_group_name != null ? module.cos_resource_group[0].resource_group_id : module.resource_group.resource_group_id
@@ -350,7 +354,7 @@ resource "time_sleep" "wait_for_authorization_policy" {
350354
create_duration = "30s"
351355
}
352356

353-
# Data source to account settings for retrieving cross account id
357+
# Data source to account settings for retrieving COS cross account id
354358
data "ibm_iam_account_settings" "iam_cos_account_settings" {
355359
provider = ibm.cos
356360
}
@@ -365,10 +369,38 @@ resource "ibm_iam_authorization_policy" "policy" {
365369
source_service_account = data.ibm_iam_account_settings.iam_cos_account_settings.account_id
366370
source_service_name = "cloud-object-storage"
367371
source_resource_instance_id = local.cos_instance_guid
368-
target_service_name = local.kms_service
369-
target_resource_instance_id = local.existing_kms_guid
370372
roles = ["Reader"]
371-
description = "Allow the COS instance with GUID ${local.cos_instance_guid} reader access to the kms_service instance GUID ${local.existing_kms_guid}"
373+
description = "Allow the COS instance ${local.cos_instance_guid} to read the ${local.kms_service} key ${local.cos_kms_key_id} from the instance ${local.existing_kms_guid}"
374+
resource_attributes {
375+
name = "serviceName"
376+
operator = "stringEquals"
377+
value = local.kms_service
378+
}
379+
resource_attributes {
380+
name = "accountId"
381+
operator = "stringEquals"
382+
value = local.kms_account_id
383+
}
384+
resource_attributes {
385+
name = "serviceInstance"
386+
operator = "stringEquals"
387+
value = local.existing_kms_guid
388+
}
389+
resource_attributes {
390+
name = "resourceType"
391+
operator = "stringEquals"
392+
value = "key"
393+
}
394+
resource_attributes {
395+
name = "resource"
396+
operator = "stringEquals"
397+
value = local.cos_kms_key_id
398+
}
399+
# Scope of policy now includes the key, so ensure to create new policy before
400+
# destroying old one to prevent any disruption to every day services.
401+
lifecycle {
402+
create_before_destroy = true
403+
}
372404
}
373405

374406
module "cos_instance" {
@@ -377,7 +409,7 @@ module "cos_instance" {
377409
}
378410
count = var.existing_cos_instance_crn == null && length(coalesce(local.buckets_config, [])) != 0 ? 1 : 0 # no need to call COS module if consumer is using existing COS instance
379411
source = "terraform-ibm-modules/cos/ibm//modules/fscloud"
380-
version = "8.11.11"
412+
version = "8.14.1"
381413
resource_group_id = local.cos_resource_group_id
382414
create_cos_instance = true
383415
cos_instance_name = var.prefix != null ? "${var.prefix}-${var.cos_instance_name}" : var.cos_instance_name
@@ -394,7 +426,7 @@ module "cos_bucket" {
394426
}
395427
count = length(coalesce(local.buckets_config, [])) != 0 ? 1 : 0 # no need to call COS module if consumer is using existing COS bucket
396428
source = "terraform-ibm-modules/cos/ibm//modules/buckets"
397-
version = "8.11.11"
429+
version = "8.14.1"
398430
bucket_configs = [
399431
for value in local.buckets_config :
400432
{

0 commit comments

Comments
 (0)