> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cockroachlabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Security and access control

> This page describes how to secure the order service's access to CockroachDB.

This page describes how to secure the order service's access to CockroachDB. The guiding principle is **least privilege**: every identity holds only the privileges its job requires, and nothing more. A compromised credential or a buggy code path can then damage only what that identity could already reach.

The patterns here (roles, `GRANT`/`REVOKE`, dedicated SQL users, and transport encryption) are well-established CockroachDB features. Where a capability is version-specific, this page says so and points to the current documentation rather than asserting behavior for a given release.

## Principle of least privilege

The order service connects to the database as an application identity. That identity should be able to do exactly what the request flow requires (read and write the order, inventory, payment, and outbox tables) and nothing else. It should **not** be a superuser, should **not** own the schema, and should **not** hold privileges on tables it never touches.

Separating privileges this way bounds the blast radius of any single failure:

* A leaked application credential cannot drop tables or read unrelated data.
* A reporting query cannot modify order or payment state.
* Schema changes require a separate, deliberately privileged identity.

## Role-based access control

CockroachDB uses SQL roles and users to model identities. A **role** is a named bundle of privileges; a **user** is a role that can log in. Granting a role to a user gives the user that role's privileges, so you define privileges once on a role and assign the role to as many users as needed.

Define a dedicated application role with only the table privileges the order service needs:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
-- Application role: the privileges the order service requires.
CREATE ROLE app_role;

GRANT SELECT, INSERT, UPDATE ON TABLE
  orders, order_lines, order_events, idempotency_keys,
  inventory, reservations,
  payment_transactions, payment_events, outbox,
  shipments, shipment_lines, returns, return_lines
  TO app_role;
```

Create the login user the service authenticates as, and grant it the role:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
-- Login user for the running service; grant it the application role.
CREATE USER order_service WITH PASSWORD '<set-via-secret-manager>';
GRANT app_role TO order_service;
```

Note what is deliberately absent: `DROP`, `DELETE` on most tables, and any privilege on objects the service does not use. Grant `DELETE` only where the design actually deletes rows, and prefer background cleanup (for example, Row-Level TTL) over granting broad delete rights to the live service. Withdraw a privilege granted in error with `REVOKE`:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
REVOKE DELETE ON TABLE orders FROM app_role;
```

### A read-only reporting role

Analytics, dashboards, and regulators need to read order and payment history but must never change it. Model them as a separate role with read-only privileges, and route those reads to follower reads so they do not contend with the write path:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
CREATE ROLE reporting_ro;
GRANT SELECT ON TABLE
  orders, order_lines, order_events,
  payment_transactions, payment_events,
  shipments, shipment_lines, returns, return_lines
  TO reporting_ro;

CREATE USER regulator_auditor WITH PASSWORD '<set-via-secret-manager>';
GRANT reporting_ro TO regulator_auditor;
```

> [!TIP]
>
> **Separate identities for separate jobs**
> Use distinct roles for the live service, for schema migrations, for reporting, and for operators. Each gets only what its job needs. This keeps an audit of "who can do what" legible and makes it safe to rotate or revoke one identity without affecting the others.

## Transport security and authentication

Run the cluster in secure mode so that all client-to-node and node-to-node traffic is encrypted with **TLS** (Transport Layer Security). In secure mode, clients connect over TLS and present credentials; connections that cannot establish TLS are refused. In the order service, this means the connection string enables TLS (for example, `sslmode=verify-full`) rather than disabling it.

Authentication verifies *who* a connection is before any privilege check applies. CockroachDB supports more than one authentication method, and the methods available and their configuration depend on your deployment and release.

> [!NOTE]
>
> **Verify version-specific security features**
> Authentication methods (and their exact configuration), as well as finer-grained authorization such as column-level privileges and row-level security, are version-specific. Do not assume a particular method or feature is available on a given release. Confirm it against the current CockroachDB documentation before designing around it:
>
> * Authorization: [https://www.cockroachlabs.com/docs/stable/security-reference/authorization](https://www.cockroachlabs.com/docs/stable/security-reference/authorization)
> * Authorization how-to: [https://www.cockroachlabs.com/docs/stable/authorization](https://www.cockroachlabs.com/docs/stable/authorization)

## Handling sensitive and PII data

Order management necessarily touches personal data (names, addresses) and money. Reduce risk by storing less and separating what you must store.

* **Minimize what you store.** Keep only the personal data the service actually needs to fulfill and account for an order. Data you never store cannot leak.
* **The payment provider is the system of record for card data.** Do not store raw card numbers. The provider holds the card data; your `payment_transactions` table holds your *intent* and the provider's opaque reference (`provider_ref`), not the instrument itself. See [Payments and callbacks](https://docs.cockroachlabs.com/docs/cookbook/order-management/payments-and-callbacks.md), where the provider is defined as authoritative for money movement.
* **Separate payment data from order data by privilege.** Scope access so that an identity able to read order history is not automatically able to read payment detail, and vice versa. Grant payment-table privileges only to the roles that genuinely need them.
* **Confine PII to read-only roles for reporting.** Regulators and analysts read through `reporting_ro`; they can audit history without the ability to alter it.

> [!WARNING]
>
> **Store secrets outside the schema**
> Database passwords, provider API keys, and TLS material belong in a secret manager, not in source, configuration files, or table rows. The placeholders above (`<set-via-secret-manager>`) are deliberate: resolve them at deploy time from a secret store, and rotate them on a schedule and after any suspected exposure.

## Summary

Give the order service a dedicated role with exactly the table privileges the request flow needs, and nothing more. Keep reporting and audit access read-only and separate. Run the cluster in secure mode so all traffic is TLS-encrypted, authenticate every connection, and let the payment provider remain authoritative for card data. For any version-specific authorization or authentication capability, verify availability against the current release before relying on it.
