Skip to content

Commit a485039

Browse files
authored
feat: added support to create service credentials using existing service ID with new input service_credentials_existing_serviceid_crn<br>* added support to pass a a list of custom parameters to the service credential creation using new input service_credentials_parameters<br>- Example: service_credentials_parameters = { "service-endpoints" : "public" } (#221)
1 parent d792a05 commit a485039

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,10 @@ No modules.
192192
| <a name="input_secret_type"></a> [secret\_type](#input\_secret\_type) | Type of secret to create, must be one of: arbitrary, username\_password, imported\_cert, service\_credentials | `string` | n/a | yes |
193193
| <a name="input_secret_username"></a> [secret\_username](#input\_secret\_username) | Username of the secret to create. Applies only to `username_password` secret types. When `null`, an `arbitrary` secret is created. | `string` | `null` | no |
194194
| <a name="input_secrets_manager_guid"></a> [secrets\_manager\_guid](#input\_secrets\_manager\_guid) | The instance ID of the Secrets Manager instance where the secret will be added. | `string` | n/a | yes |
195+
| <a name="input_service_credentials_existing_serviceid_crn"></a> [service\_credentials\_existing\_serviceid\_crn](#input\_service\_credentials\_existing\_serviceid\_crn) | The optional parameter 'serviceid\_crn' for creating service credentials. If not passed in, a new Service ID will be created. For more information see https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/sm_service_credentials_secret#parameters | `string` | `null` | no |
196+
| <a name="input_service_credentials_parameters"></a> [service\_credentials\_parameters](#input\_service\_credentials\_parameters) | List of all custom parameters for service credential. | `map(string)` | `null` | no |
195197
| <a name="input_service_credentials_source_service_crn"></a> [service\_credentials\_source\_service\_crn](#input\_service\_credentials\_source\_service\_crn) | The CRN of the source service instance to create the service credential. | `string` | `null` | no |
196-
| <a name="input_service_credentials_source_service_hmac"></a> [service\_credentials\_source\_service\_hmac](#input\_service\_credentials\_source\_service\_hmac) | The optional boolean parameter HMAC for creating specific kind of credentials. For more information see https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/sm_service_credentials_secret#parameters | `bool` | `false` | no |
198+
| <a name="input_service_credentials_source_service_hmac"></a> [service\_credentials\_source\_service\_hmac](#input\_service\_credentials\_source\_service\_hmac) | The optional boolean parameter 'HMAC' for creating specific kind of credentials. For more information see https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/sm_service_credentials_secret#parameters | `bool` | `false` | no |
197199
| <a name="input_service_credentials_source_service_role"></a> [service\_credentials\_source\_service\_role](#input\_service\_credentials\_source\_service\_role) | The role to give the service credential in the source service. | `string` | `null` | no |
198200
| <a name="input_service_credentials_ttl"></a> [service\_credentials\_ttl](#input\_service\_credentials\_ttl) | The time-to-live (TTL) to assign to generated service credentials (in seconds). | `number` | `"7776000"` | no |
199201

examples/complete/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,5 @@ module "secret_manager_service_credential" {
235235
secret_type = "service_credentials" #checkov:skip=CKV_SECRET_6
236236
service_credentials_source_service_crn = module.cloud_object_storage.cos_instance_id
237237
service_credentials_source_service_role = "Writer"
238+
service_credentials_parameters = { "service-endpoints" : "public" }
238239
}

main.tf

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ locals {
3232
auto_rotation_validate_check = regex("^${local.auto_rotation_validate_msg}$", (!local.auto_rotation_validate_condition ? local.auto_rotation_validate_msg : ""))
3333

3434
auto_rotation_enabled = var.secret_auto_rotation == true ? [1] : []
35+
36+
# Prevent user from inputting a custom set of service credential parameters while also enabling specific parameter inputs
37+
custom_parameters_validate_condition = var.service_credentials_parameters != null && (var.service_credentials_source_service_hmac == true || var.service_credentials_existing_serviceid_crn != null)
38+
custom_parameters_validate_msg = "You are passing in a custom set of service credential parameters while also using variables that auto-set parameters."
39+
# tflint-ignore: terraform_unused_declarations
40+
custom_parameters_validate_check = regex("^${local.custom_parameters_validate_msg}$", (!local.custom_parameters_validate_condition ? local.custom_parameters_validate_msg : ""))
3541
}
3642

3743
resource "ibm_sm_arbitrary_secret" "arbitrary_secret" {
@@ -92,6 +98,19 @@ resource "ibm_sm_imported_certificate" "imported_cert" {
9298
endpoint_type = var.endpoint_type
9399
}
94100

101+
locals {
102+
# there is a known issue with ternaries in merge, moved them out: https://github.com/hashicorp/terraform/issues/33310
103+
local_service_credentials_source_service_hmac = var.service_credentials_source_service_hmac ? { "HMAC" : var.service_credentials_source_service_hmac } : null
104+
local_service_credentials_serviceid_crn = var.service_credentials_existing_serviceid_crn != null ? { "serviceid_crn" : var.service_credentials_existing_serviceid_crn } : null
105+
parameters = (
106+
var.service_credentials_parameters != null ? var.service_credentials_parameters :
107+
merge(
108+
local.local_service_credentials_source_service_hmac,
109+
local.local_service_credentials_serviceid_crn,
110+
)
111+
)
112+
}
113+
95114
resource "ibm_sm_service_credentials_secret" "service_credentials_secret" {
96115
count = var.secret_type == "service_credentials" ? 1 : 0 #checkov:skip=CKV_SECRET_6
97116
region = var.region
@@ -110,7 +129,7 @@ resource "ibm_sm_service_credentials_secret" "service_credentials_secret" {
110129
role {
111130
crn = "crn:v1:bluemix:public:iam::::serviceRole:${var.service_credentials_source_service_role}"
112131
}
113-
parameters = var.service_credentials_source_service_hmac ? { "HMAC" : var.service_credentials_source_service_hmac } : null
132+
parameters = local.parameters
114133
}
115134

116135
## This for_each block is NOT a loop to attach to multiple rotation blocks.

variables.tf

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,24 @@ variable "service_credentials_source_service_role" {
112112
default = null
113113
}
114114

115+
variable "service_credentials_parameters" {
116+
type = map(string)
117+
description = "List of all custom parameters for service credential."
118+
default = null
119+
}
120+
121+
variable "service_credentials_source_service_hmac" {
122+
type = bool
123+
description = "The optional boolean parameter 'HMAC' for creating specific kind of credentials. For more information see https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/sm_service_credentials_secret#parameters"
124+
default = false
125+
}
126+
127+
variable "service_credentials_existing_serviceid_crn" {
128+
type = string
129+
description = "The optional parameter 'serviceid_crn' for creating service credentials. If not passed in, a new Service ID will be created. For more information see https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/sm_service_credentials_secret#parameters"
130+
default = null
131+
}
132+
115133
variable "endpoint_type" {
116134
type = string
117135
description = "The endpoint type to communicate with the provided secrets manager instance. Possible values are `public` or `private`"
@@ -122,10 +140,5 @@ variable "endpoint_type" {
122140
}
123141
}
124142

125-
variable "service_credentials_source_service_hmac" {
126-
type = bool
127-
description = "The optional boolean parameter HMAC for creating specific kind of credentials. For more information see https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/sm_service_credentials_secret#parameters"
128-
default = false
129-
}
130143

131144
##############################################################################

0 commit comments

Comments
 (0)