diff --git a/docs/extensibility/supervisor-middleware/index.mdx b/docs/extensibility/supervisor-middleware/index.mdx index 4cdd39cddb..7a169aec46 100644 --- a/docs/extensibility/supervisor-middleware/index.mdx +++ b/docs/extensibility/supervisor-middleware/index.mdx @@ -20,6 +20,8 @@ A network policy decides whether an agent can call an API. Use middleware when y The [content guard example](https://github.com/ai-openshell/openshell/tree/main/examples/supervisor-middleware-content-guard) shows a complete middleware service that performs checks like these. +Middleware can also add headers to a request. The [ANS identity example](https://github.com/ai-openshell/openshell/tree/main/examples/supervisor-middleware-ans-identity) runs an external service that signs each allowed request with an agent identity while the signing key stays outside the sandbox. It uses the legacy HTTP hooks and is verified against OpenShell v0.1.7. + ## How Middleware Processes Traffic The sandbox supervisor calls middleware after it checks network policy and before it forwards traffic to the external service: diff --git a/examples/supervisor-middleware-ans-identity/README.md b/examples/supervisor-middleware-ans-identity/README.md new file mode 100644 index 0000000000..c73236994b --- /dev/null +++ b/examples/supervisor-middleware-ans-identity/README.md @@ -0,0 +1,109 @@ + + +# Supervisor Middleware ANS Identity + +> [!WARNING] +> Supervisor middleware is a research preview. Its policy and service contracts may change without compatibility guarantees. Use it only to prototype and evaluate middleware integrations. + +This example attaches a verifiable agent identity to the HTTP requests a sandbox sends, without giving the sandbox the signing key. An external middleware service, the [ANS identity extension](https://github.com/agentnameservice/ans-openshell), signs each allowed request as a registered [Agent Name Service](https://github.com/agentnameservice/ans) (ANS) agent. A reference receiving service verifies the signature and reports which agent called. The example runs a real gateway, sandbox supervisor, and sandbox on the Docker driver, and it changes no OpenShell runtime code. + +> [!IMPORTANT] +> The extension implements the legacy `EvaluateHttpRequest` RPC, which OpenShell removes in 0.2.0. This example is verified against OpenShell v0.1.7 only. The extension has not moved to [HTTP hooks](../../docs/extensibility/supervisor-middleware/operations.mdx) yet. Start new middleware from the [content guard example](../supervisor-middleware-content-guard/README.md), which implements them. + +The extension, its fixtures, and its smoke test live in the extension repository. This directory holds a launcher that runs them at a pinned commit, reference copies of the middleware registration and sandbox policy the test renders, and this walkthrough. + +## Trust boundary + +The sandboxed workload holds no signing key and no credential. The sandbox supervisor, which runs outside the sandbox, checks network policy and then calls the middleware with the allowed request. The middleware confirms that the operator approved this sandbox for the requested identity, signs the request with the agent's key, and attaches the agent's transparency-log receipt and status token. OpenShell applies those header changes before the request leaves. The receiving service verifies the receipt and the status token against the transparency log's root keys, and the proof against the agent certificate that the status token lists. It learns which agent called without a shared secret and without calling the middleware. + +OpenShell overwrites identity headers that the workload sets. The middleware denies a sandbox the operator has not approved, so its request never leaves. If the middleware is unavailable, the policy's `on_error: fail_closed` blocks the request instead of sending it unsigned. + +## Prerequisites + +- Linux (x86_64 or arm64) or macOS on Apple silicon. +- Docker, reachable over the Unix socket of the current `docker context`, with access to `ghcr.io` and `nvcr.io` for the pinned OpenShell runtime images and the workload base image. +- `git`, Go 1.26 or later, `curl`, `jq`, `tar`, and `sha256sum` or `shasum` on the host. +- A non-loopback IPv4 address of this machine that containers can reach. The smoke test detects one. Set `ANS_SMOKE_HOST` to override it. During the run the gateway, the middleware, and the receiving service listen on all interfaces. + +The example does not use an installed OpenShell. It downloads the pinned OpenShell CLI and gateway release assets, verifies their SHA-256 checksums, and keeps the gateway's database, TLS bundle, and CLI registration in a temporary directory. Its gateway uses a `sandbox_label` of its own on the Docker daemon, so it does not touch an existing OpenShell installation that shares the daemon. + +## Run the smoke example + +Run the launcher from the repository root: + +```shell +./examples/supervisor-middleware-ans-identity/smoke.sh +``` + +The launcher fetches the extension at the pinned commit into a temporary directory and runs its smoke test. The test does the following: + +1. Builds the middleware, the receiving service, and a fixture generator from that checkout. +2. Mints a disposable transparency-log root key, one registered identity, the gateway's JWT signing key, and a private CA for the middleware's gRPC listener. +3. Starts the receiving service, the middleware, and an authenticated gateway with the middleware registered. +4. Creates a sandbox with the policy below and approves that sandbox's gateway-issued ID for the identity. +5. Runs the checks in this table. + +| Check | Expected observation | +| --- | --- | +| Authorized sandbox | A plain `POST /v1/tasks` from the workload reaches the receiving service signed. The service returns the operator-assigned ANS name and agent ID. | +| Forged identity headers | OpenShell overwrites `DPoP`, `X-SCITT-Receipt`, and `X-ANS-Status-Token` headers that the workload sets. The service still verifies the assigned identity. | +| Unsigned direct request | The receiving service rejects a request that bypasses OpenShell with `401` and logs the rejection. The next two checks rely on that log. | +| Unapproved sandbox | A second sandbox whose ID is not approved gets `403 middleware_denied` with reason `ans_identity_unauthorized`. The receiving service's log does not grow, so it never saw the request. | +| Middleware unavailable | With the middleware stopped, the request gets `403 middleware_failed` under `on_error: fail_closed`. The receiving service's log does not grow. | + +A passing run ends with this line: + +```text +ALL PASS 5 checks against OpenShell v0.1.7 (5d25211) +``` + +The test then removes its sandboxes, processes, containers, volumes, and temporary material, including the minted keys. On failure it exits non-zero and keeps the logs, including each sandbox's supervisor container log. The extension repository's own tests cover replay and body tampering at the verifier. + +To send requests yourself, leave the stack running after the checks pass: + +```shell +./examples/supervisor-middleware-ans-identity/smoke.sh --keep-running +``` + +The test prints the commands to use the running stack. Press Ctrl-C to clean up. + +To print the complete gateway and middleware configuration without starting a gateway or a sandbox, run: + +```shell +./examples/supervisor-middleware-ans-identity/smoke.sh --print-config +``` + +## Configuration + +[`gateway.toml`](gateway.toml) shows the middleware registration. The gateway authenticates the service by the CA that issued its serving certificate and calls it with gateway-issued JWTs, which the service verifies against the gateway's JWKS. `binding_policy = "allowlist"` with `bindings = ["http_request_pre_credentials"]` requires the request operation. OpenShell refuses a service that stops advertising it instead of skipping the service. + +[`policy.yaml`](policy.yaml) attaches the middleware to the receiving service's host and allows `curl` to `POST /v1/tasks` there. The entry uses `order: 100`, so it runs after request middleware with a lower `order`. `on_error: fail_closed` blocks the request when the middleware is unreachable or fails. When the middleware cannot sign, it denies the request. The `config.identity` field names an identity from the middleware's configuration. The operator must also approve the sandbox's gateway-issued ID for that identity in the middleware's `sandbox_identities` map before the middleware signs anything. Sandbox names and labels cannot grant identity access. + +## Fixture trust and production trust + +The test mints one transparency-log root key per run. It hands that key to the middleware and the receiving service and pins the identity's receipt and status token to files, so the middleware verifies them at startup and contacts no registry. In production you give the middleware the real transparency log's base URL and published root keys. It fetches and refreshes the receipt and status token itself, and the receiving service pins the same published keys. This example does not exercise that fetch path. The proof, the headers, the verifier, and the sandbox authorization path run the same code in both cases. + +The gateway's TLS bundle and the CLI's client certificate come from `openshell-gateway generate-certs`. The test replaces only the gateway's JWT signing key, with the one the fixtures minted, so the middleware's JWKS matches. + +## Validated versions + +| Component | Version | Pinned by | +| --- | --- | --- | +| ANS identity extension | [v0.1.2](https://github.com/agentnameservice/ans-openshell/releases/tag/v0.1.2) | Commit `e964b815d50c00dab925b10d1a155166479ffb7a` in [`smoke.sh`](smoke.sh) | +| OpenShell, last verified | v0.1.7 | The extension's `openshell-release.env` at that commit. It pins the CLI and gateway release assets by SHA-256 and the supervisor and sandbox runtime images by manifest digest. | + +The launcher fetches that commit and stops if the fetched commit differs. The tag is named for readers. The extension's CI runs the same test against that OpenShell release on every push to its main branch on `ubuntu-latest`. The test also passes on macOS (Apple silicon) with Rancher Desktop. + +No OpenShell CI job runs this example. The pin moves by pull request to this directory after the extension's test passes against a newer OpenShell release. That pull request changes the commit in `smoke.sh` and every version on this page. To try another extension commit, set `ANS_OPENSHELL_COMMIT` to its full SHA. + +## Limitations + +- The test downloads only the CLI and gateway release assets. OpenShell v0.1.11 and later ship the Docker driver as a separate binary, so the test cannot start a gateway from those releases. +- The middleware reads its sandbox approvals at startup. To approve a sandbox you edit the map and restart the service. On OpenShell v0.1.7, matching requests then fail closed with `middleware_failed` for a few seconds, until the supervisor's channel to the service reconnects. The test retries its first request through that window. +- The middleware cannot sign a request whose body or URL path contains an OpenShell credential placeholder. OpenShell resolves placeholders after request middleware runs, which would invalidate the signature, so the middleware denies those requests. +- The fixture status token lives one hour. A `--keep-running` stack stops signing when it lapses. +- The pinned OpenShell release has no Intel macOS gateway build, so the example runs on Linux and Apple silicon only. + +## Ownership + +The launcher in this directory runs the extension's smoke test unchanged. The [extension repository](https://github.com/agentnameservice/ans-openshell) maintains the extension, the test, its OpenShell pin, and its fixtures under its own contribution process. File problems with the extension or the test there. File problems with this launcher or this page in the OpenShell repository. diff --git a/examples/supervisor-middleware-ans-identity/gateway.toml b/examples/supervisor-middleware-ans-identity/gateway.toml new file mode 100644 index 0000000000..20d5c43126 --- /dev/null +++ b/examples/supervisor-middleware-ans-identity/gateway.toml @@ -0,0 +1,19 @@ +# SPDX-License-Identifier: Apache-2.0 + +# Middleware registration for the ANS identity example. The smoke test renders +# the complete gateway configuration, including gateway authentication and the +# Docker driver, with the addresses and certificates of the run. This fragment +# shows the registration it adds. Run `smoke.sh --print-config` for the rest. +# +# OpenShell authenticates the service by the CA that issued its serving +# certificate and requires the request operation, so OpenShell refuses a +# service that stops advertising it instead of skipping the service. + +[[openshell.supervisor.middleware]] +name = "ans-identity-smoke" +grpc_endpoint = "https://:50051" +tls_ca_cert_path = "/path/to/example-ca.pem" +max_payload_bytes = 1048576 +timeout = "2s" +binding_policy = "allowlist" +bindings = ["http_request_pre_credentials"] diff --git a/examples/supervisor-middleware-ans-identity/policy.yaml b/examples/supervisor-middleware-ans-identity/policy.yaml new file mode 100644 index 0000000000..499c2d7833 --- /dev/null +++ b/examples/supervisor-middleware-ans-identity/policy.yaml @@ -0,0 +1,37 @@ +# SPDX-License-Identifier: Apache-2.0 + +# Sandbox policy for the ANS identity example. The smoke test renders the same +# policy with the port the receiving service listens on for that run. This copy +# shows the shape. The workload may POST /v1/tasks to the receiving service on +# the gateway host, and the ANS middleware signs every such request. The +# middleware denies a request it cannot sign, and on_error: fail_closed blocks +# the request when the middleware is unreachable. +version: 1 + +network_middlewares: + ans-identity: + name: ANS identity + middleware: ans-identity-smoke + order: 100 + on_error: fail_closed + endpoints: + include: + - host.openshell.internal + config: + mode: sign + identity: example-agent + +network_policies: + callee: + name: callee + endpoints: + - host: host.openshell.internal + port: 8443 + protocol: rest + enforcement: enforce + rules: + - allow: + method: POST + path: /v1/tasks + binaries: + - path: /usr/bin/curl diff --git a/examples/supervisor-middleware-ans-identity/smoke.sh b/examples/supervisor-middleware-ans-identity/smoke.sh new file mode 100755 index 0000000000..7c47831c84 --- /dev/null +++ b/examples/supervisor-middleware-ans-identity/smoke.sh @@ -0,0 +1,92 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: Apache-2.0 + +set -euo pipefail + +# Fetches the ANS identity extension at a pinned commit and runs its OpenShell +# smoke test. That test downloads the OpenShell release it pins, starts a +# gateway on the Docker driver with the extension registered, creates +# sandboxes, and asserts the identity guarantees described in README.md. + +ANS_OPENSHELL_REPO="${ANS_OPENSHELL_REPO:-https://github.com/agentnameservice/ans-openshell.git}" +# The commit is the pin. It is the v0.1.2 release. Git tags can be moved, so +# the tag is named here only for readers. +ANS_OPENSHELL_COMMIT="${ANS_OPENSHELL_COMMIT:-e964b815d50c00dab925b10d1a155166479ffb7a}" + +usage() { + cat <&2 + usage >&2 + exit 2 + ;; + esac +done + +if [[ ! "$ANS_OPENSHELL_COMMIT" =~ ^[0-9a-f]{40}$ ]]; then + echo "ANS_OPENSHELL_COMMIT must be a full 40-character commit SHA" >&2 + exit 2 +fi + +for cmd in git go docker curl jq tar; do + command -v "$cmd" >/dev/null 2>&1 || { + echo "missing required command: $cmd" >&2 + exit 1 + } +done + +WORK="$(mktemp -d)" +cleanup() { + rm -rf "$WORK" +} +trap cleanup EXIT +# Bash runs these after the smoke test returns, so the test finishes removing +# its own sandboxes, processes, and keys before its checkout goes. +trap 'exit 130' INT +trap 'exit 143' TERM +trap 'exit 129' HUP + +EXTENSION="$WORK/ans-openshell" +echo "INFO fetching the ANS identity extension at $ANS_OPENSHELL_COMMIT" +git -c init.defaultBranch=main init --quiet "$EXTENSION" +git -C "$EXTENSION" remote add origin "$ANS_OPENSHELL_REPO" +git -C "$EXTENSION" fetch --quiet --depth 1 origin "$ANS_OPENSHELL_COMMIT" +git -C "$EXTENSION" -c advice.detachedHead=false checkout --quiet FETCH_HEAD + +fetched="$(git -C "$EXTENSION" rev-parse HEAD)" +if [[ "$fetched" != "$ANS_OPENSHELL_COMMIT" ]]; then + echo "fetched commit $fetched is not the pinned commit $ANS_OPENSHELL_COMMIT" >&2 + exit 1 +fi + +"$EXTENSION/examples/openshell-smoke/smoke.sh" "$@"