# `checks.script-test-agent-ssh-cert` — runs ../agent-modules/ssh.nix's helper, # the text of the unit `hive-agent-ssh-cert`, against a stub `bao` and a real # `ssh-keygen`, in both entries: the timer's and the `ssh` one. The cases are in # ./agent-ssh-cert.sh. # # The one edit to the text is its `/run/` prefix, moved under the build # directory because the sandbox has no writable `/run`. The helper adds its own # tools after the caller's `PATH`, so the stub in front wins. { pkgs, lib, self, nixosSystem, }: let inherit (import ../module-eval/lib.nix { inherit pkgs lib self nixosSystem ; }) agentWith ; machine = agentWith { services.hyperhive.agent.bao.addr = "https://bao.t.local:8200"; }; # `login` prints a token; `write` signs `public_key=@` with the test CA # for `FAKE_SIGN_VALID` (an `ssh-keygen -V` interval), or answers 403 when # `FAKE_SIGN_MODE` is `refuse`, or fails otherwise when it is `fail`. A call # without the token the last login printed fails, so a script that signs # without a login of its own cannot pass. fakeBao = pkgs.writeShellScriptBin "bao" '' umask 022 echo "$1" >> "$FAKE_BAO_LOG" case "$1" in login) printf 'tok-%s' "$(wc -l < "$FAKE_BAO_LOG")" ;; write) if [ "''${BAO_TOKEN-}" != "tok-$(( $(wc -l < "$FAKE_BAO_LOG") - 1 ))" ]; then echo "fake bao: write without the token of the login just before it" >&2 exit 97 fi case "$FAKE_SIGN_MODE" in refuse) printf 'Error writing data: Code: 403. Errors:\n* permission denied\n' >&2 exit 2 ;; fail) printf 'Error writing data: Code: 503. Errors:\n* sealed\n' >&2 exit 2 ;; esac pub= for a in "$@"; do case "$a" in public_key=@*) pub=''${a#public_key=@} ;; esac done tmp=$(mktemp -d) cp "$pub" "$tmp/k.pub" ${pkgs.openssh}/bin/ssh-keygen -q -s "$FAKE_CA_KEY" -I fake -n a1 -V "$FAKE_SIGN_VALID" "$tmp/k.pub" cat "$tmp/k-cert.pub" ;; *) echo "fake bao: unexpected call: $*" >&2 exit 98 ;; esac ''; script = pkgs.writeText "agent-ssh-cert-script" machine.systemd.services.hive-agent-ssh-cert.script; in pkgs.runCommand "hyperhive-script-test-agent-ssh-cert" { nativeBuildInputs = [ pkgs.openssh pkgs.coreutils pkgs.util-linux ]; SSH_CERT_SCRIPT = script; FAKE_BAO_BIN = "${fakeBao}/bin"; SSH_CERT_PATH = lib.makeBinPath [ pkgs.coreutils pkgs.bash ]; } '' ${pkgs.bash}/bin/bash ${./agent-ssh-cert.sh} touch "$out" ''