Skip to content

Commit adad206

Browse files
authored
fix: generated access permissions (#553)
* chore: switch SM to service auth for IAM config engine * chore: add prefix support to service ID names * chore: add ibmcloud-api prop * chore: update access permissions on service api and access group
1 parent 4114dca commit adad206

File tree

12 files changed

+123
-29
lines changed

12 files changed

+123
-29
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ statement instead the previous block.
7474

7575
| Name | Type |
7676
|------|------|
77+
| [ibm_cd_tekton_pipeline_property.cc_pipeline_ibmcloud_api](https://registry.terraform.io/providers/IBM-Cloud/ibm/1.70.0/docs/resources/cd_tekton_pipeline_property) | resource |
78+
| [ibm_cd_tekton_pipeline_property.cd_pipeline_ibmcloud_api](https://registry.terraform.io/providers/IBM-Cloud/ibm/1.70.0/docs/resources/cd_tekton_pipeline_property) | resource |
79+
| [ibm_cd_tekton_pipeline_property.ci_pipeline_ibmcloud_api](https://registry.terraform.io/providers/IBM-Cloud/ibm/1.70.0/docs/resources/cd_tekton_pipeline_property) | resource |
80+
| [ibm_cd_tekton_pipeline_property.pr_pipeline_ibmcloud_api](https://registry.terraform.io/providers/IBM-Cloud/ibm/1.70.0/docs/resources/cd_tekton_pipeline_property) | resource |
7781
| [ibm_cd_tekton_pipeline_trigger.ci_pipeline_webhook](https://registry.terraform.io/providers/IBM-Cloud/ibm/1.70.0/docs/resources/cd_tekton_pipeline_trigger) | resource |
7882
| [ibm_cd_tekton_pipeline_trigger_property.ci_pipeline_webhook_branch_property](https://registry.terraform.io/providers/IBM-Cloud/ibm/1.70.0/docs/resources/cd_tekton_pipeline_trigger_property) | resource |
7983
| [ibm_cd_tekton_pipeline_trigger_property.ci_pipeline_webhook_name_property](https://registry.terraform.io/providers/IBM-Cloud/ibm/1.70.0/docs/resources/cd_tekton_pipeline_trigger_property) | resource |
@@ -523,6 +527,7 @@ statement instead the previous block.
523527
| <a name="input_evidence_repo_name"></a> [evidence\_repo\_name](#input\_evidence\_repo\_name) | Set to use a custom name for the Evidence repository. | `string` | `""` | no |
524528
| <a name="input_evidence_repo_secret_group"></a> [evidence\_repo\_secret\_group](#input\_evidence\_repo\_secret\_group) | Secret group for the Evidence repository secret. Defaults to the value set in `sm_secret_group` if not set. Only used with `Secrets Manager`. | `string` | `""` | no |
525529
| <a name="input_force_create_standard_api_key"></a> [force\_create\_standard\_api\_key](#input\_force\_create\_standard\_api\_key) | Set to `true` to force create a standard api key. By default the generated apikey will be a service api key. It is recommended to use a Git Token when using the service api key. In the case where the user has been invited to an account and that user not the account owner, during toolchain creation the default compliance repositories will be created in that user's account and the service api will not have access to those repositories. In this case a Git Token for the repositories is required. See `repo_git_token_secret_name` for more details. The alternative is to set `force_create_standard_api_key` to `true` to create a standard api key. | `bool` | `false` | no |
530+
| <a name="input_ibmcloud_api"></a> [ibmcloud\_api](#input\_ibmcloud\_api) | The environment URL. When left unset this will default to `https://cloud.ibm.com` | `string` | `""` | no |
526531
| <a name="input_ibmcloud_api_key"></a> [ibmcloud\_api\_key](#input\_ibmcloud\_api\_key) | The API key used to create the toolchains. (See deployment guide.) | `string` | n/a | yes |
527532
| <a name="input_inventory_group"></a> [inventory\_group](#input\_inventory\_group) | Specify the Git user or group for the inventory repository. | `string` | `""` | no |
528533
| <a name="input_inventory_repo_auth_type"></a> [inventory\_repo\_auth\_type](#input\_inventory\_repo\_auth\_type) | Select the method of authentication that is used to access the Git repository. Valid values are 'oauth' or 'pat'. Defaults to `oauth` when unset. `pat` is a git `personal access token`. | `string` | `""` | no |

ibm_catalog.json

+14
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,13 @@
15311531
"description": "Set to `true` to force create a standard api key. By default the generated apikey will be a service api key. It is recommended to use a Git Token when using the service api key. In the case where the user has been invited to an account and that user not the account owner, during toolchain creation the default compliance repositories will be created in that user's account and the service api will not have access to those repositories. In this case a Git Token for the repositories is required. See `repo_git_token_secret_name` for more details. The alternative is to set `force_create_standard_api_key` to `true` to create a standard api key.",
15321532
"required": false
15331533
},
1534+
{
1535+
"key": "ibmcloud_api",
1536+
"type": "string",
1537+
"default_value": "",
1538+
"description": "The environment URL. When left unset this will default to `https://cloud.ibm.com`",
1539+
"required": false
1540+
},
15341541
{
15351542
"key": "inventory_group",
15361543
"type": "string",
@@ -3672,6 +3679,13 @@
36723679
"description": "Set to `true` to force create a standard api key. By default the generated apikey will be a service api key. It is recommended to use a Git Token when using the service api key. In the case where the user has been invited to an account and that user not the account owner, during toolchain creation the default compliance repositories will be created in that user's account and the service api will not have access to those repositories. In this case a Git Token for the repositories is required. See `repo_git_token_secret_name` for more details. The alternative is to set `force_create_standard_api_key` to `true` to create a standard api key.",
36733680
"required": false
36743681
},
3682+
{
3683+
"key": "ibmcloud_api",
3684+
"type": "string",
3685+
"default_value": "",
3686+
"description": "The environment URL. When left unset this will default to `https://cloud.ibm.com`",
3687+
"required": false
3688+
},
36753689
{
36763690
"key": "inventory_group",
36773691
"type": "string",

main.tf

+36
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ module "prereqs" {
326326
create_kubernetes_access_policy = var.create_kubernetes_access_policy
327327
toolchain_access_group_name = var.toolchain_access_group_name
328328
create_access_group = var.create_access_group
329+
prefix = var.prefix
329330
}
330331

331332
module "devsecops_ci_toolchain" {
@@ -1188,6 +1189,41 @@ resource "null_resource" "ci_pipeline_run" {
11881189
}
11891190
}
11901191

1192+
############# Additional pipeline property for Stack airgap support ############################
1193+
1194+
resource "ibm_cd_tekton_pipeline_property" "ci_pipeline_ibmcloud_api" {
1195+
count = (var.create_ci_toolchain == true && var.ibmcloud_api != "") ? 1 : 0
1196+
name = "ibmcloud-api"
1197+
type = "text"
1198+
value = var.ibmcloud_api
1199+
pipeline_id = module.devsecops_ci_toolchain[0].ci_pipeline_id
1200+
}
1201+
1202+
resource "ibm_cd_tekton_pipeline_property" "pr_pipeline_ibmcloud_api" {
1203+
count = (var.create_ci_toolchain == true && var.ibmcloud_api != "") ? 1 : 0
1204+
name = "ibmcloud-api"
1205+
type = "text"
1206+
value = var.ibmcloud_api
1207+
pipeline_id = module.devsecops_ci_toolchain[0].pr_pipeline_id
1208+
}
1209+
1210+
resource "ibm_cd_tekton_pipeline_property" "cd_pipeline_ibmcloud_api" {
1211+
count = (var.create_cd_toolchain == true && var.ibmcloud_api != "") ? 1 : 0
1212+
name = "ibmcloud-api"
1213+
type = "text"
1214+
value = var.ibmcloud_api
1215+
pipeline_id = module.devsecops_cd_toolchain[0].cd_pipeline_id
1216+
}
1217+
1218+
resource "ibm_cd_tekton_pipeline_property" "cc_pipeline_ibmcloud_api" {
1219+
count = (var.create_cc_toolchain == true && var.ibmcloud_api != "") ? 1 : 0
1220+
name = "ibmcloud-api"
1221+
type = "text"
1222+
value = var.ibmcloud_api
1223+
pipeline_id = module.devsecops_cc_toolchain[0].cc_pipeline_id
1224+
}
1225+
1226+
11911227
#############################################################
11921228
## Example resources to extend the ci_toolchain created above
11931229
#############################################################

prereqs/main.tf

+40-29
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,24 @@ resource "time_static" "timestamp" {
5656

5757
resource "ibm_iam_service_id" "pipeline_service_id" {
5858
count = (local.create_pipeline_service_api_key) ? 1 : 0
59-
name = var.service_name_pipeline
59+
name = (var.prefix == "") ? var.service_name_pipeline : format("${var.prefix}-%s", var.service_name_pipeline)
6060
}
6161

6262
resource "ibm_iam_service_id" "cos_service_id" {
6363
count = (local.create_cos_service_api_key) ? 1 : 0
64-
name = var.service_name_cos
64+
name = (var.prefix == "") ? var.service_name_cos : format("${var.prefix}-%s", var.service_name_cos)
6565
}
6666

6767
data "ibm_iam_service_id" "pipeline_service_id" {
6868
count = (local.create_pipeline_service_api_key) ? 1 : 0
6969
depends_on = [ibm_iam_service_id.pipeline_service_id]
70-
name = var.service_name_pipeline
70+
name = (var.prefix == "") ? var.service_name_pipeline : format("${var.prefix}-%s", var.service_name_pipeline)
7171
}
7272

7373
data "ibm_iam_service_id" "cos_service_id" {
7474
count = (local.create_cos_service_api_key) ? 1 : 0
7575
depends_on = [ibm_iam_service_id.cos_service_id]
76-
name = var.service_name_cos
76+
name = (var.prefix == "") ? var.service_name_cos : format("${var.prefix}-%s", var.service_name_cos)
7777
}
7878

7979
resource "ibm_iam_service_policy" "cos_policy" {
@@ -122,6 +122,16 @@ resource "ibm_iam_service_policy" "cr_policy" {
122122
}
123123
}
124124

125+
resource "ibm_iam_service_policy" "toolchain_policy" {
126+
count = (local.create_pipeline_service_api_key) ? 1 : 0
127+
iam_service_id = ibm_iam_service_id.pipeline_service_id[0].id
128+
roles = ["Editor"]
129+
resources {
130+
service = "toolchain"
131+
resource_group_id = data.ibm_resource_group.resource_group.id
132+
}
133+
}
134+
125135
resource "ibm_iam_service_policy" "cd_policy" {
126136
count = (local.create_pipeline_service_api_key) ? 1 : 0
127137
iam_service_id = ibm_iam_service_id.pipeline_service_id[0].id
@@ -137,7 +147,8 @@ resource "ibm_iam_service_policy" "kube_policy" {
137147
iam_service_id = ibm_iam_service_id.pipeline_service_id[0].id
138148
roles = ["Manager", "Editor"]
139149
resources {
140-
service = "containers-kubernetes"
150+
service = "containers-kubernetes"
151+
resource_group_id = data.ibm_resource_group.resource_group.id
141152
}
142153
}
143154

@@ -265,18 +276,25 @@ resource "ibm_sm_arbitrary_secret" "private_worker_secret" {
265276

266277
################## IAM CREDENTIALS SERVICE API KEYS ###############################
267278

268-
resource "ibm_sm_iam_credentials_configuration" "iam_credentials_configuration" {
269-
count = (local.create_pipeline_service_api_key == true || local.create_auto_rotatable_cos_service_api_key == true) ? 1 : 0
270-
instance_id = (local.sm_instance_id != "") ? local.sm_instance_id : var.sm_instance_id
271-
region = var.sm_location
272-
name = "iam_credentials_config"
273-
api_key = var.ibmcloud_api_key
274-
endpoint_type = var.sm_endpoint_type
279+
resource "ibm_iam_authorization_policy" "secretsmanager_iam_group_auth_policy" {
280+
count = (local.create_pipeline_service_api_key == true || local.create_auto_rotatable_cos_service_api_key == true) ? 1 : 0
281+
source_service_name = "secrets-manager"
282+
source_resource_instance_id = (local.sm_instance_id != "") ? local.sm_instance_id : var.sm_instance_id
283+
target_service_name = "iam-groups"
284+
roles = ["Groups Service Member Manage"]
285+
}
286+
287+
resource "ibm_iam_authorization_policy" "secretsmanager_iam_identitiy_auth_policy" {
288+
count = (local.create_pipeline_service_api_key == true || local.create_auto_rotatable_cos_service_api_key == true) ? 1 : 0
289+
source_service_name = "secrets-manager"
290+
source_resource_instance_id = (local.sm_instance_id != "") ? local.sm_instance_id : var.sm_instance_id
291+
target_service_name = "iam-identity"
292+
roles = ["Operator"]
275293
}
276294

277295
resource "ibm_sm_iam_credentials_secret" "iam_pipeline_apikey_credentials_secret" {
278296
count = (local.create_pipeline_service_api_key) ? 1 : 0
279-
depends_on = [ibm_sm_secret_group.sm_secret_group, data.ibm_sm_secret_group.existing_sm_secret_group, ibm_sm_iam_credentials_configuration.iam_credentials_configuration]
297+
depends_on = [ibm_sm_secret_group.sm_secret_group, data.ibm_sm_secret_group.existing_sm_secret_group, ibm_iam_authorization_policy.secretsmanager_iam_group_auth_policy, ibm_iam_authorization_policy.secretsmanager_iam_identitiy_auth_policy]
280298
instance_id = data.ibm_resource_instance.sm_instance[0].guid
281299
region = var.sm_location
282300
name = var.iam_api_key_secret_name
@@ -294,7 +312,7 @@ resource "ibm_sm_iam_credentials_secret" "iam_pipeline_apikey_credentials_secret
294312

295313
resource "ibm_sm_iam_credentials_secret" "iam_cos_apikey_credentials_secret" {
296314
count = (local.create_auto_rotatable_cos_service_api_key) ? 1 : 0
297-
depends_on = [ibm_sm_secret_group.sm_secret_group, data.ibm_sm_secret_group.existing_sm_secret_group, ibm_sm_iam_credentials_configuration.iam_credentials_configuration]
315+
depends_on = [ibm_sm_secret_group.sm_secret_group, data.ibm_sm_secret_group.existing_sm_secret_group, ibm_iam_authorization_policy.secretsmanager_iam_group_auth_policy, ibm_iam_authorization_policy.secretsmanager_iam_identitiy_auth_policy]
298316
instance_id = data.ibm_resource_instance.sm_instance[0].guid
299317
region = var.sm_location
300318
name = var.cos_api_key_secret_name
@@ -355,18 +373,10 @@ resource "ibm_sm_arbitrary_secret" "secret_cos_api_key" {
355373

356374
resource "ibm_iam_access_group" "toolchain_access_group" {
357375
count = (var.create_access_group == true) ? 1 : 0
358-
name = var.toolchain_access_group_name
376+
name = (var.prefix == "") ? var.toolchain_access_group_name : format("${var.prefix}-%s", var.toolchain_access_group_name)
359377
description = "Access group used for DevSecOps toolchain operations."
360378
}
361379

362-
363-
#resource "ibm_iam_access_group_members" "service_ids" {
364-
# count = (var.create_access_group == true && local.create_pipeline_service_api_key) ? 1 : 0
365-
# access_group_id = ibm_iam_access_group.toolchain_access_group[0].id
366-
# iam_service_ids = local.service_id_list
367-
#}
368-
369-
370380
resource "ibm_iam_access_group_policy" "resource_group_policy" {
371381
count = (var.create_access_group == true && local.create_api_key == true) ? 1 : 0
372382
access_group_id = ibm_iam_access_group.toolchain_access_group[0].id
@@ -380,7 +390,7 @@ resource "ibm_iam_access_group_policy" "resource_group_policy" {
380390
resource "ibm_iam_access_group_policy" "toolchain_group_policy" {
381391
count = (var.create_access_group == true && local.create_api_key == true) ? 1 : 0
382392
access_group_id = ibm_iam_access_group.toolchain_access_group[0].id
383-
roles = ["Editor"]
393+
roles = ["Editor", "PipelineRunner"]
384394
resources {
385395
service = "toolchain"
386396
resource_group_id = data.ibm_resource_group.resource_group.id
@@ -392,13 +402,14 @@ resource "ibm_iam_access_group_policy" "sm_group_policy" {
392402
access_group_id = ibm_iam_access_group.toolchain_access_group[0].id
393403
roles = ["Manager"]
394404
resources {
395-
service = "secrets-manager"
405+
service = "secrets-manager"
406+
resource_group_id = data.ibm_resource_group.resource_group.id
396407
}
397408
}
398409

399410
### Access policies specific to restricting the use of a standard api keys to users added to the access group
400411
resource "ibm_iam_access_group_policy" "cr_group_policy" {
401-
count = (var.create_access_group == true && (local.create_pipeline_api_key == true || local.create_provided_api_key == true)) ? 1 : 0
412+
count = (var.create_access_group == true && (local.create_api_key == true || local.create_provided_api_key == true)) ? 1 : 0
402413
access_group_id = ibm_iam_access_group.toolchain_access_group[0].id
403414
roles = ["Manager"]
404415
resources {
@@ -407,7 +418,7 @@ resource "ibm_iam_access_group_policy" "cr_group_policy" {
407418
}
408419

409420
resource "ibm_iam_access_group_policy" "continuous_delivery_group_policy" {
410-
count = (var.create_access_group == true && (local.create_pipeline_api_key == true || local.create_provided_api_key == true)) ? 1 : 0
421+
count = (var.create_access_group == true && (local.create_api_key == true || local.create_provided_api_key == true)) ? 1 : 0
411422
access_group_id = ibm_iam_access_group.toolchain_access_group[0].id
412423
roles = ["Writer"]
413424
resources {
@@ -417,7 +428,7 @@ resource "ibm_iam_access_group_policy" "continuous_delivery_group_policy" {
417428
}
418429

419430
resource "ibm_iam_access_group_policy" "ce_group_policy" {
420-
count = (var.create_access_group == true && var.create_code_engine_access_policy == true && (local.create_pipeline_api_key == true || local.create_provided_api_key == true)) ? 1 : 0
431+
count = ((var.create_access_group == true && var.create_code_engine_access_policy == true) && (local.create_api_key == true || local.create_provided_api_key == true)) ? 1 : 0
421432
access_group_id = ibm_iam_access_group.toolchain_access_group[0].id
422433
roles = ["Manager", "Editor"]
423434
resources {
@@ -427,7 +438,7 @@ resource "ibm_iam_access_group_policy" "ce_group_policy" {
427438
}
428439

429440
resource "ibm_iam_access_group_policy" "kube_group_policy" {
430-
count = (var.create_access_group == true && var.create_kubernetes_access_policy == true && (local.create_pipeline_api_key == true || local.create_provided_api_key == true)) ? 1 : 0
441+
count = ((var.create_access_group == true && var.create_kubernetes_access_policy == true) && (local.create_api_key == true || local.create_provided_api_key == true)) ? 1 : 0
431442
access_group_id = ibm_iam_access_group.toolchain_access_group[0].id
432443
roles = ["Manager", "Editor"]
433444
resources {

prereqs/variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,9 @@ variable "create_access_group" {
249249
description = "Set to `true` to create an access group for the operations of the DevSecOps toolchains."
250250
default = false
251251
}
252+
253+
variable "prefix" {
254+
type = string
255+
description = "A prefix that is added to the toolchain resources."
256+
default = ""
257+
}

solutions/code-engine/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ No resources.
241241
| <a name="input_evidence_repo_name"></a> [evidence\_repo\_name](#input\_evidence\_repo\_name) | Set to use a custom name for the Evidence repository. | `string` | `""` | no |
242242
| <a name="input_evidence_repo_secret_group"></a> [evidence\_repo\_secret\_group](#input\_evidence\_repo\_secret\_group) | Secret group for the Evidence repository secret. Defaults to the value set in `sm_secret_group` if not set. Only used with `Secrets Manager`. | `string` | `""` | no |
243243
| <a name="input_force_create_standard_api_key"></a> [force\_create\_standard\_api\_key](#input\_force\_create\_standard\_api\_key) | Set to `true` to force create a standard api key. By default the generated apikey will be a service api key. It is recommended to use a Git Token when using the service api key. In the case where the user has been invited to an account and that user not the account owner, during toolchain creation the default compliance repositories will be created in that user's account and the service api will not have access to those repositories. In this case a Git Token for the repositories is required. See `repo_git_token_secret_name` for more details. The alternative is to set `force_create_standard_api_key` to `true` to create a standard api key. | `bool` | `false` | no |
244+
| <a name="input_ibmcloud_api"></a> [ibmcloud\_api](#input\_ibmcloud\_api) | The environment URL. When left unset this will default to `https://cloud.ibm.com` | `string` | `""` | no |
244245
| <a name="input_ibmcloud_api_key"></a> [ibmcloud\_api\_key](#input\_ibmcloud\_api\_key) | The API key used to create the toolchains. (See deployment guide.) | `string` | n/a | yes |
245246
| <a name="input_inventory_group"></a> [inventory\_group](#input\_inventory\_group) | Specify the Git user or group for the inventory repository. | `string` | `""` | no |
246247
| <a name="input_inventory_repo_auth_type"></a> [inventory\_repo\_auth\_type](#input\_inventory\_repo\_auth\_type) | Select the method of authentication that is used to access the Git repository. Valid values are 'oauth' or 'pat'. Defaults to `oauth` when unset. `pat` is a git `personal access token`. | `string` | `""` | no |

0 commit comments

Comments
 (0)