Skip to content

Commit

Permalink
feat: The COS KMS auth policy created by the DA is now scoped to the …
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
ocofaigh authored Nov 1, 2024
1 parent 2218c24 commit b4b6491
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"files": "go.sum|^.secrets.baseline$",
"lines": null
},
"generated_at": "2023-12-11T06:39:44Z",
"generated_at": "2023-12-12T06:39:44Z",
"plugins_used": [
{
"name": "AWSKeyDetector"
Expand Down
46 changes: 39 additions & 7 deletions solutions/instances/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ locals {
kms_region = ((length(coalesce(local.buckets_config, [])) != 0) ?
(var.existing_cos_kms_key_crn == null ? module.kms_instance_crn_parser[0].region : null) : null)

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
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
parsed_kms_key_crn = local.cos_kms_key_crn != null ? split(":", local.cos_kms_key_crn) : []
cos_kms_key_id = length(local.parsed_kms_key_crn) > 0 ? local.parsed_kms_key_crn[9] : null
cos_kms_scope = length(local.parsed_kms_key_crn) > 0 ? local.parsed_kms_key_crn[6] : null
kms_account_id = length(local.parsed_kms_key_crn) > 0 ? split("/", local.cos_kms_scope)[1] : null

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
cos_resource_group_id = var.cos_resource_group_name != null ? module.cos_resource_group[0].resource_group_id : module.resource_group.resource_group_id
Expand Down Expand Up @@ -350,7 +354,7 @@ resource "time_sleep" "wait_for_authorization_policy" {
create_duration = "30s"
}

# Data source to account settings for retrieving cross account id
# Data source to account settings for retrieving COS cross account id
data "ibm_iam_account_settings" "iam_cos_account_settings" {
provider = ibm.cos
}
Expand All @@ -365,10 +369,38 @@ resource "ibm_iam_authorization_policy" "policy" {
source_service_account = data.ibm_iam_account_settings.iam_cos_account_settings.account_id
source_service_name = "cloud-object-storage"
source_resource_instance_id = local.cos_instance_guid
target_service_name = local.kms_service
target_resource_instance_id = local.existing_kms_guid
roles = ["Reader"]
description = "Allow the COS instance with GUID ${local.cos_instance_guid} reader access to the kms_service instance GUID ${local.existing_kms_guid}"
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}"
resource_attributes {
name = "serviceName"
operator = "stringEquals"
value = local.kms_service
}
resource_attributes {
name = "accountId"
operator = "stringEquals"
value = local.kms_account_id
}
resource_attributes {
name = "serviceInstance"
operator = "stringEquals"
value = local.existing_kms_guid
}
resource_attributes {
name = "resourceType"
operator = "stringEquals"
value = "key"
}
resource_attributes {
name = "resource"
operator = "stringEquals"
value = local.cos_kms_key_id
}
# Scope of policy now includes the key, so ensure to create new policy before
# destroying old one to prevent any disruption to every day services.
lifecycle {
create_before_destroy = true
}
}

module "cos_instance" {
Expand All @@ -377,7 +409,7 @@ module "cos_instance" {
}
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
source = "terraform-ibm-modules/cos/ibm//modules/fscloud"
version = "8.11.11"
version = "8.14.1"
resource_group_id = local.cos_resource_group_id
create_cos_instance = true
cos_instance_name = var.prefix != null ? "${var.prefix}-${var.cos_instance_name}" : var.cos_instance_name
Expand All @@ -394,7 +426,7 @@ module "cos_bucket" {
}
count = length(coalesce(local.buckets_config, [])) != 0 ? 1 : 0 # no need to call COS module if consumer is using existing COS bucket
source = "terraform-ibm-modules/cos/ibm//modules/buckets"
version = "8.11.11"
version = "8.14.1"
bucket_configs = [
for value in local.buckets_config :
{
Expand Down

0 comments on commit b4b6491

Please sign in to comment.