Skip to content

Add postgres user credential reset to terraform provider #1120

@mardonner

Description

@mardonner

Problem description

I recently noticed that the portal allows to reset the credentials of users on my PostgreSQL Flex instances. Probably via this API call. It would be awesome to have this feature in the terraform provider. I'm not sure if this is possible however.

Proposed solution

In the case of my Secrets Manager, there is no password reset. I rotate the credentials by deleting and recreating the user on it. This works fine for me. My pipeline deals with passing those new credentials to my application.

Rough example:

resource "null_resource" "credential_rotation_trigger" {
  triggers = {
    rotate = var.rotate_credentials ? timestamp() : ""
  }
}
resource "stackit_secretsmanager_instance" "sm_cluster" {
  count      = var.sm_enabled ? 1 : 0
  project_id = var.stackit_project_id
  name = local.sm_cluster_name
  acls = local.acls_sm
}

resource "stackit_secretsmanager_user" "sm_cluster_user_ro" {
  count         = var.sm_enabled ? 1 : 0
  project_id    = var.stackit_project_id
  instance_id   = stackit_secretsmanager_instance.sm_cluster[0].instance_id
  description   = local.sm_cluster_ro_user_description
  write_enabled = false
  lifecycle {
    create_before_destroy = true
    replace_triggered_by  = [null_resource.credential_rotation_trigger]
  }
}

var.rotate_credentials is an input parameter in my pipeline dialogue so it is a one off task and don't have to set it in my .tfvars file and then remember to change it back.

For Postgres users this is another story. I can't use this mechanism for multiple reasons.

  1. Deleting the user is not possible if other objects on the database (outside of the terraform provider) depend on it. (e.g. schemas, etc.). Trying to do so results in a 500 error response from the api.
  2. Even if this is not the case, since user and database (not instance) resource are dependent on each other, when adopting the mechanism above, I'd also need to delete the database resource in order to create a new user with new credentials. I can't just drop my database.
  3. I don't know if its possible for the user to change the password on the instance by themselves via psql command but let's say this is possible. Are the API/Portal aware of this change and does the terraform provider also respond accordingly to that change? I mean regarding state drift.
    Also, while we're at it. I assume, users created with createrole (Remove the role validation for Postgres #1116), outside of the portal/terraform will not appear in the portal, correct?

A clear and concise description of what you want to happen.

Again, I don't know if this is possible and don't know how an implementation would look like, but maybe something like this?

resource "stackit_postgresflex_user" "example" {
  project_id  = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  username    = "username"
  roles       = ["role"]
  reset_password = var.rotate_credentials
}
variable "rotate_credentials" {
  description = "whether to rotate credentials"
  type        = bool
  default     = false
}

The resource should then not track the attribute reset_password in the state file, so the rotation is always and only triggered if reset_password is true. In the example above, if rotate_credentials is set to true, subsequent plan runs should always report a change on the password and uri attribute.

Alternative solutions (optional)

I'd prefer to do this in terraform instead of manually in the portal. Manual doesn't scale.

Additional information

EDIT: I just noticed that a credential reset in the portal is not recognized by terraform. So if you do that, any resource/provider/output will not be aware that the password changed and can now no longer connect to the db. Just a fact to be aware of.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions