Create random passwords only when needed, use write-only attribute for passwords
This commit is contained in:
committed by
Wiktor Niesiobędzki
parent
e6ec5de733
commit
aecc4d53b9
@@ -438,7 +438,7 @@ module "db" {
|
||||
| [ssl](variables.tf#L265) | Setting to enable SSL, set config and certificates. | <code title="object({ client_certificates = optional(list(string)) mode = optional(string) })">object({…})</code> | | <code>{}</code> |
|
||||
| [terraform_deletion_protection](variables.tf#L280) | Prevent terraform from deleting instances. | <code>bool</code> | | <code>true</code> |
|
||||
| [time_zone](variables.tf#L292) | The time_zone to be used by the database engine (supported only for SQL Server), in SQL Server timezone format. | <code>string</code> | | <code>null</code> |
|
||||
| [users](variables.tf#L298) | Map of users to create in the primary instance (and replicated to other replicas). For MySQL, anything after the first `@` (if present) will be used as the user's host. Set PASSWORD to null if you want to get an autogenerated password. The user types available are: 'BUILT_IN', 'CLOUD_IAM_USER' or 'CLOUD_IAM_SERVICE_ACCOUNT'. | <code title="map(object({ password = optional(string) type = optional(string) }))">map(object({…}))</code> | | <code>null</code> |
|
||||
| [users](variables.tf#L298) | Map of users to create in the primary instance (and replicated to other replicas). For MySQL, anything after the first `@` (if present) will be used as the user's host. Set PASSWORD to null if you want to get an autogenerated password. The user types available are: 'BUILT_IN', 'CLOUD_IAM_USER' or 'CLOUD_IAM_SERVICE_ACCOUNT'. | <code title="map(object({ password = optional(string) password_version = optional(number) type = optional(string, "BUILT_IN") }))">map(object({…}))</code> | | <code>{}</code> |
|
||||
|
||||
## Outputs
|
||||
|
||||
|
||||
@@ -21,19 +21,19 @@ locals {
|
||||
has_replicas = length(var.replicas) > 0
|
||||
is_regional = var.availability_type == "REGIONAL" ? true : false
|
||||
users = {
|
||||
for k, v in coalesce(var.users, {}) : k =>
|
||||
for k, v in var.users : k =>
|
||||
local.is_mysql
|
||||
? {
|
||||
name = coalesce(v.type, "BUILT_IN") == "BUILT_IN" ? split("@", k)[0] : k
|
||||
host = coalesce(v.type, "BUILT_IN") == "BUILT_IN" ? try(split("@", k)[1], null) : null
|
||||
password = coalesce(v.type, "BUILT_IN") == "BUILT_IN" ? try(random_password.passwords[k].result, v.password) : null
|
||||
type = coalesce(v.type, "BUILT_IN")
|
||||
name = v.type == "BUILT_IN" ? split("@", k)[0] : k
|
||||
host = v.type == "BUILT_IN" ? try(split("@", k)[1], null) : null
|
||||
password = v.type == "BUILT_IN" ? try(random_password.passwords[k].result, v.password) : null
|
||||
type = v.type
|
||||
}
|
||||
: {
|
||||
name = local.is_postgres ? try(trimsuffix(k, ".gserviceaccount.com"), k) : k
|
||||
host = null
|
||||
password = coalesce(v.type, "BUILT_IN") == "BUILT_IN" ? try(random_password.passwords[k].result, v.password) : null
|
||||
type = coalesce(v.type, "BUILT_IN")
|
||||
password = v.type == "BUILT_IN" ? try(random_password.passwords[k].result, v.password) : null
|
||||
type = v.type
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -285,9 +285,9 @@ resource "google_sql_database" "databases" {
|
||||
|
||||
resource "random_password" "passwords" {
|
||||
for_each = toset([
|
||||
for k, v in coalesce(var.users, {}) :
|
||||
for k, v in var.users :
|
||||
k
|
||||
if v.password == null
|
||||
if v.password == null && v.type == "BUILT_IN"
|
||||
])
|
||||
length = try(var.password_validation_policy.min_length, 16)
|
||||
special = true
|
||||
|
||||
@@ -127,8 +127,10 @@ output "self_links" {
|
||||
output "user_passwords" {
|
||||
description = "Map of containing the password of all users created through terraform."
|
||||
value = {
|
||||
for name, user in google_sql_user.users :
|
||||
name => user.password
|
||||
for k, v in local.users : k => v.password
|
||||
}
|
||||
sensitive = true
|
||||
depends_on = [
|
||||
google_sql_user.users
|
||||
]
|
||||
}
|
||||
|
||||
@@ -298,8 +298,10 @@ variable "time_zone" {
|
||||
variable "users" {
|
||||
description = "Map of users to create in the primary instance (and replicated to other replicas). For MySQL, anything after the first `@` (if present) will be used as the user's host. Set PASSWORD to null if you want to get an autogenerated password. The user types available are: 'BUILT_IN', 'CLOUD_IAM_USER' or 'CLOUD_IAM_SERVICE_ACCOUNT'."
|
||||
type = map(object({
|
||||
password = optional(string)
|
||||
type = optional(string)
|
||||
password = optional(string)
|
||||
password_version = optional(number)
|
||||
type = optional(string, "BUILT_IN")
|
||||
}))
|
||||
default = null
|
||||
default = {}
|
||||
nullable = false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user