# ory/keto architecture

> Architecture diagram of the GitHub repository ory/keto, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ory/keto
- Repository: https://github.com/ory/keto

## Overview

1. **What this repo is**
   Ory Keto is an authorization server for Zanzibar-style relationship-based access control. The core architectural unit is not “users and roles” but **relation tuples** plus **permission checks** over those tuples and namespace definitions.

2. **Main runtime entrypoint**
   The process starts in `main.go`, which wires the Cobra-style command tree under `cmd/`. The important executable modes are:
   - `cmd/server` for the actual API server
   - `cmd/migrate` for database schema migration
   - `cmd/check`, `cmd/expand`, `cmd/relationtuple`, `cmd/namespace`, `cmd/status` for operational/CLI access

3. **External-facing API boundaries**
   The repo exposes multiple API surfaces:
   - **REST/HTTP API**, backed by generated client/server definitions under `spec/`, `doc_swagger.go`, `ketoapi/`, and `internal/httpclient/`
   - **gRPC APIs**, generated from `proto/ory/keto/...` for relation tuples, expand, check, namespaces, version, and OPL syntax
   These are parallel interfaces over the same domain engine and persistence layer.

4. **Authorization domain core**
   The heart of the system lives in `internal/check`, `internal/expand`, and `internal/relationtuple`.
   - `internal/check` evaluates permission queries.
   - `internal/expand` builds permission trees / subject set expansions.
   - `internal/relationtuple` handles tuple read/write/transact operations.
   These packages are the business logic boundary between APIs and storage.

5. **How permission evaluation works**
   A check request flows from API handler to the check engine, which traverses tuple relations, rewrite rules, computed usersets, and direct memberships. The engine is split into steps in `internal/check/step/`, which makes the evaluation pipeline explicit: direct lookup, dispatch, expand, rewrite, invert, traverse, and execution orchestration.

6. **Namespace and OPL subsystem**
   Namespaces are defined through the **Ory Permission Language**. Parsing and validation happen in `schema/`, with AST/types in `internal/namespace/ast` and handlers in `internal/namespace/namespacehandler`.
   The system supports loading namespace definitions from config and watching them for change via `internal/driver/config` and `internal/driver/config/namespace_watcher.go`. This is a distinct boundary: **policy definition** is separate from **relationship data**.

7. **Persistence layer**
   Tuple and mapping data are stored in SQL databases through `internal/persistence/sql/`.
   The storage layer is designed for multiple dialects: PostgreSQL, MySQL, CockroachDB, and SQLite (mostly dev/test). SQL migrations are maintained per dialect under `internal/persistence/sql/migrations/` and are executed via the `oryx/popx` migration subsystem.

8. **Tuple identity and UUID mapping**
   Keto normalizes relation tuple identifiers through UUID mapping logic in `internal/relationtuple/uuid_mapping.go` and `internal/persistence/sql/uuid_mapping.go`.
   This is important architecturally because the system preserves stable tuple identity across storage and migrations while still supporting efficient SQL access patterns.

9. **Server composition and config**
   `internal/driver/` is the composition root for the server: configuration loading, registry selection, logging interceptors, daemon lifecycle, and database connection setup.
   Configuration is built from files/env/flags via the shared `oryx/configx` stack, and runtime behavior can include namespace cache watching, tracing, and logging integration.

10. **Client/tooling boundary**
   The repo includes a first-class CLI and SDK story:
   - The CLI subcommands under `cmd/` are used to manage namespaces, relation tuples, and checks.
   - `internal/httpclient/` is a generated REST client.
   - `proto/` contains generated Go/JS/TS bindings for gRPC and public APIs.
   These are not just examples; they are part of the repo’s delivery surface and test matrix.

11. **Deployment and infrastructure**
   The repository is containerized and ships several Docker variants under `.docker/` and compose files for common setups (`docker-compose-postgres.yml`, `docker-compose-mysql.yml`, `docker-compose-tracing.yml`, `docker-compose.yml`).
   The architecture assumes an external SQL database and optional observability backends via OpenTelemetry/Jaeger/Zipkin/OTLP.

12. **Observability and operational hooks**
   Keto integrates with metrics and tracing through shared `oryx` packages plus `x/events`.
   There are dedicated health/status endpoints and checks, plus debug/serve tests in `cmd/` and e2e tests in `internal/e2e/`, showing that readiness, liveness, and API behavior are part of the intended operational architecture.

