Skip to Content
AgentShield
Architecture

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

RoutePurpose
GET /health/liveProcess liveness. Reports whether authorization is wired
GET /health/ready200 when PostgreSQL and Redis answer within one second, otherwise 503
POST /api/v1/runtime/authorizeAuthenticate, apply budgets, evaluate policy, record the decision
POST /api/v1/runtime/executeRun 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

gateway/main.go
event-relay/main.go
chaos-harness/main.go
httpserver — routing, body limits, error envelope, security headers
authz — authenticate, decide, execute orchestration
store — PostgreSQL decisions, executions, approvals, audit
nonce — Redis nonce reservation
limits — budgets and circuit breaker
adapter — registry, sample.echo, http.request
analysis — intelligence engine client
auditchain — canonical JSON and chain hashing
redact — credential-like key redaction
jsondoc — strict JSON parsing, duplicate keys rejected
relay — outbox publisher, evidence, reliability
telemetry — Prometheus metrics
chaos — catalog scenario harness

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

  1. 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 carry X-Content-Type-Options: nosniff, X-Frame-Options: DENY, Referrer-Policy: no-referrer, and Cache-Control: no-store.
  2. Authentication. Signature, clock, and nonce. See Authentication.
  3. Budgets. Redis counters. See Budgets and limits.
  4. Decision. Identity checks, policy load, evaluation, and a single recording transaction. See Authorization.
  5. 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 downEffect
PostgreSQLReadiness 503. Authorize and execute return 500 internal_error. No allow
RedisReadiness 503. Nonce reservation and budgets fail, returning 500. No allow
Intelligence engineAnalysis reports unavailable within INTELLIGENCE_TIMEOUT. Calls complete normally
External APIExecution 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

VariableDefaultNotes
GATEWAY_HTTP_ADDR127.0.0.1:8081Listen address
DATABASE_URL—postgres:// URL. Production requires the agentshield_app role
REDIS_URL—redis:// or rediss://
GATEWAY_MAX_BODY_BYTES10485761 to 1 MiB
GATEWAY_SHUTDOWN_TIMEOUT10s1 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_TIMEOUT500msUp to 5 s
GATEWAY_METRICS_ADDR127.0.0.1:9081Must differ from the API address. off disables
AGENTSHIELD_ENVdevelopmentdevelopment, 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 200

Performance

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.

Was this page helpful?

On this page