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