# Authorization

> IAM policies and native PostgreSQL authorization, and how the two combine.

---

LLMS index: [llms.txt](/llms.txt)

---

> [!NOTE]
> S3 IAM users, policies, and service accounts are part of the core `pgsilo` MVP. SQL application role grants (`silo_api` / `silo_bucket`) and joint S3-to-PostgreSQL authorization (`iam_and_pg` mode) are **Preview** features in the MVP release.

`pgsilo` separates three kinds of identities:

1. **Database owner (`silo_owner`)**: the trusted ordinary PostgreSQL role used by `silo server` gateways via `SILO_PG_DSN`.
2. **SQL application logins (e.g., `app_sql`)**: independent unprivileged PostgreSQL roles that call `silo_api` functions.
3. **S3 identities**: S3 root credentials, IAM users, service accounts, and STS temporary sessions.

Never connect applications or S3 bindings as the installation owner (`silo_owner`) or a PostgreSQL superuser.

## SQL application privileges {#sql-privileges}

Create an unprivileged login role for SQL callers:

```sql
CREATE ROLE app_sql LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE
  NOREPLICATION NOBYPASSRLS;
\password app_sql
```

As the installation owner (`silo_owner`), grant schema `USAGE`, function `EXECUTE`, and table privileges on the target bucket's ACL relation in `silo_bucket`:

```sql
GRANT USAGE ON SCHEMA silo_api, silo_bucket TO app_sql;
GRANT EXECUTE ON FUNCTION
  silo_api.put_object(text,text,bytea,text,jsonb,boolean,text),
  silo_api.get_object(text,text,uuid),
  silo_api.stat_object(text,text,uuid),
  silo_api.read_object(text,text,bigint,bigint,uuid,bigint),
  silo_api.list_objects(text,text,text,integer)
TO app_sql;

SELECT format('GRANT SELECT, INSERT ON TABLE %I.%I TO %I',
              n.nspname, c.relname, 'app_sql')
FROM silo.bucket_acl a
JOIN silo.buckets b ON b.id = a.bucket_id
JOIN pg_catalog.pg_class c ON c.oid = a.relation_oid
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE b.name = 'attachments' AND n.nspname = 'silo_bucket'
\gexec
```

| SQL operation | Required bucket ACL privilege |
| --- | --- |
| `stat_object`, `get_object`, `read_object`, `list_objects` | `SELECT` |
| `put_object` (including unconditional overwrite) | `INSERT` |
| Conditional `put_object`, `copy_object`, `delete_object` | Operation privileges plus `SELECT` |
| `copy_object` | Source `SELECT` and destination `INSERT` |
| `delete_object`, `drop_bucket` | `DELETE` (`drop_bucket` also requires an empty bucket) |
| `bucket_info`, `list_buckets` | At least one of `SELECT`, `INSERT`, or `DELETE` |
| `create_bucket` | `EXECUTE` on `silo_api.create_bucket(text, text)` |
{.wide}

Callers must connect directly as `app_sql` (`session_user` is checked; `SET ROLE` is rejected). Recreating a dropped bucket generates a new ACL relation in `silo_bucket` and requires fresh `GRANT` statements.

## Joint authorization (`iam_and_pg`) {#joint-authorization}

By default (`iam` mode), S3 requests are authorized solely by S3 IAM and bucket policies. Optional `iam_and_pg` mode requires **both** S3 IAM evaluation and the bound PostgreSQL role's `silo_bucket` privileges to succeed:

- An explicit IAM `Deny`, service-account policy restriction, or STS session policy restriction always denies the request; PostgreSQL `GRANT` statements never override an IAM denial.
- The authorization mode is stored in the shared database (`silo.set_native_auth_mode`) and applies across all gateways connected to that database.
- Anonymous S3 requests have no PostgreSQL role binding and are rejected in `iam_and_pg` mode.

