Skip to content

Commit 18786b5

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 (#236)
1 parent fc0700f commit 18786b5

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

solutions/standard/main.tf

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ locals {
1010
elasticsearch_key_name = var.prefix != null ? "${var.prefix}-${var.elasticsearch_key_name}" : var.elasticsearch_key_name
1111
elasticsearch_key_ring_name = var.prefix != null ? "${var.prefix}-${var.elasticsearch_key_ring_name}" : var.elasticsearch_key_ring_name
1212

13-
kms_key_crn = var.existing_kms_key_crn != null ? var.existing_kms_key_crn : module.kms[0].keys[format("%s.%s", local.elasticsearch_key_ring_name, local.elasticsearch_key_name)].crn
13+
kms_key_crn = var.existing_kms_key_crn != null ? var.existing_kms_key_crn : module.kms[0].keys[format("%s.%s", local.elasticsearch_key_ring_name, local.elasticsearch_key_name)].crn
14+
create_cross_account_auth_policy = !var.skip_iam_authorization_policy && var.ibmcloud_kms_api_key != null
15+
kms_service_name = local.kms_key_crn != null ? (
16+
can(regex(".*kms.*", local.kms_key_crn)) ? "kms" : can(regex(".*hs-crypto.*", local.kms_key_crn)) ? "hs-crypto" : null
17+
) : null
1418
}
1519

1620
#######################################################################################################################
@@ -28,6 +32,29 @@ module "resource_group" {
2832
# KMS root key for Elasticsearch
2933
#######################################################################################################################
3034

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 = "databases-for-elasticsearch"
44+
source_resource_group_id = module.resource_group.resource_group_id
45+
target_service_name = local.kms_service_name
46+
target_resource_instance_id = local.existing_kms_instance_guid
47+
roles = ["Reader"]
48+
description = "Allow all Elastic Search instances in the resource group ${module.resource_group.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_instance_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"
55+
}
56+
57+
3158
module "kms" {
3259
providers = {
3360
ibm = ibm.kms
@@ -64,11 +91,12 @@ module "kms" {
6491

6592
module "elasticsearch" {
6693
source = "../../modules/fscloud"
94+
depends_on = [time_sleep.wait_for_authorization_policy]
6795
resource_group_id = module.resource_group.resource_group_id
6896
name = var.prefix != null ? "${var.prefix}-${var.name}" : var.name
6997
region = var.region
7098
plan = var.plan
71-
skip_iam_authorization_policy = var.skip_iam_authorization_policy
99+
skip_iam_authorization_policy = var.skip_iam_authorization_policy || local.create_cross_account_auth_policy
72100
elasticsearch_version = var.elasticsearch_version
73101
existing_kms_instance_guid = local.existing_kms_instance_guid
74102
kms_key_crn = local.kms_key_crn

solutions/standard/provider.tf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
provider "ibm" {
2-
alias = "kms"
32
ibmcloud_api_key = var.ibmcloud_api_key
43
region = var.region
5-
ibmcloud_timeout = 60
4+
}
5+
6+
provider "ibm" {
7+
alias = "kms"
8+
ibmcloud_api_key = var.ibmcloud_kms_api_key != null ? var.ibmcloud_kms_api_key : var.ibmcloud_api_key
9+
region = local.existing_kms_instance_region
610
}

solutions/standard/variables.tf

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ variable "tags" {
117117
default = []
118118
}
119119

120+
variable "ibmcloud_kms_api_key" {
121+
type = string
122+
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 Elastic Search instance. Leave this input empty if the same account owns both instances."
123+
sensitive = true
124+
default = null
125+
}
126+
120127
variable "kms_endpoint_type" {
121128
type = string
122129
description = "The type of endpoint to use to communicate with the KMS instance. Possible values: `public`, `private`."
@@ -134,14 +141,14 @@ variable "existing_kms_key_crn" {
134141
}
135142

136143
variable "existing_kms_instance_crn" {
137-
description = "The CRN of a Hyper Protect Crypto Services or Key Protect instance in the same account as the Databases for Elasticsearch instance. This value is used to create an authorization policy if `skip_iam_authorization_policy` is false. If not specified, a root key is created."
144+
description = "The CRN of the KMS instance (Hyper Protect Crypto Services or Key Protect). Required only if `existing_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`."
138145
type = string
139146
default = null
140147
}
141148

142149
variable "skip_iam_authorization_policy" {
143150
type = bool
144-
description = "Whether to create an IAM authorization policy that permits all Databases for Elasticsearch instances in the resource group to read the encryption key from the Hyper Protect Crypto Services instance specified in the `existing_kms_instance_crn` variable."
151+
description = "Set to true to skip the creation of an IAM authorization policy that permits all Elastic Search 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."
145152
default = false
146153
}
147154

solutions/standard/version.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@ terraform {
77
source = "IBM-Cloud/ibm"
88
version = "1.67.1"
99
}
10+
11+
time = {
12+
source = "hashicorp/time"
13+
version = "0.11.2"
14+
}
1015
}
1116
}

0 commit comments

Comments
 (0)