Skip to content

Commit 20c16a9

Browse files
authored
feat: added support to the DA for use case where KMS is in a different account by adding new optional variable ibmcloud_kms_api_key (#147)
1 parent ac214a2 commit 20c16a9

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

solutions/standard/main.tf

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,33 @@ locals {
2525
parsed_existing_kms_instance_crn = var.existing_kms_instance_crn != null ? split(":", var.existing_kms_instance_crn) : []
2626
kms_region = length(local.parsed_existing_kms_instance_crn) > 0 ? local.parsed_existing_kms_instance_crn[5] : null
2727
existing_kms_guid = length(local.parsed_existing_kms_instance_crn) > 0 ? local.parsed_existing_kms_instance_crn[7] : null
28+
create_cross_account_auth_policy = !var.skip_kms_iam_authorization_policy && var.ibmcloud_kms_api_key != null
29+
30+
kms_service_name = local.kms_key_crn != null ? (
31+
can(regex(".*kms.*", local.kms_key_crn)) ? "kms" : can(regex(".*hs-crypto.*", local.kms_key_crn)) ? "hs-crypto" : null
32+
) : null
33+
}
34+
35+
data "ibm_iam_account_settings" "iam_account_settings" {
36+
count = local.create_cross_account_auth_policy ? 1 : 0
37+
}
38+
39+
resource "ibm_iam_authorization_policy" "kms_policy" {
40+
count = local.create_cross_account_auth_policy ? 1 : 0
41+
provider = ibm.kms
42+
source_service_account = data.ibm_iam_account_settings.iam_account_settings[0].account_id
43+
source_service_name = "secrets-manager"
44+
source_resource_group_id = module.resource_group[0].resource_group_id
45+
target_service_name = local.kms_service_name
46+
target_resource_instance_id = local.existing_kms_guid
47+
roles = ["Reader"]
48+
description = "Allow all Secrets Manager instances in the resource group ${module.resource_group[0].resource_group_id} in the account ${data.ibm_iam_account_settings.iam_account_settings[0].account_id} to read from the ${local.kms_service_name} instance GUID ${local.existing_kms_guid}"
49+
}
50+
51+
# workaround for https://github.com/IBM-Cloud/terraform-provider-ibm/issues/4478
52+
resource "time_sleep" "wait_for_authorization_policy" {
53+
depends_on = [ibm_iam_authorization_policy.kms_policy]
54+
create_duration = "30s"
2855
}
2956

3057
# KMS root key for Secrets Manager secret encryption
@@ -72,6 +99,7 @@ locals {
7299

73100
module "secrets_manager" {
74101
count = var.existing_secrets_manager_crn != null ? 0 : 1
102+
depends_on = [time_sleep.wait_for_authorization_policy]
75103
source = "../.."
76104
resource_group_id = module.resource_group[0].resource_group_id
77105
region = var.region
@@ -83,7 +111,7 @@ module "secrets_manager" {
83111
kms_encryption_enabled = true
84112
existing_kms_instance_guid = local.existing_kms_guid
85113
kms_key_crn = local.kms_key_crn
86-
skip_kms_iam_authorization_policy = var.skip_kms_iam_authorization_policy
114+
skip_kms_iam_authorization_policy = var.skip_kms_iam_authorization_policy || local.create_cross_account_auth_policy
87115
# event notifications dependency
88116
enable_event_notification = var.existing_event_notification_instance_crn != null ? true : false
89117
existing_en_instance_crn = var.existing_event_notification_instance_crn

solutions/standard/provider.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ provider "ibm" {
22
ibmcloud_api_key = var.ibmcloud_api_key
33
region = var.region
44
}
5-
65
provider "ibm" {
76
alias = "kms"
8-
ibmcloud_api_key = var.ibmcloud_api_key
7+
ibmcloud_api_key = var.ibmcloud_kms_api_key != null ? var.ibmcloud_kms_api_key : var.ibmcloud_api_key
98
region = local.kms_region
109
}

solutions/standard/variables.tf

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ variable "iam_engine_name" {
183183

184184
variable "skip_kms_iam_authorization_policy" {
185185
type = bool
186-
description = "Set to true to skip the creation of an IAM authorization policy that permits all Secrets Manager instances in the resource group to read the encryption key. If set to false, pass in a value for the Key Protect or Hyper Protect Crypto Service instance in the existing_kms_instance_crn variable."
186+
description = "Set to true to skip the creation of an IAM authorization policy that permits all Secrets Manager instances in the resource group to read the encryption key from the KMS instance. If set to false, pass in a value for the KMS instance in the `existing_kms_instance_crn` variable. If a value is specified for `ibmcloud_kms_api_key`, the policy is created in the KMS account."
187187
default = false
188188
}
189189

@@ -200,7 +200,7 @@ variable "existing_secrets_manager_kms_key_crn" {
200200
variable "existing_kms_instance_crn" {
201201
type = string
202202
default = null
203-
description = "The CRN of the Hyper Protect Crypto Services or Key Protect instance. Applies only if `existing_secrets_manager_kms_key_crn` is not specified."
203+
description = "The CRN of the KMS instance (Hyper Protect Crypto Services or Key Protect). Required only if `existing_secrets_manager_crn` or `existing_secrets_manager_kms_key_crn` is not specified. If the KMS instance is in different account you must also provide a value for `ibmcloud_kms_api_key`."
204204
}
205205

206206
variable "kms_endpoint_type" {
@@ -225,6 +225,13 @@ variable "kms_key_name" {
225225
description = "The name for the new root key. Applies only if `existing_secrets_manager_kms_key_crn` is not specified. If a prefix input variable is passed, it is added to the value in the `<prefix>-value` format."
226226
}
227227

228+
variable "ibmcloud_kms_api_key" {
229+
type = string
230+
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 Secrets Manager instance. Leave this input empty if the same account owns both instances."
231+
sensitive = true
232+
default = null
233+
}
234+
228235
########################################################################################################################
229236
# Event Notifications
230237
########################################################################################################################

solutions/standard/version.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ terraform {
88
}
99
time = {
1010
source = "hashicorp/time"
11-
version = ">= 0.9.1, < 1.0.0"
11+
version = "0.9.1"
1212
}
1313
}
1414
}

0 commit comments

Comments
 (0)