Bound PostgreSQL roles must be independent ordinary roles (`NOLOGIN` roles are recommended for S3-only principals) and must **not**:

- Hold `SUPERUSER`, `CREATEDB`, `CREATEROLE`, `REPLICATION`, or `BYPASSRLS`
- Inherit the installation owner (`silo_owner`), schema owners, `pg_read_all_data`, or `pg_write_all_data`
- Hold privileges on the private `silo` schema
- Hold `WITH GRANT OPTION` on `silo_api` functions or `CREATE` on `silo_api` / `silo_bucket`

## Enabling `iam_and_pg` mode {#enabling-joint-auth}

After creating the IAM user `app-s3` and granting bucket privileges to `app_sql`, create a dedicated `NOLOGIN` role for the S3 root identity and bind both principals inside a transaction as `silo_owner`:

```sql
CREATE ROLE silo_native_root NOLOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE
  NOREPLICATION NOBYPASSRLS;
```

```sql
BEGIN;
GRANT USAGE ON SCHEMA silo_bucket TO silo_native_root;

SELECT format('GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE %I.%I TO %I',
              n.nspname, c.relname, 'silo_native_root')
FROM silo.bucket_acl a
JOIN silo.buckets b ON b.id = a.bucket_id
JOIN pg_catalog.pg_class c ON c.oid = a.relation_oid
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE b.name = 'attachments' AND n.nspname = 'silo_bucket'
\gexec

SELECT silo.bind_native_root('silo_native_root'::regrole);
SELECT silo.bind_native_principal('iam:app-s3', 'app_sql'::regrole);
SELECT silo.set_native_auth_mode('iam_and_pg');
COMMIT;
```

Before switching an active deployment to `iam_and_pg`, bind every required root and IAM user identity and grant privileges on all target buckets.

## S3-to-PostgreSQL privilege mapping {#privilege-mapping}

| S3 operation class | Required PostgreSQL privilege in `iam_and_pg` mode |
| --- | --- |
| Object `GET`/`HEAD`, version reads, listings, tag/retention/bucket-config reads | `SELECT` on bucket ACL relation |
| Object `PUT`, tag writes, retention/legal-hold writes, annotation `PUT` | `INSERT` on bucket ACL relation |
| Object/version `DELETE`, `AbortMultipartUpload`, annotation `DELETE` | `DELETE` on bucket ACL relation |
| Bucket configuration changes, governance-retention bypass | `UPDATE` on bucket ACL relation (plus operation-specific privileges) |
| `DeleteBucket`, `ForceDeleteBucket` | `DELETE` and `UPDATE` on bucket ACL relation |
| `CreateBucket` | `EXECUTE` on `silo_api.create_bucket(text, text)` |
| Conditional writes/deletes and annotation mutations with `x-amz-object-if-match` | Operation privilege plus `SELECT` on bucket ACL relation |
| `CopyObject` | Source `SELECT` and destination `INSERT` (plus annotation IAM actions when copying annotations) |
{.wide}

Service accounts and STS temporary credentials inherit the PostgreSQL role binding of their parent IAM user while enforcing their own IAM and session-policy restrictions.

## Revoking bindings and restoring default mode {#revocation}

As `silo_owner`, unbind principals or switch back to `iam` mode:

```sql
SELECT silo.unbind_native_principal('iam:app-s3');
-- To unbind the root identity:
-- SELECT silo.unbind_native_principal('root:');

-- To restore IAM-only S3 authorization:
SELECT silo.set_native_auth_mode('iam');
```

Bindings track both the PostgreSQL role OID and the IAM user generation. Dropping and recreating a role or IAM user with the same name requires re-running `silo.bind_native_principal`. Switching S3 back to `iam` mode does not relax privileges on the `silo_api` SQL functions.

---

Backlinks:

- [Reference](/docs/s3/reference/)
- [SQL API](/docs/s3/reference/sql-api/)
- [Objects from SQL](/docs/s3/tutorial/sql/)
