Skip to content

Commit dd35c3f

Browse files
authored
feat: added the ability to create a COS bucket in an instance that exists in a different account to the Clpud Logs instance (#65)
1 parent 6f59fa9 commit dd35c3f

File tree

6 files changed

+128
-8
lines changed

6 files changed

+128
-8
lines changed

ibm_catalog.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@
197197
}
198198
]
199199
},
200+
{
201+
"key": "ibmcloud_cos_api_key"
202+
},
200203
{
201204
"key": "cloud_logs_data_cos_bucket_name"
202205
},
@@ -233,6 +236,9 @@
233236
}
234237
]
235238
},
239+
{
240+
"key": "skip_cloud_logs_cos_auth_policy"
241+
},
236242
{
237243
"key": "skip_cos_kms_iam_auth_policy"
238244
},
@@ -329,6 +335,13 @@
329335
"crn:v1:bluemix:public:event-notifications::::serviceRole:Event-Source-Manager",
330336
"crn:v1:bluemix:public:iam::::role:Viewer"
331337
]
338+
},
339+
{
340+
"role_crns": [
341+
"crn:v1:bluemix:public:iam::::role:Administrator"
342+
],
343+
"service_name": "iam-identity",
344+
"notes": "[Optional] Required if creating COS buckets in a separate account from the account Cloud Logs is created in."
332345
}
333346
],
334347
"architecture": {
@@ -521,6 +534,9 @@
521534
}
522535
]
523536
},
537+
{
538+
"key": "ibmcloud_cos_api_key"
539+
},
524540
{
525541
"key": "cloud_logs_data_cos_bucket_name"
526542
},
@@ -552,6 +568,9 @@
552568
}
553569
]
554570
},
571+
{
572+
"key": "skip_cloud_logs_cos_auth_policy"
573+
},
555574
{
556575
"key": "skip_cos_kms_iam_auth_policy"
557576
},
@@ -626,6 +645,13 @@
626645
"crn:v1:bluemix:public:event-notifications::::serviceRole:Event-Source-Manager",
627646
"crn:v1:bluemix:public:iam::::role:Viewer"
628647
]
648+
},
649+
{
650+
"role_crns": [
651+
"crn:v1:bluemix:public:iam::::role:Administrator"
652+
],
653+
"service_name": "iam-identity",
654+
"notes": "[Optional] Required if creating COS buckets in a separate account from the account Cloud Logs is created in."
629655
}
630656
],
631657
"architecture": {

solutions/fully-configurable/main.tf

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@ locals {
1616
prefix = var.prefix != null ? trimspace(var.prefix) != "" ? "${var.prefix}-" : "" : ""
1717
create_cloud_logs = var.existing_cloud_logs_crn == null
1818
cloud_logs_crn = local.create_cloud_logs ? module.cloud_logs[0].crn : var.existing_cloud_logs_crn
19+
# Even though we're only performing a comparison (var.ibmcloud_cos_api_key != null),
20+
# Terraform treats the entire value as "tainted" due to sensitivity.
21+
# Later, in the cloud_logs module, where the data_storage input variable is used in a for_each loop,
22+
# the loop fails with the error: "Sensitive values, or values derived from sensitive values, cannot be used as for_each arguments."
23+
# However, since we use nonsensitive() solely for logical comparison, we are not exposing any secret values to logs and it's safe to use. Issue https://github.ibm.com/GoldenEye/issues/issues/13562.
24+
skip_cos_auth_policy = nonsensitive(var.ibmcloud_cos_api_key) != null ? true : var.skip_cloud_logs_cos_auth_policy
1925
}
2026

2127
module "cloud_logs" {
28+
depends_on = [time_sleep.wait_for_cos_authorization_policy[0]]
2229
count = local.create_cloud_logs ? 1 : 0
2330
source = "../.."
2431
resource_group_id = module.resource_group.resource_group_id
@@ -33,14 +40,16 @@ module "cloud_logs" {
3340
cbr_rules = var.cloud_logs_cbr_rules
3441
data_storage = {
3542
logs_data = {
36-
enabled = true
37-
bucket_crn = module.buckets.buckets[local.data_bucket_name].bucket_crn
38-
bucket_endpoint = module.buckets.buckets[local.data_bucket_name].s3_endpoint_direct
43+
enabled = true
44+
bucket_crn = module.buckets.buckets[local.data_bucket_name].bucket_crn
45+
bucket_endpoint = module.buckets.buckets[local.data_bucket_name].s3_endpoint_direct
46+
skip_cos_auth_policy = local.skip_cos_auth_policy
3947
},
4048
metrics_data = {
41-
enabled = true
42-
bucket_crn = module.buckets.buckets[local.metrics_bucket_name].bucket_crn
43-
bucket_endpoint = module.buckets.buckets[local.metrics_bucket_name].s3_endpoint_direct
49+
enabled = true
50+
bucket_crn = module.buckets.buckets[local.metrics_bucket_name].bucket_crn
51+
bucket_endpoint = module.buckets.buckets[local.metrics_bucket_name].s3_endpoint_direct
52+
skip_cos_auth_policy = local.skip_cos_auth_policy
4453
}
4554
}
4655
logs_routing_tenant_regions = var.logs_routing_tenant_regions
@@ -68,7 +77,8 @@ locals {
6877
kms_key_crn = var.kms_encryption_enabled_buckets ? var.existing_kms_key_crn != null ? var.existing_kms_key_crn : module.kms[0].keys[format("%s.%s", local.key_ring_name, local.key_name)].crn : null
6978
kms_key_id = var.existing_kms_instance_crn != null ? module.kms[0].keys[format("%s.%s", local.key_ring_name, local.key_name)].key_id : var.existing_kms_key_crn != null ? module.existing_kms_key_crn_parser[0].resource : null
7079

71-
create_cross_account_auth_policy = var.existing_cloud_logs_crn == null ? !var.skip_cos_kms_iam_auth_policy && var.ibmcloud_kms_api_key == null ? false : true : false
80+
create_cross_account_auth_policy = var.existing_cloud_logs_crn == null ? !var.skip_cos_kms_iam_auth_policy && var.ibmcloud_kms_api_key == null ? false : true : false
81+
create_cross_account_cos_auth_policy = var.existing_cloud_logs_crn == null && var.ibmcloud_cos_api_key != null && !var.skip_cloud_logs_cos_auth_policy
7282
}
7383

7484
module "existing_cos_instance_crn_parser" {
@@ -131,6 +141,62 @@ module "buckets" {
131141
]
132142
}
133143

144+
module "bucket_crns" {
145+
for_each = module.buckets.buckets
146+
source = "terraform-ibm-modules/common-utilities/ibm//modules/crn-parser"
147+
version = "1.1.0"
148+
crn = each.value.bucket_id
149+
}
150+
151+
data "ibm_iam_account_settings" "iam_account_settings" {
152+
count = local.create_cross_account_cos_auth_policy ? 1 : 0
153+
}
154+
155+
resource "ibm_iam_authorization_policy" "cos_policy" {
156+
provider = ibm.cos
157+
count = local.create_cross_account_cos_auth_policy ? length(module.buckets.bucket_configs) : 0
158+
source_service_account = data.ibm_iam_account_settings.iam_account_settings[0].account_id
159+
source_service_name = "logs"
160+
roles = ["Writer"]
161+
description = "Allow Cloud logs instances `Writer` access to the COS bucket with ID ${module.bucket_crns[module.buckets.bucket_configs[count.index].bucket_name].resource}, in the COS instance with ID ${module.existing_cos_instance_crn_parser.service_instance}."
162+
163+
resource_attributes {
164+
name = "serviceName"
165+
operator = "stringEquals"
166+
value = "cloud-object-storage"
167+
}
168+
169+
resource_attributes {
170+
name = "accountId"
171+
operator = "stringEquals"
172+
value = module.existing_cos_instance_crn_parser.account_id
173+
}
174+
175+
resource_attributes {
176+
name = "serviceInstance"
177+
operator = "stringEquals"
178+
value = module.existing_cos_instance_crn_parser.service_instance
179+
}
180+
181+
resource_attributes {
182+
name = "resourceType"
183+
operator = "stringEquals"
184+
value = "bucket"
185+
}
186+
187+
resource_attributes {
188+
name = "resource"
189+
operator = "stringEquals"
190+
value = module.bucket_crns[module.buckets.bucket_configs[count.index].bucket_name].resource
191+
}
192+
}
193+
194+
resource "time_sleep" "wait_for_cos_authorization_policy" {
195+
depends_on = [ibm_iam_authorization_policy.cos_policy]
196+
count = var.ibmcloud_cos_api_key != null && !var.skip_cloud_logs_cos_auth_policy ? length(module.buckets.bucket_configs) : 0
197+
create_duration = "30s"
198+
}
199+
134200
module "existing_kms_crn_parser" {
135201
count = var.existing_kms_instance_crn != null ? 1 : 0
136202
source = "terraform-ibm-modules/common-utilities/ibm//modules/crn-parser"

solutions/fully-configurable/providers.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ provider "ibm" {
1717

1818
provider "ibm" {
1919
alias = "cos"
20-
ibmcloud_api_key = var.ibmcloud_api_key
20+
ibmcloud_api_key = var.ibmcloud_cos_api_key != null ? var.ibmcloud_cos_api_key : var.ibmcloud_api_key
2121
region = var.region
2222
visibility = var.provider_visibility
2323
}

solutions/fully-configurable/variables.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ variable "region" {
5050
# COS
5151
########################################################################################################################
5252

53+
variable "ibmcloud_cos_api_key" {
54+
type = string
55+
description = "The IBM Cloud API key that can create Cloud Object Storage (COS) buckets. If not specified, the 'ibmcloud_api_key' variable is used. Specify this key if the COS instance is in an account that's different from the one associated with the cloud logs resources. Do not set if the same account owns all the instances."
56+
sensitive = true
57+
default = null
58+
}
59+
5360
variable "existing_cos_instance_crn" {
5461
type = string
5562
description = "The CRN of an existing Object Storage instance."
@@ -101,6 +108,12 @@ variable "skip_cos_kms_iam_auth_policy" {
101108
default = false
102109
}
103110

111+
variable "skip_cloud_logs_cos_auth_policy" {
112+
type = bool
113+
description = "To skip creating an IAM authorization policy that allows the IBM Cloud logs to write to the Cloud Object Storage bucket, set this variable to `true`."
114+
default = false
115+
}
116+
104117
variable "existing_monitoring_crn" {
105118
type = string
106119
nullable = true

solutions/security-enforced/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module "security_enforced" {
22
source = "../fully-configurable"
33
ibmcloud_api_key = var.ibmcloud_api_key
44
ibmcloud_kms_api_key = var.ibmcloud_kms_api_key
5+
ibmcloud_cos_api_key = var.ibmcloud_cos_api_key
56
provider_visibility = "private"
67
prefix = var.prefix
78
region = var.region
@@ -16,6 +17,7 @@ module "security_enforced" {
1617
cloud_logs_cos_buckets_class = var.cloud_logs_cos_buckets_class
1718
management_endpoint_type_for_buckets = "private"
1819
skip_cos_kms_iam_auth_policy = var.skip_cos_kms_iam_auth_policy
20+
skip_cloud_logs_cos_auth_policy = var.skip_cloud_logs_cos_auth_policy
1921
existing_monitoring_crn = var.existing_monitoring_crn
2022
kms_encryption_enabled_buckets = true
2123
existing_kms_instance_crn = var.existing_kms_instance_crn

solutions/security-enforced/variables.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ variable "region" {
3939
# COS
4040
########################################################################################################################
4141

42+
variable "ibmcloud_cos_api_key" {
43+
type = string
44+
description = "The IBM Cloud API key that can create Cloud Object Storage (COS) buckets. If not specified, the 'ibmcloud_api_key' variable is used. Specify this key if the COS instance is in an account that's different from the one associated with the cloud logs resources. Leave empty if the same account owns all the instances."
45+
sensitive = true
46+
default = null
47+
}
48+
4249
variable "existing_cos_instance_crn" {
4350
type = string
4451
description = "The CRN of an existing Object Storage instance."
@@ -75,6 +82,12 @@ variable "skip_cos_kms_iam_auth_policy" {
7582
default = false
7683
}
7784

85+
variable "skip_cloud_logs_cos_auth_policy" {
86+
type = bool
87+
description = "To skip creating an IAM authorization policy that allows the IBM Cloud logs to write to the Cloud Object Storage bucket, set this variable to `true`."
88+
default = false
89+
}
90+
7891
variable "existing_monitoring_crn" {
7992
type = string
8093
nullable = true

0 commit comments

Comments
 (0)