1# Some tests to ensure sudo is working properly.
2{ pkgs, ... }:
3let
4 inherit (pkgs.lib) mkIf optionalString;
5 password = "helloworld";
6in
7 import ./make-test-python.nix ({ lib, pkgs, ...} : {
8 name = "sudo-rs";
9 meta.maintainers = pkgs.sudo-rs.meta.maintainers;
10
11 nodes.machine =
12 { lib, ... }:
13 {
14 environment.systemPackages = [ pkgs.faketty ];
15 users.groups = { foobar = {}; barfoo = {}; baz = { gid = 1337; }; };
16 users.users = {
17 test0 = { isNormalUser = true; extraGroups = [ "wheel" ]; };
18 test1 = { isNormalUser = true; password = password; };
19 test2 = { isNormalUser = true; extraGroups = [ "foobar" ]; password = password; };
20 test3 = { isNormalUser = true; extraGroups = [ "barfoo" ]; };
21 test4 = { isNormalUser = true; extraGroups = [ "baz" ]; };
22 test5 = { isNormalUser = true; };
23 };
24
25 security.sudo-rs = {
26 enable = true;
27 wheelNeedsPassword = false;
28
29 extraRules = [
30 # SUDOERS SYNTAX CHECK (Test whether the module produces a valid output;
31 # errors being detected by the visudo checks.
32
33 # These should not create any entries
34 { users = [ "notest1" ]; commands = [ ]; }
35 { commands = [ { command = "ALL"; options = [ ]; } ]; }
36
37 # Test defining commands with the options syntax, though not setting any options
38 { users = [ "notest2" ]; commands = [ { command = "ALL"; options = [ ]; } ]; }
39
40
41 # CONFIGURATION FOR TEST CASES
42 { users = [ "test1" ]; groups = [ "foobar" ]; commands = [ "ALL" ]; }
43 { groups = [ "barfoo" 1337 ]; commands = [ { command = "ALL"; options = [ "NOPASSWD" ]; } ]; }
44 { users = [ "test5" ]; commands = [ { command = "ALL"; options = [ "NOPASSWD" ]; } ]; runAs = "test1:barfoo"; }
45 ];
46 };
47 };
48
49 nodes.strict = { ... }: {
50 environment.systemPackages = [ pkgs.faketty ];
51 users.users = {
52 admin = { isNormalUser = true; extraGroups = [ "wheel" ]; };
53 noadmin = { isNormalUser = true; };
54 };
55
56 security.sudo-rs = {
57 enable = true;
58 wheelNeedsPassword = false;
59 execWheelOnly = true;
60 };
61 };
62
63 testScript =
64 ''
65 with subtest("users in wheel group should have passwordless sudo"):
66 machine.succeed('faketty -- su - test0 -c "sudo -u root true"')
67
68 with subtest("test1 user should have sudo with password"):
69 machine.succeed('faketty -- su - test1 -c "echo ${password} | sudo -S -u root true"')
70
71 with subtest("test1 user should not be able to use sudo without password"):
72 machine.fail('faketty -- su - test1 -c "sudo -n -u root true"')
73
74 with subtest("users in group 'foobar' should be able to use sudo with password"):
75 machine.succeed('faketty -- su - test2 -c "echo ${password} | sudo -S -u root true"')
76
77 with subtest("users in group 'barfoo' should be able to use sudo without password"):
78 machine.succeed("sudo -u test3 sudo -n -u root true")
79
80 with subtest("users in group 'baz' (GID 1337)"):
81 machine.succeed("sudo -u test4 sudo -n -u root echo true")
82
83 with subtest("test5 user should be able to run commands under test1"):
84 machine.succeed("sudo -u test5 sudo -n -u test1 true")
85
86 with subtest("test5 user should not be able to run commands under root"):
87 machine.fail("sudo -u test5 sudo -n -u root true 2>/dev/null")
88
89 with subtest("users in wheel should be able to run sudo despite execWheelOnly"):
90 strict.succeed('faketty -- su - admin -c "sudo -u root true"')
91
92 with subtest("non-wheel users should be unable to run sudo thanks to execWheelOnly"):
93 strict.fail('faketty -- su - noadmin -c "sudo --help"')
94 '';
95 })