Skip to content

Commit 7577fea

Browse files
Shikha MaheshwariShikha Maheshwari
authored andcommitted
cross-account kms key support
1 parent b281752 commit 7577fea

File tree

7 files changed

+56
-15
lines changed

7 files changed

+56
-15
lines changed

ibm_catalog.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,9 @@
351351
{
352352
"key": "kms_key_name"
353353
},
354+
{
355+
"key": "ibmcloud_kms_api_key"
356+
},
354357
{
355358
"key": "management_endpoint_type_for_bucket",
356359
"options": [

solutions/fully-configurable/DA-types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,6 @@ This variable defines a map of existing reserved IP names and values to attach w
310310

311311
```hcl
312312
{
313-
name = "10.10.10.4"
313+
name = "vpe-reserved-ip"
314314
}
315315
```

solutions/fully-configurable/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ This solution supports provisioning and configuring the following infrastructure
3737
|------|---------|
3838
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.9.0 |
3939
| <a name="requirement_ibm"></a> [ibm](#requirement\_ibm) | 1.76.1 |
40+
| <a name="requirement_time"></a> [time](#requirement\_time) | 0.13.0 |
4041

4142
### Modules
4243

@@ -55,7 +56,8 @@ This solution supports provisioning and configuring the following infrastructure
5556

5657
| Name | Type |
5758
|------|------|
58-
| [ibm_iam_authorization_policy.cos_kms_iam_auth_policy](https://registry.terraform.io/providers/IBM-Cloud/ibm/1.76.1/docs/resources/iam_authorization_policy) | resource |
59+
| [ibm_iam_authorization_policy.cos_kms_policy](https://registry.terraform.io/providers/IBM-Cloud/ibm/1.76.1/docs/resources/iam_authorization_policy) | resource |
60+
| [time_sleep.wait_for_cross_account_authorization_policy](https://registry.terraform.io/providers/hashicorp/time/0.13.0/docs/resources/sleep) | resource |
5961

6062
### Inputs
6163

@@ -84,6 +86,7 @@ This solution supports provisioning and configuring the following infrastructure
8486
| <a name="input_flow_logs_cos_bucket_minimum_retention_days"></a> [flow\_logs\_cos\_bucket\_minimum\_retention\_days](#input\_flow\_logs\_cos\_bucket\_minimum\_retention\_days) | The minimum number of days that an object must be kept unmodified in the flow logs cloud object storage. | `number` | `90` | no |
8587
| <a name="input_flow_logs_cos_bucket_name"></a> [flow\_logs\_cos\_bucket\_name](#input\_flow\_logs\_cos\_bucket\_name) | Name of the Cloud Object Storage bucket to be created to collect VPC flow logs. | `string` | `"flow-logs-bucket"` | no |
8688
| <a name="input_ibmcloud_api_key"></a> [ibmcloud\_api\_key](#input\_ibmcloud\_api\_key) | The IBM Cloud API key to deploy resources. | `string` | n/a | yes |
89+
| <a name="input_ibmcloud_kms_api_key"></a> [ibmcloud\_kms\_api\_key](#input\_ibmcloud\_kms\_api\_key) | 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 Cloud Object Storage instance. Leave this input empty if the same account owns both instances. | `string` | `null` | no |
8790
| <a name="input_kms_encryption_enabled_bucket"></a> [kms\_encryption\_enabled\_bucket](#input\_kms\_encryption\_enabled\_bucket) | Set to true to encrypt the Cloud Object Storage Flow Logs bucket with a KMS key. If set to true, a value must be passed for existing\_flow\_logs\_bucket\_kms\_key\_crn (to use that key) or existing\_kms\_instance\_crn (to create a new key). Value cannot be set to true if enable\_vpc\_flow\_logs is set to false. | `bool` | `false` | no |
8891
| <a name="input_kms_endpoint_type"></a> [kms\_endpoint\_type](#input\_kms\_endpoint\_type) | The type of endpoint to use for communicating with the KMS. Possible values: `public`, `private`. Applies only if `existing_flow_logs_bucket_kms_key_crn` is not specified. | `string` | `"private"` | no |
8992
| <a name="input_kms_key_name"></a> [kms\_key\_name](#input\_kms\_key\_name) | The name of the key to encrypt the flow logs Cloud Object Storage bucket. If an existing key is used, this variable is not required. If the prefix input variable is passed, the name of the key is prefixed to the value in the `<prefix>-value` format. | `string` | `"flow-logs-cos-key"` | no |

solutions/fully-configurable/main.tf

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ module "existing_cos_crn_parser" {
2525
}
2626

2727
locals {
28-
cos_instance_guid = var.existing_cos_instance_crn != null ? module.existing_cos_crn_parser[0].service_instance : null
29-
bucket_name = "${local.prefix}${var.flow_logs_cos_bucket_name}"
30-
kms_guid = var.kms_encryption_enabled_bucket ? (length(module.existing_kms_key_crn_parser) > 0 ? module.existing_kms_key_crn_parser[0].service_instance : module.existing_kms_instance_crn_parser[0].service_instance) : null
31-
kms_account_id = var.kms_encryption_enabled_bucket ? (length(module.existing_kms_key_crn_parser) > 0 ? module.existing_kms_key_crn_parser[0].account_id : module.existing_kms_instance_crn_parser[0].account_id) : null
32-
kms_service = var.kms_encryption_enabled_bucket ? (length(module.existing_kms_key_crn_parser) > 0 ? module.existing_kms_key_crn_parser[0].service_name : module.existing_kms_instance_crn_parser[0].service_name) : null
33-
cos_kms_key_crn = var.kms_encryption_enabled_bucket ? (length(module.existing_kms_key_crn_parser) > 0 ? var.existing_flow_logs_bucket_kms_key_crn : module.kms[0].keys[format("%s.%s", local.kms_key_ring_name, local.kms_key_name)].crn) : null
34-
create_cos_kms_iam_auth_policy = var.enable_vpc_flow_logs && var.kms_encryption_enabled_bucket && !var.skip_cos_kms_iam_auth_policy
28+
cos_instance_guid = var.existing_cos_instance_crn != null ? module.existing_cos_crn_parser[0].service_instance : null
29+
cos_account_id = var.existing_cos_instance_crn != null ? module.existing_cos_crn_parser[0].account_id : null
30+
bucket_name = "${local.prefix}${var.flow_logs_cos_bucket_name}"
31+
kms_guid = var.kms_encryption_enabled_bucket ? (length(module.existing_kms_key_crn_parser) > 0 ? module.existing_kms_key_crn_parser[0].service_instance : module.existing_kms_instance_crn_parser[0].service_instance) : null
32+
kms_account_id = var.kms_encryption_enabled_bucket ? (length(module.existing_kms_key_crn_parser) > 0 ? module.existing_kms_key_crn_parser[0].account_id : module.existing_kms_instance_crn_parser[0].account_id) : null
33+
kms_service_name = var.kms_encryption_enabled_bucket ? (length(module.existing_kms_key_crn_parser) > 0 ? module.existing_kms_key_crn_parser[0].service_name : module.existing_kms_instance_crn_parser[0].service_name) : null
34+
cos_kms_key_crn = var.kms_encryption_enabled_bucket ? (length(module.existing_kms_key_crn_parser) > 0 ? var.existing_flow_logs_bucket_kms_key_crn : module.kms[0].keys[format("%s.%s", local.kms_key_ring_name, local.kms_key_name)].crn) : null
35+
create_cos_kms_iam_auth_policy = var.enable_vpc_flow_logs && var.kms_encryption_enabled_bucket && !var.skip_cos_kms_iam_auth_policy
36+
create_cross_account_cos_kms_auth_policy = (local.create_cos_kms_iam_auth_policy && var.ibmcloud_kms_api_key == null) ? false : (local.cos_account_id != local.kms_account_id)
3537

3638
# configuration for the flow logs bucket
3739
bucket_config = [{
@@ -41,7 +43,7 @@ locals {
4143
kms_encryption_enabled = var.kms_encryption_enabled_bucket
4244
kms_guid = local.kms_guid
4345
kms_key_crn = local.cos_kms_key_crn
44-
skip_iam_authorization_policy = var.skip_cos_kms_iam_auth_policy
46+
skip_iam_authorization_policy = local.create_cross_account_cos_kms_auth_policy || var.skip_cos_kms_iam_auth_policy
4547
management_endpoint_type = var.management_endpoint_type_for_bucket
4648
storage_class = var.cos_bucket_class
4749
resource_instance_id = var.existing_cos_instance_crn
@@ -69,22 +71,25 @@ locals {
6971
# Create COS bucket using the defined bucket configuration
7072
module "cos_buckets" {
7173
count = var.enable_vpc_flow_logs ? 1 : 0
74+
depends_on = [time_sleep.wait_for_cross_account_authorization_policy[0]]
7275
source = "terraform-ibm-modules/cos/ibm//modules/buckets"
7376
version = "8.19.2"
7477
bucket_configs = local.bucket_config
7578
}
7679

77-
# Create IAM Authorization Policy to allow COS to access KMS for the encryption key
78-
resource "ibm_iam_authorization_policy" "cos_kms_iam_auth_policy" {
79-
count = local.create_cos_kms_iam_auth_policy ? 1 : 0
80+
# Create IAM Authorization Policy to allow COS to access KMS for the encryption key, if cross account KMS is passed in
81+
resource "ibm_iam_authorization_policy" "cos_kms_policy" {
82+
count = local.create_cross_account_cos_kms_auth_policy ? 1 : 0
83+
provider = ibm.kms
84+
source_service_account = local.cos_account_id
8085
source_service_name = "cloud-object-storage"
8186
source_resource_instance_id = local.cos_instance_guid
8287
roles = ["Reader"]
83-
description = "Allow the COS instance ${local.cos_instance_guid} to read the ${local.kms_service} key ${local.cos_kms_key_crn} from the instance ${local.kms_guid}"
88+
description = "Allow the COS instance ${local.cos_instance_guid} to read the ${local.kms_service_name} key ${local.cos_kms_key_crn} from the instance ${local.kms_guid}"
8489
resource_attributes {
8590
name = "serviceName"
8691
operator = "stringEquals"
87-
value = local.kms_service
92+
value = local.kms_service_name
8893
}
8994
resource_attributes {
9095
name = "accountId"
@@ -113,6 +118,14 @@ resource "ibm_iam_authorization_policy" "cos_kms_iam_auth_policy" {
113118
}
114119
}
115120

121+
# workaround for https://github.com/IBM-Cloud/terraform-provider-ibm/issues/4478
122+
resource "time_sleep" "wait_for_cross_account_authorization_policy" {
123+
depends_on = [ibm_iam_authorization_policy.cos_kms_policy]
124+
count = local.create_cross_account_cos_kms_auth_policy ? 1 : 0
125+
126+
create_duration = "30s"
127+
}
128+
116129
#######################################################################################################################
117130
# KMS Key
118131
#######################################################################################################################
@@ -143,7 +156,11 @@ locals {
143156
create_kms_key = (var.enable_vpc_flow_logs && var.kms_encryption_enabled_bucket) ? (var.existing_flow_logs_bucket_kms_key_crn == null ? (var.existing_kms_instance_crn != null ? true : false) : false) : false
144157
}
145158

159+
# KMS root key for flow logs COS bucket
146160
module "kms" {
161+
providers = {
162+
ibm = ibm.kms
163+
}
147164
count = local.create_kms_key ? 1 : 0 # no need to create any KMS resources if not passing an existing KMS CRN or existing KMS key CRN is provided
148165
source = "terraform-ibm-modules/kms-all-inclusive/ibm"
149166
version = "4.19.5"

solutions/fully-configurable/provider.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@ provider "ibm" {
77
region = var.region
88
visibility = var.provider_visibility
99
}
10+
11+
provider "ibm" {
12+
alias = "kms"
13+
ibmcloud_api_key = var.ibmcloud_kms_api_key != null ? var.ibmcloud_kms_api_key : var.ibmcloud_api_key
14+
region = local.kms_region
15+
visibility = var.provider_visibility
16+
}

solutions/fully-configurable/variables.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,13 @@ variable "kms_key_name" {
592592
description = "The name of the key to encrypt the flow logs Cloud Object Storage bucket. If an existing key is used, this variable is not required. If the prefix input variable is passed, the name of the key is prefixed to the value in the `<prefix>-value` format."
593593
}
594594

595+
variable "ibmcloud_kms_api_key" {
596+
type = string
597+
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 Cloud Object Storage instance. Leave this input empty if the same account owns both instances."
598+
sensitive = true
599+
default = null
600+
}
601+
595602
##############################################################################
596603
# Optional VPC Variables
597604
##############################################################################

solutions/fully-configurable/version.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@ terraform {
66
source = "IBM-Cloud/ibm"
77
version = "1.76.1"
88
}
9+
time = {
10+
source = "hashicorp/time"
11+
version = "0.13.0"
12+
}
913
}
1014
}

0 commit comments

Comments
 (0)