1 import ./make-test-python.nix ({ pkgs, lib, ...}:
5 meta = with lib.maintainers; {
6 maintainers = [ rnhmjoj ];
9 # server for testing SSH
10 nodes.server = { ... }: {
11 imports = [ ../modules/profiles/minimal.nix ];
13 users.users.alice.isNormalUser = true;
14 services.openssh.enable = true;
17 # machine for testing GnuPG
18 nodes.machine = { pkgs, ... }: {
19 imports = [ ../modules/profiles/minimal.nix ];
21 users.users.alice.isNormalUser = true;
22 services.getty.autologinUser = "alice";
24 environment.shellInit = ''
25 # preset a key passphrase in gpg-agent
29 ssh) grips=$(awk '/^[0-9A-F]/{print $1}' "''${GNUPGHOME:-$HOME/.gnupg}/sshcontrol") ;;
30 pgp) grips=$(gpg --with-keygrip --list-secret-keys | awk '/Keygrip/{print $3}') ;;
33 # try to preset the passphrase for each key found
34 for grip in $grips; do
35 "$(gpgconf --list-dirs libexecdir)/gpg-preset-passphrase" -c -P "$2" "$grip"
40 programs.gnupg.agent.enable = true;
41 programs.gnupg.agent.enableSSHSupport = true;
49 def as_alice(command: str) -> str:
51 Wraps a command to run it as Alice in a login shell
53 quoted = shlex.quote(command)
54 return "su --login alice --command " + quoted
59 with subtest("Wait for the autologin"):
60 machine.wait_until_tty_matches("1", "alice@machine")
62 with subtest("Can generate a PGP key"):
63 # Note: this needs a tty because of pinentry
64 machine.send_chars("gpg --gen-key\n")
65 machine.wait_until_tty_matches("1", "Real name:")
66 machine.send_chars("Alice\n")
67 machine.wait_until_tty_matches("1", "Email address:")
68 machine.send_chars("alice@machine\n")
69 machine.wait_until_tty_matches("1", "Change")
70 machine.send_chars("O\n")
71 machine.wait_until_tty_matches("1", "Please enter")
72 machine.send_chars("pgp_p4ssphrase")
73 machine.send_key("tab")
74 machine.send_chars("pgp_p4ssphrase")
75 machine.wait_until_tty_matches("1", "Passphrases match")
76 machine.send_chars("\n")
77 machine.wait_until_tty_matches("1", "public and secret key created")
79 with subtest("Confirm the key is in the keyring"):
80 machine.wait_until_succeeds(as_alice("gpg --list-secret-keys | grep -q alice@machine"))
82 with subtest("Can generate and add an SSH key"):
83 machine.succeed(as_alice("ssh-keygen -t ed25519 -f alice -N ssh_p4ssphrase"))
85 # Note: apparently this must be run before using the OpenSSH agent
86 # socket for the first time in a tty. It's not needed for `ssh`
87 # because there's a hook that calls it automatically (only in NixOS).
88 machine.send_chars("gpg-connect-agent updatestartuptty /bye\n")
90 # Note: again, this needs a tty because of pinentry
91 machine.send_chars("ssh-add alice\n")
92 machine.wait_until_tty_matches("1", "Enter passphrase")
93 machine.send_chars("ssh_p4ssphrase\n")
94 machine.wait_until_tty_matches("1", "Please enter")
95 machine.send_chars("ssh_agent_p4ssphrase")
96 machine.send_key("tab")
97 machine.send_chars("ssh_agent_p4ssphrase")
98 machine.wait_until_tty_matches("1", "Passphrases match")
99 machine.send_chars("\n")
101 with subtest("Confirm the SSH key has been registered"):
102 machine.wait_until_succeeds(as_alice("ssh-add -l | grep -q alice@machine"))
104 with subtest("Can preset the key passphrases in the agent"):
105 machine.succeed(as_alice("echo allow-preset-passphrase > .gnupg/gpg-agent.conf"))
106 machine.succeed(as_alice("pkill gpg-agent"))
107 machine.succeed(as_alice("preset_key pgp pgp_p4ssphrase"))
108 machine.succeed(as_alice("preset_key ssh ssh_agent_p4ssphrase"))
110 with subtest("Can encrypt and decrypt a message"):
111 machine.succeed(as_alice("echo Hello | gpg -e -r alice | gpg -d | grep -q Hello"))
113 with subtest("Can log into the server"):
114 # Install Alice's public key
115 public_key = machine.succeed(as_alice("cat alice.pub"))
116 server.succeed("mkdir /etc/ssh/authorized_keys.d")
117 server.succeed(f"printf '{public_key}' > /etc/ssh/authorized_keys.d/alice")
119 server.wait_for_open_port(22)
120 machine.succeed(as_alice("ssh -i alice -o StrictHostKeyChecking=no server exit"))