Skip to content

Commit e37408d

Browse files
BenbentwotionichmCopilot
authored
Managed Admin Password for DocumentDb (#124)
* feat: add support for managed master credentials Added one additional nullable variable that will allow users to manage their master credentials using AWS Secrets Manager. This is a native feature of the core resource. * chore: update `manage_master_user_password` documentation Updated the documentation to be a bit more clear and aligned with the actual documentation. * fix: ssm logic corrected Was originally set to check whether `manage_master_user_password` is null, which is not the correct test. * (feat) Update create_password logic * fix: Set master_password output to null when password is managed * feat: Refactor conditional expressions * chore: fix documentation Co-authored-by: Copilot <[email protected]> * fix: Update faulty conditional Co-authored-by: Ben <[email protected]> * Revert "fix: Update faulty conditional" This reverts commit 876b50b. * fix: correct logic for manage_master_user_password variable * fix: Remove validation block * refactor: master password handling - Simplify logic for managing master password and Secrets Manager - Set default for manage_master_user_password to false - Clarify variable description and output behavior * refactor: improve master password management logic and validation in DocumentDB cluster * Update variables to be proper order - forget if TF short ciruits at this low version * fix: update SSM parameter store condition to handle null master password values * update outputs.tf --------- Co-authored-by: Jan Costandius <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 2feab76 commit e37408d

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

main.tf

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
locals {
22
enabled = module.this.enabled
3-
create_password = local.enabled && var.master_password == null
4-
3+
create_password = local.enabled && var.master_password == null && var.manage_master_user_password == null
4+
// If both `var.master_password` and `var.manage_master_user_password` are set to null, the module will create a random password
5+
// else if `var.master_password` is set to null - master_password is set to null as `manage_master_user_password` is set to true
6+
// else `local.master_password` is set to the value provided in `var.master_password`
57
master_password = local.create_password ? one(random_password.password[*].result) : var.master_password
68
}
79

@@ -64,10 +66,12 @@ resource "random_password" "password" {
6466
}
6567

6668
resource "aws_docdb_cluster" "default" {
67-
count = local.enabled ? 1 : 0
68-
cluster_identifier = module.this.id
69-
master_username = var.master_username
69+
count = local.enabled ? 1 : 0
70+
cluster_identifier = module.this.id
71+
master_username = var.master_username
72+
# If `master_password` or `manage_master_user_password` is set, the other MUST be set to null, otherwise it will cause an error.
7073
master_password = local.master_password
74+
manage_master_user_password = var.manage_master_user_password
7175
backup_retention_period = var.retention_period
7276
preferred_backup_window = var.preferred_backup_window
7377
preferred_maintenance_window = var.preferred_maintenance_window
@@ -142,7 +146,7 @@ module "dns_master" {
142146
source = "cloudposse/route53-cluster-hostname/aws"
143147
version = "0.13.0"
144148

145-
enabled = local.enabled && var.zone_id != "" ? true : false
149+
enabled = local.enabled && var.zone_id != ""
146150
dns_name = local.cluster_dns_name
147151
zone_id = var.zone_id
148152
records = coalescelist(aws_docdb_cluster.default[*].endpoint, [""])
@@ -154,7 +158,7 @@ module "dns_replicas" {
154158
source = "cloudposse/route53-cluster-hostname/aws"
155159
version = "0.13.0"
156160

157-
enabled = local.enabled && var.zone_id != "" ? true : false
161+
enabled = local.enabled && var.zone_id != ""
158162
dns_name = local.replicas_dns_name
159163
zone_id = var.zone_id
160164
records = coalescelist(aws_docdb_cluster.default[*].reader_endpoint, [""])
@@ -166,7 +170,7 @@ module "ssm_write_db_password" {
166170
source = "cloudposse/ssm-parameter-store/aws"
167171
version = "0.13.0"
168172

169-
enabled = local.enabled && var.ssm_parameter_enabled == true ? true : false
173+
enabled = local.enabled && var.ssm_parameter_enabled && var.manage_master_user_password != null
170174
parameter_write = [
171175
{
172176
name = format("%s%s", var.ssm_parameter_path_prefix, module.this.id)

outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ output "master_username" {
44
}
55

66
output "master_password" {
7-
value = join("", aws_docdb_cluster.default[*].master_password)
8-
description = "Password for the master DB user"
7+
value = var.manage_master_user_password != null ? join("", aws_docdb_cluster.default[*].master_password) : null
8+
description = "Password for the master DB user. If `manage_master_user_password` is set to true, this will be set to null."
99
sensitive = true
1010
}
1111

variables.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ variable "master_password" {
9999
description = "(Required unless a snapshot_identifier is provided) Password for the master DB user. Note that this may show up in logs, and it will be stored in the state file. Please refer to the DocumentDB Naming Constraints"
100100
}
101101

102+
variable "manage_master_user_password" {
103+
type = bool
104+
description = "Whether to manage the master user password using AWS Secrets Manager."
105+
default = null
106+
validation {
107+
condition = var.manage_master_user_password == null || var.manage_master_user_password == true
108+
error_message = "Error: `manage_master_user_password` must be set to `true` or `null`"
109+
}
110+
}
111+
102112
variable "retention_period" {
103113
type = number
104114
default = 5
@@ -239,3 +249,4 @@ variable "allow_major_version_upgrade" {
239249
description = "Specifies whether major version upgrades are allowed. See https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/docdb_cluster#allow_major_version_upgrade"
240250
default = false
241251
}
252+

0 commit comments

Comments
 (0)