Skip to content

Commit 4780227

Browse files
maheshwarishikhaShikha MaheshwariShikha Maheshwariocofaigh
authored
fix: Modify code to utilize existing_cos_kms_key_crn along with existing_kms_instance_crn (#190)
* fix: update condition to use existing_cos_kms_key_crn * fix: update code * fix: update test and variable description * fix: removed log analysis related parameters from upgrade test * fix: update upgrade test * fix: add CRN parser * fix: update code * fix: remove extra comment --------- Co-authored-by: Shikha Maheshwari <[email protected]> Co-authored-by: Shikha Maheshwari <[email protected]> Co-authored-by: Conall Ó Cofaigh <[email protected]>
1 parent c9423a5 commit 4780227

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

solutions/instances/main.tf

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
locals {
66

7+
# tflint-ignore: terraform_unused_declarations
8+
validate_existing_kms_inputs = (var.existing_cos_kms_key_crn != null && !var.skip_cos_kms_auth_policy) ? (var.existing_kms_instance_crn == null ? tobool("The existing_kms_instance_crn is not provided and is required to configure the COS - KMS authorization policy") : true) : true
79
# tflint-ignore: terraform_unused_declarations
810
validate_existing_cloud_monitoring = var.cloud_monitoring_provision && var.existing_cloud_monitoring_crn != null ? tobool("if cloud_monitoring_provision is set to true, then existing_cloud_monitoring_crn should be null and vice versa") : true
911
# tflint-ignore: terraform_unused_declarations
@@ -17,9 +19,26 @@ locals {
1719
at_cos_target_bucket_name = var.prefix != null ? "${var.prefix}-${var.at_cos_target_bucket_name}" : var.at_cos_target_bucket_name
1820

1921
cos_instance_crn = var.existing_cos_instance_crn != null ? var.existing_cos_instance_crn : length(module.cos_instance) != 0 ? module.cos_instance[0].cos_instance_crn : null
20-
existing_kms_guid = ((var.existing_cloud_logs_metrics_bucket_crn != null && var.existing_cloud_logs_data_bucket_crn != null && var.existing_at_cos_target_bucket_name != null) || (!var.manage_log_archive_cos_bucket && !var.enable_at_event_routing_to_cos_bucket && !var.cloud_logs_provision)) ? null : var.existing_kms_instance_crn != null ? element(split(":", var.existing_kms_instance_crn), length(split(":", var.existing_kms_instance_crn)) - 3) : tobool("The CRN of the existing KMS is not provided.")
2122
cos_instance_guid = var.existing_cos_instance_crn == null ? length(module.cos_instance) != 0 ? module.cos_instance[0].cos_instance_guid : null : element(split(":", var.existing_cos_instance_crn), length(split(":", var.existing_cos_instance_crn)) - 3)
22-
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
23+
24+
# fetch KMS GUID from existing_kms_instance_crn if KMS resources are required
25+
existing_kms_guid = ((var.existing_cos_kms_key_crn != null && var.skip_cos_kms_auth_policy) ? null :
26+
((length(coalesce(local.buckets_config, [])) == 0) ||
27+
(!var.manage_log_archive_cos_bucket && !var.enable_at_event_routing_to_cos_bucket && !var.cloud_logs_provision)) ? null :
28+
var.existing_kms_instance_crn != null ? module.kms_instance_crn_parser[0].service_instance : tobool("The CRN of the existing KMS instance is not provided."))
29+
30+
# get KMS service type : Key Protect (kms) or Hyper Protect Crypto Services(hs-crypto)
31+
kms_service = var.existing_kms_instance_crn != null ? (
32+
can(regex(".*kms.*", var.existing_kms_instance_crn)) ? "kms" : (
33+
can(regex(".*hs-crypto.*", var.existing_kms_instance_crn)) ? "hs-crypto" : null
34+
)
35+
) : null
36+
37+
# fetch KMS region from existing_kms_instance_crn if KMS resources are required and existing_cos_kms_key_crn is not provided
38+
kms_region = ((length(coalesce(local.buckets_config, [])) != 0) ?
39+
(var.existing_cos_kms_key_crn == null ? module.kms_instance_crn_parser[0].region : null) : null)
40+
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
2342

2443
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
2544
cos_resource_group_id = var.cos_resource_group_name != null ? module.cos_resource_group[0].resource_group_id : module.resource_group.resource_group_id
@@ -71,13 +90,6 @@ locals {
7190
days = 366
7291
} : null
7392

74-
kms_service = var.existing_kms_instance_crn != null ? (
75-
can(regex(".*kms.*", var.existing_kms_instance_crn)) ? "kms" : (
76-
can(regex(".*hs-crypto.*", var.existing_kms_instance_crn)) ? "hs-crypto" : null
77-
)
78-
) : null
79-
80-
kms_region = (length(coalesce(local.buckets_config, [])) != 0) ? (var.existing_cos_kms_key_crn == null ? element(split(":", var.existing_kms_instance_crn), length(split(":", var.existing_kms_instance_crn)) - 5) : null) : null
8193
at_cos_route = var.enable_at_event_routing_to_cos_bucket ? [{
8294
route_name = local.at_cos_route_name
8395
locations = ["*", "global"]
@@ -289,6 +301,14 @@ resource "ibm_iam_authorization_policy" "atracker_cos" {
289301
# KMS Key
290302
#######################################################################################################################
291303

304+
# If existing KMS intance CRN passed, parse details from it
305+
module "kms_instance_crn_parser" {
306+
count = var.existing_kms_instance_crn != null ? 1 : 0
307+
source = "terraform-ibm-modules/common-utilities/ibm//modules/crn-parser"
308+
version = "1.1.0"
309+
crn = var.existing_kms_instance_crn
310+
}
311+
292312
module "kms" {
293313
providers = {
294314
ibm = ibm.kms

solutions/instances/variables.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ variable "existing_at_cos_target_bucket_endpoint" {
384384

385385
variable "skip_cos_kms_auth_policy" {
386386
type = bool
387-
description = "To skip creating an IAM authorization policy that allows the created Cloud Object Storage instance to read the encryption key from the key management service (KMS) instance, set this variable to `true`. Before you can create an encrypted Cloud Object Storage bucket, an authorization policy must exist."
387+
description = "To skip creating an IAM authorization policy that allows the Cloud Object Storage instance to read the encryption key from the key management service (KMS) instance, set this variable to `true`. Before you can create an encrypted Cloud Object Storage bucket, an authorization policy must exist."
388388
default = false
389389
}
390390

@@ -417,7 +417,7 @@ variable "management_endpoint_type_for_bucket" {
417417
variable "existing_kms_instance_crn" {
418418
type = string
419419
default = null
420-
description = "The CRN of the key management service (KMS) that is used for the Cloud Object Storage bucket root key. If you are not using an existing KMS root key, you must specify this CRN. If the existing Cloud Object Storage bucket details are passed as an input, this value is not required."
420+
description = "The CRN of the key management service (KMS) that is used to create keys for encrypting the Cloud Object Storage bucket. If you are not using an existing KMS root key, you must specify this CRN. If you are using an existing KMS root key, an existing COS instance and auth policy is not set for COS to KMS, you must specify this CRN. If the existing Cloud Object Storage bucket details are passed as an input, this value is not required."
421421
}
422422

423423
variable "existing_cos_kms_key_crn" {

tests/pr_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ func TestRunExistingResourcesInstances(t *testing.T) {
300300
"resource_group_name": terraform.Output(t, existingTerraformOptions, "resource_group_name"),
301301
"use_existing_resource_group": true,
302302
"existing_kms_instance_crn": permanentResources["hpcs_south_crn"],
303+
"existing_cos_kms_key_crn": permanentResources["hpcs_south_root_key_crn"],
303304
"kms_endpoint_type": "public",
304305
"existing_cos_instance_crn": terraform.Output(t, existingTerraformOptions, "cos_crn"),
305306
"management_endpoint_type_for_bucket": "public",

0 commit comments

Comments
 (0)