# Manage a login

Set or remove a login's expiry, rotate its password, or revoke it.

Replace _ewdb-0123456789ab_ with your database's id and _login-id_ with the login's id from
`rds.logins.list`.

## Prerequisites

- `rds:ModifyDBLogin` for the expiry, `rds:RotateDBLoginPassword` to rotate, `rds:DeleteDBLogin` to
  revoke; `rds:DescribeDBLogins` to see the **Logins** tab.
- The database is **available**. Otherwise the console's buttons are disabled and the API answers
  409 `This database is <status>. Logins can be changed once it is available.`
- In the console, also `rds:DescribeDBInstances` to open the database's page (without it the page is
  not found).

## Set or remove the expiry

**Console**
1. Open the database, then choose **Logins**.
2. On the login's row, choose **Expiry**.
3. Choose **1 day**, **7 days**, **30 days** (the default), **90 days** or **1 year**, then
   **Set expiry**. On a login that has an expiry, **Remove expiry** clears it.
   An exact date is set through the CLI, the API or Terraform.

**CLI**
```sh
dcloud rds logins modify ewdb-0123456789ab login-id --valid-until 2027-03-31T18:00:00Z
```

**API**
```http
PATCH /rds/api/instances/ewdb-0123456789ab/logins/3f8b6c2e-1d4a-4b7e-9c2f-5a6b7c8d9e0f
{ "validUntil": "2027-03-31T18:00:00Z" }
```

`null` removes the expiry.

**Terraform**
Set `valid_until` on the `dcloud_db_login` resource.

The expiry is the role's `VALID UNTIL`, any time from five minutes to two years ahead. After that
time Postgres refuses the password on both ports. Sessions that are still open are ended within
five minutes. An expiry outside that window is refused with 400
`The expiry must be at least five minutes from now.` or
`The expiry can be at most two years from now.`; to cut someone off now, revoke the login. An
expired login stays listed, marked expired, until you revoke it or give it a new expiry.

## Rotate the password

**Console**
1. On the login's row, choose **Rotate**.
2. Choose **Rotate password**, or **Rotate and end sessions** to also end the sessions the old
   password opened.

**CLI**
```sh
dcloud rds logins rotate-password ewdb-0123456789ab login-id --end-sessions
```

**API**
```http
POST /rds/api/instances/ewdb-0123456789ab/logins/3f8b6c2e-1d4a-4b7e-9c2f-5a6b7c8d9e0f/rotate
{ "endSessions": false }
```

**Terraform:** not available. Rotation is an action, not configuration.

The new password is filed as a new version of the same secret. The person who could reveal the old
password reveals the new one. The old password is refused at the next connection, and sessions it
opened keep running unless you end them. If the database refuses the new password, the secret goes
back to the previous version and the old password keeps working.

## Revoke the login

**Console**
1. On the login's row, choose **Revoke**.
2. Choose **Revoke** to confirm.

**CLI**
```sh
dcloud rds logins delete ewdb-0123456789ab login-id
```

**API**
```http
DELETE /rds/api/instances/ewdb-0123456789ab/logins/3f8b6c2e-1d4a-4b7e-9c2f-5a6b7c8d9e0f
```

**Terraform**
Remove the `dcloud_db_login` resource and run `terraform apply`.

## Result

Revoking ends the login's open sessions, drops its role (anything it owns in the database's own
database goes to the master user) and deletes its secret. A role already gone from the server is
removed from the list with a warning. Every change is on **Activity** (phase `logins`).

## If a change is refused

- 409 `The role “alice” is not on the server any more. Revoke the login and create it again.`
  (expiry or rotation).
- 409 `The role “alice” on the server is not the one this login created, so it was left alone.`
- 409 `The organization has no enabled encryption key, so the new password could not be filed in Secrets.`
  (rotation).

## Related

- [Create a login](create-a-login.md)
- `rds.logins.rotate-password` · `dcloud rds logins rotate-password` · `dcloud_db_login`
