Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase the default complexity of Cloud SQL DB passwords #2886

Conversation

lyricnz
Copy link
Contributor

@lyricnz lyricnz commented Feb 13, 2025

Per #2885

NOTE: this changes the type of root_password variable from a string (default null) to an object with password (same behaviour as old string) and random_password attributes (override password with a randomly-generated password). if value was not previously provided, default behaviour is the same.


Checklist

I applicable, I acknowledge that I have:

  • Read the contributing guide
  • Ran terraform fmt on all modified files
  • Regenerated the relevant README.md files using tools/tfdoc.py
  • Made sure all relevant tests pass

@lyricnz
Copy link
Contributor Author

lyricnz commented Feb 13, 2025

Added code to explicitly generate a root_password also (using the same logic). Otherwise the DB fails to create because the default (Google generated) password doesn't match the password policy.

As mentioned in hashicorp/terraform-provider-google#17265

@lyricnz
Copy link
Contributor Author

lyricnz commented Feb 13, 2025

Added code to explicitly generate a root_password also (using the same logic). Otherwise the DB fails to create because the default (Google generated) password doesn't match the password policy.

Maybe the module shouldn't do this? And rely on the caller passing a valid root_password, rather than generating it here? (but that's inconsistent with the pattern for non-root users, which do generate passwords if not provided)

@lyricnz
Copy link
Contributor Author

lyricnz commented Feb 17, 2025

Changed the root_password input to an object, as suggested. Here is the results of my testing with this code (the first case is the original bug #2885)

DB with no root_password config

Input:

module "db-none" {
  source     = "SOMEPLACE/lyricnz/cloud-foundation-fabric/modules/cloudsql-instance" # this PR
  # no root_password config
  password_validation_policy = {
    enabled                     = true
    default_complexity          = true
    disallow_username_substring = true
    min_length                  = 20
    reuse_interval              = 5
  }
}

Output:

Fails to create: googleapi: Error 400: Invalid request: Root user's password is not policy compliant: Length of password cannot be less than 20. Password does not meet the complexity criteria. ., invalid

DB with explicit password (too short)

Input:

module "db-explicit-short" {
  root_password = {
    password = "xxx"
  }
  password_validation_policy = { ... as above ... }
}

Output:

Fails to create: googleapi: Error 400: Invalid request: Root user's password is not policy compliant: Length of password cannot be less than 20. ., invalid

DB with explicit password (OK)

Input:

module "db-explicit" {
  root_password = {
    password = "Qy[E7N&Jv49u[F9jf3Ec"
  }
  password_validation_policy = { ... as above ... }
}

Output:

$ terraform state show -show-sensitive module.db-explicit.google_sql_database_instance.primary | grep root
    root_password                  = "Qy[E7N&Jv49u[F9jf3Ec"

DB with random_password

Input:

module "db-random" {
  root_password = {
    random_password = true
  }
  password_validation_policy = { ... as above ... }
}

Output:

$ terraform state show -show-sensitive module.db-random.google_sql_database_instance.primary | grep root
    root_password                  = "K*7bTarLtA-1!W=KK@$c"

DB with both explicit password and random_password (override)

Input:

module "db-override" {
  root_password = {
    password = "Qy[E7N&Jv49u[F9jf3Ec"
    random_password = true
  }
  password_validation_policy = { ... as above ... }
}

Output:

$ terraform state show -show-sensitive module.db-override.google_sql_database_instance.primary | grep root
    root_password                  = "FZ1F4aQ3&qVKM5B<tUrx"

@ludoo
Copy link
Collaborator

ludoo commented Feb 17, 2025

This is great, thanks for taking the time to run the changes. Can I ask you one last effort

  • check the examples in the README to ensure the cases above are explained (if they are missing, a simple paragraph or code block is all we need)
  • check that everything works without setting password validation

…g both a password and `random_password`.

Fix test for stronger password generation.
@lyricnz
Copy link
Contributor Author

lyricnz commented Feb 18, 2025

I tested with no password_policy:

module "db-none" {
  source     = "../modules/cloudsql-instance"
  name       = "test-db-no-root-password"
}

module "db-no-policy-password" {
  source     = "../modules/cloudsql-instance"
  name       = "test-db-no-policy-password"
  root_password = {
    password = "Qy[E7N&Jv49u[F9jf3Ec"
  }
}

module "db-no-policy-random" {
  source     = "../modules/cloudsql-instance"
  root_password = {
    random_password = true
  }
}

They all appeared to work fine. The validation caught the following error:

module "db-both" {
  source     = "../modules/cloudsql-instance"
  root_password = {
    password = "xyzzy"
    random_password = true
  }
}

error

│   on main.tf line 93, in module "db-both":
│   93:   root_password = {
│   94:     password = "xyzzy"
│   95:     random_password = true
│   96:   }
│     ├────────────────
│     │ var.root_password.password is "xyzzy"
│     │ var.root_password.random_password is true
│
│ Cannot provide root_password.password and root_password.random_password at the same time
│
│ This was checked by the validation rule at ../modules/cloudsql-instance/variables.tf:259,3-13.

Copy link
Collaborator

@juliocc juliocc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Thanks @lyricnz!

@juliocc juliocc enabled auto-merge (squash) February 18, 2025 10:33
@juliocc juliocc merged commit 642ebfe into GoogleCloudPlatform:master Feb 18, 2025
14 checks passed
@lyricnz lyricnz deleted the feature/cloud-sql-password-complexity branch February 18, 2025 21:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants