1 # Some tests to ensure sudo is working properly.
4 inherit (pkgs.lib) mkIf optionalString;
5 password = "helloworld";
7 import ./make-test-python.nix ({ lib, pkgs, ...} : {
9 meta.maintainers = pkgs.sudo-rs.meta.maintainers;
14 environment.systemPackages = [ pkgs.faketty ];
15 users.groups = { foobar = {}; barfoo = {}; baz = { gid = 1337; }; };
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; };
27 wheelNeedsPassword = false;
30 # SUDOERS SYNTAX CHECK (Test whether the module produces a valid output;
31 # errors being detected by the visudo checks.
33 # These should not create any entries
34 { users = [ "notest1" ]; commands = [ ]; }
35 { commands = [ { command = "ALL"; options = [ ]; } ]; }
37 # Test defining commands with the options syntax, though not setting any options
38 { users = [ "notest2" ]; commands = [ { command = "ALL"; options = [ ]; } ]; }
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"; }
49 nodes.strict = { ... }: {
50 environment.systemPackages = [ pkgs.faketty ];
52 admin = { isNormalUser = true; extraGroups = [ "wheel" ]; };
53 noadmin = { isNormalUser = true; };
58 wheelNeedsPassword = false;
65 with subtest("users in wheel group should have passwordless sudo"):
66 machine.succeed('faketty -- su - test0 -c "sudo -u root true"')
68 with subtest("test1 user should have sudo with password"):
69 machine.succeed('faketty -- su - test1 -c "echo ${password} | sudo -S -u root true"')
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"')
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"')
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")
80 with subtest("users in group 'baz' (GID 1337)"):
81 machine.succeed("sudo -u test4 sudo -n -u root echo true")
83 with subtest("test5 user should be able to run commands under test1"):
84 machine.succeed("sudo -u test5 sudo -n -u test1 true")
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")
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"')
92 with subtest("non-wheel users should be unable to run sudo thanks to execWheelOnly"):
93 strict.fail('faketty -- su - noadmin -c "sudo --help"')