KATIBA: policy
KATIBA is a small Rust library that makes deterministic policy decisions. You give it a policy and a request. It answers allow, deny or require approval, and the same input always gives the same answer.
KATIBA is not a service. It has no API, database or network access. In Ngamia, the tools engine (TE) uses it in-process to decide whether a caller may see or run a tool.
| Crate | Purpose |
|---|---|
katiba-engine | Policy model, immutable snapshots, evaluation |
katiba-testkit | Fixed sample policy, snapshot, principal and request for integration tests |
What KATIBA does and does not do
KATIBA does:
- parse and validate a typed policy document, rejecting unknown fields;
- compute a content revision (
sha256:…) for it; - match a request against the rules and pick one winner;
- deny by default when nothing matches;
- fail closed: bad input, unknown versions and a revision mismatch are errors, never an allow.
The host (TE) must do the rest:
- authenticate the user, app and tenant;
- validate tool arguments;
- choose which policy is active and where it is stored;
- enforce the decision, run any approval workflow, and keep the audit trail.
Decision model
A request has a principal (user, app, tenant), an action (action_id, optional resource_id, and a host-computed action_digest), the pinned policy revision and a correlation ID.
A rule matches only when all of these hold:
- tenant equals the principal’s tenant;
- app equals the principal’s app;
- the rule has no
user, or it equals the principal’s user; action_idis equal;- the rule has no
resource_id, or it equals the request’s resource.
Strings are compared exactly. KATIBA does not trim, lowercase or alias them. A rule with a resource does not match a request without one.
When several rules match, one wins:
- highest
priorityfirst; - at equal priority, Deny, then RequireApproval, then Allow;
- then the smallest rule
id.
Effects are not merged. A high-priority allow beats a lower-priority deny.
| Outcome | Meaning |
|---|---|
Allow | A matching rule allows the action. The host still runs its own checks. |
Deny | Stop. Carries reason_code and safe_reason. |
RequireApproval | A separate, trusted approval process must decide. This is not permission. |
| No match | Deny with reason_code no_matching_rule. |
| Error | No decision. Treat it as not allowed. |
Each decision carries the policy ID, revision, matched rule ID and a deterministic decision_id. The decision ID is correlation metadata. It is not a signature, audit record or idempotency key.
Policy file format
A policy is JSON. Every field is checked; unknown fields fail parsing.
{
"schema_version": 2,
"policy_id": "te-platform-floor",
"rules": [
{
"id": "orders-status-invoke",
"priority": 100,
"scope": { "tenant": "*", "app": "*" },
"action": { "action_id": "tool.invoke", "resource_id": "orders.status@2" },
"effect": { "kind": "allow" }
},
{
"id": "refunds-review",
"priority": 200,
"scope": { "tenant": "TENANT_UUID", "app": "platform" },
"action": { "action_id": "payments.refund" },
"effect": {
"kind": "require_approval",
"approval_policy_ref": "approvals.finance-review",
"reason_code": "refund_review",
"safe_reason": "This refund needs an authorized review."
}
}
]
}| Field | Rule |
|---|---|
schema_version | 1 or 2. Other values are rejected. |
policy_id | Required name. Not an authenticated publisher. |
rules | At most 10,000; rule IDs must be unique. An empty list denies everything. |
priority | Unsigned integer; higher wins. |
scope.tenant, scope.app | Required. Exact in v1. In v2 either may be * (any). |
scope.user | Optional exact user. Omitted means any user in that tenant and app. |
action.action_id | Required, exact. Never a wildcard. |
action.resource_id | Optional exact resource. Omitted means any resource. |
effect.kind | allow, deny or require_approval |
reason_code | Deny and approval only: 1–64 bytes of a-z, 0-9, ., _, - |
safe_reason | Deny and approval only: up to 2,048 bytes, shown to callers |
approval_policy_ref | Approval only: an opaque reference for the host’s workflow |
Identifiers are at most 256 bytes, not blank, with no control characters.
Model v1 and v2
Model v2 adds one thing: scope.tenant and scope.app may be *. It exists for operator policies, such as TE’s platform floor, that cover every tenant. In a v1 document * is an ordinary string. A wildcard rule is not ranked as less specific, so give a tenant-specific exception a higher priority than the wildcard rule it overrides. Hosts that store tenant-owned policies must reject wildcard documents; TE does.
Revisions
The revision is sha256: plus 64 lowercase hex characters. It is computed from the typed document with rules sorted by ID, so reordering rules or changing JSON whitespace does not change it. Every evaluation names the revision it expects. A different revision is an error, not a fallback. A revision pins content; it does not prove who wrote or approved the policy.
How TE uses KATIBA
For every discovery or invocation, TE evaluates one request per action:
tool.discoverortool.invoke, and- each permission in the tool manifest’s
required_permissions.
The principal is the verified subject, application and tenant. For calls from NAK in the platform, the application is platform. The resource is always the exact tool_id@version. Every action must be allowed.
TE checks two policy layers, and both must allow:
| Layer | Where it lives | Wildcards |
|---|---|---|
| Platform floor | File TE_KATIBA_POLICY_FILE, pinned by TE_KATIBA_POLICY_REVISION | Allowed (v2) |
| Tenant policy | Active revision in TE’s PostgreSQL | Not allowed; must match the caller’s tenant and app exactly |
A tenant with no active policy is denied. Tool admin actions (tool.admin.*) and policy admin actions (policy.admin.*) are also KATIBA actions. Policy admin actions are checked only against the floor, so a tenant policy cannot grant itself management rights.
TE example policies: examples/platform-floor-policy.v2.json and examples/production-policy.json.
Risk and approval
KATIBA has no risk field. Risk (low, medium, high, critical) and side_effecting are declared in the TE tool manifest. You express “this needs a human” in policy with a require_approval rule.
The engine can turn RequireApproval into Allow with evaluate_with_approval, but only for a host-verified approval grant bound to the same revision, approval reference, principal, action, resource and digest, not expired, and not approved by the requester. A grant never overrides a Deny.
No Ngamia component uses approval grants yet. TE has no approval contract and treats RequireApproval as a deny. NAK has no approval flow and offers only read-only tools. Write deny or require_approval rules for side-effecting tools; both block them today.
Validate a policy
Use TE’s checker from the TE workspace. It parses the file (UTF-8, at most 1 MiB), validates it with KATIBA and prints the policy ID and revision:
cargo run -p te-server --bin te-policy-check -- policy.json
# policy_id=te-platform-floor
# revision=sha256:…In CI or a deploy job, fail if the file is not the reviewed one:
cargo run -p te-server --bin te-policy-check -- policy.json \
--expect-revision "$TE_KATIBA_POLICY_REVISION"It exits non-zero on an unreadable, invalid or mismatched policy. Ship the file and its revision together; TE refuses to start when they differ. Changing the floor means a new deploy, because TE does not hot-reload it.
For a tenant policy draft, TE’s simulate route evaluates it against an exact published tool and the floor without calling a provider.
Limits
- No wildcards except
*for tenant and app in v2. No expressions, argument predicates or dynamic lookups. - One snapshot per engine. No loading, storage, distribution or reload.
- No identity checks: principal strings are trusted as given.
action_digestis not verified against arguments; it only feeds the decision ID.- No approval workflow, audit log, signatures or policy provenance.
- Not independently deployable, and not enough on its own to authorize side effects.
Run the tests
From KATIBA/:
cargo test --workspace --locked
cargo clippy --workspace --all-targets --locked -- -D warnings