# Permissions in code

Source: https://docs.autessa.com/docs/cdk/permissions

> Define roles, grants, row-level security, API key scopes and firewall policies in code, and see every change that widens access before you deploy.

# Permissions in code

(Preview)

The deploy API and the Autessa CDK are in preview and not yet released. Names and shapes may change before they are generally available.

Who can see and use each resource is part of your project. Every diff shows permission changes, and marks any change that gives someone more access as WIDENS.

## What you can define

| Resource | What it controls |
| --- | --- |
| `Role` | Which table rows people see. Used with `RowLevelSecurity`, or with `permissions` strings like `tickets_READ`. Not a company role. |
| `Grant` | Gives an existing company role, a group, or a person access to one resource at a level. |
| `RowLevelSecurity` | On a `Table`: which rows each `Role` can read, write or delete. |
| App `grants` | What a hosted app's visitors may reach: tools, agents, managers and tables. See [App grants](#app-grants). |
| Column `masked` / `sensitive` | On a `Table` column: hide or protect the value. |
| `ApiKeyScope` | What API keys with this scope may reach. |
| `FirewallPolicy` | A named AI Application Firewall policy. |

## Access levels

| Level | Meaning |
| --- | --- |
| `VIEW` | See it. |
| `USE` | Run it: call the tool, chat with the agent, open the app. |
| `EDIT` | Change it. |
| `ADMIN` | Change it, share it and delete it. |

Levels go from narrowest to widest: `VIEW`, `USE`, `EDIT`, `ADMIN`.

## Define permissions

A grant to a company role, a grant to one person, and a row rule for a project role. `agent` and `tickets` are resources defined earlier in the project. A grant's principal is an existing company role by name, a group, or a person. A project `Role` only controls table rows, so it can't be a grant's principal.

**Python:**

```python
from autessa.cdk import Grant, Role, RowLevelSecurity, company_role, member

Grant(project, "supportUsesAgent", principal=company_role("Support"), resource=agent, level="USE")

lead = project.variable("supportLeadEmail")
Grant(project, "leadEditsTickets", principal=member(lead), resource=tickets, level="EDIT")

emea = Role(project, "emeaAgents", name="EMEA agents")
RowLevelSecurity(project, "ticketsInRegion", table=tickets,
    operations=["READ", "WRITE"],
    predicate={"column": "region", "equals": "EMEA"},
    roles=[emea])
```

Use `group("name")` for a group of people.

**TypeScript:**

```typescript
import { Grant, Role } from "@autessa/cdk";

new Grant(project, "supportUsesAgent", { principal: { companyRole: "Support" }, resource: agent, level: "USE" });

const lead = project.variable("supportLeadEmail");
new Grant(project, "leadEditsTickets", { principal: { member: `${lead}` }, resource: tickets, level: "EDIT" });

const emea = new Role(project, "emeaAgents", { name: "EMEA agents" });
// Row-level security is part of the table:
// new Table(project, "tickets", {
//   columns: [...],
//   rowLevelSecurity: {
//     inRegion: { operations: ["READ", "WRITE"], predicate: { column: "region", equals: "EMEA" }, roles: [emea] },
//   },
// });
```

Use `{ group: "name" }` for a group of people.

**Java:**

```java
p.grant("supportUsesAgent").companyRole("Support").on(agent).level(Grant.Level.USE);

Variable lead = p.variable("supportLeadEmail");
p.grant("leadEditsTickets").member(lead).on(tickets).level(Grant.Level.EDIT);

Role emea = p.role("emeaAgents").name("EMEA agents");
tickets.rowLevelSecurity("in-region",
    List.of(Table.Operation.READ, Table.Operation.WRITE),
    Map.of("column", "region", "equals", "EMEA"),
    emea);
```

Use `.group("name")` for a group of people.

Use a variable for people. Email addresses differ between tenants, so keep them out of code.

In `autessa.yaml`, these go under `permissions.roles`, `permissions.grants`, `permissions.rowLevelSecurity` and `permissions.apiKeyScopes`. A grant's principal is a company role, a group or a person. Firewall policies go under the top-level `firewallPolicies`, each with a `policy` object.

## App grants

A hosted app's grants are the tools, agents, managers and tables its visitors may reach. They live in the project, next to the app's code, so a change that adds a tool to the app can grant it in the same pull request.

**Python:**

```python
App(project, "Portal", dir="apps/portal", auth={"mode": "PUBLIC", "public_slug": "bills"},
    grants={"tools": [pay_bill, lookup_bill], "agents": [helper],
            "tables": [{"table": bills, "operations": ["READ"]}]})
```

**TypeScript:**

```typescript
new App(project, "Portal", {
  dir: "apps/portal",
  auth: { mode: "PUBLIC", publicSlug: "bills" },
  grants: { tools: [payBill, lookupBill], agents: [helper], tables: [{ table: bills, operations: ["READ"] }] },
});
```

**Java:**

```java
p.app("portal").dir("apps/portal")
    .auth(Map.of("mode", "PUBLIC", "publicSlug", "bills"))
    .grants(g -> g.tools(payBill, lookupBill).agents(helper).table(bills, "READ"));
```

- **Every deploy applies them.** Autessa saves exactly these grants as the app's access. Nothing goes beyond what the deploy key's owner can reach. An unchanged set writes nothing.
- **The diff shows them.** You see a line like `grants of app Portal: <before> -> <after>`. It is marked `WIDENS` when it adds anything, and not when it only removes.
- **Leave `grants` out and the app is left alone.** Its grants stay as they are, so older projects keep working. An empty `grants` grants nothing.
- **Grants are explicit.** An app's `tools`, `agents` and `tables` settings only say what its code uses. They grant nothing.
- **Checked before deploy.** `build` and the offline checks refuse a grant to a name that isn't in the project, and grants on an app whose sign-in is `COMPANY_MEMBERS`. Those apps act as each signed-in member, so grants don't apply.
- Table operations are `READ`, `WRITE` and `DELETE`.

In `autessa.yaml`, they go under each app's `grants`, by logical ID.

## WIDENS

`diff` compares each permission before and after. The server marks a change that gives someone more access than before. The CDK also marks, on its own:

- a new grant, and
- a level that goes up, like `VIEW` to `EDIT`.

```text
! access  Grant            supportUsesAgent  ROLE:support VIEW -> ROLE:support EDIT  WIDENS
```

Narrowing changes are listed too, without the mark. The TypeScript CLI and the deploy action print `WIDENS ACCESS`. In CI, the [deploy action](/docs/cdk/deploy-action.md) posts the diff on the pull request, and each runner has a flag that fails when access widens (see [Commands](/docs/cdk/commands.md#flags)).

A key reaching a resource this same deploy creates isn't marked `WIDENS`, because nothing that exists today becomes reachable. Grants on a new resource still are, and so is a key gaining anything that already exists, including an adopted resource.

## Drift

Someone may change a permission by hand in the UI. That is drift. `diff` lists drifted permissions in their own section, and marks the ones that widen access.

Each stage in `autessa.stages.yaml` takes `permissions: { drift: report }` (the default) or `permissions: { drift: reset }`. In this preview, rely on `report`: review the drift in the diff, then either update your code to match or change the permission back.

## Related

- [Commands](/docs/cdk/commands.md)
- [Project files](/docs/cdk/project.md)
- [Sharing and access](/docs/guide/platform/sharing.md)
- [AI Application Firewall](/docs/features/ai-application-firewall.md)
- [API keys and scopes](/docs/start/api-keys.md)

