Skip to content

Commit 9126ee6

Browse files
bharathkkbmikecook
andauthored
feat!: Switch to random_password instead of random_id (#308)
* use random_password instead of random_id - random_id byte_length = 8 (integers) contains 26.6 bits of entropy. hex encoding does not change that entropy. Instead use random_password length = 32 restricted to (upper, lower, int) which contains 190.5 bits of entropy. - Restrict random_password to special = false to prevent issues with allowed characters. * upgrade guide for default pass Co-authored-by: Michael Cook <[email protected]>
1 parent 6a15c26 commit 9126ee6

File tree

5 files changed

+44
-18
lines changed

5 files changed

+44
-18
lines changed

docs/upgrading_to_sql_db_11.0.0.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ The 11.0.0 release of SQL DB is a backward incompatible release. This incompatib
44

55
## Migration Instructions
66

7+
### Add support for setting disk_autoresize_limit
8+
79
Prior to the 11.0.0 release, all instances could only be created without a limit.
810

911
```hcl
@@ -91,3 +93,23 @@ module "pg" {
9193
]
9294
}
9395
```
96+
97+
### Switched to using random_password to generate default passwords
98+
99+
With the 11.0.0 release, the `random_id` resource used to generate default passwords has been replaced with `random_password` resource. This improves the default behavior by generating stronger passwords as defaults. To continue using the previously generated password and prevent updates to the `google_sql_user` resources, specify the old password via `user_password` variable or `additional_users.password`. It is recommended to store this value in [Secret Manager](https://cloud.google.com/secret-manager/docs/creating-and-accessing-secrets#secretmanager-create-secret-gcloud) as opposed to passing it in via plain text.
100+
101+
```diff
102+
+ data "google_secret_manager_secret_version" "user_password" {
103+
+ secret_id = "pg-user-pass"
104+
+ project = var.project_id
105+
+ }
106+
107+
module "pg" {
108+
source = "GoogleCloudPlatform/sql-db/google//modules/postgresql"
109+
- version = "~> 10.0"
110+
+ version = "~> 11.0"
111+
112+
project_id = var.project_id
113+
+ user_password = data.google_secret_manager_secret_version.user_password.secret_data
114+
}
115+
```

modules/mysql/main.tf

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,23 +153,25 @@ resource "google_sql_database" "additional_databases" {
153153
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
154154
}
155155

156-
resource "random_id" "user-password" {
156+
resource "random_password" "user-password" {
157157
keepers = {
158158
name = google_sql_database_instance.default.name
159159
}
160160

161-
byte_length = 8
162-
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
161+
length = 32
162+
special = false
163+
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
163164
}
164165

165-
resource "random_id" "additional_passwords" {
166+
resource "random_password" "additional_passwords" {
166167
for_each = local.users
167168
keepers = {
168169
name = google_sql_database_instance.default.name
169170
}
170171

171-
byte_length = 8
172-
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
172+
length = 32
173+
special = false
174+
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
173175
}
174176

175177
resource "google_sql_user" "default" {
@@ -178,7 +180,7 @@ resource "google_sql_user" "default" {
178180
project = var.project_id
179181
instance = google_sql_database_instance.default.name
180182
host = var.user_host
181-
password = var.user_password == "" ? random_id.user-password.hex : var.user_password
183+
password = var.user_password == "" ? random_password.user-password.result : var.user_password
182184
depends_on = [
183185
null_resource.module_depends_on,
184186
google_sql_database_instance.default,
@@ -190,7 +192,7 @@ resource "google_sql_user" "additional_users" {
190192
for_each = local.users
191193
project = var.project_id
192194
name = each.value.name
193-
password = lookup(each.value, "password", random_id.user-password.hex)
195+
password = lookup(each.value, "password", random_password.user-password.result)
194196
host = lookup(each.value, "host", var.user_host)
195197
instance = google_sql_database_instance.default.name
196198
type = lookup(each.value, "type", "BUILT_IN")

modules/mysql/outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ output "read_replica_instance_names" {
8888

8989
output "generated_user_password" {
9090
description = "The auto generated default user password if not input password was provided"
91-
value = random_id.user-password.hex
91+
value = random_password.user-password.result
9292
sensitive = true
9393
}
9494

modules/postgresql/main.tf

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,31 +164,33 @@ resource "google_sql_database" "additional_databases" {
164164
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
165165
}
166166

167-
resource "random_id" "user-password" {
167+
resource "random_password" "user-password" {
168168
keepers = {
169169
name = google_sql_database_instance.default.name
170170
}
171171

172-
byte_length = 8
173-
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
172+
length = 32
173+
special = false
174+
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
174175
}
175176

176-
resource "random_id" "additional_passwords" {
177+
resource "random_password" "additional_passwords" {
177178
for_each = local.users
178179
keepers = {
179180
name = google_sql_database_instance.default.name
180181
}
181182

182-
byte_length = 8
183-
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
183+
length = 32
184+
special = false
185+
depends_on = [null_resource.module_depends_on, google_sql_database_instance.default]
184186
}
185187

186188
resource "google_sql_user" "default" {
187189
count = var.enable_default_user ? 1 : 0
188190
name = var.user_name
189191
project = var.project_id
190192
instance = google_sql_database_instance.default.name
191-
password = var.user_password == "" ? random_id.user-password.hex : var.user_password
193+
password = var.user_password == "" ? random_password.user-password.result : var.user_password
192194
depends_on = [
193195
null_resource.module_depends_on,
194196
google_sql_database_instance.default,
@@ -200,7 +202,7 @@ resource "google_sql_user" "additional_users" {
200202
for_each = local.users
201203
project = var.project_id
202204
name = each.value.name
203-
password = coalesce(each.value["password"], random_id.additional_passwords[each.value.name].hex)
205+
password = coalesce(each.value["password"], random_password.additional_passwords[each.value.name].result)
204206
instance = google_sql_database_instance.default.name
205207
depends_on = [
206208
null_resource.module_depends_on,

modules/postgresql/outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ output "read_replica_instance_names" {
9393

9494
output "generated_user_password" {
9595
description = "The auto generated default user password if not input password was provided"
96-
value = random_id.user-password.hex
96+
value = random_password.user-password.result
9797
sensitive = true
9898
}
9999

0 commit comments

Comments
 (0)