at 23.11-pre 4.2 kB view raw
1# Some tests to ensure doas is working properly. 2import ./make-test-python.nix ( 3 { lib, ... }: { 4 name = "doas"; 5 meta.maintainers = with lib.maintainers; [ cole-h ]; 6 7 nodes.machine = 8 { ... }: 9 { 10 users.groups = { foobar = {}; barfoo = {}; baz = { gid = 1337; }; }; 11 users.users = { 12 test0 = { isNormalUser = true; extraGroups = [ "wheel" ]; }; 13 test1 = { isNormalUser = true; }; 14 test2 = { isNormalUser = true; extraGroups = [ "foobar" ]; }; 15 test3 = { isNormalUser = true; extraGroups = [ "barfoo" ]; }; 16 test4 = { isNormalUser = true; extraGroups = [ "baz" ]; }; 17 test5 = { isNormalUser = true; }; 18 test6 = { isNormalUser = true; }; 19 test7 = { isNormalUser = true; }; 20 }; 21 22 security.doas = { 23 enable = true; 24 wheelNeedsPassword = false; 25 26 extraRules = [ 27 { users = [ "test1" ]; groups = [ "foobar" ]; } 28 { users = [ "test2" ]; noPass = true; setEnv = [ "CORRECT" "HORSE=BATTERY" ]; } 29 { groups = [ "barfoo" 1337 ]; noPass = true; } 30 { users = [ "test5" ]; noPass = true; keepEnv = true; runAs = "test1"; } 31 { users = [ "test6" ]; noPass = true; keepEnv = true; setEnv = [ "-STAPLE" ]; } 32 { users = [ "test7" ]; noPass = true; setEnv = [ "-SSH_AUTH_SOCK" ]; } 33 ]; 34 }; 35 }; 36 37 testScript = '' 38 with subtest("users in wheel group should have passwordless doas"): 39 machine.succeed('su - test0 -c "doas -u root true"') 40 41 with subtest("test1 user should not be able to use doas without password"): 42 machine.fail('su - test1 -c "doas -n -u root true"') 43 44 with subtest("test2 user should be able to keep some env"): 45 if "CORRECT=1" not in machine.succeed('su - test2 -c "CORRECT=1 doas env"'): 46 raise Exception("failed to keep CORRECT") 47 48 if "HORSE=BATTERY" not in machine.succeed('su - test2 -c "doas env"'): 49 raise Exception("failed to setenv HORSE=BATTERY") 50 51 with subtest("users in group 'barfoo' shouldn't require password"): 52 machine.succeed("doas -u test3 doas -n -u root true") 53 54 with subtest("users in group 'baz' (GID 1337) shouldn't require password"): 55 machine.succeed("doas -u test4 doas -n -u root echo true") 56 57 with subtest("test5 user should be able to run commands under test1"): 58 machine.succeed("doas -u test5 doas -n -u test1 true") 59 60 with subtest("test5 user should not be able to run commands under root"): 61 machine.fail("doas -u test5 doas -n -u root true") 62 63 with subtest("test6 user should be able to keepenv"): 64 envs = ["BATTERY=HORSE", "CORRECT=false"] 65 out = machine.succeed( 66 'su - test6 -c "BATTERY=HORSE CORRECT=false STAPLE=Tr0ub4dor doas env"' 67 ) 68 69 if not all(env in out for env in envs): 70 raise Exception("failed to keep BATTERY or CORRECT") 71 if "STAPLE=Tr0ub4dor" in out: 72 raise Exception("failed to exclude STAPLE") 73 74 with subtest("test7 should not have access to SSH_AUTH_SOCK"): 75 if "SSH_AUTH_SOCK=HOLEY" in machine.succeed( 76 'su - test7 -c "SSH_AUTH_SOCK=HOLEY doas env"' 77 ): 78 raise Exception("failed to exclude SSH_AUTH_SOCK") 79 80 # Test that the doas setuid wrapper precedes the unwrapped version in PATH after 81 # calling doas. 82 # The PATH set by doas is defined in 83 # ../../pkgs/tools/security/doas/0001-add-NixOS-specific-dirs-to-safe-PATH.patch 84 with subtest("recursive calls to doas from subprocesses should succeed"): 85 machine.succeed('doas -u test0 sh -c "doas -u test0 true"') 86 87 with subtest("test0 should inherit TERMINFO_DIRS from the user environment"): 88 dirs = machine.succeed( 89 "su - test0 -c 'doas -u root $SHELL -c \"echo \$TERMINFO_DIRS\"'" 90 ) 91 92 if not "test0" in dirs: 93 raise Exception(f"user profile TERMINFO_DIRS is not preserved: {dirs}") 94 ''; 95 } 96)