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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new database_secrets_mount resource #1400

Merged
merged 6 commits into from Apr 5, 2022

Conversation

benashz
Copy link
Contributor

@benashz benashz commented Apr 4, 2022

This PR introduces a new resource for provisioning multiple database secret engines under a dedicated database mount. It is the combination of vault_mount and vault_database_secret_backend_connection.

Caveats:

The vault_database_secrets_mount resource will be replaced

  • On update, a database engine block is removed
  • On update, the name is changed for any configured engine

Example usage:

resource "vault_database_secrets_mount" "db" {
  path = "db"

  mssql {
    name           = "db1"
    username       = "sa"
    password       = "super_secret_1"
    connection_url = "sqlserver://{{username}}:{{password}}@127.0.0.1:1433"
    allowed_roles = [
      "dev1",
    ]
  }

  postgresql {
    name              = "db2"
    username          = "postgres"
    password          = "super_secret_2"
    connection_url    = "postgresql://{{username}}:{{password}}@127.0.0.1:5432/postgres"
    verify_connection = true
    allowed_roles = [
      "dev2",
    ]
  }
}

resource "vault_database_secret_backend_role" "dev1" {
  name    = "dev1"
  backend = vault_database_secrets_mount.db.path
  db_name = vault_database_secrets_mount.db.mssql[0].name
  creation_statements = [
    "CREATE LOGIN [{{name}}] WITH PASSWORD = '{{password}}';",
    "CREATE USER [{{name}}] FOR LOGIN [{{name}}];",
    "GRANT SELECT ON SCHEMA::dbo TO [{{name}}];",
  ]
}

resource "vault_database_secret_backend_role" "dev2" {
  name    = "dev2"
  backend = vault_database_secrets_mount.db.path
  db_name = vault_database_secrets_mount.db.postgresql[0].name
  creation_statements = [
    "CREATE ROLE \"{{name}}\" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL '{{expiration}}';",
    "GRANT SELECT ON ALL TABLES IN SCHEMA public TO \"{{name}}\";",
  ]
}

Community Note

  • Please vote on this pull request by adding a 馃憤 reaction to the original pull request comment to help the community and maintainers prioritize this request
  • Please do not leave "+1" comments, they generate extra noise for pull request followers and do not help prioritize the request

Release note for CHANGELOG:


Output from acceptance testing:

$ make testacc TESTARGS='-v -test.run TestAccDatabaseSecretsMount_mssql*'

==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test -v -v -test.run TestAccDatabaseSecretsMount_mssql* -timeout 30m ./...

=== RUN   TestAccDatabaseSecretsMount_mssql
--- PASS: TestAccDatabaseSecretsMount_mssql (10.47s)
=== RUN   TestAccDatabaseSecretsMount_mssql_multi
--- PASS: TestAccDatabaseSecretsMount_mssql_multi (17.30s)
PASS
ok      github.com/hashicorp/terraform-provider-vault/vault     34.174s

Copy link
Contributor

@vinay-gopalan vinay-gopalan left a comment

Choose a reason for hiding this comment

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

Looking great! A few minor suggestions/queries

vault/resource_database_secrets_mount.go Outdated Show resolved Hide resolved
vault/resource_database_secrets_mount.go Show resolved Hide resolved
vault/resource_database_secrets_mount.go Show resolved Hide resolved
vault/resource_database_secrets_mount.go Outdated Show resolved Hide resolved
vault/resource_mount.go Outdated Show resolved Hide resolved
website/docs/r/database_secrets_mount.md Outdated Show resolved Hide resolved
website/docs/r/database_secrets_mount.md Show resolved Hide resolved
@@ -148,6 +148,10 @@ func (i *dbEngine) Name() string {
return i.name
Copy link
Contributor

Choose a reason for hiding this comment

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

These aren't part of your changes, but I'm wondering if we need both dbEngine.Name() and dbEngine.String(). Looks like they are duplicate functions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, String() should probably return Name(). String() implements the Stringer interface

@benashz benashz added this to the 3.5.0 milestone Apr 5, 2022
Copy link
Contributor

@vinay-gopalan vinay-gopalan left a comment

Choose a reason for hiding this comment

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

Looks good!

@benashz benashz merged commit ffb53ed into main Apr 5, 2022
@benashz benashz deleted the VAULT-5299/add-r-db-secrets-mount branch April 5, 2022 19:56
marcboudreau pushed a commit to marcboudreau/terraform-provider-vault that referenced this pull request Nov 6, 2022
Introduces new resource for provisioning multiple database secret engines
under a dedicated database mount. It is the combination of `vault_mount`
and `vault_database_secret_backend_connection` resources.
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.

None yet

2 participants