Skip to content

Commit 46f081c

Browse files
authored
feat: added support to create a KMS key in a different account to the Observability instances using new optional input variable ibmcloud_kms_api_key (#102)
1 parent 0064fa9 commit 46f081c

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

solutions/instances/main.tf

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ locals {
7777

7878
at_routes = concat(local.at_cos_route, local.at_log_analysis_route)
7979

80+
apply_auth_policy = (var.skip_cos_kms_auth_policy || (length(coalesce(local.bucket_config_map, [])) == 0)) ? 0 : 1
81+
8082
}
8183

8284
#######################################################################################################################
@@ -196,11 +198,19 @@ resource "time_sleep" "wait_for_authorization_policy" {
196198
create_duration = "30s"
197199
}
198200

201+
# Data source to account settings for retrieving cross account id
202+
data "ibm_iam_account_settings" "iam_account_settings" {
203+
count = local.apply_auth_policy
204+
}
205+
199206
# The auth policy is being created here instead of in COS module because of this limitation: https://github.com/terraform-ibm-modules/terraform-ibm-observability-da/issues/8
200207

201208
# Create IAM Authorization Policy to allow COS to access KMS for the encryption key
202209
resource "ibm_iam_authorization_policy" "policy" {
203-
count = (var.skip_cos_kms_auth_policy || (length(coalesce(local.bucket_config_map, [])) == 0)) ? 0 : 1
210+
count = (var.skip_cos_kms_auth_policy || (length(coalesce(local.bucket_config_map, [])) == 0)) ? 0 : 1
211+
# Conditionals with providers aren't possible, using ibm.kms as provider incase cross account is enabled
212+
provider = ibm.kms
213+
source_service_account = data.ibm_iam_account_settings.iam_account_settings[0].account_id
204214
source_service_name = "cloud-object-storage"
205215
source_resource_instance_id = local.cos_instance_guid
206216
target_service_name = local.kms_service

solutions/instances/provider.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ provider "ibm" {
3131

3232
provider "ibm" {
3333
alias = "kms"
34-
ibmcloud_api_key = var.ibmcloud_api_key
34+
ibmcloud_api_key = var.ibmcloud_kms_api_key != null ? var.ibmcloud_kms_api_key : var.ibmcloud_api_key
3535
region = local.kms_region
3636
}

solutions/instances/variables.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ variable "ibmcloud_api_key" {
88
sensitive = true
99
}
1010

11+
variable "ibmcloud_kms_api_key" {
12+
type = string
13+
description = "The IBM Cloud API key that can create a root key and key ring in the key management service (KMS) instance. If not specified, the 'ibmcloud_api_key' variable is used. Specify this key if the instance in `existing_kms_instance_crn` is in an account that's different from the Object Storage instance. Leave empty if the same account owns both instances."
14+
sensitive = true
15+
default = null
16+
}
17+
1118
variable "use_existing_resource_group" {
1219
type = bool
1320
description = "Whether to use an existing resource group."
@@ -321,7 +328,7 @@ variable "existing_cos_kms_key_crn" {
321328

322329
variable "kms_endpoint_type" {
323330
type = string
324-
description = "The type of endpoint to use to communicate with the key management service (KMS). Specify one of the following values for the endpoint type: `public` or `private` (default)."
331+
description = "The type of endpoint to use for communicating with the Key Protect or Hyper Protect Crypto Services instance. Possible values: `public`, `private`. Applies only if `existing_cos_kms_key_crn` is not specified."
325332
default = "private"
326333
validation {
327334
condition = can(regex("public|private", var.kms_endpoint_type))

0 commit comments

Comments
 (0)