Skip to content

Commit 94ef3de

Browse files
authored
feat: Create random passwords for additional_users (#236)
* Creates random passwords for additional_users For every Cloud SQL instance type, if `additional_users.password` it's an empty string, the module will create a random password. Also, adds an output to retrieve users and passwords from other resources. * Updates README.md * Adds missing key on mssql module * Fixes key on MSSQL module * Updates outputs for every module
1 parent 79f5988 commit 94ef3de

File tree

9 files changed

+65
-2
lines changed

9 files changed

+65
-2
lines changed

modules/mssql/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ The following dependency must be available for SQL Server module:
4848

4949
| Name | Description |
5050
|------|-------------|
51+
| additional\_users | List of maps of additional users and passwords |
5152
| generated\_user\_password | The auto generated default user password if not input password was provided |
5253
| instance\_address | The IPv4 addesses assigned for the master instance |
5354
| instance\_connection\_name | The connection name of the master instance to be used in connection strings |

modules/mssql/main.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ resource "random_password" "user-password" {
155155
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
156156
}
157157

158+
resource "random_password" "additional_passwords" {
159+
for_each = local.users
160+
length = 8
161+
special = true
162+
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
163+
}
164+
158165
resource "google_sql_user" "default" {
159166
name = var.user_name
160167
project = var.project_id
@@ -167,7 +174,7 @@ resource "google_sql_user" "additional_users" {
167174
for_each = local.users
168175
project = var.project_id
169176
name = each.value.name
170-
password = lookup(each.value, "password", random_password.user-password.result)
177+
password = lookup(each.value, "password", random_password.additional_passwords[each.value.name].result)
171178
instance = google_sql_database_instance.default.name
172179
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
173180
}

modules/mssql/outputs.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ output "generated_user_password" {
6161
sensitive = true
6262
}
6363

64+
output "additional_users" {
65+
description = "List of maps of additional users and passwords"
66+
value = [for r in google_sql_user.additional_users :
67+
{
68+
name = r.name
69+
password = r.password
70+
}
71+
]
72+
sensitive = true
73+
}
74+
6475
output "root_password" {
6576
description = "MSSERVER password for the root user. If not set, a random one will be generated and available in the root_password output variable."
6677
value = coalesce(var.root_password, random_password.root-password.result)

modules/mysql/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Note: CloudSQL provides [disk autoresize](https://cloud.google.com/sql/docs/mysq
5151

5252
| Name | Description |
5353
|------|-------------|
54+
| additional\_users | List of maps of additional users and passwords |
5455
| generated\_user\_password | The auto generated default user password if not input password was provided |
5556
| instance\_connection\_name | The connection name of the master instance to be used in connection strings |
5657
| instance\_first\_ip\_address | The first IPv4 address of the addresses assigned for the master instance. |

modules/mysql/main.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,16 @@ resource "random_id" "user-password" {
160160
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
161161
}
162162

163+
resource "random_id" "additional_passwords" {
164+
for_each = local.users
165+
keepers = {
166+
name = google_sql_database_instance.default.name
167+
}
168+
169+
byte_length = 8
170+
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
171+
}
172+
163173
resource "google_sql_user" "default" {
164174
count = var.enable_default_user ? 1 : 0
165175
name = var.user_name

modules/mysql/outputs.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ output "generated_user_password" {
9292
sensitive = true
9393
}
9494

95+
output "additional_users" {
96+
description = "List of maps of additional users and passwords"
97+
value = [for r in google_sql_user.additional_users :
98+
{
99+
name = r.name
100+
password = r.password
101+
}
102+
]
103+
sensitive = true
104+
}
105+
95106
output "public_ip_address" {
96107
description = "The first public (PRIMARY) IPv4 address assigned for the master instance"
97108
value = google_sql_database_instance.default.public_ip_address

modules/postgresql/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Note: CloudSQL provides [disk autoresize](https://cloud.google.com/sql/docs/mysq
5252

5353
| Name | Description |
5454
|------|-------------|
55+
| additional\_users | List of maps of additional users and passwords |
5556
| generated\_user\_password | The auto generated default user password if not input password was provided |
5657
| instance\_connection\_name | The connection name of the master instance to be used in connection strings |
5758
| instance\_first\_ip\_address | The first IPv4 address of the addresses assigned. |

modules/postgresql/main.tf

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@ resource "random_id" "user-password" {
171171
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
172172
}
173173

174+
resource "random_id" "additional_passwords" {
175+
for_each = local.users
176+
keepers = {
177+
name = google_sql_database_instance.default.name
178+
}
179+
180+
byte_length = 8
181+
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
182+
}
183+
174184
resource "google_sql_user" "default" {
175185
count = var.enable_default_user ? 1 : 0
176186
name = var.user_name
@@ -184,7 +194,7 @@ resource "google_sql_user" "additional_users" {
184194
for_each = local.users
185195
project = var.project_id
186196
name = each.value.name
187-
password = coalesce(each.value["password"], random_id.user-password.hex)
197+
password = coalesce(each.value["password"], random_id.additional_passwords[each.value.name].hex)
188198
instance = google_sql_database_instance.default.name
189199
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
190200
}

modules/postgresql/outputs.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ output "generated_user_password" {
9797
sensitive = true
9898
}
9999

100+
output "additional_users" {
101+
description = "List of maps of additional users and passwords"
102+
value = [for r in google_sql_user.additional_users :
103+
{
104+
name = r.name
105+
password = r.password
106+
}
107+
]
108+
sensitive = true
109+
}
110+
100111
// Resources
101112
output "primary" {
102113
value = google_sql_database_instance.default

0 commit comments

Comments
 (0)