Skip to content

Commit 825452d

Browse files
fix: fixed bug which occrued when not provisioning a KMIP certificate but an adapter is created which attempts to index a null value. As part of this fix the kmip input variable has been marked as "sensitive" since it can contain a certificate value (#657)
1 parent 3b7a5ec commit 825452d

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

examples/complete/main.tf

+12
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ module "kms_root_key" {
9999
]
100100
}
101101

102+
module "kms_root_key_2" {
103+
source = "../.."
104+
kms_instance_id = ibm_resource_instance.key_protect_instance.guid
105+
key_name = "${var.prefix}-root-key-2"
106+
107+
kmip = [
108+
{
109+
name = "${var.prefix}-kmip-adapter-2"
110+
}
111+
]
112+
}
113+
102114
##############################################################################
103115
# KMS standard key
104116
##############################################################################

main.tf

+19-5
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,46 @@ locals {
3838
# tflint-ignore: terraform_unused_declarations
3939
kmip_root_key_validation = (length(var.kmip) > 0 && var.standard_key) ? tobool("When providing a value for `kmip`, the key being created must be a root key.") : true
4040

41-
kmip_certs = flatten([
41+
# for-each for adapter resource
42+
adapter_map = {
43+
for adapter in nonsensitive(var.kmip) : adapter.name => adapter
44+
}
45+
46+
# add adapter name to certificate map
47+
kmip_cert_list = flatten([
4248
[
43-
for adapter in var.kmip : [
49+
for adapter in nonsensitive(var.kmip) : [
4450
for certificate in adapter.certificates : {
4551
adapter_name = adapter.name
4652
certificate_name = try(certificate.name, null)
4753
certificate = certificate.certificate
4854
# Check if filepath string is given, used in ibm_kms_kmip_client_cert call
4955
cert_is_file = length(regexall("^.+\\.pem$", certificate.certificate)) > 0
5056
}
51-
]
57+
] if lookup(adapter, "certificates", null) != null
5258
]
5359
])
5460

61+
# for-each for cert resource
62+
kmip_cert_map = {
63+
for idx, cert in nonsensitive(local.kmip_cert_list) : "${cert.adapter_name}-${idx}" => cert
64+
}
65+
66+
# building adapter output
5567
kmip_adapter_id_output = {
5668
for idx, _ in ibm_kms_kmip_adapter.kmip_adapter :
5769
idx => ibm_kms_kmip_adapter.kmip_adapter[idx].adapter_id
5870
}
71+
72+
# building cert output
5973
kmip_cert_id_output = {
6074
for idx, _ in ibm_kms_kmip_client_cert.kmip_cert :
6175
idx => ibm_kms_kmip_client_cert.kmip_cert[idx].cert_id
6276
}
6377
}
6478

6579
resource "ibm_kms_kmip_adapter" "kmip_adapter" {
66-
for_each = { for adapter in var.kmip : adapter.name => adapter }
80+
for_each = local.adapter_map
6781
instance_id = var.kms_instance_id
6882
profile = "native_1.0"
6983
profile_data = {
@@ -75,7 +89,7 @@ resource "ibm_kms_kmip_adapter" "kmip_adapter" {
7589
}
7690

7791
resource "ibm_kms_kmip_client_cert" "kmip_cert" {
78-
for_each = { for idx, obj in local.kmip_certs : "${obj.adapter_name}-${idx}" => obj }
92+
for_each = local.kmip_cert_map
7993
endpoint_type = var.endpoint_type
8094
instance_id = var.kms_instance_id
8195
adapter_id = ibm_kms_kmip_adapter.kmip_adapter[each.value.adapter_name].adapter_id

variables.tf

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ variable "kmip" {
6666
certificate = string
6767
})))
6868
}))
69+
sensitive = true
6970
description = "Allows a key to utilize the key management interoperability protocol (KMIP), for more information see https://cloud.ibm.com/docs/key-protect?topic=key-protect-kmip"
7071
default = []
7172

0 commit comments

Comments
 (0)