1 import ./make-test-python.nix ({ pkgs, ... }:
3 let inherit (import ./ssh-keys.nix pkgs)
4 snakeOilPrivateKey snakeOilPublicKey snakeOilEd25519PrivateKey snakeOilEd25519PublicKey;
7 meta = with pkgs.lib.maintainers; {
8 maintainers = [ aszlig ];
17 services.openssh.enable = true;
18 security.pam.services.sshd.limits =
19 [ { domain = "*"; item = "memlock"; type = "-"; value = 1024; } ];
20 users.users.root.openssh.authorizedKeys.keys = [
25 server-allowed-users =
29 services.openssh = { enable = true; settings.AllowUsers = [ "alice" "bob" ]; };
30 users.groups = { alice = { }; bob = { }; carol = { }; };
32 alice = { isNormalUser = true; group = "alice"; openssh.authorizedKeys.keys = [ snakeOilPublicKey ]; };
33 bob = { isNormalUser = true; group = "bob"; openssh.authorizedKeys.keys = [ snakeOilPublicKey ]; };
34 carol = { isNormalUser = true; group = "carol"; openssh.authorizedKeys.keys = [ snakeOilPublicKey ]; };
42 services.openssh = { enable = true; startWhenNeeded = true; };
43 security.pam.services.sshd.limits =
44 [ { domain = "*"; item = "memlock"; type = "-"; value = 1024; } ];
45 users.users.root.openssh.authorizedKeys.keys = [
50 server-lazy-socket = {
51 virtualisation.vlans = [ 1 2 ];
54 startWhenNeeded = true;
56 listenAddresses = [ { addr = "0.0.0.0"; } ];
58 users.users.root.openssh.authorizedKeys.keys = [
63 server-localhost-only =
68 enable = true; listenAddresses = [ { addr = "127.0.0.1"; port = 22; } ];
72 server-localhost-only-lazy =
77 enable = true; startWhenNeeded = true; listenAddresses = [ { addr = "127.0.0.1"; port = 22; } ];
86 enable = true; listenAddresses = [ { addr = "127.0.0.1"; port = 22; } { addr = "[::]"; port = 22; } ];
88 # Combined test for two (predictable) Match criterias
89 Match LocalAddress 127.0.0.1 LocalPort 22
92 # Separate tests for Match criterias
97 Match Host nohost.example
99 Match LocalAddress 127.0.0.1
103 Match RDomain nohost.example
105 Match Address 127.0.0.1
116 package = pkgs.opensshPackages.openssh.override {
120 { type = "ed25519"; path = "/etc/ssh/ssh_host_ed25519_key"; }
123 # Since this test is against an OpenSSH-without-OpenSSL,
124 # we have to override NixOS's defaults ciphers (which require OpenSSL)
125 # and instead set these to null, which will mean OpenSSH uses its defaults.
126 # Expectedly, OpenSSH's defaults don't require OpenSSL when it's compiled
129 KexAlgorithms = null;
133 users.users.root.openssh.authorizedKeys.keys = [
134 snakeOilEd25519PublicKey
143 package = pkgs.opensshPackages.openssh.override {
150 users.users.root.openssh.authorizedKeys.keys = [
157 virtualisation.vlans = [ 1 2 ];
165 server.wait_for_unit("sshd", timeout=30)
166 server_allowed_users.wait_for_unit("sshd", timeout=30)
167 server_localhost_only.wait_for_unit("sshd", timeout=30)
168 server_match_rule.wait_for_unit("sshd", timeout=30)
169 server_no_openssl.wait_for_unit("sshd", timeout=30)
170 server_no_pam.wait_for_unit("sshd", timeout=30)
172 server_lazy.wait_for_unit("sshd.socket", timeout=30)
173 server_localhost_only_lazy.wait_for_unit("sshd.socket", timeout=30)
174 server_lazy_socket.wait_for_unit("sshd.socket", timeout=30)
176 with subtest("manual-authkey"):
177 client.succeed("mkdir -m 700 /root/.ssh")
179 '${pkgs.openssh}/bin/ssh-keygen -t ed25519 -f /root/.ssh/id_ed25519 -N ""'
181 public_key = client.succeed(
182 "${pkgs.openssh}/bin/ssh-keygen -y -f /root/.ssh/id_ed25519"
184 public_key = public_key.strip()
185 client.succeed("chmod 600 /root/.ssh/id_ed25519")
187 server.succeed("mkdir -m 700 /root/.ssh")
188 server.succeed("echo '{}' > /root/.ssh/authorized_keys".format(public_key))
189 server_lazy.succeed("mkdir -m 700 /root/.ssh")
190 server_lazy.succeed("echo '{}' > /root/.ssh/authorized_keys".format(public_key))
192 client.wait_for_unit("network.target")
194 "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no server 'echo hello world' >&2",
198 "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no server 'ulimit -l' | grep 1024",
203 "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no server-lazy 'echo hello world' >&2",
207 "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no server-lazy 'ulimit -l' | grep 1024",
211 with subtest("socket activation on a non-standard port"):
213 "cat ${snakeOilPrivateKey} > privkey.snakeoil"
215 client.succeed("chmod 600 privkey.snakeoil")
216 # The final segment in this IP is allocated according to the alphabetical order of machines in this test.
218 "ssh -p 2222 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i privkey.snakeoil root@192.168.2.5 true",
222 with subtest("configured-authkey"):
224 "cat ${snakeOilPrivateKey} > privkey.snakeoil"
226 client.succeed("chmod 600 privkey.snakeoil")
228 "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i privkey.snakeoil server true",
232 "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i privkey.snakeoil server-lazy true",
236 with subtest("localhost-only"):
237 server_localhost_only.succeed("ss -nlt | grep '127.0.0.1:22'")
238 server_localhost_only_lazy.succeed("ss -nlt | grep '127.0.0.1:22'")
240 with subtest("match-rules"):
241 server_match_rule.succeed("ss -nlt | grep '127.0.0.1:22'")
243 with subtest("allowed-users"):
245 "cat ${snakeOilPrivateKey} > privkey.snakeoil"
247 client.succeed("chmod 600 privkey.snakeoil")
249 "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i privkey.snakeoil alice@server-allowed-users true",
253 "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i privkey.snakeoil bob@server-allowed-users true",
257 "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i privkey.snakeoil carol@server-allowed-users true",
261 with subtest("no-openssl"):
263 "cat ${snakeOilEd25519PrivateKey} > privkey.snakeoil"
265 client.succeed("chmod 600 privkey.snakeoil")
267 "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i privkey.snakeoil server-no-openssl true",
271 with subtest("no-pam"):
273 "cat ${snakeOilPrivateKey} > privkey.snakeoil"
275 client.succeed("chmod 600 privkey.snakeoil")
277 "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i privkey.snakeoil server-no-pam true",