aboutsummaryrefslogtreecommitdiffstats
path: root/nix/test.nix
diff options
context:
space:
mode:
Diffstat (limited to 'nix/test.nix')
-rw-r--r--nix/test.nix119
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")
+ '';
+}