How ory/keto works
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: registry.go Driver root · persister.go SQL store · root.go Ops cmds · check_service.proto gRPC APIs
Components 26
Entry & CLIs
Process entrypoint and operational subcommands.
- main.go · Wires the command tree and boots the process.
main.go - CLI root · Top-level command wiring for server and operational tools.
cmd/root.go - Server cmd · Starts the authorization API server.
cmd/server/serve.go - Migrate cmd · Runs schema migration workflows.
cmd/migrate/root.go - Ops cmds · Check, expand, status, namespace, and relation tuple administration.
cmd/check/root.go
API Surfaces
REST, gRPC, and generated client/server boundaries.
- REST spec · API contract and generated REST surface.
spec/api.json - Swagger docs · Embedded REST documentation and schema glue.
doc_swagger.go - HTTP client · Generated REST client for the public API.
internal/httpclient/client.go - API models · Shared request/response encoders and public API definitions.
ketoapi/public_api_definitions.go - gRPC APIs · 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 · Evaluates authorization checks across tuple relations and rewrite rules.
internal/check/engine.go - Eval steps · Direct, dispatch, expand, rewrite, invert, traverse, and execution orchestration.
internal/check/step.go - Expand engine · Builds subject-set and permission expansion trees.
internal/expand/engine.go - Tuple service · Manages relation tuple reads, writes, and transactional operations.
internal/relationtuple/handler.go - Namespace svc · Handles namespace definitions and OPL-backed policy materialization.
internal/namespace/namespacehandler/handler.go - OPL parser · Lexes, parses, and validates namespace definitions.
schema/parser.go - Policy AST · 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 · Persists tuples and traversals in SQL backends.
internal/persistence/sql/persister.go - UUID map · Maps tuple identity to stable UUID-backed storage identifiers.
internal/persistence/sql/uuid_mapping.go - Migrations · Per-dialect SQL and migration orchestration.
internal/persistence/sql/migrations/uuidmapping/uuid_mapping_migrator.go - popx · Shared migration subsystem used to apply SQL changes.
oryx/popx/migrator.go
Composition & Ops
Configuration, daemon lifecycle, observability, and external integrations.
- Driver root · Loads config, selects registry, and assembles runtime dependencies.
internal/driver/registry.go - Config watch · Loads config and watches namespace sources for changes.
internal/driver/config/provider.go - Telemetry · Tracing, metrics, and logging integration.
oryx/otelx/otel.go - DB layer · Dialect-specific SQL connection and DSN helpers.
internal/x/dbx/dsn_postgres.go - E2E suite · End-to-end checks across CLI, REST, gRPC, and storage.
internal/e2e/full_suit_test.go
Connections 39
- 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.