13. **Testing and validation boundaries**
   The repo has strong test coverage around parser correctness, engine behavior, persistence behavior, migrations, and end-to-end API compatibility.
   Fuzz seeds in `.fuzzer/` and parser fuzz tests in `schema/` indicate the OPL parser is treated as a high-risk boundary.

14. **How to think about the system in a diagram**
   A good architecture diagram would center on:
   - Clients/CLI/gRPC/REST API on the left
   - API handlers and command entrypoints in the middle
   - Check/expand/tuple/namespace engines as the core domain layer
   - SQL persistence and migrations below
   - Configuration/watchers/tracing/logging as sidecar concerns
   - External DB and observability systems on the right

Main entry points: [`internal/driver/registry.go`](https://github.com/ory/keto/blob/master/internal/driver/registry.go) (Driver root), [`internal/persistence/sql/persister.go`](https://github.com/ory/keto/blob/master/internal/persistence/sql/persister.go) (SQL store), [`cmd/check/root.go`](https://github.com/ory/keto/blob/master/cmd/check/root.go) (Ops cmds), [`proto/ory/keto/relation_tuples/v1alpha2/check_service.proto`](https://github.com/ory/keto/blob/master/proto/ory/keto/relation_tuples/v1alpha2/check_service.proto) (gRPC APIs)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_entry["Entry & CLIs"]
  node_main(("main.go<br/>entrypoint<br/>[main.go]"))
  node_cmd_root["CLI root<br/>cobra root<br/>[root.go]"]
  node_cmd_server["Server cmd<br/>http/grpc server<br/>[serve.go]"]
  node_cmd_migrate["Migrate cmd<br/>db migrations<br/>[root.go]"]
  node_cmd_ops["Ops cmds<br/>cli tools<br/>[root.go]"]
end

subgraph group_grp_api["API Surfaces"]
  node_spec_openapi["REST spec<br/>openapi spec<br/>[api.json]"]
  node_swagger_doc["Swagger docs<br/>openapi doc<br/>[doc_swagger.go]"]
  node_httpclient["HTTP client<br/>generated client<br/>[client.go]"]
  node_ketoapi["API models<br/>api encoding"]
  node_proto_services["gRPC APIs<br/>protobuf services"]
end

subgraph group_grp_core["Authorization Core"]
  node_check_engine["Check engine<br/>permission eval<br/>[engine.go]"]
  node_check_steps["Eval steps<br/>pipeline steps<br/>[step.go]"]
  node_expand_engine["Expand engine<br/>tree expansion<br/>[engine.go]"]
  node_tuple_handlers["Tuple service<br/>tuple read/write<br/>[handler.go]"]
  node_namespace_handler["Namespace svc<br/>opl policy<br/>[handler.go]"]
  node_opl_parser["OPL parser<br/>language parser<br/>[parser.go]"]
  node_namespace_ast["Policy AST<br/>namespace model<br/>[ast_definitions.go]"]
end

subgraph group_grp_data["Storage & Migrations"]
  node_sql_store[("SQL store<br/>tuple persistence<br/>[persister.go]")]
  node_uuid_map["UUID map<br/>identity mapping<br/>[uuid_mapping.go]"]
  node_migrations["Migrations"]
  node_popx["popx<br/>migration engine<br/>[migrator.go]"]
end

subgraph group_grp_runtime["Composition & Ops"]
  node_driver_root["Driver root<br/>composition root<br/>[registry.go]"]
  node_config_watch["Config watch<br/>config/provider<br/>[provider.go]"]
  node_otel_log["Telemetry<br/>tracing/logging<br/>[otel.go]"]
  node_db_pool["DB layer<br/>sql connectors<br/>[dsn_postgres.go]"]
  node_e2e_tests(("E2E suite<br/>validation<br/>[full_suit_test.go]"))
end

node_main -->|"dispatches"| node_cmd_root
node_cmd_root -->|"starts"| node_cmd_server
node_cmd_root -->|"runs"| node_cmd_migrate
node_cmd_root -->|"exposes"| node_cmd_ops
node_cmd_server -->|"composes"| node_driver_root
node_driver_root -->|"loads"| node_config_watch
node_driver_root -->|"instruments"| node_otel_log
node_driver_root -->|"connects"| node_db_pool
node_driver_root -->|"injects"| node_sql_store
node_driver_root -->|"injects"| node_check_engine
node_driver_root -->|"injects"| node_expand_engine
node_driver_root -->|"injects"| node_tuple_handlers
node_driver_root -->|"injects"| node_namespace_handler
node_cmd_migrate -->|"invokes"| node_popx
node_cmd_ops -->|"calls"| node_check_engine
node_cmd_ops -->|"calls"| node_expand_engine
node_cmd_ops -->|"calls"| node_tuple_handlers
node_cmd_ops -->|"calls"| node_namespace_handler
node_spec_openapi -->|"documents"| node_swagger_doc
node_spec_openapi -->|"generates"| node_httpclient
node_spec_openapi -->|"shares models"| node_ketoapi
node_proto_services -->|"wraps"| node_check_engine
node_proto_services -->|"wraps"| node_expand_engine
node_proto_services -->|"wraps"| node_tuple_handlers
node_proto_services -->|"wraps"| node_namespace_handler
node_check_engine -->|"executes"| node_check_steps
node_check_engine -->|"reads"| node_sql_store
node_expand_engine -->|"reads"| node_sql_store
node_tuple_handlers -->|"reads/writes"| node_sql_store
node_tuple_handlers -->|"normalizes"| node_uuid_map
node_namespace_handler -->|"validates"| node_opl_parser
node_namespace_handler -->|"builds"| node_namespace_ast
node_opl_parser -->|"produces"| node_namespace_ast
node_sql_store -->|"uses"| node_uuid_map
node_sql_store -->|"evolves via"| node_migrations
node_migrations -->|"runs on"| node_popx
node_e2e_tests -->|"covers"| node_cmd_server
node_e2e_tests -->|"covers"| node_proto_services
node_e2e_tests -->|"covers"| node_spec_openapi

click node_main "https://github.com/ory/keto/blob/master/main.go"
click node_cmd_root "https://github.com/ory/keto/blob/master/cmd/root.go"
click node_cmd_server "https://github.com/ory/keto/blob/master/cmd/server/serve.go"
click node_cmd_migrate "https://github.com/ory/keto/blob/master/cmd/migrate/root.go"
click node_cmd_ops "https://github.com/ory/keto/blob/master/cmd/check/root.go"
click node_spec_openapi "https://github.com/ory/keto/blob/master/spec/api.json"
click node_swagger_doc "https://github.com/ory/keto/blob/master/doc_swagger.go"
click node_httpclient "https://github.com/ory/keto/blob/master/internal/httpclient/client.go"
click node_ketoapi "https://github.com/ory/keto/blob/master/ketoapi/public_api_definitions.go"
click node_proto_services "https://github.com/ory/keto/blob/master/proto/ory/keto/relation_tuples/v1alpha2/check_service.proto"
click node_check_engine "https://github.com/ory/keto/blob/master/internal/check/engine.go"
click node_check_steps "https://github.com/ory/keto/blob/master/internal/check/step.go"
click node_expand_engine "https://github.com/ory/keto/blob/master/internal/expand/engine.go"
click node_tuple_handlers "https://github.com/ory/keto/blob/master/internal/relationtuple/handler.go"
click node_namespace_handler "https://github.com/ory/keto/blob/master/internal/namespace/namespacehandler/handler.go"
click node_opl_parser "https://github.com/ory/keto/blob/master/schema/parser.go"
click node_namespace_ast "https://github.com/ory/keto/blob/master/internal/namespace/ast/ast_definitions.go"
click node_driver_root "https://github.com/ory/keto/blob/master/internal/driver/registry.go"
click node_config_watch "https://github.com/ory/keto/blob/master/internal/driver/config/provider.go"
click node_otel_log "https://github.com/ory/keto/blob/master/oryx/otelx/otel.go"
click node_db_pool "https://github.com/ory/keto/blob/master/internal/x/dbx/dsn_postgres.go"
click node_sql_store "https://github.com/ory/keto/blob/master/internal/persistence/sql/persister.go"
click node_uuid_map "https://github.com/ory/keto/blob/master/internal/persistence/sql/uuid_mapping.go"
click node_migrations "https://github.com/ory/keto/blob/master/internal/persistence/sql/migrations/uuidmapping/uuid_mapping_migrator.go"
click node_popx "https://github.com/ory/keto/blob/master/oryx/popx/migrator.go"
click node_e2e_tests "https://github.com/ory/keto/blob/master/internal/e2e/full_suit_test.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_cmd_root,node_cmd_server,node_cmd_migrate,node_cmd_ops toneBlue
class node_spec_openapi,node_swagger_doc,node_httpclient,node_ketoapi,node_proto_services toneAmber
class node_check_engine,node_check_steps,node_expand_engine,node_tuple_handlers,node_namespace_handler,node_opl_parser,node_namespace_ast toneMint
class node_sql_store,node_uuid_map,node_migrations,node_popx toneRose
class node_driver_root,node_config_watch,node_otel_log,node_db_pool,node_e2e_tests toneIndigo
```

## Components

### Entry & CLIs

Process entrypoint and operational subcommands.

- [main.go](https://github.com/ory/keto/blob/master/main.go): entrypoint · Wires the command tree and boots the process. · `main.go`
- [CLI root](https://github.com/ory/keto/blob/master/cmd/root.go): cobra root · Top-level command wiring for server and operational tools. · `cmd/root.go`
- [Server cmd](https://github.com/ory/keto/blob/master/cmd/server/serve.go): http/grpc server · Starts the authorization API server. · `cmd/server/serve.go`
- [Migrate cmd](https://github.com/ory/keto/blob/master/cmd/migrate/root.go): db migrations · Runs schema migration workflows. · `cmd/migrate/root.go`
- [Ops cmds](https://github.com/ory/keto/blob/master/cmd/check/root.go): cli tools · Check, expand, status, namespace, and relation tuple administration. · `cmd/check/root.go`

### API Surfaces

REST, gRPC, and generated client/server boundaries.

- [REST spec](https://github.com/ory/keto/blob/master/spec/api.json): openapi spec · API contract and generated REST surface. · `spec/api.json`
- [Swagger docs](https://github.com/ory/keto/blob/master/doc_swagger.go): openapi doc · Embedded REST documentation and schema glue. · `doc_swagger.go`
- [HTTP client](https://github.com/ory/keto/blob/master/internal/httpclient/client.go): generated client · Generated REST client for the public API. · `internal/httpclient/client.go`
- [API models](https://github.com/ory/keto/blob/master/ketoapi/public_api_definitions.go): api encoding · Shared request/response encoders and public API definitions. · `ketoapi/public_api_definitions.go`
- [gRPC APIs](https://github.com/ory/keto/blob/master/proto/ory/keto/relation_tuples/v1alpha2/check_service.proto): protobuf services · Relation tuple, check, expand, namespaces, version, and OPL syntax services. · `proto/ory/keto/relation_tuples/v1alpha2/check_service.proto`

### Authorization Core

Tuple, check, expand, and namespace logic.

- [Check engine](https://github.com/ory/keto/blob/master/internal/check/engine.go): permission eval · Evaluates authorization checks across tuple relations and rewrite rules. · `internal/check/engine.go`
- [Eval steps](https://github.com/ory/keto/blob/master/internal/check/step.go): pipeline steps · Direct, dispatch, expand, rewrite, invert, traverse, and execution orchestration. · `internal/check/step.go`
- [Expand engine](https://github.com/ory/keto/blob/master/internal/expand/engine.go): tree expansion · Builds subject-set and permission expansion trees. · `internal/expand/engine.go`
- [Tuple service](https://github.com/ory/keto/blob/master/internal/relationtuple/handler.go): tuple read/write · Manages relation tuple reads, writes, and transactional operations. · `internal/relationtuple/handler.go`
- [Namespace svc](https://github.com/ory/keto/blob/master/internal/namespace/namespacehandler/handler.go): opl policy · Handles namespace definitions and OPL-backed policy materialization. · `internal/namespace/namespacehandler/handler.go`
- [OPL parser](https://github.com/ory/keto/blob/master/schema/parser.go): language parser · Lexes, parses, and validates namespace definitions. · `schema/parser.go`
- [Policy AST](https://github.com/ory/keto/blob/master/internal/namespace/ast/ast_definitions.go): namespace model · AST and namespace definition structures used by policy loading. · `internal/namespace/ast/ast_definitions.go`

### Storage & Migrations

SQL persistence, tuple identity, and migration support.

- [SQL store](https://github.com/ory/keto/blob/master/internal/persistence/sql/persister.go): tuple persistence · Persists tuples and traversals in SQL backends. · `internal/persistence/sql/persister.go`
- [UUID map](https://github.com/ory/keto/blob/master/internal/persistence/sql/uuid_mapping.go): identity mapping · Maps tuple identity to stable UUID-backed storage identifiers. · `internal/persistence/sql/uuid_mapping.go`
- [Migrations](https://github.com/ory/keto/blob/master/internal/persistence/sql/migrations/uuidmapping/uuid_mapping_migrator.go): dialect migrations · Per-dialect SQL and migration orchestration. · `internal/persistence/sql/migrations/uuidmapping/uuid_mapping_migrator.go`
- [popx](https://github.com/ory/keto/blob/master/oryx/popx/migrator.go): migration engine · Shared migration subsystem used to apply SQL changes. · `oryx/popx/migrator.go`

### Composition & Ops

Configuration, daemon lifecycle, observability, and external integrations.

- [Driver root](https://github.com/ory/keto/blob/master/internal/driver/registry.go): composition root · Loads config, selects registry, and assembles runtime dependencies. · `internal/driver/registry.go`
- [Config watch](https://github.com/ory/keto/blob/master/internal/driver/config/provider.go): config/provider · Loads config and watches namespace sources for changes. · `internal/driver/config/provider.go`
- [Telemetry](https://github.com/ory/keto/blob/master/oryx/otelx/otel.go): tracing/logging · Tracing, metrics, and logging integration. · `oryx/otelx/otel.go`
- [DB layer](https://github.com/ory/keto/blob/master/internal/x/dbx/dsn_postgres.go): sql connectors · Dialect-specific SQL connection and DSN helpers. · `internal/x/dbx/dsn_postgres.go`
- [E2E suite](https://github.com/ory/keto/blob/master/internal/e2e/full_suit_test.go): validation · End-to-end checks across CLI, REST, gRPC, and storage. · `internal/e2e/full_suit_test.go`

## Connections

- main.go → CLI root: dispatches · Main program hands off to the CLI command tree.
- CLI root → Server cmd: starts · Root command exposes the server mode.
- CLI root → Migrate cmd: runs · Root command exposes migration operations.
- CLI root → Ops cmds: exposes · Root command exposes check, expand, status, namespace, and tuple tools.
- Server cmd → Driver root: composes · Server startup resolves runtime dependencies through the driver root.
- Driver root → Config watch: loads · Composition root reads runtime config and namespace sources.
- Driver root → Telemetry: instruments · Driver root wires tracing, metrics, and logging.
- Driver root → DB layer: connects · Driver root establishes database connectivity.
- Driver root → SQL store: injects · Persistence implementation is assembled into the runtime.
- Driver root → Check engine: injects · Check engine is wired into the server boundary.
- Driver root → Expand engine: injects · Expand engine is wired into the server boundary.
- Driver root → Tuple service: injects · Tuple handlers are wired into the server boundary.
- Driver root → Namespace svc: injects · Namespace service is wired into the server boundary.
- Migrate cmd → popx: invokes · Migration command uses the shared migration runner.
- Ops cmds → Check engine: calls · CLI check command exercises the permission engine.
- Ops cmds → Expand engine: calls · CLI expand command exercises the expansion engine.
- Ops cmds → Tuple service: calls · CLI relation-tuple commands manage tuple data.
- Ops cmds → Namespace svc: calls · CLI namespace commands validate and generate policy definitions.
- REST spec → Swagger docs: documents · OpenAPI spec is embedded into docs and generated artifacts.
- REST spec → HTTP client: generates · REST spec drives the generated HTTP client.
- REST spec → API models: shares models · Public REST models and encoders align with the spec.
- gRPC APIs → Check engine: wraps · gRPC check service delegates to core permission evaluation.
- gRPC APIs → Expand engine: wraps · gRPC expand service delegates to tree expansion.
- gRPC APIs → Tuple service: wraps · gRPC relation-tuple services delegate to tuple management.
- gRPC APIs → Namespace svc: wraps · gRPC namespaces and OPL services delegate to namespace logic.
- Check engine → Eval steps: executes · Permission evaluation proceeds through stepwise traversal and rewrite processing.
- Check engine → SQL store: reads · Engine depends on tuple persistence for authorization decisions.
- Expand engine → SQL store: reads · Expansion queries tuple storage for relationship traversal.
- Tuple service → SQL store: reads/writes · Tuple service persists relationship data.
- Tuple service → UUID map: normalizes · Tuple service uses UUID mapping for stable identifiers.
- Namespace svc → OPL parser: validates · Namespace service consumes the OPL parser and validator.
- Namespace svc → Policy AST: builds · Parsed policies are represented as namespace AST structures.
- OPL parser → Policy AST: produces · Parser output feeds the namespace model.
- SQL store → UUID map: uses · SQL persistence relies on UUID mapping tables and logic.
- SQL store → Migrations: evolves via · Schema and storage layout are maintained by migrations.
- Migrations → popx: runs on · Dialect-specific migrations execute through popx.
- E2E suite → Server cmd: covers · End-to-end tests exercise the running server.
- E2E suite → gRPC APIs: covers · End-to-end tests validate the gRPC API surface.
- E2E suite → REST spec: covers · End-to-end tests validate REST behavior.
