2023-03-16 09:34:48 +00:00
|
|
|
|
# Test that ‘nix copy’ works over ssh.
|
2023-08-14 12:28:02 +00:00
|
|
|
|
# Run interactively with:
|
|
|
|
|
# rm key key.pub; nix run .#hydraJobs.tests.nix-copy.driverInteractive
|
2023-03-16 09:34:48 +00:00
|
|
|
|
|
|
|
|
|
{ lib, config, nixpkgs, hostPkgs, ... }:
|
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
pkgs = config.nodes.client.nixpkgs.pkgs;
|
|
|
|
|
|
|
|
|
|
pkgA = pkgs.cowsay;
|
|
|
|
|
pkgB = pkgs.wget;
|
|
|
|
|
pkgC = pkgs.hello;
|
|
|
|
|
pkgD = pkgs.tmux;
|
|
|
|
|
|
|
|
|
|
in {
|
|
|
|
|
name = "nix-copy";
|
|
|
|
|
|
|
|
|
|
enableOCR = true;
|
|
|
|
|
|
|
|
|
|
nodes =
|
|
|
|
|
{ client =
|
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
{ virtualisation.writableStore = true;
|
|
|
|
|
virtualisation.additionalPaths = [ pkgA pkgD.drvPath ];
|
|
|
|
|
nix.settings.substituters = lib.mkForce [ ];
|
|
|
|
|
nix.settings.experimental-features = [ "nix-command" ];
|
|
|
|
|
services.getty.autologinUser = "root";
|
2023-05-17 07:34:45 +00:00
|
|
|
|
programs.ssh.extraConfig = ''
|
|
|
|
|
Host *
|
|
|
|
|
ControlMaster auto
|
|
|
|
|
ControlPath ~/.ssh/master-%h:%r@%n:%p
|
|
|
|
|
ControlPersist 15m
|
|
|
|
|
'';
|
2023-03-16 09:34:48 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
server =
|
|
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
{ services.openssh.enable = true;
|
2024-03-07 08:58:15 +00:00
|
|
|
|
services.openssh.settings.PermitRootLogin = "yes";
|
2024-06-08 15:57:17 +00:00
|
|
|
|
users.users.root.hashedPasswordFile = lib.mkForce null;
|
2023-03-16 09:34:48 +00:00
|
|
|
|
virtualisation.writableStore = true;
|
|
|
|
|
virtualisation.additionalPaths = [ pkgB pkgC ];
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
testScript = { nodes }: ''
|
|
|
|
|
# fmt: off
|
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
|
|
# Create an SSH key on the client.
|
|
|
|
|
subprocess.run([
|
|
|
|
|
"${pkgs.openssh}/bin/ssh-keygen", "-t", "ed25519", "-f", "key", "-N", ""
|
|
|
|
|
], capture_output=True, check=True)
|
|
|
|
|
|
|
|
|
|
start_all()
|
|
|
|
|
|
2024-03-09 10:01:28 +00:00
|
|
|
|
server.succeed("systemctl start network-online.target")
|
|
|
|
|
client.succeed("systemctl start network-online.target")
|
|
|
|
|
server.wait_for_unit("network-online.target")
|
|
|
|
|
client.wait_for_unit("network-online.target")
|
2024-10-25 14:04:08 +00:00
|
|
|
|
server.wait_for_unit("multi-user.target")
|
|
|
|
|
client.wait_for_unit("multi-user.target")
|
2024-03-09 10:01:28 +00:00
|
|
|
|
|
|
|
|
|
server.wait_for_unit("sshd.service")
|
2023-03-16 09:34:48 +00:00
|
|
|
|
|
|
|
|
|
# Copy the closure of package A from the client to the server using password authentication,
|
|
|
|
|
# and check that all prompts are visible
|
2024-10-25 14:04:08 +00:00
|
|
|
|
# NOTE: this used to also check password prompts, but the test implementation was monumentally
|
|
|
|
|
# fragile (and hence broke constantly). since ssh interacts with /dev/tty directly fixing this
|
|
|
|
|
# requires some proper cli automation, which we do not have. however, since ssh interacts with
|
|
|
|
|
# /dev/tty directly and the host key message goes there too, we don't even need to check this.
|
2023-03-16 09:34:48 +00:00
|
|
|
|
server.fail("nix-store --check-validity ${pkgA}")
|
2024-10-25 14:04:08 +00:00
|
|
|
|
client.succeed("echo | script -f /dev/stdout -c 'nix copy --to ssh://server ${pkgA}' | grep 'continue connecting'")
|
2023-03-16 09:34:48 +00:00
|
|
|
|
|
2024-10-25 14:04:08 +00:00
|
|
|
|
client.copy_from_host("key", "/root/.ssh/id_ed25519.setup")
|
|
|
|
|
client.succeed("chmod 600 /root/.ssh/id_ed25519.setup")
|
2023-03-16 09:34:48 +00:00
|
|
|
|
# Install the SSH key on the server.
|
|
|
|
|
server.copy_from_host("key.pub", "/root/.ssh/authorized_keys")
|
|
|
|
|
server.succeed("systemctl restart sshd")
|
2024-10-25 14:04:08 +00:00
|
|
|
|
|
|
|
|
|
client.succeed("NIX_SSHOPTS='-oStrictHostKeyChecking=no -i /root/.ssh/id_ed25519.setup' nix copy --to ssh://server ${pkgA}")
|
|
|
|
|
server.succeed("nix-store --check-validity ${pkgA}")
|
|
|
|
|
# Check that ControlMaster is working
|
|
|
|
|
client.succeed("nix copy --to ssh://server ${pkgA}")
|
|
|
|
|
|
|
|
|
|
client.succeed("cp /root/.ssh/id_ed25519.setup /root/.ssh/id_ed25519")
|
|
|
|
|
|
2024-03-09 10:01:28 +00:00
|
|
|
|
client.succeed(f"ssh -o StrictHostKeyChecking=no {server.name} 'echo hello world' >&2")
|
2023-06-13 02:42:05 +00:00
|
|
|
|
client.succeed(f"ssh -O check {server.name}")
|
|
|
|
|
client.succeed(f"ssh -O exit {server.name}")
|
|
|
|
|
client.fail(f"ssh -O check {server.name}")
|
|
|
|
|
|
|
|
|
|
# Check that an explicit master will work
|
|
|
|
|
client.succeed(f"ssh -MNfS /tmp/master {server.name}")
|
|
|
|
|
client.succeed(f"ssh -S /tmp/master -O check {server.name}")
|
|
|
|
|
client.succeed("NIX_SSHOPTS='-oControlPath=/tmp/master' nix copy --to ssh://server ${pkgA} >&2")
|
|
|
|
|
client.succeed(f"ssh -S /tmp/master -O exit {server.name}")
|
2023-03-16 09:34:48 +00:00
|
|
|
|
|
|
|
|
|
# Copy the closure of package B from the server to the client, using ssh-ng.
|
|
|
|
|
client.fail("nix-store --check-validity ${pkgB}")
|
|
|
|
|
# Shouldn't download untrusted paths by default
|
|
|
|
|
client.fail("nix copy --from ssh-ng://server ${pkgB} >&2")
|
|
|
|
|
client.succeed("nix copy --no-check-sigs --from ssh-ng://server ${pkgB} >&2")
|
|
|
|
|
client.succeed("nix-store --check-validity ${pkgB}")
|
|
|
|
|
|
|
|
|
|
# Copy the derivation of package D's derivation from the client to the server.
|
|
|
|
|
server.fail("nix-store --check-validity ${pkgD.drvPath}")
|
|
|
|
|
client.succeed("nix copy --derivation --to ssh://server ${pkgD.drvPath} >&2")
|
|
|
|
|
server.succeed("nix-store --check-validity ${pkgD.drvPath}")
|
|
|
|
|
'';
|
|
|
|
|
}
|