diff --git a/AGENTS.md b/AGENTS.md index 1a73463..1b94e84 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -50,7 +50,8 @@ of devenv's scripts, `devenv test` or the demo's host side. web app, Pocket ID and the box VM); `devenv processes logs ` shows a process's output and `devenv processes down` stops them. `demo` boots the agents' VM on the terminal and holds it until `poweroff`, so an agent should run it in the background with stdin from a pipe it controls. - `slop-approve` in the devenv shell answers requests, and `demo-box` SSHes into the box VM. Stop + `slop-approve` in the devenv shell answers requests, `demo-box` SSHes into the box VM, and + `demo-merge ` moves the demo repository's protected `master`. Stop the agents' VM with `poweroff` on its console or by stopping that background task, not with `pkill -f`, whose pattern also matches the shell running it. - `nix fmt` — formats Nix with `nixpkgs-fmt`. **Ask before running it on a file you didn't write**: diff --git a/devenv.nix b/devenv.nix index 6fadfba..5499b29 100644 --- a/devenv.nix +++ b/devenv.nix @@ -2,7 +2,8 @@ # checks are in `flake.nix`. # # `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. +# its web app and builder, Pocket ID for approvers to sign in with, the box VM its SSH targets +# reach, and the Git repository the agent pushes to. { pkgs, lib, config, ... }: let demo = import ./nix/demo/settings.nix; @@ -10,9 +11,54 @@ let # Sockets, whose paths must be short run = "${config.devenv.runtime}/slop-demo"; issuer = "http://localhost:${toString demo.ports.pocketId}"; + # Bare repositories the agents' VM reaches with `git://` + repositories = "${state}/git"; appUrl = "http://localhost:${toString demo.ports.web}"; - # `@group@` becomes the user's primary group, whose members may approve + # The builder: `slop-broker builder`, which the broker reaches through `ssh` (see `ssh` below). + # The repository is a local path, so its key is never used. + builderConfig = (pkgs.formats.toml { }).generate "slop-demo-builder.toml" { + repository = "${repositories}/repo.git"; + key = "/nonexistent"; + branch = "master"; + directory = "${state}/builder"; + # The host's own Nix first, so builds go through its daemon as the user + path = [ + "${pkgs.git}/bin" + "${pkgs.openssh}/bin" + "/run/current-system/sw/bin" + "/nix/var/nix/profiles/default/bin" + "${pkgs.nix}/bin" + ]; + }; + + # The broker's `ssh`: the host `builder` runs the builder from this checkout's build directly, as + # its forced command would; everything else is the real `ssh`. + ssh = pkgs.writeShellScript "slop-demo-ssh" '' + host="" previous="" + for arg; do + if [ "$previous" = -- ]; then host=$arg; break; fi + previous=$arg + done + + if [ "$host" = builder ]; then + exec ${config.devenv.root}/target/debug/slop-broker builder --config ${builderConfig} + fi + exec ${pkgs.openssh}/bin/ssh "$@" + ''; + + # Only `demo-merge` moves `master`, standing in for branch protection and a person merging + preReceive = pkgs.writeShellScript "slop-demo-pre-receive" '' + while read -r old new ref; do + if [ "$ref" = refs/heads/master ]; then + echo "master is protected: push a branch, and merge it with \`demo-merge \` on the host" >&2 + exit 1 + fi + done + ''; + + # `@group@` becomes the user's primary group, whose members may approve, and `@dev@` the demo + # person's age public key brokerConfig = (pkgs.formats.toml { }).generate "slop-demo.toml" { requesters.agent = { vsock_cid = demo.agentCid; policy = "agent"; }; policies.agent.ops = { @@ -25,6 +71,12 @@ let { match.target = "vault"; action = "deny"; } ]; }; + # Approvals show whether the file and its recipients match `master`; reading always asks + secret = { + action = "ask"; + leases = [ "5m" ]; + rules = [{ match.action = "keys"; action = "auto"; }]; + }; }; vsock_port = 6262; @@ -33,7 +85,7 @@ let audit_log = "${state}/audit.jsonl"; ssh = { - ssh = "${pkgs.openssh}/bin/ssh"; + ssh = "${ssh}"; key = "${state}/broker-key"; known_hosts = "${state}/known_hosts"; targets = lib.genAttrs [ "web" "router" "vault" ] (_: { @@ -43,6 +95,17 @@ let }); }; + deploy = { + builder = { hostname = "builder"; user = "builder"; }; + nix = "${pkgs.nix}/bin/nix"; + cache = "${state}/nix-cache"; + }; + + secrets = { + identity = "${state}/broker-age.key"; + names.dev = "@dev@"; + }; + web = { listen = "127.0.0.1:${toString demo.ports.web}"; url = appUrl; @@ -69,7 +132,7 @@ in # `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 curl jq ]; + packages = with pkgs; [ pkg-config openssl sqlite git nixpkgs-fmt curl jq rage ]; # For `slop-approve` in this shell env.SLOP_APPROVE = "unix:${run}/approve.sock"; @@ -86,9 +149,32 @@ in echo "The broker isn't running: start \`devenv up\` first." >&2 exit 1 fi + # Git from the VM: connections to 10.0.2.100:9418, an address only the guest sees, each + # run a `git daemon` on the host serving the demo's repositories. Commas are doubled for + # QEMU. + daemon="${pkgs.git}/bin/git daemon --inetd --export-all --enable=receive-pack --base-path=${repositories}" + export QEMU_NET_OPTS="guestfwd=tcp:10.0.2.100:9418-cmd:''${daemon//,/,,}" exec nix run "$DEVENV_ROOT#demo" -- "$@" ''; }; + demo-merge = { + description = "Fast-forward the demo repository's `master` to a branch, as a person merging"; + exec = '' + set -eu + branch=''${1:?usage: demo-merge } + cd ${repositories}/repo.git + if ! git rev-parse --verify --quiet "refs/heads/$branch" >/dev/null; then + echo "there's no branch $branch; the agent pushes one with \`git push origin \`" >&2 + exit 1 + fi + if ! git merge-base --is-ancestor master "refs/heads/$branch"; then + echo "$branch isn't master plus new commits; rebase it on master first" >&2 + exit 1 + fi + git update-ref refs/heads/master "refs/heads/$branch" + echo "master is now at $(git rev-parse --short master)" + ''; + }; demo-box = { description = "SSH into the demo's box VM as `box`, with the broker's key"; exec = '' @@ -106,7 +192,7 @@ in ''; }; demo-reset = { - description = "Delete the demo's state (Pocket ID's users and passkeys, keys, audit log)"; + description = "Delete the demo's state (Pocket ID's users and passkeys, keys, repository, audit log)"; exec = ''rm -rf ${state}''; }; vm-test = { @@ -122,7 +208,7 @@ in exec = '' set -eu umask 077 - mkdir -p ${state}/box ${state}/pocket-id ${state}/web ${run} + mkdir -p ${state}/box ${state}/pocket-id ${state}/web ${state}/builder ${run} if [ ! -e ${state}/pocket-id.env ]; then key() { ${pkgs.openssl}/bin/openssl rand -hex 32; } @@ -136,6 +222,57 @@ in ''; }; + # Once: the repository, with secrets encrypted to the demo person and the broker. The hook is + # installed every time, so it follows changes here. + "slop-demo:repo" = { + description = "Seed the demo's repository and age identities"; + after = [ "slop-demo:keys" ]; + exec = '' + set -eu + umask 077 + export PATH=${lib.makeBinPath (with pkgs; [ coreutils git rage openssl wireguard-tools ])} + + if [ ! -d ${repositories}/repo.git ]; then + for name in dev broker; do + [ -e ${state}/$name-age.key ] || rage-keygen -o ${state}/$name-age.key 2>/dev/null + rage-keygen -y ${state}/$name-age.key > ${state}/$name-age.pub + done + dev=$(cat ${state}/dev-age.pub) broker=$(cat ${state}/broker-age.pub) + + work=$(mktemp -d) + cp -r ${./nix/demo/repo}/. "$work" + chmod -R u+w "$work" + mkdir "$work/secrets" + cat > "$work/secrets.nix" < ${state}/broker.toml + sed -e "s/@group@/$(id -gn)/" -e "s/@dev@/$(cat ${state}/dev-age.pub)/" ${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" ]; + after = [ "slop-demo:pocket-id" "slop-demo:repo" ]; ready = { exec = "test -S ${run}/approve.sock"; period = 1; diff --git a/docs/architecture.md b/docs/architecture.md index eae16e2..30916ff 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -28,7 +28,7 @@ How the code is put together, and how to extend it. The design and its rationale | `tests/broker.rs` | Handler tests over an in-memory duplex stream with a fake `ssh` | | `src/web/` | The approvals web app: routes (`mod.rs`), OIDC sign-in and passkey confirmation (`auth.rs`), Web Push (`push.rs`), the audit log by request (`history.rs`), the pages (`page.rs`) and static assets | | `templates/`, `askama.toml` | The web app's HTML, as [askama](https://askama.rs) templates compiled into the broker; `askama.toml` passes everything they show through `ops::printable` before escaping it | -| `nix/` | The package, NixOS modules (`modules/nixos`), home-manager module (`modules/home-manager`), the VM test (`vm-test.nix`, `checks..vm`), clippy and rustfmt checks (`lints.nix`) and the demo's VMs (`demo/`, `packages..demo` and `demo-box`) | +| `nix/` | The package, NixOS modules (`modules/nixos`), home-manager module (`modules/home-manager`), the VM test (`vm-test.nix`, `checks..vm`), clippy and rustfmt checks (`lints.nix`) and the demo's VMs and repository template (`demo/`, `packages..demo` and `demo-box`) | | `devenv.nix` | The development environment, its scripts and the demo's host side (`devenv up`); `nix develop` gives a plainer shell from `flake.nix` without them | | `.gitea/workflows/ci.yaml` | CI: `nix flake check` | diff --git a/docs/demo.md b/docs/demo.md index ee25e84..b36f2b0 100644 --- a/docs/demo.md +++ b/docs/demo.md @@ -1,8 +1,9 @@ # 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. +the agents are in a VM that reaches it only over vsock, its SSH targets are another VM, and the +agent works in a clone of a Git repository whose main branch it can't push to. 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: @@ -26,13 +27,16 @@ demo # the agents' VM, on this terminal | [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 repository, seeded from [`repo/`](../nix/demo/repo) | `.devenv/state/slop-demo/git/repo.git` | `git daemon`, one per connection from the agents' VM | +| The builder (`slop-broker builder`, from this checkout) | Host | The broker, through its `ssh` | 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. + forwarded ports, and only vsock and Git link the agents' VM to the host (see + [the repository](#the-repository)). - **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. @@ -42,6 +46,8 @@ CID are in [`settings.nix`](../nix/demo/settings.nix). The broker's configuratio - `slop ssh router` asks and needs a passkey (`confirm = "passkey"`), so only the web app can approve it. - `slop ssh vault` is denied. + - `slop secret keys` runs without asking; the other secret requests ask, with a 5-minute + lease, except reading, which asks every time. ## Approving @@ -58,6 +64,47 @@ requests in the background with stdin closed: slop ssh web -- uptime ` in the devenv shell fast-forwards `master` to a branch, as a person + merging after review. + +The agents' VM clones it to `~/repo` at login. The `demo` script forwards `10.0.2.100:9418`, an +address only the VM sees, to a `git daemon` on the host for each connection (QEMU's `guestfwd`), +so the VM can fetch and push over `git://` and reach nothing else. + +The broker reaches its builder over SSH, as in a real deployment. Here its `ssh` is a wrapper that +runs `slop-broker builder` from this checkout's build when the host is `builder`, and the real +`ssh` otherwise. The builder fetches from the repository's path and evaluates with the host's Nix. + +### Secrets + +In `~/repo` on the agents' VM: + +```sh +slop secret keys secrets/app.env.age # runs without asking +echo hunter2 | slop secret set secrets/app.env.age DB_PASSWORD +slop secret generate secrets/wg.key.age --kind wireguard # prints the public key +cp secrets/app.env.age secrets/notes.age +slop secret read secrets/notes.age ` runs one command there. Its console 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` +database (so passkeys survive), the keys, the repository and the builder's mirror, 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 diff --git a/docs/design.md b/docs/design.md index cf67794..236925a 100644 --- a/docs/design.md +++ b/docs/design.md @@ -496,6 +496,6 @@ Both wait until a concrete need comes up. `vm-test` in the devenv shell, or `nix build -L .#checks.x86_64-linux.vm`. - **CI** ([`ci.yaml`](../.gitea/workflows/ci.yaml)): every push runs `nix flake check`, which builds the package with its tests, checks clippy and rustfmt (`nix/lints.nix`), and runs the VM test. -- **Demo** ([`demo.md`](demo.md)): not a test, but the quickest way to try the web app and - passkey approvals by hand. +- **Demo** ([`demo.md`](demo.md)): not a test, but the quickest way to try the web app, passkey + approvals and secrets (with the builder and a protected main branch) by hand. - New operations add tests against fakes of what they call (deploy-rs, the DNS provider's API). diff --git a/nix/demo/agent.nix b/nix/demo/agent.nix index 8566562..9f1b088 100644 --- a/nix/demo/agent.nix +++ b/nix/demo/agent.nix @@ -1,9 +1,13 @@ # 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. +# vsock, like a real agents' VM, and a clone of the demo repository, which it reaches through a +# QEMU forward to `git daemon` on the host. `devenv up` runs the broker; `demo` boots this VM on the +# terminal and sets up the forward. { pkgs, slop-broker }: let inherit (pkgs) lib; settings = import ./settings.nix; + # Only the guest sees this address; the `demo` script forwards it to the host + repository = "git://10.0.2.100/repo.git"; help = pkgs.writeShellScriptBin "demo-help" '' cat <<'EOF' @@ -19,6 +23,16 @@ let slop requests your pending requests and leases slop guide the guide agents get + Secrets, in ~/repo (a clone of the demo repository; master only moves with `demo-merge` on + the host): + + slop secret keys secrets/app.env.age runs without asking + echo hunter2 | slop secret set secrets/app.env.age DB_PASSWORD + slop secret generate secrets/wg.key.age --kind wireguard + cp secrets/app.env.age secrets/notes.age + slop secret read secrets/notes.age the approval says it's secrets/app.env.age + git switch -c secrets && git commit -am "Rotate secrets" && git push origin secrets + 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). @@ -58,8 +72,22 @@ in ''; }; - environment.systemPackages = [ slop-broker help ]; + environment.systemPackages = [ slop-broker help pkgs.git ]; + # For `slop secret`, which evaluates `secrets.nix` + nix.settings.experimental-features = [ "nix-command" "flakes" ]; + environment.etc.gitconfig.text = '' + [user] + name = agent + email = agent@example.org + ''; + programs.bash.loginShellInit = '' - if [ "$USER" = agent ]; then demo-help; fi + if [ "$USER" = agent ]; then + if [ ! -d ~/repo ] && ! git clone -q ${repository} ~/repo; then + echo "Cloning ${repository} failed; start the VM with \`demo\` on the host, which forwards it." >&2 + fi + cd ~/repo 2>/dev/null || true + demo-help + fi ''; })).vm diff --git a/nix/demo/repo/README.md b/nix/demo/repo/README.md new file mode 100644 index 0000000..c5d79fb --- /dev/null +++ b/nix/demo/repo/README.md @@ -0,0 +1,11 @@ +# slop-broker demo repository + +The repository the demo's agent works in. `master` is protected: pushes to it are refused, and +only `demo-merge ` on the host moves it, standing in for a person merging after review. + +- `secrets.nix` is the agenix rules file: who each secret is encrypted to. `dev` is the demo's + person and `broker` is the broker's own identity. +- `secrets/app.env.age` holds `DB_USER` and `DB_PASSWORD`; `secrets/wg.key.age` is a WireGuard + private key, as a whole file. + +Change secrets with `slop secret` (`slop guide` explains it), commit, and push a branch. diff --git a/nix/demo/repo/flake.nix b/nix/demo/repo/flake.nix new file mode 100644 index 0000000..8ba2f42 --- /dev/null +++ b/nix/demo/repo/flake.nix @@ -0,0 +1,7 @@ +# The demo's repository, which the agent clones, changes and pushes. `devenv up` seeds it once, +# with `secrets.nix` and the secrets beside this file. +{ + description = "slop-broker demo repository"; + + outputs = { self }: { }; +}