-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.tf
More file actions
391 lines (358 loc) · 15.7 KB
/
Copy pathmain.tf
File metadata and controls
391 lines (358 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
##############################################################################
# App Config module
##############################################################################
resource "ibm_resource_instance" "app_config" {
resource_group_id = var.resource_group_id
location = var.region
name = var.app_config_name
service = "apprapp"
plan = var.app_config_plan
service_endpoints = var.app_config_service_endpoints
tags = var.resource_tags
}
##############################################################################
# Attach Access Tags
##############################################################################
data "ibm_iam_access_tag" "access_tag" {
for_each = length(var.access_tags) != 0 ? toset(var.access_tags) : []
name = each.value
}
resource "ibm_resource_tag" "app_config_tag" {
depends_on = [data.ibm_iam_access_tag.access_tag] # Force dependency on data source validation to ensure access_tags exist and are valid before use.
count = length(var.access_tags) == 0 ? 0 : 1
resource_id = ibm_resource_instance.app_config.crn
tags = var.access_tags
tag_type = "access"
}
##############################################################################
# Collections
##############################################################################
locals {
collections_map = {
for obj in var.app_config_collections :
(obj.name) => obj
}
}
resource "ibm_app_config_collection" "collections" {
for_each = local.collections_map
guid = ibm_resource_instance.app_config.guid
name = each.value.name
collection_id = each.value.collection_id
description = each.value.description
tags = each.value.tags
}
##############################################################################
# Configuration aggregator
##############################################################################
# workaround for error "ReplaceSettingsWithContext failed: given app-config instance is not found"
# https://github.ibm.com/devx-app-services/planning/issues/25953
resource "time_sleep" "wait" {
count = var.enable_config_aggregator ? 1 : 0
depends_on = [ibm_resource_instance.app_config]
create_duration = "30s"
}
# Create the required Trusted Profile
module "config_aggregator_trusted_profile" {
count = var.enable_config_aggregator ? 1 : 0
source = "terraform-ibm-modules/trusted-profile/ibm"
version = "4.0.0"
trusted_profile_name = var.config_aggregator_trusted_profile_name
trusted_profile_description = "Trusted Profile for App Configuration instance ${ibm_resource_instance.app_config.guid} with required access for configuration aggregator"
trusted_profile_identity = {
identifier = ibm_resource_instance.app_config.crn
identity_type = "crn"
}
# unique_identifier should not be updated as it will create a breaking change for trusted profile. For more information please check https://github.com/terraform-ibm-modules/terraform-ibm-trusted-profile/releases/tag/v3.0.0 .
trusted_profile_policies = [
{
unique_identifier = "config-aggregator-trusted-profile-0"
roles = ["Viewer", "Service Configuration Reader"]
account_management = true
description = "All Account Management Services"
},
{
unique_identifier = "config-aggregator-trusted-profile-1"
roles = ["Viewer", "Service Configuration Reader", "Reader"]
resource_attributes = [{
name = "serviceType"
value = "service"
operator = "stringEquals"
}]
description = "All Identity and Access enabled services"
}
]
trusted_profile_links = [{
unique_identifier = "config-aggregator-trusted-profile-0"
cr_type = "VSI"
links = [{
crn = ibm_resource_instance.app_config.crn
}]
}]
}
# If enterprise account, create custom role "Template Assignment Reader"
# This role is used in the trusted profile to grant permission to read IAM template assignments.
# It is required by the App Config enterprise-level trusted profile to manage IAM templates."
locals {
custom_role = "Template Assignment Reader"
}
resource "ibm_iam_custom_role" "template_assignment_reader" {
count = var.enable_config_aggregator && var.config_aggregator_enterprise_id != null ? 1 : 0
name = "TemplateAssignmentReader"
service = "iam-identity"
display_name = local.custom_role
description = "Custom role to allow reading IAM template assignments"
actions = ["iam-identity.profile-assignment.read"]
}
# If enterprise account, create trusted profile for App Config enterprise-level permissions
module "config_aggregator_trusted_profile_enterprise" {
count = var.enable_config_aggregator && var.config_aggregator_enterprise_id != null ? 1 : 0
source = "terraform-ibm-modules/trusted-profile/ibm"
version = "4.0.0"
trusted_profile_name = var.config_aggregator_enterprise_trusted_profile_name
trusted_profile_description = "Trusted Profile for App Configuration instance ${ibm_resource_instance.app_config.guid} with required access for configuration aggregator for enterprise accounts"
trusted_profile_identity = {
identifier = ibm_resource_instance.app_config.crn
identity_type = "crn"
}
trusted_profile_policies = [
{
unique_identifier = "config-aggregator-trusted-profile-0"
roles = ["Viewer", local.custom_role]
resource_attributes = [{
name = "service_group_id"
value = "IAM"
operator = "stringEquals"
}]
description = "IAM access with custom role"
},
{
unique_identifier = "config-aggregator-trusted-profile-1"
roles = ["Viewer"]
resources = [{
service = "enterprise"
}]
description = "Enterprise viewer and template reader access"
}
]
trusted_profile_links = [{
unique_identifier = "config-aggregator-trusted-profile-0"
cr_type = "VSI"
links = [{
crn = ibm_resource_instance.app_config.crn
}]
}]
}
# If enterprise account, create trusted profile template
module "config_aggregator_trusted_profile_template" {
count = var.enable_config_aggregator && var.config_aggregator_enterprise_id != null ? 1 : 0
source = "terraform-ibm-modules/trusted-profile/ibm//modules/trusted-profile-template"
version = "4.0.0"
template_name = var.config_aggregator_enterprise_trusted_profile_template_name
template_description = "Trusted Profile template for App Configuration instance ${ibm_resource_instance.app_config.guid} with required access for configuration aggregator"
profile_name = var.config_aggregator_trusted_profile_name
profile_description = "Trusted Profile for App Configuration instance ${ibm_resource_instance.app_config.guid} with required access for configuration aggregator"
identities = [
{
type = "crn"
iam_id = "crn-${ibm_resource_instance.app_config.crn}"
identifier = ibm_resource_instance.app_config.crn
}
]
account_group_ids_to_assign = var.config_aggregator_enterprise_account_group_ids_to_assign
account_ids_to_assign = var.config_aggregator_enterprise_account_ids_to_assign
policy_templates = [
{
name = "identity-access"
description = "Policy template for identity services"
roles = ["Viewer", "Reader"]
attributes = [{
key = "serviceType"
value = "service" # assigns access to All Identity and Access enabled services
operator = "stringEquals"
}]
},
{
name = "platform-access"
description = "Policy template for platform services"
roles = ["Viewer", "Service Configuration Reader"]
attributes = [{
key = "serviceType"
value = "platform_service" # assigns access to All Account Management services
operator = "stringEquals"
}]
}
]
}
# Define an aggregation
resource "ibm_config_aggregator_settings" "config_aggregator_settings" {
depends_on = [time_sleep.wait]
count = var.enable_config_aggregator ? 1 : 0
instance_id = ibm_resource_instance.app_config.guid
region = ibm_resource_instance.app_config.location
resource_collection_regions = var.config_aggregator_resource_collection_regions
resource_collection_enabled = true
trusted_profile_id = module.config_aggregator_trusted_profile[0].profile_id
dynamic "additional_scope" {
for_each = var.enable_config_aggregator && var.config_aggregator_enterprise_id != null ? [1] : []
content {
type = "Enterprise"
enterprise_id = var.config_aggregator_enterprise_id
profile_template {
id = module.config_aggregator_trusted_profile_template[0].trusted_profile_template_id
trusted_profile_id = module.config_aggregator_trusted_profile_enterprise[0].profile_id
}
}
}
}
##############################################################################
# Context Based Restrictions
##############################################################################
module "cbr_rule" {
count = length(var.cbr_rules) > 0 ? length(var.cbr_rules) : 0
source = "terraform-ibm-modules/cbr/ibm//modules/cbr-rule-module"
version = "1.36.3"
rule_description = var.cbr_rules[count.index].description
enforcement_mode = var.cbr_rules[count.index].enforcement_mode
rule_contexts = var.cbr_rules[count.index].rule_contexts
resources = [{
attributes = [
{
name = "accountId"
value = var.cbr_rules[count.index].account_id
operator = "stringEquals"
},
{
name = "serviceInstance"
value = ibm_resource_instance.app_config.guid
operator = "stringEquals"
},
{
name = "serviceName"
value = "apprapp"
operator = "stringEquals"
}
],
tags = var.cbr_rules[count.index].tags
}]
}
##############################################################################
# Key Management services' integration
##############################################################################
resource "random_string" "kms_integration_id" {
count = var.kms_encryption_enabled ? 1 : 0
length = 10
special = true
override_special = "-"
upper = false
}
module "kms_crn_parser" {
count = var.kms_encryption_enabled ? 1 : 0
source = "terraform-ibm-modules/common-utilities/ibm//modules/crn-parser"
version = "1.6.0"
crn = var.existing_kms_instance_crn
}
# KMS values
locals {
validate_kms_plan = var.app_config_plan == "enterprise" && var.existing_kms_instance_crn != null
kms_service = local.validate_kms_plan ? try(module.kms_crn_parser[0].service_name, null) : null
kms_account_id = local.validate_kms_plan ? try(module.kms_crn_parser[0].account_id, null) : null
target_resource_instance_id = local.validate_kms_plan ? try(module.kms_crn_parser[0].service_instance, null) : null
}
resource "ibm_iam_authorization_policy" "kms_policy" {
count = var.kms_encryption_enabled && !var.skip_app_config_kms_auth_policy ? 1 : 0
source_service_name = "apprapp"
source_resource_instance_id = ibm_resource_instance.app_config.guid
roles = ["Reader"]
description = "Allow App Configuration instance to read the ${local.kms_service} key ${var.root_key_id} from the instance GUID ${local.target_resource_instance_id}"
resource_attributes {
name = "serviceName"
operator = "stringEquals"
value = local.kms_service
}
resource_attributes {
name = "accountId"
operator = "stringEquals"
value = local.kms_account_id
}
resource_attributes {
name = "serviceInstance"
operator = "stringEquals"
value = local.target_resource_instance_id
}
resource_attributes {
name = "resourceType"
operator = "stringEquals"
value = "key"
}
resource_attributes {
name = "resource"
operator = "stringEquals"
value = var.root_key_id
}
# Scope of policy now includes the key, so ensure to create new policy before
# destroying old one to prevent any disruption to every day services.
lifecycle {
create_before_destroy = true
}
}
# workaround for https://github.com/IBM-Cloud/terraform-provider-ibm/issues/4478
resource "time_sleep" "wait_for_kms_authorization_policy" {
count = var.kms_encryption_enabled && !var.skip_app_config_kms_auth_policy ? 1 : 0
depends_on = [ibm_iam_authorization_policy.kms_policy]
create_duration = "30s"
}
resource "ibm_app_config_integration_kms" "app_config_integration_kms" {
depends_on = [time_sleep.wait_for_kms_authorization_policy]
count = var.kms_encryption_enabled ? 1 : 0
guid = ibm_resource_instance.app_config.guid
integration_id = "kms-${random_string.kms_integration_id[0].result}"
kms_instance_crn = var.existing_kms_instance_crn
kms_endpoint = var.kms_endpoint_url
root_key_id = var.root_key_id
}
##############################################################################
# Event Notification services' integration
##############################################################################
resource "random_string" "en_integration_id" {
count = var.enable_event_notifications ? 1 : 0
length = 10
special = true
override_special = "-"
upper = false
}
module "en_crn_parser" {
count = var.enable_event_notifications ? 1 : 0
source = "terraform-ibm-modules/common-utilities/ibm//modules/crn-parser"
version = "1.6.0"
crn = var.existing_event_notifications_instance_crn
}
resource "ibm_iam_authorization_policy" "en_policy" {
count = var.enable_event_notifications && !var.skip_app_config_event_notifications_auth_policy ? 1 : 0
source_service_name = "apprapp"
source_resource_instance_id = ibm_resource_instance.app_config.guid
roles = ["Event Source Manager"]
target_service_name = "event-notifications"
target_resource_instance_id = module.en_crn_parser[0].service_instance
description = "Allow App Configuration instance to create source and send notifications for configuration change with the instance GUID ${module.en_crn_parser[0].service_instance}"
# Scope of policy now includes the key, so ensure to create new policy before
# destroying old one to prevent any disruption to every day services.
lifecycle {
create_before_destroy = true
}
}
# workaround for https://github.com/IBM-Cloud/terraform-provider-ibm/issues/4478
resource "time_sleep" "wait_for_en_authorization_policy" {
count = var.enable_event_notifications && !var.skip_app_config_event_notifications_auth_policy ? 1 : 0
depends_on = [ibm_iam_authorization_policy.en_policy]
create_duration = "30s"
}
resource "ibm_app_config_integration_en" "app_config_integration_en" {
depends_on = [time_sleep.wait_for_en_authorization_policy]
count = var.enable_event_notifications ? 1 : 0
guid = ibm_resource_instance.app_config.guid
integration_id = "en-${random_string.en_integration_id[0].result}"
en_instance_crn = var.existing_event_notifications_instance_crn
en_endpoint = var.event_notifications_endpoint_url
en_source_name = var.app_config_event_notifications_source_name
description = var.event_notifications_integration_description
}