diff options
| author | tv <tv@krebsco.de> | 2026-11-30 13:13:44 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-30 19:51:09 +0000 |
| commit | e1644b9d0f72e9bad824e75e3b294968b06a454e (patch) | |
| tree | 15941ea7931c722c4df8f8b964f4ed9976fb030f /nix/test.nix | |
| parent | f67a9a0c80e60dbe44c96ec8573ae78a35852ac4 (diff) | |
nix: the package, a NixOS module, its VM test
The module makes the schema from options (/etc/enstate/schema.json),
every slot its own view unless it says otherwise, and names the views'
paths (read-only `path` options) for the services that read them. It
configures none of them: a service whose LoadCredential= reads a view is
ordered after the unit that puts the views in place, by itself. That
unit runs --migrate at boot and when the schema changes; the check
before a switch is --migrate --dry-run against the new schema. The
test: a consumer with a LoadCredential= of its own; apply from a source
list, rotate, switch across a rename and back, a switch refused, a
reboot.
Diffstat (limited to 'nix/test.nix')
| -rw-r--r-- | nix/test.nix | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/nix/test.nix b/nix/test.nix new file mode 100644 index 0000000..f2739eb --- /dev/null +++ b/nix/test.nix @@ -0,0 +1,119 @@ +# The NixOS module on a VM (docs/design.md, section 13): a key that a service reads by +# LoadCredential= of its own; applied from a source list, rotated without switching; a +# switch across a rename and back, with the rotated key; a switch refused before +# switching; a reboot. +{ pkgs }: + +pkgs.testers.runNixOSTest { + name = "enstate"; + + nodes.machine = + { config, lib, ... }: + let + key = { + type = "application/octet-stream"; + secret = true; + reactions.restart = [ "consumer.service" ]; + }; + # The consumer gets its credential as hrm's services do, by LoadCredential= of + # its own; the ordering after enstate.service follows from that. + # (Given with a priority, so a specialisation's wins over the base's.) + credential = priority: config: name: { + environment.CRED = lib.mkOverride priority name; + serviceConfig.LoadCredential = lib.mkOverride priority "${name}:${config.enstate.channels.secrets.slots.${name}.path}"; + }; + in + { + imports = [ ./module.nix ]; + + enstate.enable = true; + enstate.channels.secrets.slots."tinc.key" = key; + + # Writes down which credential it got, and what is in it. + systemd.services.consumer = lib.mkMerge [ + { + wantedBy = [ "multi-user.target" ]; + script = '' + echo "$CRED $(cat "$CREDENTIALS_DIRECTORY/$CRED")" > /run/consumer-seen + exec sleep infinity + ''; + } + (credential 100 config "tinc.key") + ]; + + specialisation.renamed.configuration = + { config, ... }: + { + enstate.channels.secrets.slots = lib.mkForce { + "retiolum.key" = key // { + renamedFrom = "tinc.key"; + }; + }; + systemd.services.consumer = credential 50 config "retiolum.key"; + }; + + specialisation.retyped.configuration = { + enstate.channels.secrets.slots = lib.mkForce { + "tinc.key" = key // { + type = "text/plain"; + }; + }; + }; + }; + + testScript = '' + def submit(name, key): + machine.succeed( + f"mkdir -p /root/{name}", + f"printf '{key}' > /root/{name}/key", + f"""echo '{{"channel": "secrets", "complete": true, """ + f""""items": {{"tinc.key": {{"source": {{"file": "key"}}}}}}}}' """ + f"> /root/{name}/sources.json", + f"enstate --pack /root/{name}/sources.json --out /root/{name}/submission", + f"enstate --plan /root/{name}/submission", + f"enstate --apply /root/{name}/submission", + ) + + def seen(expected): + machine.wait_until_succeeds(f"grep -qx '{expected}' /run/consumer-seen") + + machine.wait_for_unit("multi-user.target") + + with subtest("the consumer reads a view by LoadCredential=, and is ordered after enstate"): + machine.succeed("systemctl show -p After consumer.service | grep -q enstate.service") + machine.succeed("systemctl show -p Wants consumer.service | grep -q enstate.service") + + with subtest("without state, the required slot is missing, and its consumer fails"): + machine.succeed("enstate --status | grep -q 'missing tinc.key'") + machine.fail("systemctl is-active consumer.service") + + with subtest("applied: the key is the consumer's credential, and it started"): + submit("first", "K") + machine.wait_for_unit("consumer.service") + seen("tinc.key K") + machine.succeed("test $(stat -c %a /run/enstate/views/secrets.tinc.key/tinc.key) = 600") + + with subtest("rotated without switching: the consumer is restarted"): + submit("second", "L") + seen("tinc.key L") + + with subtest("a switch across a rename: the key carries over"): + machine.succeed("/run/current-system/specialisation/renamed/bin/switch-to-configuration test") + seen("retiolum.key L") + machine.fail("test -e /run/enstate/views/secrets.tinc.key") + + with subtest("and back: the old name, the same key"): + machine.succeed("/run/booted-system/bin/switch-to-configuration test") + seen("tinc.key L") + + with subtest("a switch to a type the state can't fill is refused before switching"): + machine.fail("/run/booted-system/specialisation/retyped/bin/switch-to-configuration test") + machine.succeed("enstate --status | grep -q 'filled tinc.key'") + + with subtest("after a reboot, the views are there again"): + machine.shutdown() + machine.start() + machine.wait_for_unit("consumer.service") + seen("tinc.key L") + ''; +} |
