Gateway Architecture
The Go runtime gateway. Its internal packages, request handling, dependencies, failure behavior, and configuration.
The runtime gateway is the enforcement point. It is a single Go binary with a deliberately small surface: four HTTP routes, two data stores, and a set of in-process libraries. Everything an agent can do goes through it.
Routes
| Route | Purpose |
|---|---|
GET /health/live | Process liveness. Reports whether authorization is wired |
GET /health/ready | 200 when PostgreSQL and Redis answer within one second, otherwise 503 |
POST /api/v1/runtime/authorize | Authenticate, apply budgets, evaluate policy, record the decision |
POST /api/v1/runtime/execute | Run an allowed or approved decision through its adapter, at most once |
Every other path returns 404 not_found. There are no stub routes that return success.
Internal packages
authz.Service depends only on interfaces (Directory, PolicyLoader, Store, Nonces, Limiter, Circuit, Analyzer). The PostgreSQL and Redis implementations are wired in cmd/gateway/main.go. The same service runs against in-memory fakes in tests, benchmarks, and the chaos harness.
Request handling
- HTTP layer. The server reads at most
GATEWAY_MAX_BODY_BYTES(1 MiB, the maximum). Headers time out after 5 s, reads and writes after 10 s. Responses carryX-Content-Type-Options: nosniff,X-Frame-Options: DENY,Referrer-Policy: no-referrer, andCache-Control: no-store. - Authentication. Signature, clock, and nonce. See Authentication.
- Budgets. Redis counters. See Budgets and limits.
- Decision. Identity checks, policy load, evaluation, and a single recording transaction. See Authorization.
- Execution. Claim, run, complete. See Tool execution lifecycle.
Each request emits a structured log line with a trace id, span id, route, and duration, and updates Prometheus counters. Logs never include request bodies.
Failure behavior
| Dependency down | Effect |
|---|---|
| PostgreSQL | Readiness 503. Authorize and execute return 500 internal_error. No allow |
| Redis | Readiness 503. Nonce reservation and budgets fail, returning 500. No allow |
| Intelligence engine | Analysis reports unavailable within INTELLIGENCE_TIMEOUT. Calls complete normally |
| External API | Execution fails with timeout or adapter_error. Five failures open the circuit |
If DATABASE_URL or REDIS_URL is unset, the gateway still starts and answers liveness, but readiness stays 503 and the runtime routes return 500 ("authorization is not configured"). It never authorizes without its stores.
Configuration
| Variable | Default | Notes |
|---|---|---|
GATEWAY_HTTP_ADDR | 127.0.0.1:8081 | Listen address |
DATABASE_URL | — | postgres:// URL. Production requires the agentshield_app role |
REDIS_URL | — | redis:// or rediss:// |
GATEWAY_MAX_BODY_BYTES | 1048576 | 1 to 1 MiB |
GATEWAY_SHUTDOWN_TIMEOUT | 10s | 1 s to 1 min |
GATEWAY_TOOLS_CONFIG | — | Tools file enabling http.request |
INTELLIGENCE_URL | — | Enables content analysis |
INTELLIGENCE_TOKEN | — | Bearer token for the analyzer; required in production |
INTELLIGENCE_TIMEOUT | 500ms | Up to 5 s |
GATEWAY_METRICS_ADDR | 127.0.0.1:9081 | Must differ from the API address. off disables |
AGENTSHIELD_ENV | development | development, test, or production |
Startup fails, rather than running in a weaker mode, when configuration is invalid: an unparseable tools file, a missing credential environment variable named by the tools file, allow_insecure_http in production, or a metrics address equal to the API address.
Health check subcommand
Distroless images have no shell or curl. The binary checks its own readiness:
gateway healthcheck # exit code 0 when /health/ready returns 200Performance
Benchmarks with every dependency in memory measure about 126 µs for a full signed authorization, most of it Ed25519 verification (about 81 µs). Real latency is dominated by the PostgreSQL transaction and Redis round trips. See benchmarks/README.md in the repository for the recorded baseline and conditions.
Implementation references
Was this page helpful?