2024-09-01 16:30:42 +02:00
|
|
|
{ lib, testers }:
|
2024-02-08 14:47:26 +01:00
|
|
|
let
|
2024-09-01 16:30:42 +02:00
|
|
|
userPasswordTest = name: extraConfig: testers.runNixOSTest {
|
2024-02-08 14:47:26 +01:00
|
|
|
inherit name;
|
2023-11-05 14:40:23 +01:00
|
|
|
nodes.machine = { config, lib, ... }: {
|
2024-02-08 14:47:26 +01:00
|
|
|
imports = [
|
|
|
|
../../modules/sops
|
|
|
|
extraConfig
|
|
|
|
];
|
2021-10-19 18:26:43 +02:00
|
|
|
sops = {
|
2024-04-16 17:07:24 -07:00
|
|
|
age.keyFile = "/run/age-keys.txt";
|
2021-10-19 18:26:43 +02:00
|
|
|
defaultSopsFile = ./test-assets/secrets.yaml;
|
|
|
|
secrets.test_key.neededForUsers = true;
|
|
|
|
secrets."nested/test/file".owner = "example-user";
|
|
|
|
};
|
2024-10-06 17:29:14 +02:00
|
|
|
system.switch.enable = true;
|
2021-10-19 18:26:43 +02:00
|
|
|
|
2024-08-31 18:59:45 +02:00
|
|
|
users.users.example-user = lib.mkMerge [
|
|
|
|
(lib.mkIf (! config.systemd.sysusers.enable) {
|
|
|
|
isNormalUser = true;
|
|
|
|
hashedPasswordFile = config.sops.secrets.test_key.path;
|
|
|
|
})
|
|
|
|
(lib.mkIf config.systemd.sysusers.enable {
|
|
|
|
isSystemUser = true;
|
|
|
|
group = "users";
|
|
|
|
hashedPasswordFile = config.sops.secrets.test_key.path;
|
|
|
|
})
|
|
|
|
];
|
2021-10-19 18:26:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
testScript = ''
|
|
|
|
start_all()
|
2024-02-08 14:47:26 +01:00
|
|
|
machine.wait_for_unit("multi-user.target")
|
|
|
|
|
2021-10-19 18:26:43 +02:00
|
|
|
machine.succeed("getent shadow example-user | grep -q :test_value:") # password was set
|
|
|
|
machine.succeed("cat /run/secrets/nested/test/file | grep -q 'another value'") # regular secrets work...
|
2024-02-08 14:47:26 +01:00
|
|
|
user = machine.succeed("stat -c%U /run/secrets/nested/test/file").strip() # ...and are owned...
|
|
|
|
assert user == "example-user", f"Expected 'example-user', got '{user}'"
|
2021-11-13 14:17:51 +01:00
|
|
|
machine.succeed("cat /run/secrets-for-users/test_key | grep -q 'test_value'") # the user password still exists
|
|
|
|
|
2024-02-08 14:47:26 +01:00
|
|
|
# BUG in nixos's overlayfs... systemd crashes on switch-to-configuration test
|
2024-09-01 16:30:42 +02:00
|
|
|
'' + lib.optionalString (!(extraConfig ? system.etc.overlay.enable)) ''
|
2021-11-13 14:17:51 +01:00
|
|
|
machine.succeed("/run/current-system/bin/switch-to-configuration test")
|
|
|
|
machine.succeed("cat /run/secrets/nested/test/file | grep -q 'another value'") # the regular secrets still work after a switch
|
|
|
|
machine.succeed("cat /run/secrets-for-users/test_key | grep -q 'test_value'") # the user password is still present after a switch
|
2021-10-19 18:26:43 +02:00
|
|
|
'';
|
|
|
|
};
|
2024-02-08 14:47:26 +01:00
|
|
|
in {
|
2024-09-01 16:30:42 +02:00
|
|
|
ssh-keys = testers.runNixOSTest {
|
2024-02-08 14:47:26 +01:00
|
|
|
name = "sops-ssh-keys";
|
|
|
|
nodes.server = { ... }: {
|
|
|
|
imports = [ ../../modules/sops ];
|
|
|
|
services.openssh.enable = true;
|
|
|
|
services.openssh.hostKeys = [{
|
|
|
|
type = "rsa";
|
|
|
|
bits = 4096;
|
|
|
|
path = ./test-assets/ssh-key;
|
|
|
|
}];
|
|
|
|
sops.defaultSopsFile = ./test-assets/secrets.yaml;
|
|
|
|
sops.secrets.test_key = { };
|
|
|
|
};
|
|
|
|
|
|
|
|
testScript = ''
|
|
|
|
start_all()
|
|
|
|
server.succeed("cat /run/secrets/test_key | grep -q test_value")
|
|
|
|
'';
|
|
|
|
};
|
2021-10-19 18:26:43 +02:00
|
|
|
|
2024-09-01 16:30:42 +02:00
|
|
|
pruning = testers.runNixOSTest {
|
2021-11-06 21:29:22 +01:00
|
|
|
name = "sops-pruning";
|
2022-05-25 08:51:40 +02:00
|
|
|
nodes.machine = { lib, ... }: {
|
2021-11-06 21:29:22 +01:00
|
|
|
imports = [ ../../modules/sops ];
|
|
|
|
sops = {
|
2024-04-16 17:07:24 -07:00
|
|
|
age.keyFile = "/run/age-keys.txt";
|
2021-11-06 21:29:22 +01:00
|
|
|
defaultSopsFile = ./test-assets/secrets.yaml;
|
2022-07-09 00:07:09 +02:00
|
|
|
secrets.test_key = { };
|
2021-11-06 21:29:22 +01:00
|
|
|
keepGenerations = lib.mkDefault 0;
|
|
|
|
};
|
|
|
|
|
2024-04-16 17:07:24 -07:00
|
|
|
# must run before sops sets up keys
|
|
|
|
boot.initrd.postDeviceCommands = ''
|
|
|
|
cp -r ${./test-assets/age-keys.txt} /run/age-keys.txt
|
|
|
|
chmod -R 700 /run/age-keys.txt
|
|
|
|
'';
|
|
|
|
|
2021-11-06 21:29:22 +01:00
|
|
|
specialisation.pruning.configuration.sops.keepGenerations = 10;
|
|
|
|
};
|
|
|
|
|
|
|
|
testScript = ''
|
|
|
|
# Force us to generation 100
|
|
|
|
machine.succeed("mkdir /run/secrets.d/{2..99} /run/secrets.d/non-numeric")
|
|
|
|
machine.succeed("ln -fsn /run/secrets.d/99 /run/secrets")
|
|
|
|
machine.succeed("/run/current-system/activate")
|
|
|
|
machine.succeed("test -d /run/secrets.d/100")
|
|
|
|
|
|
|
|
# Ensure nothing is pruned, these are just random numbers
|
|
|
|
machine.succeed("test -d /run/secrets.d/1")
|
|
|
|
machine.succeed("test -d /run/secrets.d/90")
|
|
|
|
machine.succeed("test -d /run/secrets.d/non-numeric")
|
|
|
|
|
|
|
|
machine.succeed("/run/current-system/specialisation/pruning/bin/switch-to-configuration test")
|
|
|
|
print(machine.succeed("ls -la /run/secrets.d/"))
|
|
|
|
|
|
|
|
# Ensure stuff was properly pruned.
|
|
|
|
# We are now at generation 101 so 92 must exist when we keep 10 generations
|
|
|
|
# and 91 must not.
|
|
|
|
machine.fail("test -d /run/secrets.d/91")
|
|
|
|
machine.succeed("test -d /run/secrets.d/92")
|
|
|
|
machine.succeed("test -d /run/secrets.d/non-numeric")
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-09-01 16:30:42 +02:00
|
|
|
age-keys = testers.runNixOSTest {
|
2022-07-09 00:07:09 +02:00
|
|
|
name = "sops-age-keys";
|
2024-10-16 01:30:11 +02:00
|
|
|
nodes.machine = { config, ... }: {
|
2022-07-09 00:07:09 +02:00
|
|
|
imports = [ ../../modules/sops ];
|
|
|
|
sops = {
|
2024-04-16 17:07:24 -07:00
|
|
|
age.keyFile = "/run/age-keys.txt";
|
2022-07-09 00:07:09 +02:00
|
|
|
defaultSopsFile = ./test-assets/secrets.yaml;
|
2024-10-16 01:30:11 +02:00
|
|
|
secrets = {
|
|
|
|
test_key = { };
|
|
|
|
|
|
|
|
test_key_someuser_somegroup = {
|
|
|
|
uid = config.users.users."someuser".uid;
|
|
|
|
gid = config.users.groups."somegroup".gid;
|
|
|
|
key = "test_key";
|
|
|
|
};
|
|
|
|
test_key_someuser_root = {
|
|
|
|
uid = config.users.users."someuser".uid;
|
|
|
|
key = "test_key";
|
|
|
|
};
|
|
|
|
test_key_root_root = {
|
|
|
|
key = "test_key";
|
|
|
|
};
|
|
|
|
test_key_1001_1001 = {
|
|
|
|
uid = 1001;
|
|
|
|
gid = 1001;
|
|
|
|
key = "test_key";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
users.users."someuser" = {
|
|
|
|
uid = 1000;
|
|
|
|
group = "somegroup";
|
|
|
|
isNormalUser = true;
|
|
|
|
};
|
|
|
|
users.groups."somegroup" = {
|
|
|
|
gid = 1000;
|
2022-07-09 00:07:09 +02:00
|
|
|
};
|
2024-04-16 17:07:24 -07:00
|
|
|
|
|
|
|
# must run before sops sets up keys
|
|
|
|
boot.initrd.postDeviceCommands = ''
|
|
|
|
cp -r ${./test-assets/age-keys.txt} /run/age-keys.txt
|
|
|
|
chmod -R 700 /run/age-keys.txt
|
|
|
|
'';
|
2022-07-09 00:07:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
testScript = ''
|
|
|
|
start_all()
|
|
|
|
machine.succeed("cat /run/secrets/test_key | grep -q test_value")
|
2024-10-16 01:30:11 +02:00
|
|
|
|
|
|
|
with subtest("test ownership"):
|
|
|
|
machine.succeed("[ $(stat -c%u /run/secrets/test_key_someuser_somegroup) = '1000' ]")
|
|
|
|
machine.succeed("[ $(stat -c%g /run/secrets/test_key_someuser_somegroup) = '1000' ]")
|
|
|
|
machine.succeed("[ $(stat -c%U /run/secrets/test_key_someuser_somegroup) = 'someuser' ]")
|
|
|
|
machine.succeed("[ $(stat -c%G /run/secrets/test_key_someuser_somegroup) = 'somegroup' ]")
|
|
|
|
|
|
|
|
machine.succeed("[ $(stat -c%u /run/secrets/test_key_someuser_root) = '1000' ]")
|
|
|
|
machine.succeed("[ $(stat -c%g /run/secrets/test_key_someuser_root) = '0' ]")
|
|
|
|
machine.succeed("[ $(stat -c%U /run/secrets/test_key_someuser_root) = 'someuser' ]")
|
|
|
|
machine.succeed("[ $(stat -c%G /run/secrets/test_key_someuser_root) = 'root' ]")
|
|
|
|
|
|
|
|
machine.succeed("[ $(stat -c%u /run/secrets/test_key_1001_1001) = '1001' ]")
|
|
|
|
machine.succeed("[ $(stat -c%g /run/secrets/test_key_1001_1001) = '1001' ]")
|
|
|
|
machine.succeed("[ $(stat -c%U /run/secrets/test_key_1001_1001) = 'UNKNOWN' ]")
|
|
|
|
machine.succeed("[ $(stat -c%G /run/secrets/test_key_1001_1001) = 'UNKNOWN' ]")
|
2022-07-09 00:07:09 +02:00
|
|
|
'';
|
2021-08-27 20:09:28 +02:00
|
|
|
};
|
|
|
|
|
2024-09-01 16:30:42 +02:00
|
|
|
age-ssh-keys = testers.runNixOSTest {
|
2022-07-09 00:07:09 +02:00
|
|
|
name = "sops-age-ssh-keys";
|
|
|
|
nodes.machine = {
|
|
|
|
imports = [ ../../modules/sops ];
|
|
|
|
services.openssh.enable = true;
|
|
|
|
services.openssh.hostKeys = [{
|
|
|
|
type = "ed25519";
|
|
|
|
path = ./test-assets/ssh-ed25519-key;
|
|
|
|
}];
|
2024-10-16 01:30:11 +02:00
|
|
|
|
2022-07-09 00:07:09 +02:00
|
|
|
sops = {
|
|
|
|
defaultSopsFile = ./test-assets/secrets.yaml;
|
|
|
|
secrets.test_key = { };
|
|
|
|
# Generate a key and append it to make sure it appending doesn't break anything
|
|
|
|
age = {
|
|
|
|
keyFile = "/tmp/testkey";
|
|
|
|
generateKey = true;
|
|
|
|
};
|
2021-09-30 15:28:39 +02:00
|
|
|
};
|
2021-08-27 20:09:28 +02:00
|
|
|
};
|
|
|
|
|
2022-07-09 00:07:09 +02:00
|
|
|
testScript = ''
|
|
|
|
start_all()
|
|
|
|
machine.succeed("cat /run/secrets/test_key | grep -q test_value")
|
|
|
|
'';
|
2021-08-27 20:09:28 +02:00
|
|
|
};
|
2021-08-27 00:49:58 +02:00
|
|
|
|
2024-09-01 16:30:42 +02:00
|
|
|
pgp-keys = testers.runNixOSTest {
|
2022-07-09 00:07:09 +02:00
|
|
|
name = "sops-pgp-keys";
|
2024-10-16 01:30:11 +02:00
|
|
|
nodes.server = { lib, config, ... }: {
|
2022-07-09 00:07:09 +02:00
|
|
|
imports = [ ../../modules/sops ];
|
2020-07-19 23:23:38 +01:00
|
|
|
|
2022-07-09 00:07:09 +02:00
|
|
|
users.users.someuser = {
|
|
|
|
isSystemUser = true;
|
|
|
|
group = "nogroup";
|
|
|
|
};
|
|
|
|
|
|
|
|
sops.gnupg.home = "/run/gpghome";
|
|
|
|
sops.defaultSopsFile = ./test-assets/secrets.yaml;
|
|
|
|
sops.secrets.test_key.owner = config.users.users.someuser.name;
|
|
|
|
sops.secrets."nested/test/file".owner = config.users.users.someuser.name;
|
|
|
|
sops.secrets.existing-file = {
|
|
|
|
key = "test_key";
|
|
|
|
path = "/run/existing-file";
|
|
|
|
};
|
|
|
|
# must run before sops
|
|
|
|
system.activationScripts.gnupghome = lib.stringAfter [ "etc" ] ''
|
|
|
|
cp -r ${./test-assets/gnupghome} /run/gpghome
|
|
|
|
chmod -R 700 /run/gpghome
|
|
|
|
|
|
|
|
touch /run/existing-file
|
|
|
|
'';
|
|
|
|
# Useful for debugging
|
|
|
|
#environment.systemPackages = [ pkgs.gnupg pkgs.sops ];
|
|
|
|
#environment.variables = {
|
|
|
|
# GNUPGHOME = "/run/gpghome";
|
|
|
|
# SOPS_GPG_EXEC="${pkgs.gnupg}/bin/gpg";
|
|
|
|
# SOPSFILE = "${./test-assets/secrets.yaml}";
|
|
|
|
#};
|
|
|
|
};
|
|
|
|
testScript = ''
|
|
|
|
def assertEqual(exp: str, act: str) -> None:
|
|
|
|
if exp != act:
|
|
|
|
raise Exception(f"'{exp}' != '{act}'")
|
2020-07-19 23:23:38 +01:00
|
|
|
|
|
|
|
|
2022-07-09 00:07:09 +02:00
|
|
|
start_all()
|
2020-07-19 23:23:38 +01:00
|
|
|
|
2022-07-09 00:07:09 +02:00
|
|
|
value = server.succeed("cat /run/secrets/test_key")
|
|
|
|
assertEqual("test_value", value)
|
2020-07-19 23:23:38 +01:00
|
|
|
|
2022-07-09 00:07:09 +02:00
|
|
|
server.succeed("runuser -u someuser -- cat /run/secrets/test_key >&2")
|
|
|
|
value = server.succeed("cat /run/secrets/nested/test/file")
|
|
|
|
assertEqual(value, "another value")
|
|
|
|
|
|
|
|
target = server.succeed("readlink -f /run/existing-file")
|
|
|
|
assertEqual("/run/secrets.d/1/existing-file", target.strip())
|
|
|
|
'';
|
|
|
|
};
|
2021-09-02 11:00:57 +02:00
|
|
|
|
2024-09-01 16:30:42 +02:00
|
|
|
templates = testers.runNixOSTest {
|
2023-03-23 23:04:05 +08:00
|
|
|
name = "sops-templates";
|
2024-04-16 17:07:24 -07:00
|
|
|
nodes.machine = { config, lib, ... }: {
|
2023-03-23 23:04:05 +08:00
|
|
|
imports = [ ../../modules/sops ];
|
|
|
|
sops = {
|
2024-04-16 17:07:24 -07:00
|
|
|
age.keyFile = "/run/age-keys.txt";
|
2023-03-23 23:04:05 +08:00
|
|
|
defaultSopsFile = ./test-assets/secrets.yaml;
|
|
|
|
secrets.test_key = { };
|
|
|
|
};
|
|
|
|
|
2024-04-16 17:07:24 -07:00
|
|
|
# must run before sops sets up keys
|
|
|
|
boot.initrd.postDeviceCommands = ''
|
|
|
|
cp -r ${./test-assets/age-keys.txt} /run/age-keys.txt
|
|
|
|
chmod -R 700 /run/age-keys.txt
|
|
|
|
'';
|
|
|
|
|
2023-03-23 23:04:05 +08:00
|
|
|
sops.templates.test_template = {
|
|
|
|
content = ''
|
|
|
|
This line is not modified.
|
|
|
|
The next value will be replaced by ${config.sops.placeholder.test_key}
|
|
|
|
This line is also not modified.
|
|
|
|
'';
|
|
|
|
mode = "0400";
|
|
|
|
owner = "someuser";
|
|
|
|
group = "somegroup";
|
|
|
|
};
|
2023-04-18 12:47:12 +08:00
|
|
|
sops.templates.test_default.content = ''
|
|
|
|
Test value: ${config.sops.placeholder.test_key}
|
|
|
|
'';
|
2023-03-23 23:04:05 +08:00
|
|
|
|
|
|
|
users.groups.somegroup = {};
|
|
|
|
users.users.someuser = {
|
|
|
|
isSystemUser = true;
|
|
|
|
group = "somegroup";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
testScript = ''
|
|
|
|
start_all()
|
|
|
|
machine.succeed("[ $(stat -c%U /run/secrets-rendered/test_template) = 'someuser' ]")
|
|
|
|
machine.succeed("[ $(stat -c%G /run/secrets-rendered/test_template) = 'somegroup' ]")
|
2023-04-18 12:47:12 +08:00
|
|
|
machine.succeed("[ $(stat -c%U /run/secrets-rendered/test_default) = 'root' ]")
|
|
|
|
machine.succeed("[ $(stat -c%G /run/secrets-rendered/test_default) = 'root' ]")
|
2023-03-23 23:04:05 +08:00
|
|
|
|
|
|
|
expected = """
|
|
|
|
This line is not modified.
|
|
|
|
The next value will be replaced by test_value
|
|
|
|
This line is also not modified.
|
|
|
|
"""
|
|
|
|
rendered = machine.succeed("cat /run/secrets-rendered/test_template")
|
|
|
|
|
2023-04-18 12:47:12 +08:00
|
|
|
expected_default = """
|
|
|
|
Test value: test_value
|
|
|
|
"""
|
|
|
|
rendered_default = machine.succeed("cat /run/secrets-rendered/test_default")
|
|
|
|
|
|
|
|
if rendered.strip() != expected.strip() or rendered_default.strip() != expected_default.strip():
|
2023-03-23 23:04:05 +08:00
|
|
|
raise Exception("Template is not rendered correctly")
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-09-01 16:30:42 +02:00
|
|
|
restart-and-reload = testers.runNixOSTest {
|
2022-03-14 17:30:56 +01:00
|
|
|
name = "sops-restart-and-reload";
|
2022-05-25 08:51:40 +02:00
|
|
|
nodes.machine = { pkgs, lib, config, ... }: {
|
2022-07-09 00:07:09 +02:00
|
|
|
imports = [ ../../modules/sops ];
|
2022-03-14 17:30:56 +01:00
|
|
|
|
|
|
|
sops = {
|
2024-04-16 17:07:24 -07:00
|
|
|
age.keyFile = "/run/age-keys.txt";
|
2022-03-14 17:30:56 +01:00
|
|
|
defaultSopsFile = ./test-assets/secrets.yaml;
|
|
|
|
secrets.test_key = {
|
|
|
|
restartUnits = [ "restart-unit.service" "reload-unit.service" ];
|
|
|
|
reloadUnits = [ "reload-trigger.service" ];
|
|
|
|
};
|
|
|
|
};
|
2024-10-06 17:29:14 +02:00
|
|
|
system.switch.enable = true;
|
2022-03-14 17:30:56 +01:00
|
|
|
|
2024-04-16 17:07:24 -07:00
|
|
|
# must run before sops sets up keys
|
|
|
|
boot.initrd.postDeviceCommands = ''
|
|
|
|
cp -r ${./test-assets/age-keys.txt} /run/age-keys.txt
|
|
|
|
chmod -R 700 /run/age-keys.txt
|
|
|
|
'';
|
|
|
|
|
2022-03-14 17:30:56 +01:00
|
|
|
systemd.services."restart-unit" = {
|
|
|
|
description = "Restart unit";
|
|
|
|
# not started on boot
|
2022-07-09 00:07:09 +02:00
|
|
|
serviceConfig = { ExecStart = "/bin/sh -c 'echo ok > /restarted'"; };
|
2022-03-14 17:30:56 +01:00
|
|
|
};
|
|
|
|
systemd.services."reload-unit" = {
|
|
|
|
description = "Reload unit";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
reloadIfChanged = true;
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
|
|
|
ExecStart = "/bin/sh -c true";
|
|
|
|
ExecReload = "/bin/sh -c 'echo ok > /reloaded'";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
systemd.services."reload-trigger" = {
|
|
|
|
description = "Reload trigger unit";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
|
|
|
ExecStart = "/bin/sh -c true";
|
|
|
|
ExecReload = "/bin/sh -c 'echo ok > /reloaded'";
|
|
|
|
};
|
|
|
|
};
|
2024-10-06 17:29:14 +02:00
|
|
|
|
2022-07-09 00:07:09 +02:00
|
|
|
};
|
|
|
|
testScript = ''
|
|
|
|
machine.wait_for_unit("multi-user.target")
|
|
|
|
machine.fail("test -f /restarted")
|
|
|
|
machine.fail("test -f /reloaded")
|
|
|
|
|
|
|
|
# Nothing is to be restarted after boot
|
|
|
|
machine.fail("ls /run/nixos/*-list")
|
|
|
|
|
|
|
|
# Nothing happens when the secret is not changed
|
|
|
|
machine.succeed("/run/current-system/bin/switch-to-configuration test")
|
|
|
|
machine.fail("test -f /restarted")
|
|
|
|
machine.fail("test -f /reloaded")
|
2022-03-14 17:30:56 +01:00
|
|
|
|
2022-07-09 00:07:09 +02:00
|
|
|
# Ensure the secret is changed
|
|
|
|
machine.succeed(": > /run/secrets/test_key")
|
2022-03-14 17:30:56 +01:00
|
|
|
|
2022-07-09 00:07:09 +02:00
|
|
|
# The secret is changed, now something should happen
|
|
|
|
machine.succeed("/run/current-system/bin/switch-to-configuration test")
|
|
|
|
|
|
|
|
# Ensure something happened
|
|
|
|
machine.succeed("test -f /restarted")
|
|
|
|
machine.succeed("test -f /reloaded")
|
|
|
|
|
|
|
|
with subtest("change detection"):
|
|
|
|
machine.succeed("rm /run/secrets/test_key")
|
|
|
|
out = machine.succeed("/run/current-system/bin/switch-to-configuration test")
|
|
|
|
if "adding secret" not in out:
|
|
|
|
raise Exception("Addition detection does not work")
|
2022-03-14 17:30:56 +01:00
|
|
|
|
|
|
|
machine.succeed(": > /run/secrets/test_key")
|
2022-07-09 00:07:09 +02:00
|
|
|
out = machine.succeed("/run/current-system/bin/switch-to-configuration test")
|
|
|
|
if "modifying secret" not in out:
|
|
|
|
raise Exception("Modification detection does not work")
|
|
|
|
|
|
|
|
machine.succeed(": > /run/secrets/another_key")
|
|
|
|
out = machine.succeed("/run/current-system/bin/switch-to-configuration test")
|
|
|
|
if "removing secret" not in out:
|
|
|
|
raise Exception("Removal detection does not work")
|
|
|
|
|
|
|
|
with subtest("dry activation"):
|
|
|
|
machine.succeed("rm /run/secrets/test_key")
|
|
|
|
machine.succeed(": > /run/secrets/another_key")
|
|
|
|
out = machine.succeed("/run/current-system/bin/switch-to-configuration dry-activate")
|
|
|
|
if "would add secret" not in out:
|
|
|
|
raise Exception("Dry addition detection does not work")
|
|
|
|
if "would remove secret" not in out:
|
|
|
|
raise Exception("Dry removal detection does not work")
|
|
|
|
|
|
|
|
machine.fail("test -f /run/secrets/test_key")
|
|
|
|
machine.succeed("test -f /run/secrets/another_key")
|
|
|
|
|
|
|
|
machine.succeed("/run/current-system/bin/switch-to-configuration test")
|
|
|
|
machine.succeed("test -f /run/secrets/test_key")
|
|
|
|
machine.succeed("rm /restarted /reloaded")
|
|
|
|
machine.fail("test -f /run/secrets/another_key")
|
|
|
|
|
|
|
|
machine.succeed(": > /run/secrets/test_key")
|
|
|
|
out = machine.succeed("/run/current-system/bin/switch-to-configuration dry-activate")
|
|
|
|
if "would modify secret" not in out:
|
|
|
|
raise Exception("Dry modification detection does not work")
|
|
|
|
machine.succeed("[ $(cat /run/secrets/test_key | wc -c) = 0 ]")
|
|
|
|
|
|
|
|
machine.fail("test -f /restarted") # not done in dry mode
|
|
|
|
machine.fail("test -f /reloaded") # not done in dry mode
|
|
|
|
'';
|
2022-03-14 17:30:56 +01:00
|
|
|
};
|
2024-02-08 14:47:26 +01:00
|
|
|
|
2024-04-16 17:07:24 -07:00
|
|
|
user-passwords = userPasswordTest "sops-user-passwords" {
|
|
|
|
# must run before sops sets up keys
|
|
|
|
boot.initrd.postDeviceCommands = ''
|
|
|
|
cp -r ${./test-assets/age-keys.txt} /run/age-keys.txt
|
|
|
|
chmod -R 700 /run/age-keys.txt
|
|
|
|
'';
|
|
|
|
};
|
2024-09-01 16:30:42 +02:00
|
|
|
} // lib.optionalAttrs (lib.versionAtLeast (lib.versions.majorMinor lib.version) "24.05") {
|
|
|
|
user-passwords-sysusers = userPasswordTest "sops-user-passwords-sysusers" ({ pkgs, ... }: {
|
2024-02-08 14:47:26 +01:00
|
|
|
systemd.sysusers.enable = true;
|
|
|
|
users.mutableUsers = true;
|
|
|
|
system.etc.overlay.enable = true;
|
|
|
|
boot.initrd.systemd.enable = true;
|
|
|
|
boot.kernelPackages = pkgs.linuxPackages_latest;
|
2024-04-16 17:07:24 -07:00
|
|
|
|
2024-08-31 18:59:45 +02:00
|
|
|
# must run before sops sets up keys
|
|
|
|
systemd.services."sops-install-secrets-for-users".preStart = ''
|
|
|
|
printf '${builtins.readFile ./test-assets/age-keys.txt}' > /run/age-keys.txt
|
|
|
|
chmod -R 700 /run/age-keys.txt
|
|
|
|
'';
|
|
|
|
});
|
|
|
|
} // lib.optionalAttrs (lib.versionAtLeast (lib.versions.majorMinor lib.version) "24.11") {
|
|
|
|
user-passwords-userborn = userPasswordTest "sops-user-passwords-userborn" ({ pkgs, ... }: {
|
|
|
|
services.userborn.enable = true;
|
|
|
|
users.mutableUsers = false;
|
|
|
|
system.etc.overlay.enable = true;
|
|
|
|
boot.initrd.systemd.enable = true;
|
|
|
|
boot.kernelPackages = pkgs.linuxPackages_latest;
|
|
|
|
|
2024-04-16 17:07:24 -07:00
|
|
|
# must run before sops sets up keys
|
|
|
|
systemd.services."sops-install-secrets-for-users".preStart = ''
|
|
|
|
printf '${builtins.readFile ./test-assets/age-keys.txt}' > /run/age-keys.txt
|
|
|
|
chmod -R 700 /run/age-keys.txt
|
|
|
|
'';
|
2024-09-01 16:30:42 +02:00
|
|
|
});
|
2020-07-12 13:50:55 +01:00
|
|
|
}
|