hashicorp / terraform-provider-mysql

Terraform MySQL provider – This Terraform provider is archived per our provider archiving process: https://terraform.io/docs/internals/archiving.html
https://www.terraform.io/docs/providers/mysql/
Mozilla Public License 2.0
61 stars 189 forks source link

Add the mysql_user_password resource with support for Keybase/PGP encryption #30

Closed joestump closed 6 years ago

joestump commented 6 years ago

Attached is a mysql_user_password resource for managing MySQL passwords safely with Terraform. It can be used to assign auto-generated passwords and rotate them. Passwords are stored using PGP encryption in the TF state file.

resource "mysql_user_password" "someone" {
  user    = "${mysql_user.someone.user}"
  pgp_key = "keybase:someuser"
}

You can then output the encrypted_password attribute and decrypt:

terraform output encypted_password | base64 --decode | gpg --decrypt

To rotate the password:

terraform taint mysql_user_password.someone

Here's the main.tf I used for testing:

provider "mysql" {
  endpoint = "localhost:3306"
  username = "root"
}

resource "mysql_user" "jstump" {
  user = "jstump"
}

resource "mysql_user_password" "jstump" {
  user = "${mysql_user.jstump.user}"
  pgp_key = "keybase:joestump"
}

output "encypted_password" {
  value = "${mysql_user_password.jstump.encrypted_password}"
}

Output of make testacc:

$ MYSQL_USERNAME=root MYSQL_ENDPOINT=localhost:3306 MYSQL_PASSWORD='' make testacc
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test $(go list ./... |grep -v 'vendor') -v  -timeout 120m
?       github.com/terraform-providers/terraform-provider-mysql [no test files]
=== RUN   TestProvider
--- PASS: TestProvider (0.00s)
=== RUN   TestProvider_impl
--- PASS: TestProvider_impl (0.00s)
=== RUN   TestAccDatabase
--- PASS: TestAccDatabase (0.04s)
=== RUN   TestAccGrant
--- PASS: TestAccGrant (0.05s)
=== RUN   TestAccUserPassword_basic
--- PASS: TestAccUserPassword_basic (0.94s)
=== RUN   TestAccUser_basic
--- PASS: TestAccUser_basic (0.07s)
=== RUN   TestAccUser_deprecated
--- PASS: TestAccUser_deprecated (0.07s)
PASS
ok      github.com/terraform-providers/terraform-provider-mysql/mysql   1.204s
joestump commented 6 years ago

After much flailing, I got govendor to Do The Thing. Tests pass now and keybase:username works now as well.

To decrypt with Keybase:

terraform output encypted_password | base64 --decode | keybase pgp decrypt
joestump commented 6 years ago

@apparentlymart docs are done. I think this is ready for merge. I might take a stab at imports for users and DBs later. 👍

willejs commented 6 years ago

@joestump this is great! 🎉 😸 @vancluever What do you think of this? Do you think it can get merged?

willejs commented 6 years ago

@thomaschaaf can you approve?

joestump commented 6 years ago

@thomaschaaf @willejs @vancluever anything I can do to help move this PR along? #34 looks like it's ready for merge as well.

vancluever commented 6 years ago

Hey @joestump, very sorry for the radio silence on this one.

I just took a look, and there's a couple of things off the bat that I can see:

Let me know!

joestump commented 6 years ago

@vancluever added a basic acceptance test. Any help on cleaning up dependencies would be greatly appreciated. Thanks!

davewongillies commented 6 years ago

Sorry to be that guy, but @vancluever, I'd love to see this merged

joestump commented 6 years ago

I just got added as a maintainer on this. I've got some cleanup work to do before this will get merged in. More on this soon!

joestump commented 6 years ago

Closing this out in favor of #47, which is based on the go mod dependency management added in PR #44.