# 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"; }; # Adds a required slot: a submission for it is staged before switching. specialisation.added.configuration = { enstate.channels.secrets.slots."extra.key" = key; }; # Adds another; its submission waits in the incoming directory. specialisation.more.configuration = { enstate.channels.secrets.slots."more.key" = key; }; specialisation.retyped.configuration = { enstate.channels.secrets.slots = lib.mkForce { "tinc.key" = key // { type = "text/plain"; }; }; }; }; testScript = '' def submit(name, key, command="--apply"): 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 {command} /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("a generation that adds a required slot: staged with its own enstate, then switched to"): new = "/run/booted-system/specialisation/added" machine.fail(f"{new}/bin/switch-to-configuration test") machine.succeed( "mkdir -p /root/added", "printf X > /root/added/key", """echo '{"channel": "secrets", "items": {"extra.key": {"source": {"file": "key"}}}}' """ "> /root/added/sources.json", f"{new}/sw/bin/enstate --pack /root/added/sources.json --out /root/added/submission", f"{new}/sw/bin/enstate --plan /root/added/submission --schema {new}/etc/enstate/schema.json --config {new}/etc/enstate/config.json", f"{new}/sw/bin/enstate --stage /root/added/submission --schema {new}/etc/enstate/schema.json --config {new}/etc/enstate/config.json", ) machine.fail("test -e /run/enstate/views/secrets.extra.key") seen("tinc.key L") machine.succeed(f"{new}/bin/switch-to-configuration test") machine.wait_until_succeeds("grep -qx X /run/enstate/views/secrets.extra.key/extra.key") seen("tinc.key L") machine.succeed("/run/booted-system/bin/switch-to-configuration test") machine.succeed("enstate --status | grep -q 'kept extra.key'") with subtest("staged under an unchanged schema: the switch brings it"): submit("third", "M", "--stage") seen("tinc.key L") machine.succeed("test -e /var/lib/enstate/migrate-due") out = machine.succeed("/run/current-system/bin/switch-to-configuration test 2>&1") assert "deprecated" not in out, out seen("tinc.key M") machine.fail("test -e /var/lib/enstate/migrate-due") with subtest("incoming: a submission waits for the switch that needs it"): new = "/run/booted-system/specialisation/more" machine.fail(f"{new}/bin/switch-to-configuration test") machine.succeed( "mkdir -p /root/more", "printf Y > /root/more/key", """echo '{"channel": "secrets", "items": {"more.key": {"source": {"file": "key"}}}}' """ "> /root/more/sources.json", "enstate --pack /root/more/sources.json --out /var/lib/enstate/incoming/1-more", ) machine.succeed(f"{new}/bin/switch-to-configuration test") machine.wait_until_succeeds("grep -qx Y /run/enstate/views/secrets.more.key/more.key") machine.succeed("test -z \"$(ls -A /var/lib/enstate/incoming)\"") seen("tinc.key M") machine.succeed("/run/booted-system/bin/switch-to-configuration test") with subtest("incoming: one refused refuses the switch, and stays"): machine.succeed( "mkdir -p /var/lib/enstate/incoming/2-bad/channels/secrets", "printf x > /var/lib/enstate/incoming/2-bad/channels/secrets/nope", """echo '{"submission": 1, "channel": "secrets", "items": """ """[{"address": "secrets/nope", "op": "set"}]}' """ "> /var/lib/enstate/incoming/2-bad/manifest.json", ) machine.fail("/run/booted-system/specialisation/more/bin/switch-to-configuration test") machine.succeed("test -d /var/lib/enstate/incoming/2-bad") machine.succeed("rm -r /var/lib/enstate/incoming/2-bad") with subtest("after a reboot, the views are there again"): machine.shutdown() machine.start() machine.wait_for_unit("consumer.service") seen("tinc.key M") ''; }