|
| 1 | +############################################################################## |
| 2 | +# Secure Regional Bucket |
| 3 | +############################################################################## |
| 4 | +locals { |
| 5 | + prefix = var.prefix != null ? trimspace(var.prefix) != "" ? "${var.prefix}-" : "" : "" |
| 6 | +} |
| 7 | + |
| 8 | +locals { |
| 9 | + |
| 10 | + bucket_config = [{ |
| 11 | + access_tags = var.bucket_access_tags |
| 12 | + bucket_name = "${local.prefix}${var.bucket_name}" |
| 13 | + kms_encryption_enabled = var.kms_encryption_enabled |
| 14 | + add_bucket_name_suffix = var.add_bucket_name_suffix |
| 15 | + kms_guid = local.existing_kms_instance_guid |
| 16 | + kms_key_crn = local.kms_key_crn |
| 17 | + skip_iam_authorization_policy = local.create_cross_account_auth_policy || var.skip_cos_kms_iam_auth_policy |
| 18 | + management_endpoint_type = var.management_endpoint_type_for_bucket |
| 19 | + region_location = var.region |
| 20 | + resource_instance_id = var.existing_cos_instance_crn |
| 21 | + storage_class = var.bucket_storage_class |
| 22 | + force_delete = var.force_delete |
| 23 | + hard_quota = var.bucket_hard_quota |
| 24 | + expire_filter_prefix = var.expire_filter_prefix |
| 25 | + archive_filter_prefix = var.archive_filter_prefix |
| 26 | + object_locking_enabled = var.enable_object_locking |
| 27 | + object_lock_duration_days = var.object_lock_duration_days |
| 28 | + object_lock_duration_years = var.object_lock_duration_years |
| 29 | + |
| 30 | + activity_tracking = { |
| 31 | + read_data_events = true |
| 32 | + write_data_events = true |
| 33 | + } |
| 34 | + archive_rule = var.archive_days != null ? { |
| 35 | + enable = true |
| 36 | + days = var.archive_days |
| 37 | + type = var.archive_type |
| 38 | + } : null |
| 39 | + expire_rule = var.expire_days != null ? { |
| 40 | + enable = true |
| 41 | + days = var.expire_days |
| 42 | + } : null |
| 43 | + metrics_monitoring = { |
| 44 | + usage_metrics_enabled = true |
| 45 | + request_metrics_enabled = true |
| 46 | + management_events = true |
| 47 | + metrics_monitoring_crn = var.monitoring_crn |
| 48 | + } |
| 49 | + object_versioning = { |
| 50 | + enable = var.enable_object_versioning |
| 51 | + } |
| 52 | + retention_rule = var.enable_retention ? { |
| 53 | + default = var.default_retention_days |
| 54 | + maximum = var.maximum_retention_days |
| 55 | + minimum = var.minimum_retention_days |
| 56 | + permanent = var.enable_permanent_retention |
| 57 | + } : null |
| 58 | + cos_bucket_cbr_rules = var.cos_bucket_cbr_rules |
| 59 | + }] |
| 60 | +} |
| 61 | +####################################################################################################################### |
| 62 | +# Parse COS |
| 63 | +####################################################################################################################### |
| 64 | + |
| 65 | +module "cos_instance_crn_parser" { |
| 66 | + source = "terraform-ibm-modules/common-utilities/ibm//modules/crn-parser" |
| 67 | + version = "1.1.0" |
| 68 | + crn = var.existing_cos_instance_crn |
| 69 | +} |
| 70 | + |
| 71 | +locals { |
| 72 | + cos_instance_guid = module.cos_instance_crn_parser.service_instance |
| 73 | +} |
| 74 | +####################################################################################################################### |
| 75 | +# KMS Key |
| 76 | +####################################################################################################################### |
| 77 | + |
| 78 | +locals { |
| 79 | + existing_kms_instance_guid = var.kms_encryption_enabled ? var.existing_kms_instance_crn != null ? module.kms_instance_crn_parser[0].service_instance : var.existing_kms_key_crn != null ? module.kms_key_crn_parser[0].service_instance : null : null |
| 80 | + kms_region = var.kms_encryption_enabled ? var.existing_kms_instance_crn != null ? module.kms_instance_crn_parser[0].region : var.existing_kms_key_crn != null ? module.kms_key_crn_parser[0].region : null : null |
| 81 | + kms_service_name = var.kms_encryption_enabled ? var.existing_kms_instance_crn != null ? module.kms_instance_crn_parser[0].service_name : var.existing_kms_key_crn != null ? module.kms_key_crn_parser[0].service_name : null : null |
| 82 | + kms_account_id = var.kms_encryption_enabled ? var.existing_kms_instance_crn != null ? module.kms_instance_crn_parser[0].account_id : var.existing_kms_key_crn != null ? module.kms_key_crn_parser[0].account_id : null : null |
| 83 | + kms_key_crn = var.kms_encryption_enabled ? var.existing_kms_key_crn != null ? var.existing_kms_key_crn : module.kms[0].keys[format("%s.%s", var.cos_key_ring_name, var.cos_key_name)].crn : null |
| 84 | + kms_key_id = var.kms_encryption_enabled ? var.existing_kms_key_crn != null ? module.kms_key_crn_parser[0].resource : module.kms[0].keys[format("%s.%s", var.cos_key_ring_name, var.cos_key_name)].key_id : null |
| 85 | + create_cross_account_auth_policy = !var.skip_cos_kms_iam_auth_policy && var.ibmcloud_kms_api_key != null |
| 86 | +} |
| 87 | +######################################################################################################################## |
| 88 | +# Parse KMS info from given CRNs |
| 89 | +######################################################################################################################## |
| 90 | + |
| 91 | +module "kms_instance_crn_parser" { |
| 92 | + count = var.existing_kms_instance_crn != null ? 1 : 0 |
| 93 | + source = "terraform-ibm-modules/common-utilities/ibm//modules/crn-parser" |
| 94 | + version = "1.1.0" |
| 95 | + crn = var.existing_kms_instance_crn |
| 96 | +} |
| 97 | + |
| 98 | +module "kms_key_crn_parser" { |
| 99 | + count = var.existing_kms_key_crn != null ? 1 : 0 |
| 100 | + source = "terraform-ibm-modules/common-utilities/ibm//modules/crn-parser" |
| 101 | + version = "1.1.0" |
| 102 | + crn = var.existing_kms_key_crn |
| 103 | +} |
| 104 | + |
| 105 | +# Create IAM Authorization Policy to allow COS to access KMS for the encryption key |
| 106 | +resource "ibm_iam_authorization_policy" "cos_kms_policy" { |
| 107 | + count = local.create_cross_account_auth_policy ? 1 : 0 |
| 108 | + provider = ibm.kms |
| 109 | + source_service_account = module.cos_instance_crn_parser.account_id |
| 110 | + source_service_name = "cloud-object-storage" |
| 111 | + source_resource_instance_id = local.cos_instance_guid |
| 112 | + roles = ["Reader"] |
| 113 | + description = "Allow the COS instance ${local.cos_instance_guid} to read the ${local.kms_service_name} key ${local.kms_key_id} from the instance ${local.existing_kms_instance_guid}" |
| 114 | + resource_attributes { |
| 115 | + name = "serviceName" |
| 116 | + operator = "stringEquals" |
| 117 | + value = local.kms_service_name |
| 118 | + } |
| 119 | + resource_attributes { |
| 120 | + name = "accountId" |
| 121 | + operator = "stringEquals" |
| 122 | + value = local.kms_account_id |
| 123 | + } |
| 124 | + resource_attributes { |
| 125 | + name = "serviceInstance" |
| 126 | + operator = "stringEquals" |
| 127 | + value = local.existing_kms_instance_guid |
| 128 | + } |
| 129 | + resource_attributes { |
| 130 | + name = "resourceType" |
| 131 | + operator = "stringEquals" |
| 132 | + value = "key" |
| 133 | + } |
| 134 | + resource_attributes { |
| 135 | + name = "resource" |
| 136 | + operator = "stringEquals" |
| 137 | + value = local.kms_key_id |
| 138 | + } |
| 139 | + # Scope of policy now includes the key, so ensure to create new policy before |
| 140 | + # destroying old one to prevent any disruption to every day services. |
| 141 | + lifecycle { |
| 142 | + create_before_destroy = true |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +resource "time_sleep" "wait_for_authorization_policy" { |
| 147 | + depends_on = [ibm_iam_authorization_policy.cos_kms_policy] |
| 148 | + create_duration = "30s" |
| 149 | +} |
| 150 | + |
| 151 | +# KMS root key for COS bucket |
| 152 | +module "kms" { |
| 153 | + providers = { |
| 154 | + ibm = ibm.kms |
| 155 | + } |
| 156 | + count = var.kms_encryption_enabled && var.existing_kms_key_crn != null ? 0 : 1 # no need to create any KMS resources if passing an existing key. |
| 157 | + source = "terraform-ibm-modules/kms-all-inclusive/ibm" |
| 158 | + version = "5.1.7" |
| 159 | + create_key_protect_instance = false |
| 160 | + region = local.kms_region |
| 161 | + existing_kms_instance_crn = var.existing_kms_instance_crn |
| 162 | + key_ring_endpoint_type = var.kms_endpoint_type |
| 163 | + key_endpoint_type = var.kms_endpoint_type |
| 164 | + keys = [ |
| 165 | + { |
| 166 | + key_ring_name = var.cos_key_ring_name |
| 167 | + existing_key_ring = false |
| 168 | + keys = [ |
| 169 | + { |
| 170 | + key_name = var.cos_key_name |
| 171 | + standard_key = false |
| 172 | + rotation_interval_month = 3 |
| 173 | + dual_auth_delete_enabled = false |
| 174 | + force_delete = true |
| 175 | + } |
| 176 | + ] |
| 177 | + } |
| 178 | + ] |
| 179 | +} |
| 180 | + |
| 181 | +####################################################################################################################### |
| 182 | +# COS Bucket |
| 183 | +####################################################################################################################### |
| 184 | + |
| 185 | +module "cos" { |
| 186 | + providers = { |
| 187 | + ibm = ibm.cos |
| 188 | + } |
| 189 | + depends_on = [time_sleep.wait_for_authorization_policy] |
| 190 | + source = "../../../modules/buckets" |
| 191 | + bucket_configs = local.bucket_config |
| 192 | +} |
0 commit comments