Skip to content

Commit e682585

Browse files
authored
feat: the following variables have been removed create_resource_key, resource_key_name, resource_key_role, generate_hmac_credentials, resource_key_existing_serviceid_crn and replaced by the resource_keys variable which gives more flexibility for creating resource keys.<br>* added new resource_keys output. (#522)
BREAKING CHANGE: If you were consuming the module with `create_resource_key = true` and are now upgrading to this version, you will see that your plan will attempt to destroy a resource key. You should now use the new `resource_keys` variable to manage resource keys. Please plan accordingly before upgrading incase you are using the resource key planned for destroy else where. Please note that before this version was released, the module did not output the resource key details, so unless you were extracting the resource key from some other method, its possible you were not using the key that is getting identified for destroy.
1 parent be84e5b commit e682585

File tree

12 files changed

+118
-102
lines changed

12 files changed

+118
-102
lines changed

.secrets.baseline

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"files": "go.sum|^.secrets.baseline$",
44
"lines": null
55
},
6-
"generated_at": "2023-12-20T23:33:56Z",
6+
"generated_at": "2024-03-26T13:48:41Z",
77
"plugins_used": [
88
{
99
"name": "AWSKeyDetector"
@@ -85,6 +85,14 @@
8585
"line_number": 41,
8686
"type": "Secret Keyword",
8787
"verified_result": null
88+
},
89+
{
90+
"hashed_secret": "a7c93faaa770c377154ea9d4d0d17a9056dbfa95",
91+
"is_secret": false,
92+
"is_verified": false,
93+
"line_number": 191,
94+
"type": "Secret Keyword",
95+
"verified_result": null
8896
}
8997
],
9098
"modules/buckets/README.md": [
@@ -105,6 +113,14 @@
105113
"line_number": 15,
106114
"type": "Secret Keyword",
107115
"verified_result": null
116+
},
117+
{
118+
"hashed_secret": "a7c93faaa770c377154ea9d4d0d17a9056dbfa95",
119+
"is_secret": false,
120+
"is_verified": false,
121+
"line_number": 119,
122+
"type": "Secret Keyword",
123+
"verified_result": null
108124
}
109125
],
110126
"tests/pr_test.go": [

README.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,9 @@ You need the following permissions to run this module.
152152
| [ibm_cos_bucket.cos_bucket1](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/cos_bucket) | resource |
153153
| [ibm_iam_authorization_policy.policy](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/iam_authorization_policy) | resource |
154154
| [ibm_resource_instance.cos_instance](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/resource_instance) | resource |
155-
| [ibm_resource_key.resource_key](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/resource_key) | resource |
155+
| [ibm_resource_key.resource_keys](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/resource_key) | resource |
156156
| [ibm_resource_tag.cos_access_tag](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/resources/resource_tag) | resource |
157157
| [random_string.bucket_name_suffix](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) | resource |
158-
| [terraform_data.generate_hmac_credentials](https://registry.terraform.io/providers/hashicorp/terraform/latest/docs/resources/data) | resource |
159-
| [terraform_data.resource_key_existing_serviceid_crn](https://registry.terraform.io/providers/hashicorp/terraform/latest/docs/resources/data) | resource |
160158
| [time_sleep.wait_for_authorization_policy](https://registry.terraform.io/providers/hashicorp/time/latest/docs/resources/sleep) | resource |
161159

162160
### Inputs
@@ -177,13 +175,11 @@ You need the following permissions to run this module.
177175
| <a name="input_cos_tags"></a> [cos\_tags](#input\_cos\_tags) | Optional list of tags to be added to cloud object storage instance. Only used if 'create\_cos\_instance' it true. | `list(string)` | `[]` | no |
178176
| <a name="input_create_cos_bucket"></a> [create\_cos\_bucket](#input\_create\_cos\_bucket) | Set as true to create a new Cloud Object Storage bucket | `bool` | `true` | no |
179177
| <a name="input_create_cos_instance"></a> [create\_cos\_instance](#input\_create\_cos\_instance) | Set as true to create a new Cloud Object Storage instance. | `bool` | `true` | no |
180-
| <a name="input_create_resource_key"></a> [create\_resource\_key](#input\_create\_resource\_key) | Set as true to create a new resource key for the Cloud Object Storage instance. | `bool` | `true` | no |
181178
| <a name="input_cross_region_location"></a> [cross\_region\_location](#input\_cross\_region\_location) | Specify the cross-regional bucket location. Supported values are 'us', 'eu', and 'ap'. If you pass a value for this, ensure to set the value of var.region and var.single\_site\_location to null. | `string` | `null` | no |
182179
| <a name="input_existing_cos_instance_id"></a> [existing\_cos\_instance\_id](#input\_existing\_cos\_instance\_id) | The ID of an existing cloud object storage instance. Required if 'var.create\_cos\_instance' is false. | `string` | `null` | no |
183180
| <a name="input_existing_kms_instance_guid"></a> [existing\_kms\_instance\_guid](#input\_existing\_kms\_instance\_guid) | The GUID of the Key Protect or Hyper Protect instance in which the key specified in var.kms\_key\_crn is coming from. Required if var.skip\_iam\_authorization\_policy is false in order to create an IAM Access Policy to allow Key Protect or Hyper Protect to access the newly created COS instance. | `string` | `null` | no |
184181
| <a name="input_expire_days"></a> [expire\_days](#input\_expire\_days) | Specifies the number of days when the expire rule action takes effect. Only used if 'create\_cos\_bucket' is true. | `number` | `365` | no |
185182
| <a name="input_force_delete"></a> [force\_delete](#input\_force\_delete) | Deletes all the objects in the COS Bucket before bucket is deleted. | `bool` | `true` | no |
186-
| <a name="input_generate_hmac_credentials"></a> [generate\_hmac\_credentials](#input\_generate\_hmac\_credentials) | Set as true to generate an HMAC key in the resource key. Only used when create\_resource\_key is `true`. | `bool` | `false` | no |
187183
| <a name="input_hard_quota"></a> [hard\_quota](#input\_hard\_quota) | Sets a maximum amount of storage (in bytes) available for a bucket. If it is set to `null` then quota is disabled. | `number` | `null` | no |
188184
| <a name="input_instance_cbr_rules"></a> [instance\_cbr\_rules](#input\_instance\_cbr\_rules) | (Optional, list) List of CBR rules to create for the instance | <pre>list(object({<br> description = string<br> account_id = string<br> rule_contexts = list(object({<br> attributes = optional(list(object({<br> name = string<br> value = string<br> }))) }))<br> enforcement_mode = string<br> tags = optional(list(object({<br> name = string<br> value = string<br> })), [])<br> operations = optional(list(object({<br> api_types = list(object({<br> api_type_id = string<br> }))<br> })))<br> }))</pre> | `[]` | no |
189185
| <a name="input_kms_encryption_enabled"></a> [kms\_encryption\_enabled](#input\_kms\_encryption\_enabled) | Set as true to use KMS key encryption to encrypt data in COS bucket (only applicable when var.create\_cos\_bucket is true). | `bool` | `true` | no |
@@ -192,9 +188,7 @@ You need the following permissions to run this module.
192188
| <a name="input_object_versioning_enabled"></a> [object\_versioning\_enabled](#input\_object\_versioning\_enabled) | Enable object versioning to keep multiple versions of an object in a bucket. Cannot be used with retention rule. Only used if 'create\_cos\_bucket' is true. | `bool` | `false` | no |
193189
| <a name="input_region"></a> [region](#input\_region) | The region to provision the bucket. If you pass a value for this, do not pass one for var.cross\_region\_location or var.single\_site\_location. | `string` | `"us-south"` | no |
194190
| <a name="input_resource_group_id"></a> [resource\_group\_id](#input\_resource\_group\_id) | The resource group ID where The COS instance will be provisioned. It is required if setting input variable create\_cos\_instance to true. | `string` | `null` | no |
195-
| <a name="input_resource_key_existing_serviceid_crn"></a> [resource\_key\_existing\_serviceid\_crn](#input\_resource\_key\_existing\_serviceid\_crn) | CRN of existing serviceID to bind with resource key to be created. If null a new ServiceID is created for the resource key. | `string` | `null` | no |
196-
| <a name="input_resource_key_name"></a> [resource\_key\_name](#input\_resource\_key\_name) | The name of the resource key to be created. | `string` | `"cos-resource-key"` | no |
197-
| <a name="input_resource_key_role"></a> [resource\_key\_role](#input\_resource\_key\_role) | The role you want to be associated with your new resource key. Valid roles are 'Writer', 'Reader', 'Manager', 'Content Reader', 'Object Reader', 'Object Writer'. | `string` | `"Manager"` | no |
191+
| <a name="input_resource_keys"></a> [resource\_keys](#input\_resource\_keys) | The definition of any resource keys to be generated | <pre>list(object({<br> name = string<br> generate_hmac_credentials = optional(bool, false)<br> role = optional(string, "Reader")<br> service_id_crn = optional(string, null)<br> }))</pre> | `[]` | no |
198192
| <a name="input_retention_default"></a> [retention\_default](#input\_retention\_default) | Specifies default duration of time an object that can be kept unmodified for COS bucket. Only used if 'create\_cos\_bucket' is true. | `number` | `90` | no |
199193
| <a name="input_retention_enabled"></a> [retention\_enabled](#input\_retention\_enabled) | Retention enabled for COS bucket. Only used if 'create\_cos\_bucket' is true. | `bool` | `false` | no |
200194
| <a name="input_retention_maximum"></a> [retention\_maximum](#input\_retention\_maximum) | Specifies maximum duration of time an object that can be kept unmodified for COS bucket. Only used if 'create\_cos\_bucket' is true. | `number` | `350` | no |
@@ -222,6 +216,7 @@ You need the following permissions to run this module.
222216
| <a name="output_instance_cbr_rules"></a> [instance\_cbr\_rules](#output\_instance\_cbr\_rules) | COS instance rules |
223217
| <a name="output_kms_key_crn"></a> [kms\_key\_crn](#output\_kms\_key\_crn) | The CRN of the KMS key used to encrypt the COS bucket |
224218
| <a name="output_resource_group_id"></a> [resource\_group\_id](#output\_resource\_group\_id) | Resource Group ID |
219+
| <a name="output_resource_keys"></a> [resource\_keys](#output\_resource\_keys) | List of resource keys |
225220
| <a name="output_s3_endpoint_direct"></a> [s3\_endpoint\_direct](#output\_s3\_endpoint\_direct) | S3 direct endpoint |
226221
| <a name="output_s3_endpoint_private"></a> [s3\_endpoint\_private](#output\_s3\_endpoint\_private) | S3 private endpoint |
227222
| <a name="output_s3_endpoint_public"></a> [s3\_endpoint\_public](#output\_s3\_endpoint\_public) | S3 public endpoint |

examples/advanced/main.tf

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ module "resource_group" {
1313
##############################################################################
1414
# Create serviceID to use for resource key hmac
1515
#
16-
# NOTE: The module itself supports creating one, but this example shows how
17-
# you can use an existing one
16+
# NOTE: The module itself supports creating internally, but this example shows
17+
# how to use an existing ones
1818
##############################################################################
19-
2019
resource "ibm_iam_service_id" "resource_key_existing_serviceid" {
2120
name = "${var.prefix}-reskey-serviceid"
2221
description = "ServiceID for ${var.prefix} env to use for resource key credentials"
@@ -144,10 +143,34 @@ module "cos_bucket1" {
144143
existing_kms_instance_guid = module.key_protect_all_inclusive.kms_guid
145144
kms_key_crn = module.key_protect_all_inclusive.keys["${local.key_ring_name}.${local.key_name}"].crn
146145
sysdig_crn = module.observability_instances.cloud_monitoring_crn
147-
# If no value is passed for this variable, the module will create a new service ID for the resource key
148-
resource_key_existing_serviceid_crn = ibm_iam_service_id.resource_key_existing_serviceid.crn
149146
retention_enabled = false # disable retention for test environments - enable for stage/prod
150147
activity_tracker_crn = local.at_crn
148+
resource_keys = [
149+
{
150+
name = "${var.prefix}-writer-key"
151+
role = "Writer"
152+
service_id_crn = ibm_iam_service_id.resource_key_existing_serviceid.crn
153+
},
154+
{
155+
name = "${var.prefix}-reader-key"
156+
},
157+
{
158+
name = "${var.prefix}-manager-key"
159+
role = "Manager"
160+
},
161+
{
162+
name = "${var.prefix}-content-reader-key"
163+
role = "Content Reader"
164+
},
165+
{
166+
name = "${var.prefix}-object-reader-key"
167+
role = "Object Reader"
168+
},
169+
{
170+
name = "${var.prefix}-object-writer-key"
171+
role = "Object Writer"
172+
}
173+
]
151174
bucket_cbr_rules = [
152175
{
153176
description = "sample rule for bucket 1"

main.tf

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,15 @@ resource "ibm_resource_tag" "cos_access_tag" {
6262
tag_type = "access"
6363
}
6464

65-
# To force IBM resource key replacement when input changes
66-
resource "terraform_data" "generate_hmac_credentials" {
67-
input = var.generate_hmac_credentials
68-
}
69-
70-
# To force IBM resource key replacement when input changes
71-
resource "terraform_data" "resource_key_existing_serviceid_crn" {
72-
input = var.resource_key_existing_serviceid_crn
73-
}
74-
75-
resource "ibm_resource_key" "resource_key" {
76-
count = var.create_resource_key && var.create_cos_instance ? 1 : 0
77-
name = var.resource_key_name
78-
resource_instance_id = ibm_resource_instance.cos_instance[count.index].id
65+
resource "ibm_resource_key" "resource_keys" {
66+
for_each = { for key in var.resource_keys : key.name => key }
67+
name = each.key
68+
resource_instance_id = local.cos_instance_id
69+
role = each.value.role
7970
parameters = {
80-
"serviceid_crn" = var.resource_key_existing_serviceid_crn
81-
"HMAC" = var.generate_hmac_credentials
71+
"serviceid_crn" = each.value.service_id_crn
72+
"HMAC" = each.value.generate_hmac_credentials
8273
}
83-
role = var.resource_key_role
84-
# parameters block is outside lifecycle, this force replacement if values change
85-
lifecycle { replace_triggered_by = [terraform_data.generate_hmac_credentials, terraform_data.resource_key_existing_serviceid_crn] }
8674
}
8775

8876
locals {

modules/buckets/main.tf

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
##############################################################################
22
# terraform-ibm-cos
33
#
4-
# Create COS buckets
5-
##############################################################################
6-
7-
locals {
8-
# input variable validation
9-
# tflint-ignore: terraform_unused_declarations
10-
# validate_key_inputs = var.create_cos_bucket && var.encryption_enabled && var.kms_key_crn == null ? tobool("A value must be passed for var.kms_key_crn when both var.create_cos_bucket and var.encryption_enabled are true") : true
11-
}
12-
134
# Create COS buckets from bucket_configs
5+
##############################################################################
146

157
module "buckets" {
168
for_each = {

modules/buckets/outputs.tf

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
##############################################################################
22
# Outputs
33
##############################################################################
4+
# expand buckets, pull all elements except bucket_resource_key
5+
# iterate over list (...) and merge() back to map
46
output "buckets" {
57
description = "Map of buckets created in the Cloud Object Storage Instance"
6-
value = module.buckets
8+
value = merge([for name, bucket in module.buckets : { (name) = {
9+
"resource_group_id" = bucket.resource_group_id
10+
"s3_endpoint_private" = bucket.s3_endpoint_private
11+
"s3_endpoint_public" = bucket.s3_endpoint_public
12+
"s3_endpoint_direct" = bucket.s3_endpoint_direct
13+
"bucket_id" = bucket.bucket_id
14+
"bucket_crn" = bucket.bucket_crn
15+
"bucket_name" = bucket.bucket_name
16+
"bucket_storage_class" = bucket.bucket_storage_class
17+
"kms_key_crn" = bucket.kms_key_crn
18+
"cos_instance_id" = bucket.cos_instance_id
19+
"cos_instance_guid" = bucket.cos_instance_guid
20+
"bucket_cbr_rules" = bucket.bucket_cbr_rules
21+
"instance_cbr_rules" = bucket.instance_cbr_rules
22+
"cbr_rule_ids" = bucket.cbr_rule_ids
23+
# skip resource_keys because it is sensitive
24+
} }]...)
725
}
826

927
output "bucket_configs" {

modules/fscloud/README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,10 @@ No resources.
113113
| <a name="input_cos_plan"></a> [cos\_plan](#input\_cos\_plan) | Plan to be used for creating cloud object storage instance. Only used if 'create\_cos\_instance' it true. | `string` | `"standard"` | no |
114114
| <a name="input_cos_tags"></a> [cos\_tags](#input\_cos\_tags) | Optional list of tags to be added to cloud object storage instance. Only used if 'create\_cos\_instance' it true. | `list(string)` | `[]` | no |
115115
| <a name="input_create_cos_instance"></a> [create\_cos\_instance](#input\_create\_cos\_instance) | Set as true to create a new Cloud Object Storage instance. | `bool` | `true` | no |
116-
| <a name="input_create_resource_key"></a> [create\_resource\_key](#input\_create\_resource\_key) | Set as true to create a new resource key for the Cloud Object Storage instance. | `bool` | `false` | no |
117116
| <a name="input_existing_cos_instance_id"></a> [existing\_cos\_instance\_id](#input\_existing\_cos\_instance\_id) | The ID of an existing cloud object storage instance. Required if 'var.create\_cos\_instance' is false. | `string` | `null` | no |
118-
| <a name="input_generate_hmac_credentials"></a> [generate\_hmac\_credentials](#input\_generate\_hmac\_credentials) | Set as true to generate an HMAC key in the resource key. Only used when create\_resource\_key is `true`. | `bool` | `false` | no |
119117
| <a name="input_instance_cbr_rules"></a> [instance\_cbr\_rules](#input\_instance\_cbr\_rules) | (Optional, list) List of CBR rule to create for the instance | <pre>list(object({<br> description = string<br> account_id = string<br> rule_contexts = list(object({<br> attributes = optional(list(object({<br> name = string<br> value = string<br> }))) }))<br> enforcement_mode = string<br> tags = optional(list(object({<br> name = string<br> value = string<br> })), [])<br> operations = optional(list(object({<br> api_types = list(object({<br> api_type_id = string<br> }))<br> })))<br> }))</pre> | `[]` | no |
120118
| <a name="input_resource_group_id"></a> [resource\_group\_id](#input\_resource\_group\_id) | The resource group ID where resources will be provisioned. | `string` | n/a | yes |
121-
| <a name="input_resource_key_name"></a> [resource\_key\_name](#input\_resource\_key\_name) | The name of the resource key to be created. | `string` | `"cos-resource-key"` | no |
122-
| <a name="input_resource_key_role"></a> [resource\_key\_role](#input\_resource\_key\_role) | The role you want to be associated with your new resource key. Valid roles are 'Writer', 'Reader', 'Manager', 'Content Reader', 'Object Reader', 'Object Writer'. | `string` | `"Manager"` | no |
119+
| <a name="input_resource_keys"></a> [resource\_keys](#input\_resource\_keys) | The definition of any resource keys to be generated | <pre>list(object({<br> name = string<br> generate_hmac_credentials = optional(bool, false)<br> role = optional(string, "Reader")<br> service_id_crn = string<br> }))</pre> | `[]` | no |
123120

124121
### Outputs
125122

@@ -134,4 +131,5 @@ No resources.
134131
| <a name="output_cos_instance_name"></a> [cos\_instance\_name](#output\_cos\_instance\_name) | COS instance name |
135132
| <a name="output_instance_cbr_rules"></a> [instance\_cbr\_rules](#output\_instance\_cbr\_rules) | COS instance rules |
136133
| <a name="output_resource_group_id"></a> [resource\_group\_id](#output\_resource\_group\_id) | Resource Group ID |
134+
| <a name="output_resource_keys"></a> [resource\_keys](#output\_resource\_keys) | List of resource keys |
137135
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

modules/fscloud/main.tf

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,14 @@ module "cos_instance" {
2424
create_cos_bucket = false
2525
skip_iam_authorization_policy = true
2626
cos_instance_name = var.cos_instance_name
27-
create_resource_key = var.create_resource_key
28-
resource_key_name = var.resource_key_name
29-
resource_key_role = var.resource_key_role
30-
generate_hmac_credentials = var.generate_hmac_credentials
27+
resource_keys = var.resource_keys
3128
cos_plan = var.cos_plan
3229
cos_tags = var.cos_tags
3330
access_tags = var.access_tags
3431
}
3532

3633
locals {
37-
# Add the cos instance id to the bucket configs
34+
# Add the cos instance id to the bucket configs
3835
bucket_configs = [
3936
for config in var.bucket_configs :
4037
{

modules/fscloud/outputs.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ output "cos_instance_crn" {
3131
value = local.cos_instance_crn
3232
}
3333

34+
output "resource_keys" {
35+
description = "List of resource keys"
36+
value = module.cos_instance[0].resource_keys
37+
sensitive = true
38+
}
39+
3440
output "bucket_cbr_rules" {
3541
description = "COS bucket rules"
3642
value = module.buckets.cbr_rules

0 commit comments

Comments
 (0)