diff --git a/devenv.nix b/devenv.nix index ff5e4ed..6fadfba 100644 --- a/devenv.nix +++ b/devenv.nix @@ -1,20 +1,203 @@ # The development environment: `devenv shell`, or direnv with `.envrc`. The package, modules and # checks are in `flake.nix`. -{ pkgs, ... }: +# +# `devenv up` runs the demo's host side (see `docs/demo.md`): the broker from this checkout, with +# its web app, Pocket ID for approvers to sign in with, and the box VM its SSH targets reach. +{ pkgs, lib, config, ... }: +let + demo = import ./nix/demo/settings.nix; + state = "${config.devenv.state}/slop-demo"; + # Sockets, whose paths must be short + run = "${config.devenv.runtime}/slop-demo"; + issuer = "http://localhost:${toString demo.ports.pocketId}"; + appUrl = "http://localhost:${toString demo.ports.web}"; + + # `@group@` becomes the user's primary group, whose members may approve + brokerConfig = (pkgs.formats.toml { }).generate "slop-demo.toml" { + requesters.agent = { vsock_cid = demo.agentCid; policy = "agent"; }; + policies.agent.ops = { + query.action = "auto"; + ssh = { + action = "ask"; + leases = [ "5m" "1h" ]; + rules = [ + { match.target = "router"; action = "ask"; confirm = "passkey"; } + { match.target = "vault"; action = "deny"; } + ]; + }; + }; + + vsock_port = 6262; + approve_socket = "${run}/approve.sock"; + approver_group = "@group@"; + audit_log = "${state}/audit.jsonl"; + + ssh = { + ssh = "${pkgs.openssh}/bin/ssh"; + key = "${state}/broker-key"; + known_hosts = "${state}/known_hosts"; + targets = lib.genAttrs [ "web" "router" "vault" ] (_: { + hostname = "localhost"; + user = "box"; + ssh_opts = [ "-p" (toString demo.ports.boxSsh) ]; + }); + }; + + web = { + listen = "127.0.0.1:${toString demo.ports.web}"; + url = appUrl; + inherit issuer; + client_id = "slop-approve"; + client_secret = "${state}/oidc-secret"; + group = "slop-approvers"; + state_dir = "${state}/web"; + }; + }; + + # Pocket ID's admin API, with the key `devenv up` made + pocketIdApi = '' + api() { + local method=$1 path=$2 + shift 2 + curl -sS --fail-with-body -X "$method" "${issuer}/api/$path" \ + -H "X-API-Key: $(sed -n 's/^STATIC_API_KEY=//p' ${state}/pocket-id.env)" -H 'Content-Type: application/json' "$@" + } + ''; +in { languages.rust.enable = true; # `pkg-config` with OpenSSL for Web Push's payload encryption and SQLite for the web app's # sessions, `git` for the builder's tests - packages = with pkgs; [ pkg-config openssl sqlite git nixpkgs-fmt ]; + packages = with pkgs; [ pkg-config openssl sqlite git nixpkgs-fmt curl jq ]; + + # For `slop-approve` in this shell + env.SLOP_APPROVE = "unix:${run}/approve.sock"; scripts = { + slop-approve = { + description = "Answer the demo's requests (built from this checkout)"; + exec = ''exec cargo run -q --manifest-path "$DEVENV_ROOT/Cargo.toml" --bin slop-broker -- approve "$@"''; + }; + demo = { + description = "Boot the demo's agents' VM on this terminal; needs `devenv up` running"; + exec = '' + if [ ! -S ${run}/approve.sock ]; then + echo "The broker isn't running: start \`devenv up\` first." >&2 + exit 1 + fi + exec nix run "$DEVENV_ROOT#demo" -- "$@" + ''; + }; + demo-box = { + description = "SSH into the demo's box VM as `box`, with the broker's key"; + exec = '' + exec ${pkgs.openssh}/bin/ssh -F none -i ${state}/broker-key -o IdentitiesOnly=yes \ + -o UserKnownHostsFile=${state}/known_hosts -o StrictHostKeyChecking=accept-new \ + -p ${toString demo.ports.boxSsh} box@localhost "$@" + ''; + }; + demo-sign-in = { + description = "Print a one-time link that signs in to the demo's Pocket ID as the approver"; + exec = '' + ${pocketIdApi} + user=$(api GET 'users?search=demo' | jq -r '.data[] | select(.username == "demo") | .id') + echo "${issuer}/lc/$(api POST "users/$user/one-time-access-token" -d '{"ttl":"1h"}' | jq -r .token)" + ''; + }; + demo-reset = { + description = "Delete the demo's state (Pocket ID's users and passkeys, keys, audit log)"; + exec = ''rm -rf ${state}''; + }; vm-test = { description = "Run the NixOS VM test of the broker module (needs KVM)"; exec = ''nix build -L --no-link "$DEVENV_ROOT#checks.x86_64-linux.vm" "$@"''; }; }; + # The demo's host side; not for `devenv test` + tasks = lib.mkIf (!config.devenv.isTesting) { + "slop-demo:keys" = { + description = "Make the demo's secrets and SSH keys"; + exec = '' + set -eu + umask 077 + mkdir -p ${state}/box ${state}/pocket-id ${state}/web ${run} + + if [ ! -e ${state}/pocket-id.env ]; then + key() { ${pkgs.openssl}/bin/openssl rand -hex 32; } + printf 'ENCRYPTION_KEY=%s\nSTATIC_API_KEY=%s\n' "$(key)" "$(key)" > ${state}/pocket-id.env + fi + + # The box VM gets the broker's public key and its own host key through its shared directory + [ -e ${state}/broker-key ] || ${pkgs.openssh}/bin/ssh-keygen -q -t ed25519 -N "" -C slop-demo -f ${state}/broker-key + cp ${state}/broker-key.pub ${state}/box/broker.pub + [ -e ${state}/box/host-key ] || ${pkgs.openssh}/bin/ssh-keygen -q -t ed25519 -N "" -C slop-demo-box -f ${state}/box/host-key + ''; + }; + + # Once: the approver `demo` in the group `slop-approvers`, and the broker's OIDC client + "slop-demo:pocket-id" = { + description = "Set up the demo's approver and OIDC client in Pocket ID"; + after = [ "devenv:processes:pocket-id" ]; + status = "test -s ${state}/oidc-secret"; + exec = '' + set -eu + umask 077 + ${pocketIdApi} + + user=$(api POST users -d '{"username":"demo","email":"demo@example.org","firstName":"Demo","lastName":"Approver","displayName":"Demo Approver","isAdmin":false}' | jq -r .id) + group=$(api POST user-groups -d '{"name":"slop-approvers","friendlyName":"slop approvers"}' | jq -r .id) + api PUT "user-groups/$group/users" -d "$(jq -n --arg u "$user" '{userIds: [$u]}')" >/dev/null + + api POST oidc/clients -d '{"id":"slop-approve","name":"slop-broker demo","callbackURLs":["${appUrl}/auth/callback"],"isPublic":false,"pkceEnabled":true,"isGroupRestricted":true}' >/dev/null + api PUT oidc/clients/slop-approve/allowed-user-groups -d "$(jq -n --arg g "$group" '{userGroupIds: [$g]}')" >/dev/null + api POST oidc/clients/slop-approve/secrets | jq -j .secret > ${state}/oidc-secret.new + mv ${state}/oidc-secret.new ${state}/oidc-secret + ''; + }; + }; + + processes = lib.mkIf (!config.devenv.isTesting) { + pocket-id = { + exec = '' + set -a + . ${state}/pocket-id.env + exec ${pkgs.pocket-id}/bin/pocket-id + ''; + cwd = "${state}/pocket-id"; + env = { + APP_URL = issuer; + HOST = "127.0.0.1"; + PORT = toString demo.ports.pocketId; + ANALYTICS_DISABLED = "true"; + }; + after = [ "slop-demo:keys" ]; + ready = { + http.get = { port = demo.ports.pocketId; path = "/.well-known/openid-configuration"; }; + period = 1; + }; + }; + + slop-broker = { + exec = '' + sed "s/@group@/$(id -gn)/" ${brokerConfig} > ${state}/broker.toml + exec cargo run -q --bin slop-broker -- serve --config ${state}/broker.toml + ''; + cwd = config.devenv.root; + after = [ "slop-demo:pocket-id" ]; + ready = { + exec = "test -S ${run}/approve.sock"; + period = 1; + }; + }; + + box = { + exec = ''SHARED_DIR=${state}/box exec nix run "$DEVENV_ROOT#demo-box"''; + after = [ "slop-demo:keys" ]; + }; + }; + # `devenv test` enterTest = '' cargo clippy --all-targets -- -D warnings diff --git a/docs/demo.md b/docs/demo.md new file mode 100644 index 0000000..ee25e84 --- /dev/null +++ b/docs/demo.md @@ -0,0 +1,75 @@ +# Demo + +A setup for trying the broker by hand, laid out like a real deployment: the broker runs on the host, +the agents are in a VM that reaches it only over vsock, and its SSH targets are another VM. Paseo +and real coding agents are out of scope: you type the agent's `slop` commands yourself. + +It needs the devenv shell (direnv with `.envrc`, which provides [devenv](https://devenv.sh)) and +KVM, with `/dev/vhost-vsock` usable by your user. In one terminal: + +```sh +devenv up # the host side; `devenv up -d` runs it in the background +``` + +and in another, in the devenv shell: + +```sh +demo # the agents' VM, on this terminal +``` + +## What's in it + +| Part | Where | Runs | +|---|---|---| +| Broker, from this checkout (`cargo run`) | Host | `devenv up` (process `slop-broker`) | +| Web app | | Part of the broker | +| [Pocket ID](https://pocket-id.org), for approvers to sign in | | `devenv up` (process `pocket-id`) | +| Box VM ([`box.nix`](../nix/demo/box.nix)): `sshd` for the targets | `localhost:2222`, as `box` | `devenv up` (process `box`) | +| Agents' VM ([`agent.nix`](../nix/demo/agent.nix)), signed in as `agent` | Your terminal | `demo` | + +The ports are on the host's loopback, so they must be free there; they and the agents' VM's vsock +CID are in [`settings.nix`](../nix/demo/settings.nix). The broker's configuration is in +[`devenv.nix`](../devenv.nix): + +- **Requester:** `agent`, the agents' VM's vsock CID. The VMs have no network beyond their + forwarded ports, and only vsock links the agents' VM to the host. +- **Approvers:** members of your primary group, through `slop-approve` in the devenv shell, and the + Pocket ID user `demo` (in the group `slop-approvers`) through the web app. +- **Targets:** `web`, `router` and `vault` all reach the box VM. +- **Policy:** + - The box queries (`slop status`, `slop logs`, `slop failed`) run without asking. + - `slop ssh web` asks, and offers leases of 5 minutes or an hour. + - `slop ssh router` asks and needs a passkey (`confirm = "passkey"`), so only the web app can + approve it. + - `slop ssh vault` is denied. + +## Approving + +- **Terminal:** `slop-approve` in the devenv shell lists pending requests; answer one with + `slop-approve once||deny`. +- **Web app:** `demo-sign-in` prints a one-time link that signs in to Pocket ID as `demo`. Add a + passkey when Pocket ID asks, then sign in to ; the passkey also approves + `router` requests. Web Push works when the broker can reach the browser's push service. + +A request blocks until it's answered, and `slop ssh` forwards stdin. To keep the agent's shell, run +requests in the background with stdin closed: + +```sh +slop ssh web -- uptime ` runs one command there. Its console output is in +`devenv processes logs box`. + +## State + +The VMs keep nothing: `poweroff` (or Ctrl-A X) stops the agents' VM, and each boot starts from +scratch. The host side keeps its state in `.devenv/state/slop-demo` between runs: Pocket ID's +database (so passkeys survive), the keys, the audit log and the web app's sessions. `demo-reset` +deletes it while `devenv up` isn't running; the next `devenv up` sets Pocket ID up again. + +If the broker doesn't start, read `devenv processes logs slop-broker`, and the setup's output in +`devenv up`. diff --git a/flake.nix b/flake.nix index 28241e2..8605b44 100644 --- a/flake.nix +++ b/flake.nix @@ -21,6 +21,9 @@ packages = forSystems (pkgs: rec { slop-broker = pkgs.callPackage ./nix/package.nix { }; default = slop-broker; + # The demo's VMs (see `docs/demo.md`) + demo = import ./nix/demo/agent.nix { inherit pkgs slop-broker; }; + demo-box = import ./nix/demo/box.nix { inherit pkgs; }; }); overlays.default = final: _prev: { diff --git a/nix/demo/agent.nix b/nix/demo/agent.nix new file mode 100644 index 0000000..8566562 --- /dev/null +++ b/nix/demo/agent.nix @@ -0,0 +1,65 @@ +# The demo's agents' VM: an `agent` user with `slop`, which reaches the broker on the host over +# vsock, like a real agents' VM. `devenv up` runs the broker; `demo` boots this VM on the terminal. +{ pkgs, slop-broker }: +let + inherit (pkgs) lib; + settings = import ./settings.nix; + + help = pkgs.writeShellScriptBin "demo-help" '' + cat <<'EOF' + + slop-broker demo. You're `agent` on the agents' VM; the broker runs on the host, and the + targets `web`, `router` and `vault` are all the box VM. + + slop status web sshd.service runs without asking (policy: auto) + slop logs web sshd.service -n 20 + slop ssh web -- uptime asks an approver; leases of 5m or 1h are offered + slop ssh router -- uptime asks too, and approving needs a passkey in the web app + slop ssh vault -- true denied by policy + slop requests your pending requests and leases + slop guide the guide agents get + + Approve on the host: `slop-approve` in the devenv shell, or the web app at + http://localhost:${toString settings.ports.web} (`demo-sign-in` prints a sign-in link). + + A request blocks until it's answered. To keep this shell, run it in the background with stdin + closed (`slop ssh` forwards stdin): `slop ssh web -- uptime