Skip to content

Commit ce70bb4

Browse files
authored
fix(deps) updated required terraform version to >=1.9.0 and updated variable validation logic (#286)
1 parent c1e17fd commit ce70bb4

File tree

10 files changed

+63
-63
lines changed

10 files changed

+63
-63
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ You need the following permissions to run this module.
156156

157157
| Name | Version |
158158
|------|---------|
159-
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.0 |
159+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.9.0 |
160160
| <a name="requirement_ibm"></a> [ibm](#requirement\_ibm) | >= 1.70.0, <2.0.0 |
161161

162162
### Modules

examples/complete/main.tf

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@
33
##############################################################################
44

55
locals {
6-
payload = sensitive("secret-payload-example")
7-
validate_sm_region_cnd = var.existing_sm_instance_guid != null && var.existing_sm_instance_region == null
8-
validate_sm_region_msg = "existing_sm_instance_region must also be set when value given for existing_sm_instance_guid."
9-
# tflint-ignore: terraform_unused_declarations
10-
validate_sm_region_chk = regex(
11-
"^${local.validate_sm_region_msg}$",
12-
(!local.validate_sm_region_cnd
13-
? local.validate_sm_region_msg
14-
: ""))
6+
payload = sensitive("secret-payload-example")
157

168
sm_guid = var.existing_sm_instance_guid == null ? module.secrets_manager[0].secrets_manager_guid : var.existing_sm_instance_guid
179
sm_region = var.existing_sm_instance_region == null ? var.region : var.existing_sm_instance_region

examples/complete/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ variable "existing_sm_instance_guid" {
3838
type = string
3939
description = "Existing Secrets Manager GUID. If not provided an new instance will be provisioned"
4040
default = null
41+
42+
validation {
43+
condition = var.existing_sm_instance_guid != null ? var.existing_sm_instance_region != null : true
44+
error_message = "`existing_sm_instance_region` must also be set when value given for `existing_sm_instance_guid`."
45+
}
4146
}
4247

4348
variable "existing_sm_instance_region" {

examples/complete/version.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
terraform {
2-
required_version = ">= 1.3.0"
2+
required_version = ">= 1.9.0"
33
required_providers {
44
ibm = {
55
source = "IBM-Cloud/ibm"

examples/private/main.tf

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,7 @@
55
locals {
66
payload = sensitive("secret-payload-example")
77
secret_labels = [var.prefix, var.region]
8-
9-
validate_sm_region_cnd = var.existing_sm_instance_crn != null && var.existing_sm_instance_region == null
10-
validate_sm_region_msg = "existing_sm_instance_region must also be set when value given for existing_sm_instance_guid."
11-
# tflint-ignore: terraform_unused_declarations
12-
validate_sm_region_chk = regex(
13-
"^${local.validate_sm_region_msg}$",
14-
(!local.validate_sm_region_cnd
15-
? local.validate_sm_region_msg
16-
: ""))
17-
18-
sm_region = var.existing_sm_instance_region == null ? var.region : var.existing_sm_instance_region
8+
sm_region = var.existing_sm_instance_region == null ? var.region : var.existing_sm_instance_region
199
}
2010

2111
##############################################################################

examples/private/variables.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,16 @@ variable "existing_sm_instance_crn" {
3838
type = string
3939
description = "An existing Secrets Manager instance CRN. If not provided an new instance will be provisioned."
4040
default = null
41+
42+
validation {
43+
condition = var.existing_sm_instance_crn != null ? var.existing_sm_instance_region != null : true
44+
error_message = "`existing_sm_instance_region` must also be set when value given for `existing_sm_instance_crn`."
45+
}
4146
}
4247

4348
variable "existing_sm_instance_region" {
4449
type = string
45-
description = "The region of the existing Secrets Manager instance. Only required if value is passed into var.existing_sm_instance_guid"
50+
description = "The region of the existing Secrets Manager instance. Only required if value is passed into var.existing_sm_instance_crn"
4651
default = null
4752
}
4853

examples/private/version.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
terraform {
2-
required_version = ">= 1.3.0"
2+
required_version = ">= 1.9.0"
33
required_providers {
44
ibm = {
55
source = "IBM-Cloud/ibm"

main.tf

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,8 @@
44
# Creates Secret within existing Secret Manager instance and Secret Manager Group
55
##############################################################################
66

7-
# Validation
8-
# Approach based on https://stackoverflow.com/a/66682419
97
locals {
10-
# validate username_password or arbitrary secret has a password payload
11-
userpass_validate_condition = (var.secret_type == "username_password" || var.secret_type == "arbitrary") && var.secret_payload_password == "" #checkov:skip=CKV_SECRET_6
12-
userpass_validate_msg = "When creating a username_password or arbitrary secret, a value for `secret_payload_password` is required."
13-
# tflint-ignore: terraform_unused_declarations
14-
userpass_validate_check = regex("^${local.userpass_validate_msg}$", (!local.userpass_validate_condition ? local.userpass_validate_msg : ""))
15-
16-
# validate imported certificate has a TLS certificate
17-
imported_cert_validate_condition = var.secret_type == "imported_cert" && var.imported_cert_certificate == null #checkov:skip=CKV_SECRET_6
18-
imported_cert_validate_msg = "When creating an imported_cert secret, value for `imported_cert_certificate` cannot be null."
19-
# tflint-ignore: terraform_unused_declarations
20-
imported_cert_validate_check = regex("^${local.imported_cert_validate_msg}$", (!local.imported_cert_validate_condition ? local.imported_cert_validate_msg : ""))
21-
22-
# validate service credentials has source service information
23-
service_credentials_validate_condition = (var.secret_type == "service_credentials" && var.service_credentials_source_service_crn == null) || (var.secret_type == "service_credentials" && var.service_credentials_source_service_role_crn == null) #checkov:skip=CKV_SECRET_6
24-
service_credentials_validate_msg = "When creating a service_credentials secret, values for `service_credentials_source_service_crn` and `service_credentials_source_service_role_crn` are required."
25-
# tflint-ignore: terraform_unused_declarations
26-
service_credentials_validate_check = regex("^${local.service_credentials_validate_msg}$", (!local.service_credentials_validate_condition ? local.service_credentials_validate_msg : ""))
27-
28-
# validate auto rotation format
29-
auto_rotation_validate_condition = var.secret_auto_rotation == true && var.secret_auto_rotation_unit != "month" && var.secret_auto_rotation == true && var.secret_auto_rotation_unit != "day" || var.secret_auto_rotation == true && var.secret_auto_rotation_interval == 0
30-
auto_rotation_validate_msg = "Value for `secret_auto_rotation_unit' must be either `day` or `month` and value for `secret_auto_rotation_interval` must be higher than 0"
31-
# tflint-ignore: terraform_unused_declarations
32-
auto_rotation_validate_check = regex("^${local.auto_rotation_validate_msg}$", (!local.auto_rotation_validate_condition ? local.auto_rotation_validate_msg : ""))
33-
348
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 : ""))
419
}
4210

4311
resource "ibm_sm_arbitrary_secret" "arbitrary_secret" {

variables.tf

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,78 @@ variable "region" {
66
type = string
77
description = "The region where the Secrets Manager instance is deployed."
88
}
9+
910
variable "secrets_manager_guid" {
1011
type = string
1112
description = "The instance ID of the Secrets Manager instance where the secret will be added."
1213
}
14+
1315
variable "secret_group_id" {
1416
type = string
1517
description = "The ID of the secret group for the secret. If `null`, the `default` secret group is used."
1618
default = "default"
1719
}
20+
1821
variable "secret_type" {
1922
type = string
2023
description = "Type of secret to create, must be one of: arbitrary, username_password, imported_cert, service_credentials"
24+
2125
validation {
22-
condition = anytrue([
23-
var.secret_type == "arbitrary", #checkov:skip=CKV_SECRET_6
24-
var.secret_type == "username_password", #checkov:skip=CKV_SECRET_6
25-
var.secret_type == "imported_cert", #checkov:skip=CKV_SECRET_6
26-
var.secret_type == "service_credentials", #checkov:skip=CKV_SECRET_6
27-
])
26+
condition = contains(["arbitrary", "username_password", "imported_cert", "service_credentials"], var.secret_type) #checkov:skip=CKV_SECRET_6
2827
error_message = "Only supported secrets types are arbitrary, username_password, imported_cert, or service_credentials"
2928
}
29+
30+
validation {
31+
condition = (var.secret_type == "username_password" || var.secret_type == "arbitrary") ? var.secret_payload_password != "" : true
32+
error_message = "When creating a username_password or arbitrary secret, a value for `secret_payload_password` is required."
33+
}
34+
35+
validation {
36+
condition = var.secret_type == "imported_cert" ? var.imported_cert_certificate != null : true
37+
error_message = "When creating an imported_cert secret, value for `imported_cert_certificate` cannot be null."
38+
}
39+
40+
validation {
41+
condition = var.secret_type == "service_credentials" ? var.service_credentials_source_service_crn != null && var.service_credentials_source_service_role_crn != null : true
42+
error_message = "When creating a service_credentials secret, values for `service_credentials_source_service_crn` and `service_credentials_source_service_role_crn` are required."
43+
}
3044
}
45+
3146
variable "imported_cert_certificate" {
3247
type = string
3348
description = "The TLS certificate to import."
3449
default = null
3550
}
51+
3652
variable "imported_cert_private_key" {
3753
type = string
3854
description = "(optional) The private key for the TLS certificate to import."
3955
default = null
4056
sensitive = true
4157
}
58+
4259
variable "imported_cert_intermediate" {
4360
type = string
4461
description = "(optional) The intermediate certificate for the TLS certificate to import."
4562
default = null
4663
}
64+
4765
variable "secret_name" {
4866
type = string
4967
description = "Name of the secret to create."
5068
}
69+
5170
variable "secret_description" {
5271
type = string
5372
description = "Description of the secret to create."
5473
}
74+
5575
variable "secret_username" {
5676
type = string
5777
description = "Username of the secret to create. Applies only to `username_password` secret types. When `null`, an `arbitrary` secret is created."
5878
default = null
5979
}
80+
6081
variable "secret_labels" {
6182
type = list(string)
6283
description = "Labels of the secret to create. Up to 30 labels can be created. Labels can be 2 - 30 characters, including spaces. Special characters that are not permitted include the angled brackets (<>), comma (,), colon (:), ampersand (&), and vertical pipe character (|)."
@@ -67,26 +88,40 @@ variable "secret_labels" {
6788
error_message = "Up to 30 labels can be created. Labels can be 2 - 30 characters, including spaces. Special characters that are not permitted include the angled brackets (<>), comma (,), colon (:), ampersand (&), and vertical pipe character (|)."
6889
}
6990
}
91+
7092
variable "secret_payload_password" {
7193
type = string
7294
description = "The payload (for arbitrary secrets) or password (for username and password credentials) of the secret."
7395
sensitive = true
7496
default = "" #tfsec:ignore:general-secrets-no-plaintext-exposure
7597
}
98+
7699
variable "secret_auto_rotation" {
77100
type = bool
78101
description = "Whether to configure automatic rotation. Applies only to the `username_password` and `service_credentials` secret types."
79102
default = true
80103
}
104+
81105
variable "secret_auto_rotation_unit" {
82106
type = string
83107
description = "Specifies the unit of time for rotation of a username_password secret. Acceptable values are `day` or `month`."
84108
default = "day" #tfsec:ignore:general-secrets-no-plaintext-exposure
109+
110+
validation {
111+
condition = contains(["day", "month"], var.secret_auto_rotation_unit)
112+
error_message = "Value for `secret_auto_rotation_unit' must be either `day` or `month`."
113+
}
85114
}
115+
86116
variable "secret_auto_rotation_interval" {
87117
type = number
88118
description = "Specifies the rotation interval for the rotation unit."
89119
default = 89
120+
121+
validation {
122+
condition = var.secret_auto_rotation_interval > 0
123+
error_message = "Value for `secret_auto_rotation_interval` must be higher than 0."
124+
}
90125
}
91126

92127
variable "service_credentials_ttl" {
@@ -116,6 +151,11 @@ variable "service_credentials_parameters" {
116151
type = map(string)
117152
description = "List of all custom parameters for service credential."
118153
default = null
154+
155+
validation {
156+
condition = var.service_credentials_parameters != null ? !(var.service_credentials_source_service_hmac == true || var.service_credentials_existing_serviceid_crn != null) : true
157+
error_message = "You are passing in a custom set of service credential parameters while also using variables that auto-set parameters."
158+
}
119159
}
120160

121161
variable "service_credentials_source_service_hmac" {

version.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
terraform {
2-
required_version = ">= 1.3.0"
2+
required_version = ">= 1.9.0"
33
required_providers {
44
# Use "greater than or equal to" range in modules
55
ibm = {

0 commit comments

Comments
 (0)