2018-01-17 15:56:08 +01:00
|
|
|
# Some tests to ensure sudo is working properly.
|
|
|
|
|
|
|
|
let
|
|
|
|
password = "helloworld";
|
|
|
|
in
|
2023-04-30 16:26:19 +02:00
|
|
|
import ./make-test-python.nix ({ lib, pkgs, ...} : {
|
2018-01-17 15:56:08 +01:00
|
|
|
name = "sudo";
|
2023-04-30 16:26:19 +02:00
|
|
|
meta.maintainers = with lib.maintainers; [ lschuermann ];
|
2018-01-17 15:56:08 +01:00
|
|
|
|
2021-05-05 13:09:45 +02:00
|
|
|
nodes.machine =
|
2018-07-20 22:56:59 +02:00
|
|
|
{ lib, ... }:
|
2018-01-17 15:56:08 +01:00
|
|
|
{
|
2018-06-30 01:55:42 +02:00
|
|
|
users.groups = { foobar = {}; barfoo = {}; baz = { gid = 1337; }; };
|
2018-01-17 15:56:08 +01:00
|
|
|
users.users = {
|
|
|
|
test0 = { isNormalUser = true; extraGroups = [ "wheel" ]; };
|
|
|
|
test1 = { isNormalUser = true; password = password; };
|
|
|
|
test2 = { isNormalUser = true; extraGroups = [ "foobar" ]; password = password; };
|
|
|
|
test3 = { isNormalUser = true; extraGroups = [ "barfoo" ]; };
|
|
|
|
test4 = { isNormalUser = true; extraGroups = [ "baz" ]; };
|
|
|
|
test5 = { isNormalUser = true; };
|
|
|
|
};
|
|
|
|
|
|
|
|
security.sudo = {
|
|
|
|
enable = true;
|
|
|
|
wheelNeedsPassword = false;
|
|
|
|
|
2022-02-01 12:39:00 +01:00
|
|
|
extraConfig = ''
|
|
|
|
Defaults lecture="never"
|
|
|
|
'';
|
|
|
|
|
2018-01-17 15:56:08 +01:00
|
|
|
extraRules = [
|
|
|
|
# SUDOERS SYNTAX CHECK (Test whether the module produces a valid output;
|
|
|
|
# errors being detected by the visudo checks.
|
|
|
|
|
|
|
|
# These should not create any entries
|
|
|
|
{ users = [ "notest1" ]; commands = [ ]; }
|
|
|
|
{ commands = [ { command = "ALL"; options = [ ]; } ]; }
|
|
|
|
|
|
|
|
# Test defining commands with the options syntax, though not setting any options
|
|
|
|
{ users = [ "notest2" ]; commands = [ { command = "ALL"; options = [ ]; } ]; }
|
|
|
|
|
|
|
|
|
|
|
|
# CONFIGURATION FOR TEST CASES
|
|
|
|
{ users = [ "test1" ]; groups = [ "foobar" ]; commands = [ "ALL" ]; }
|
|
|
|
{ groups = [ "barfoo" 1337 ]; commands = [ { command = "ALL"; options = [ "NOPASSWD" "NOSETENV" ]; } ]; }
|
|
|
|
{ users = [ "test5" ]; commands = [ { command = "ALL"; options = [ "NOPASSWD" "SETENV" ]; } ]; runAs = "test1:barfoo"; }
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-05-05 13:09:45 +02:00
|
|
|
nodes.strict = { ... }: {
|
|
|
|
users.users = {
|
|
|
|
admin = { isNormalUser = true; extraGroups = [ "wheel" ]; };
|
|
|
|
noadmin = { isNormalUser = true; };
|
|
|
|
};
|
|
|
|
|
|
|
|
security.sudo = {
|
|
|
|
enable = true;
|
|
|
|
wheelNeedsPassword = false;
|
|
|
|
execWheelOnly = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-01-17 15:56:08 +01:00
|
|
|
testScript =
|
|
|
|
''
|
2019-11-24 21:06:36 +01:00
|
|
|
with subtest("users in wheel group should have passwordless sudo"):
|
|
|
|
machine.succeed('su - test0 -c "sudo -u root true"')
|
2018-01-17 15:56:08 +01:00
|
|
|
|
2019-11-24 21:06:36 +01:00
|
|
|
with subtest("test1 user should have sudo with password"):
|
|
|
|
machine.succeed('su - test1 -c "echo ${password} | sudo -S -u root true"')
|
2018-01-17 15:56:08 +01:00
|
|
|
|
2019-11-24 21:06:36 +01:00
|
|
|
with subtest("test1 user should not be able to use sudo without password"):
|
|
|
|
machine.fail('su - test1 -c "sudo -n -u root true"')
|
2018-01-17 15:56:08 +01:00
|
|
|
|
2019-11-24 21:06:36 +01:00
|
|
|
with subtest("users in group 'foobar' should be able to use sudo with password"):
|
2022-01-03 00:37:26 +01:00
|
|
|
machine.succeed('su - test2 -c "echo ${password} | sudo -S -u root true"')
|
2018-01-17 15:56:08 +01:00
|
|
|
|
2019-11-24 21:06:36 +01:00
|
|
|
with subtest("users in group 'barfoo' should be able to use sudo without password"):
|
|
|
|
machine.succeed("sudo -u test3 sudo -n -u root true")
|
2018-01-17 15:56:08 +01:00
|
|
|
|
2019-11-24 21:06:36 +01:00
|
|
|
with subtest("users in group 'baz' (GID 1337)"):
|
|
|
|
machine.succeed("sudo -u test4 sudo -n -u root echo true")
|
2018-01-17 15:56:08 +01:00
|
|
|
|
2019-11-24 21:06:36 +01:00
|
|
|
with subtest("test5 user should be able to run commands under test1"):
|
|
|
|
machine.succeed("sudo -u test5 sudo -n -u test1 true")
|
2018-01-17 15:56:08 +01:00
|
|
|
|
2019-11-24 21:06:36 +01:00
|
|
|
with subtest("test5 user should not be able to run commands under root"):
|
|
|
|
machine.fail("sudo -u test5 sudo -n -u root true")
|
2018-01-17 15:56:08 +01:00
|
|
|
|
2020-06-23 15:59:14 +02:00
|
|
|
with subtest("test5 user should be able to keep their environment"):
|
2019-11-24 21:06:36 +01:00
|
|
|
machine.succeed("sudo -u test5 sudo -n -E -u test1 true")
|
2018-01-17 15:56:08 +01:00
|
|
|
|
2019-11-24 21:06:36 +01:00
|
|
|
with subtest("users in group 'barfoo' should not be able to keep their environment"):
|
|
|
|
machine.fail("sudo -u test3 sudo -n -E -u root true")
|
2021-05-05 13:09:45 +02:00
|
|
|
|
|
|
|
with subtest("users in wheel should be able to run sudo despite execWheelOnly"):
|
|
|
|
strict.succeed('su - admin -c "sudo -u root true"')
|
|
|
|
|
|
|
|
with subtest("non-wheel users should be unable to run sudo thanks to execWheelOnly"):
|
|
|
|
strict.fail('su - noadmin -c "sudo --help"')
|
2018-01-17 15:56:08 +01:00
|
|
|
'';
|
|
|
|
})
|