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

Fix DB engine password overwrite #1876

Merged
merged 7 commits into from
Jun 6, 2023
Merged

Conversation

vinay-gopalan
Copy link
Contributor

Adds a fix to only update the password if it is a new resource or if there is a change. This fix helps the provider maintain state even when an external rotate root command has been initiated for the DB connection's password

PreConfig: func() {
client := testProvider.Meta().(*provider.ProviderMeta).GetClient()
rotateRootPath := fmt.Sprintf("%s/rotate-root/%s", backend, name)
resp, err := client.Logical().Write(rotateRootPath, nil)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The rotate root does not seem to be working as expected. At present this test passes even when it should fail since no root rotation is actually taking place. Looking into this a bit more

@vinay-gopalan vinay-gopalan marked this pull request as ready for review May 26, 2023 20:54
@vinay-gopalan vinay-gopalan requested review from a team and fairclothjm May 26, 2023 20:54
// if it actually changed to still support updating it for non-rotated cases.
passwordKey := prefix + consts.FieldPassword
if v, ok := d.GetOk(passwordKey); ok {
if d.IsNewResource() || d.HasChange(passwordKey) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The first d.IsNewResource case might be redundant. Will double check and remove if it is so

backend = vault_mount.db.path
name = "%s"
allowed_roles = ["dev", "prod"]
root_rotation_statements = []
Copy link
Contributor

Choose a reason for hiding this comment

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

@vinay-gopalan previously this was [""], which caused Vault to interpret it as a non empty list of statements, so instead of defaulting to the statement ALTER ROLE "{{username}}" WITH PASSWORD '{{password}}';, it just skipped it entirely. That's the reason why the Postgres password never got updated, even though it was changed in Vault.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah interesting! Thanks for catching that and whoops 😅

MaybeSkipDBTests(t, dbEnginePostgres)

username := "postgres"
password := "secret"
Copy link
Contributor

Choose a reason for hiding this comment

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

since there's already a Postgres container running in CI, I've updated the test to use that existing configuration instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wouldn't that cause the Postgres container in CI to have it's credentials rotated? Thereby causing our other tests that use that same container to fail?

My thought was to have this test be a local only test where we will have to set up a Docker container locally — with the knowledge that the container's password will get rotated out after and it won't be usable after this test. The idea would be the container is only set up for that test and then destroyed once it's no longer usable by other tests

Curious to hear thoughts from folks! I don't think we have any rotate root tests in CI currently so this might be new areas that might need to be documented.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I have it noted as a comment here.

This would have to be revisited if we introduced new tests that also relied on the same PG container.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll see if additional reruns causes any flakiness, and if so, I'll skip this test in CI.

Copy link
Contributor

Choose a reason for hiding this comment

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

Alternatively we can introduce https://github.com/ory/dockertest, which it looks like we're already using in Vault.

@@ -56,3 +56,11 @@ services:
- LDAP_ADMIN_PASSWORD=adminpassword
- LDAP_USERS=alice,bob,foo
- LDAP_PASSWORDS=password1,password2,password3

postgres:
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need an entirely new container for this?

Can we just reuse the existing mysql container for this and the existing rotate root test TestAccDatabaseSecretBackendConnectionTemplatedUpdateExcludePassword_mysql? If we do end up reusing that test can we just assert that the user and password are not changed in the TF state after the rotate root operation?

Copy link
Contributor

Choose a reason for hiding this comment

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

That seems reasonable to use mysql to test this change. I'm fine either way, but leaning towards a new unit test to make it obvious what we're testing for. @vinay-gopalan thoughts? I know you had originally wrote the test for Postgres.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't want this comment to block progress. So feel free to disregard if you see fit. However, it seems to me that the TestAccDatabaseSecretBackendConnectionTemplatedUpdateExcludePassword_mysql test is intended to test this exact scenario but it fails to assert that the password is unchanged in the state after the rotate root operation.

Copy link
Contributor

Choose a reason for hiding this comment

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

But, presumably, TestAccDatabaseSecretBackendConnectionTemplatedUpdateExcludePassword_mysql would have failed that assertion before this change so maybe I am wrong there.

@github-actions github-actions bot added size/S and removed size/M labels Jun 6, 2023
@github-actions github-actions bot added size/XS and removed size/S labels Jun 6, 2023
Copy link
Contributor

@fairclothjm fairclothjm left a comment

Choose a reason for hiding this comment

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

LGTM! Just a suggestion on an additional test step.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants