Skip to content

Commit 88b6546

Browse files
committed
feat: Add support for write-only password
1 parent 1946cb6 commit 88b6546

File tree

7 files changed

+80
-2
lines changed

7 files changed

+80
-2
lines changed

examples/complete-postgres/main.tf

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,54 @@ module "db_disabled" {
147147
create_db_option_group = false
148148
}
149149

150+
ephemeral "random_password" "db_password" {
151+
length = 16
152+
override_special = "!#$%&*()-_=+[]{}<>:?"
153+
}
154+
155+
resource "aws_secretsmanager_secret" "db_password" {
156+
name = "db_password"
157+
}
158+
159+
resource "aws_secretsmanager_secret_version" "db_password" {
160+
secret_id = aws_secretsmanager_secret.db_password.id
161+
secret_string_wo = ephemeral.random_password.db_password.result
162+
secret_string_wo_version = 1
163+
}
164+
165+
ephemeral "aws_secretsmanager_secret_version" "db_password" {
166+
secret_id = aws_secretsmanager_secret_version.db_password.secret_id
167+
}
168+
169+
module "db_write_only" {
170+
source = "../../"
171+
172+
identifier = local.name
173+
174+
# All available versions: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_PostgreSQL.html#PostgreSQL.Concepts
175+
engine = "postgres"
176+
engine_version = "14"
177+
engine_lifecycle_support = "open-source-rds-extended-support-disabled"
178+
family = "postgres14" # DB parameter group
179+
major_engine_version = "14" # DB option group
180+
instance_class = "db.t4g.large"
181+
182+
# NOTE: Do NOT use 'user' as the value for 'username' as it throws:
183+
# "Error creating DB Instance: InvalidParameterValue: MasterUsername
184+
# user cannot be used as it is a reserved word used by the engine"
185+
db_name = "writeOnlyPostgresql"
186+
username = "write_only_postgresql"
187+
port = 5432
188+
189+
password_wo = ephemeral.aws_secretsmanager_secret_version.db_password.secret_string
190+
password_wo_version = aws_secretsmanager_secret_version.db_password.secret_string_wo_version
191+
manage_master_user_password = false
192+
193+
multi_az = false
194+
db_subnet_group_name = module.vpc.database_subnet_group
195+
vpc_security_group_ids = [module.security_group.security_group_id]
196+
}
197+
150198
################################################################################
151199
# RDS Automated Backups Replication Module
152200
################################################################################

main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ module "db_instance" {
7878
db_name = var.db_name
7979
username = var.username
8080
password = var.manage_master_user_password ? null : var.password
81+
password_wo = !var.manage_master_user_password && var.password == null ? var.password_wo : null
82+
password_wo_version = var.password_wo_version
8183
port = var.port
8284
domain = var.domain
8385
domain_auth_secret_arn = var.domain_auth_secret_arn

modules/db_instance/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ resource "aws_db_instance" "this" {
4545
db_name = var.db_name
4646
username = !local.is_replica ? var.username : null
4747
password = !local.is_replica && var.manage_master_user_password ? null : var.password
48+
password_wo = !local.is_replica && !var.manage_master_user_password && var.password == null ? var.password_wo : null
49+
password_wo_version = var.password_wo_version
4850
port = var.port
4951
domain = var.domain
5052
domain_auth_secret_arn = var.domain_auth_secret_arn

modules/db_instance/variables.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,19 @@ variable "password" {
152152
default = null
153153
}
154154

155+
variable "password_wo" {
156+
description = "Write-Only password for the master DB user."
157+
type = string
158+
default = null
159+
ephemeral = true
160+
}
161+
162+
variable "password_wo_version" {
163+
description = "Used together with password_wo to trigger an update. Increment this value when an update to password_wo is required."
164+
type = number
165+
default = null
166+
}
167+
155168
variable "manage_master_user_password" {
156169
description = "Set to true to allow RDS to manage the master user password in Secrets Manager. Cannot be set if password is provided"
157170
type = bool

modules/db_instance/versions.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.0"
2+
required_version = ">= 1.11"
33

44
required_providers {
55
aws = {

variables.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,19 @@ variable "password" {
175175
sensitive = true
176176
}
177177

178+
variable "password_wo" {
179+
description = "Write-Only password for the master DB user. Password will only be used when `manage_master_user_password` is not set to true and `password` is not set."
180+
type = string
181+
default = null
182+
ephemeral = true
183+
}
184+
185+
variable "password_wo_version" {
186+
description = "Used together with password_wo to trigger an update. Increment this value when an update to password_wo is required."
187+
type = number
188+
default = null
189+
}
190+
178191
variable "manage_master_user_password" {
179192
description = "Set to true to allow RDS to manage the master user password in Secrets Manager"
180193
type = bool

versions.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.0"
2+
required_version = ">= 1.11"
33

44
required_providers {
55
aws = {

0 commit comments

Comments
 (0)