# Security in Managed PostgreSQL

Who may do what (IAM), what is audited, how data is encrypted at rest, in backups and in transit, and who can reach a database.

## IAM actions

Every operation is checked against your IAM policies. A database's resource name is
`rn:ews:<region>:<org>:rds:db/<db id>`.

(The table of IAM actions is generated on the web page.)
- **Branch actions** are checked on the **parent database's** resource name
  (`rn:ews:de1:<org>:rds:db/<db id>`), so a policy grants branching database by database. The
  console's capability "Branches" on a database bundles them.
- **Sensitive reads** are recorded in the audit log even when they are allowed:
  `rds:QueryDatabase` (the data browser, which reads every table with the master credential, and
  which no read-only preset includes), `rds:DescribeDBLogFiles` (the server log carries statement
  text with its values), `rds:DescribeDBQueryStats` (the statements a database spends its time
  on, which name every table and column), and `rds:GetDBBranchCredentials` (a branch's passwords,
  a binding to a branch, the CI variable). `rds:DescribeDBQueryInsights` is an ordinary read, but
  is audited too while the database keeps plans with values.
- Reading the master password or a login's password is `secrets:GetSecretValue` on its secret, and
  is recorded in the audit log.
- Choosing your own encryption key at creation needs `kms:Encrypt` on that key.
- Cancelling a restore needs `rds:RestoreDBInstance` on the **source** database.

## Audit

Every write is recorded in the audit log with its outcome. So are: reading a secret value,
browsing data, reading query statistics, reading the server log, reading branch credentials,
cancelling a statement or ending a session (also in **Activity**), cancelling a restore, and every
read of query insights (the **Queries** tab) while **Show values and plans** is on.

## Encryption at rest

Every node's data volume is encrypted with LUKS2 (AES-256-XTS). Each volume has its own random key,
held by the control plane wrapped under the platform key (see
[Key custody](/docs/kms#key-custody)). A machine gets the key only at boot (see
[What runs on your machines](what-runs-on-your-machines.md)). A volume detached from its machine,
or a copy of its blocks, is unreadable. Deleting the database deletes the keys.

This covers single nodes, every cluster member and reader, a member rebuilt after its machine was
lost (it opens its kept volume with that volume's key, or gets a new volume and key and copies from
the leader), and a database restored from a backup.

**Your own key.** Choose one of your KMS keys under **Encryption key** when you create the
database, or pass `"kmsKeyId"` to the create request. Every node volume's key is then wrapped by
it, including volumes added later (readers, rebuilt members). Disabling the KMS key stops nodes
from opening their disks at their next boot; deleting it (after its waiting period) makes the data
unreadable for good. A restore takes the source's key unless you name another (`"kmsKeyId"`, or
`null` for the platform key). The key of an existing database's disks cannot be changed; restore
into a new database with the key you want.

**Backups** are encrypted separately, with AES-256-CBC on the database machine before upload, one
passphrase per database (see [Back up a database](back-up-a-database.md)).

The API reports both as `encryption: { storage: "luks2" | "none", backups: "aes-256-cbc" | "none" }`,
and the **Overview** shows **Disk encryption** and **Backup encryption**. `storage` is `none` for a
database created before disk encryption existed: its volumes stay unencrypted, a rebuilt member's
included, and nothing changes them in place. To encrypt one, [restore it](restore-a-database.md)
into a new database, which is encrypted, and point your apps at the new one.

## Rotate the disk keys

Replace _ewdb-0123456789ab_ with your database's id. It needs `rds:RotateDBStorageKey`.

**CLI**
```sh
dcloud rds instances rotate-storage-key ewdb-0123456789ab
```

**API**
```http
POST /rds/api/instances/ewdb-0123456789ab/rotate-storage-key
```

**Console:** not available. Rotating the disk keys is in the CLI and API only.
**Terraform:** not available. A rotation is an action, not configuration.

Every node volume gets a new key: each node adds the new key to its volume, removes the old one
and reports within five minutes; nothing restarts. The answer is `202` with each volume's key
version, and the database's events say when each volume is done. The data is not re-encrypted: the
rotation replaces the passphrase that unlocks the volume, not the volume key itself.

It is refused with `409` for a database created before disk encryption ("This database was created
before disk encryption; its disks have no key to rotate. Restore it into a new database.") and
while a rotation is still in progress ("A key rotation is already in progress").

## Encryption in transit

Every port serves TLS on a database provisioned with a certificate, and a new database refuses
clients that do not use it unless **Require TLS** was cleared at creation (see
[Require TLS](require-tls.md)). Certificates are issued per database by the installation's CA and
renewed automatically; see [Connect to a database](connect.md).

## Network exposure

A database is private unless you create it with `publiclyAccessible`: only machines in its network
and the platform's control plane reach it. A public database answers on its ports from anywhere;
the password is required either way, and TLS while **Require TLS** is on. A copy made by a restore in the console is
private unless someone selects **Reachable from the internet**; through the API it takes the
source's setting unless the request says `publiclyAccessible`. See [Instances, clusters and endpoints](instances-and-clusters.md).

## Logins and the platform's role

Give each person a login of their own instead of sharing the master password (see
[Create a login](create-a-login.md)). The platform works in your database as its own role,
`ews_platform`, which your server log names and query statistics leave out (see
[What runs on your machines](what-runs-on-your-machines.md)).

## Related

- [Monitoring](monitoring.md)
- [IAM](/docs/iam)
