diff --git a/nix/vm-test.nix b/nix/vm-test.nix index f7ca7d2..eb84bab 100644 --- a/nix/vm-test.nix +++ b/nix/vm-test.nix @@ -1,14 +1,20 @@ # Runs the broker through the `services.slop-broker` module, with its real service hardening, over # vsock loopback, against a local sshd. Build it as `checks.x86_64-linux.vm` (see `README.md`). +# +# `machine` runs the broker, its builder and the requesters, and is also the deploy node `local` +# (and `other` and `denied`), with a container, `web`. { pkgs, slop-broker }: let inherit (pkgs) lib; + # SSH keys: the broker's own (`key`), a requester's and an approver's on other boxes key = pkgs.runCommand "test-key" { nativeBuildInputs = [ pkgs.openssh ]; } '' mkdir $out for k in key requester approver; do ssh-keygen -q -t ed25519 -N "" -f $out/$k; done ''; publicKey = name: builtins.readFile "${key}/${name}.pub"; + # The broker's key at runtime: not a store path, as the module requires + keyFile = "/etc/slop-broker-test/key"; # The builder's repository: a flake whose deploy-rs nodes are the broker's SSH targets repository = pkgs.runCommand "test-repository" { nativeBuildInputs = [ pkgs.git ]; } '' @@ -26,182 +32,254 @@ let }; } EOF + git -C $out init --quiet --initial-branch=master git -C $out add flake.nix git -C $out -c user.name=test -c user.email=test@example.org commit --quiet -m "Add nodes" ''; + + # A deploy node: the broker logs in as `deploy`, which may use `sudo` (for container commands) + deployNode = { + services.openssh.enable = true; + + users.users.deploy = { + isNormalUser = true; + openssh.authorizedKeys.keyFiles = [ "${key}/key.pub" ]; + }; + security.sudo.extraRules = [{ + users = [ "deploy" ]; + commands = [{ command = "ALL"; options = [ "NOPASSWD" ]; }]; + }]; + }; in pkgs.testers.runNixOSTest { name = "slop-broker"; + nodes.machine = { ... }: { - imports = [ ./modules/nixos/broker.nix ./modules/nixos/builder.nix ]; + imports = [ deployNode ./modules/nixos/broker.nix ./modules/nixos/builder.nix ]; - config = { - boot.kernelModules = [ "vsock_loopback" ]; - services.openssh.enable = true; - users.users.deploy = { isNormalUser = true; openssh.authorizedKeys.keyFiles = [ "${key}/key.pub" ]; }; - users.users.reviewer.isNormalUser = true; - users.users.agent.isNormalUser = true; - environment.systemPackages = [ pkgs.util-linux ]; - # Container commands go through `systemd-machined`, as root - security.sudo.extraRules = [ { users = [ "deploy" ]; commands = [ { command = "ALL"; options = [ "NOPASSWD" ]; } ]; } ]; - containers.web = { - autoStart = true; - config.system.stateVersion = lib.trivial.release; - }; - virtualisation.memorySize = 2048; - programs.ssh.extraConfig = '' - StrictHostKeyChecking no - UserKnownHostsFile /dev/null - LogLevel ERROR - ''; - # Not a store path, as the module requires - environment.etc."slop-broker-test/key".source = "${key}/key"; + # The agent reaches the broker over vsock loopback (CID 1); `reviewer` approves + boot.kernelModules = [ "vsock_loopback" ]; + users.users.reviewer.isNormalUser = true; + users.users.agent.isNormalUser = true; + virtualisation.memorySize = 2048; + # `script`, for answering on a terminal + environment.systemPackages = [ pkgs.util-linux ]; - services.slop-broker = { - enable = true; - package = slop-broker; - approvers = [ "reviewer" ]; - - policies.agent.ops = { - query.action = "auto"; - ssh = { action = "ask"; leases = [ "5m" ]; rules = [ { match.target = "denied"; action = "deny"; } ]; }; - }; - policies.workstation.ops.query.action = "auto"; - - requesters.agent = { - vsockCid = 1; - policy = "agent"; - }; - requesters.workstation = { - sshKeys = [ (publicKey "requester") ]; - policy = "workstation"; - }; - - sshApprovers.laptop = [ (publicKey "approver") ]; - - ssh = { - enable = true; - keyFile = "/etc/slop-broker-test/key"; - }; - deploy.enable = true; - }; - - # Only reads the repository, so its key is never used - services.slop-broker-builder = { - enable = true; - package = slop-broker; - repository = "/var/lib/slop-test-repository"; - keyFile = "/etc/slop-broker-test/key"; - brokerKeys = [ (publicKey "key") ]; - }; - systemd.tmpfiles.rules = [ "C /var/lib/slop-test-repository - - - - ${repository}" "Z /var/lib/slop-test-repository - slop-broker-build - - -" ]; + # A container for the container commands, which go through `systemd-machined` as root + containers.web = { + autoStart = true; + config.system.stateVersion = lib.trivial.release; }; + + # The broker's SSH connections trust any host key, and say nothing about it + programs.ssh.extraConfig = '' + StrictHostKeyChecking no + UserKnownHostsFile /dev/null + LogLevel ERROR + ''; + environment.etc."slop-broker-test/key".source = "${key}/key"; + + services.slop-broker = { + enable = true; + package = slop-broker; + approvers = [ "reviewer" ]; + + # Queries run without asking; `slop ssh` tests the approvals + policies.agent.ops = { + query.action = "auto"; + ssh = { + action = "ask"; + leases = [ "5m" ]; + rules = [{ match.target = "denied"; action = "deny"; }]; + }; + }; + policies.workstation.ops.query.action = "auto"; + + requesters.agent = { + vsockCid = 1; + policy = "agent"; + }; + requesters.workstation = { + sshKeys = [ (publicKey "requester") ]; + policy = "workstation"; + }; + sshApprovers.laptop = [ (publicKey "approver") ]; + + ssh = { + enable = true; + inherit keyFile; + }; + deploy.enable = true; + }; + + # The builder only reads the repository, a local copy, so its key is never used + services.slop-broker-builder = { + enable = true; + package = slop-broker; + repository = "/var/lib/slop-test-repository"; + inherit keyFile; + brokerKeys = [ (publicKey "key") ]; + }; + systemd.tmpfiles.rules = [ + "C /var/lib/slop-test-repository - - - - ${repository}" + "Z /var/lib/slop-test-repository - slop-broker-build - - -" + ]; }; + testScript = '' + # Commands as the agent, sending requests over vsock (or `broker`) + vsock = "SLOP_BROKER=vsock:1:6262 SLOP_LABEL='vm test'" + + def agent(command, broker=vsock): + return f"su agent -c \"{broker} {command}\"" + + def slop(args, broker=vsock): + """Runs `slop`, which must succeed; returns its stdout.""" + return machine.succeed(agent(f"slop {args}", broker)) + + def try_slop(args, broker=vsock): + """Runs `slop`; returns its exit status and output, stderr included.""" + return machine.execute(agent(f"slop {args}", broker) + " 2>&1") + + def slop_in_background(command, output): + """Starts an agent's command that waits for an answer, writing its output to a file.""" + machine.execute(agent(command) + f" > {output} 2>&1 &") + + # Commands as the reviewer + def approve(args): + return machine.succeed(f"su reviewer -c 'slop-approve {args}'") + + def wait_for_request(summary): + machine.wait_until_succeeds(f"su reviewer -c 'slop-approve list' | grep -q '{summary}'") + + + start_all() machine.wait_for_unit("sshd.service") machine.wait_for_unit("slop-broker.service") machine.wait_for_file("/run/slop-broker/approve.sock") - env = "SLOP_BROKER=vsock:1:6262 SLOP_LABEL='vm test'" + + # Policy # Denied by policy, without asking - status, out = machine.execute(f"su agent -c \"{env} slop ssh denied -- true\" 2>&1") + status, out = try_slop("ssh denied -- true") assert status == 125 and "denied by policy" in out, (status, out) # Fixed queries run without asking - out = machine.succeed(f"su agent -c \"{env} slop status local sshd.service\"") + out = slop("status local sshd.service") assert "sshd.service" in out, out - out = machine.succeed(f"su agent -c \"{env} slop logs local sshd.service -n 5 --since 1h\"") - print(out) + print(slop("logs local sshd.service -n 5 --since 1h")) - # Ask, approve from a terminal, and see the real ssh run - machine.execute(f"su agent -c \"{env} slop ssh local -- 'echo hi from \\$(whoami)'\" > /tmp/out 2>&1 &") - machine.wait_until_succeeds("su reviewer -c 'slop-approve list' | grep -q 'ssh local'") - listing = machine.succeed("su reviewer -c 'slop-approve list'") + # Approvals and leases + + # Ask, approve from a terminal, and see the real ssh run. Only approvers can answer. + slop_in_background("slop ssh local -- 'echo hi from \\$(whoami)'", "/tmp/out") + wait_for_request("ssh local") + listing = approve("list") print(listing) assert "vm test" in listing request = listing.split()[0][:4] - status, out = machine.execute("su agent -c 'slop-approve " + request + " once' 2>&1") + status, out = machine.execute(f"su agent -c 'slop-approve {request} once' 2>&1") assert status != 0, out - machine.succeed(f"su reviewer -c 'slop-approve {request} 5m'") + approve(f"{request} 5m") machine.wait_until_succeeds("grep -q 'hi from deploy' /tmp/out") out = machine.succeed("cat /tmp/out") print(out) assert "Permanently added" not in out, out # Covered by the lease now; stdin goes with the request - out = machine.succeed(f"echo piped | su agent -c \"{env} slop ssh local -- cat\"") + out = machine.succeed("echo piped | " + agent("slop ssh local -- cat")) assert "piped" in out, out - out = machine.succeed(f"su agent -c \"{env} slop requests\"") + out = slop("requests") assert "ssh" in out and "lease" in out, out - # Containers: queries run without asking, as for nodes; ssh asks again, since the lease is - # for the host + # Containers + + # Queries run without asking, as for nodes machine.wait_for_unit("container@web.service") - out = machine.succeed(f"su agent -c \"{env} slop status local.container-web systemd-journald.service\"") + out = slop("status local.container-web systemd-journald.service") assert "systemd-journald.service" in out, out - out = machine.succeed(f"su agent -c \"{env} slop failed local.container-web\"") - print(out) - out = machine.succeed(f"su agent -c \"{env} slop logs local.container-web systemd-journald.service -n 5\"") - print(out) - status, out = machine.execute(f"su agent -c \"{env} slop failed local.container-nope\" 2>&1") + print(slop("failed local.container-web")) + print(slop("logs local.container-web systemd-journald.service -n 5")) + + # Only `container-*` profiles are containers + status, out = try_slop("failed local.container-nope") assert status == 125 and "unknown container local.container-nope" in out, (status, out) - status, out = machine.execute(f"su agent -c \"{env} slop failed local.system\" 2>&1") + status, out = try_slop("failed local.system") assert status == 125 and "only containers" in out, (status, out) - machine.execute(f"echo from stdin | su agent -c \"{env} slop ssh local.container-web -- 'cat; echo in \\$(hostname) as \\$(whoami); exit 3'; echo exit \\$?\" > /tmp/out5 2>&1 &") - machine.wait_until_succeeds("su reviewer -c 'slop-approve list' | grep -q 'ssh local.container-web'") - machine.succeed("su reviewer -c 'slop-approve once'") + # `slop ssh` asks again, since the lease is for the host, and runs in the container as root, + # with stdin and the command's exit status + machine.execute( + "echo from stdin | " + + agent("slop ssh local.container-web -- 'cat; echo in \\$(hostname) as \\$(whoami); exit 3'; echo exit \\$?") + + " > /tmp/out5 2>&1 &" + ) + wait_for_request("ssh local.container-web") + approve("once") machine.wait_until_succeeds("grep -q 'exit 3' /tmp/out5") out = machine.succeed("cat /tmp/out5") print(out) assert "from stdin" in out and "in web as root" in out, out + # Errors + # Unknown nodes are named plainly; usage errors exit 125 like other refusals - status, out = machine.execute(f"su agent -c \"{env} slop ssh nope -- true\" 2>&1") + status, out = try_slop("ssh nope -- true") assert status == 125 and "unknown node nope;" in out, (status, out) - status, out = machine.execute(f"su agent -c \"{env} slop ssh\" 2>&1") + status, out = try_slop("ssh") assert status == 125, (status, out) - # Interactive answering on a terminal - machine.execute(f"su agent -c \"{env} slop ssh other -- echo interactive\" > /tmp/out2 2>&1 &") - machine.wait_until_succeeds("su reviewer -c 'slop-approve list' | grep -q 'ssh other'") + # Answering + + # Interactively on a terminal, where a wrong answer is asked again + slop_in_background("slop ssh other -- echo interactive", "/tmp/out2") + wait_for_request("ssh other") out = machine.succeed("(sleep 1; echo x; sleep 1; echo o) | timeout 5 su reviewer -c \"script -qec slop-approve /dev/null\" || true") print(out) assert "Not an option" in out and "Recorded: once" in out, out machine.wait_until_succeeds("grep -q interactive /tmp/out2") # The only pending request can be answered without its ID - machine.execute(f"su agent -c \"{env} slop ssh other -- true; echo \\$?\" > /tmp/out3 2>&1 &") - machine.wait_until_succeeds("su reviewer -c 'slop-approve list' | grep -q 'ssh other'") - machine.succeed("su reviewer -c 'slop-approve deny'") + slop_in_background("slop ssh other -- true; echo \\$?", "/tmp/out3") + wait_for_request("ssh other") + approve("deny") machine.wait_until_succeeds("grep -q 'denied by approver' /tmp/out3") machine.wait_until_succeeds("grep -qx 125 /tmp/out3", timeout=30) - # Over SSH: each key names its owner and role + # Over SSH + + # Each key names its owner and role: the agent's key here is the workstation's, which may + # only query, and can't approve for user, k in [("agent", "requester"), ("reviewer", "approver")]: machine.succeed(f"install -D -m600 -o {user} ${key}/{k} /home/{user}/.ssh/id_ed25519") remote = "SLOP_BROKER=ssh:slop@localhost" - out = machine.succeed(f"su agent -c \"{remote} slop status local sshd.service\"") + out = slop("status local sshd.service", broker=remote) assert "sshd.service" in out, out - status, out = machine.execute(f"su agent -c \"{remote} slop ssh local -- true\" 2>&1") + status, out = try_slop("ssh local -- true", broker=remote) assert status == 125 and "denied by policy" in out, (status, out) status, out = machine.execute("su agent -c 'SLOP_APPROVE=ssh:slop@localhost slop-approve list' 2>&1") assert status != 0 and "for sending requests" in out, (status, out) - machine.execute(f"su agent -c \"{env} slop ssh other -- echo remote approval\" > /tmp/out4 2>&1 &") - machine.wait_until_succeeds("su reviewer -c 'slop-approve list' | grep -q 'ssh other'") + # The approver's key answers as `laptop` + slop_in_background("slop ssh other -- echo remote approval", "/tmp/out4") + wait_for_request("ssh other") machine.succeed("su reviewer -c 'SLOP_APPROVE=ssh:slop@localhost slop-approve once'") machine.wait_until_succeeds("grep -q 'remote approval' /tmp/out4") + # The builder + # A builder with a store of its own serves it to the broker's key as `nix-daemon --stdio` - machine.succeed("install -m600 /etc/slop-broker-test/key /root/builder-key") + machine.succeed("install -m600 ${keyFile} /root/builder-key") system = machine.succeed("readlink -f /run/current-system").strip() store = "ssh-ng://slop-broker-build@localhost:22?ssh-key=/root/builder-key" out = machine.succeed(f"nix --extra-experimental-features nix-command path-info --store '{store}' {system}") assert out.strip() == system, out - audit = machine.succeed("su reviewer -c 'slop-approve log'") + # The audit log + + # It has the answers + audit = approve("log") print(audit) assert "agent asks: ssh local" in audit and "answered 5m by reviewer" in audit, audit assert "answered once by laptop (ssh)" in audit, audit