mirror of
https://github.com/juanfont/headscale.git
synced 2026-11-20 04:14:32 +09:00
Store-path policy: check policy -f in system.checks. Pre-switch: check all as the headscale user. Offline, no DB copy. Fixes #2898
179 lines
5.9 KiB
Nix
179 lines
5.9 KiB
Nix
# A policy headscale rejects must fail the build, and a config or policy it
|
|
# rejects must fail a switch before headscale restarts. Every check is offline.
|
|
self:
|
|
{ lib, pkgs, ... }:
|
|
let
|
|
writePolicy = name: policy: pkgs.writeText name (builtins.toJSON policy);
|
|
acceptAll = [
|
|
{
|
|
action = "accept";
|
|
src = [ "*" ];
|
|
dst = [ "*:*" ];
|
|
}
|
|
];
|
|
valid = writePolicy "valid.json" { acls = acceptAll; };
|
|
# group:missing is never defined.
|
|
undefinedGroup = writePolicy "undefined-group.json" {
|
|
acls = [
|
|
{
|
|
action = "accept";
|
|
src = [ "group:missing" ];
|
|
dst = [ "*:*" ];
|
|
}
|
|
];
|
|
};
|
|
# Stand-in users and nodes decide these tests offline.
|
|
testsPass = writePolicy "tests-pass.json" {
|
|
acls = acceptAll;
|
|
tests = [
|
|
{
|
|
src = "test@";
|
|
accept = [ "test@:22" ];
|
|
}
|
|
];
|
|
};
|
|
testsFail = writePolicy "tests-fail.json" {
|
|
acls = acceptAll;
|
|
tests = [
|
|
{
|
|
src = "test@";
|
|
deny = [ "test@:22" ];
|
|
}
|
|
];
|
|
};
|
|
|
|
# The build-time check a system with this store-path policy must pass.
|
|
policyBuild =
|
|
policy:
|
|
lib.findFirst (d: d.name == "headscale-policy-check") (throw "no headscale-policy-check")
|
|
(pkgs.nixos {
|
|
imports = [ self.nixosModules.headscale ];
|
|
services.headscale = {
|
|
enable = true;
|
|
settings.policy = {
|
|
mode = "file";
|
|
path = policy;
|
|
};
|
|
};
|
|
}).config.system.checks;
|
|
buildFailures = {
|
|
undefinedGroup = pkgs.testers.testBuildFailure (policyBuild undefinedGroup);
|
|
testsFail = pkgs.testers.testBuildFailure (policyBuild testsFail);
|
|
};
|
|
|
|
# Outside the store, so only the switch can check it.
|
|
mutablePolicy = "/var/lib/headscale/policy.json";
|
|
badServerUrl = lib.mkForce "ftp://headscale";
|
|
in
|
|
{
|
|
name = "headscale-checks";
|
|
meta.maintainers = with lib.maintainers; [ kradalby ];
|
|
|
|
nodes.headscale = {
|
|
imports = [ self.nixosModules.testkit ];
|
|
services.headscale.settings.policy = {
|
|
mode = "file";
|
|
path = valid;
|
|
};
|
|
specialisation = lib.mapAttrs (_: c: { configuration.services.headscale = c; }) {
|
|
badConfig.settings.server_url = badServerUrl;
|
|
mutable.settings.policy.path = lib.mkForce mutablePolicy;
|
|
policyOnly = {
|
|
settings.policy.path = lib.mkForce mutablePolicy;
|
|
check.config = false;
|
|
};
|
|
unchecked = {
|
|
settings.server_url = badServerUrl;
|
|
check = {
|
|
config = false;
|
|
policy = false;
|
|
};
|
|
};
|
|
newUser = {
|
|
user = "hs2";
|
|
group = "hs2";
|
|
};
|
|
testsPass.settings.policy.path = lib.mkForce testsPass;
|
|
};
|
|
};
|
|
|
|
testScript =
|
|
{ nodes, ... }:
|
|
let
|
|
toplevel = nodes.headscale.system.build.toplevel;
|
|
jq = lib.getExe pkgs.jq;
|
|
in
|
|
''
|
|
from datetime import timedelta
|
|
|
|
def stc(spec, action):
|
|
return f"${toplevel}/specialisation/{spec}/bin/switch-to-configuration {action} 2>&1"
|
|
|
|
def main_pid():
|
|
return headscale.succeed("systemctl show -P MainPID headscale.service").strip()
|
|
|
|
def served_policy():
|
|
return headscale.succeed("headscale policy get | ${jq} -S .")
|
|
|
|
def put_policy(src, mode="644"):
|
|
headscale.succeed(f"install -m {mode} {src} ${mutablePolicy}")
|
|
|
|
def assert_rejected(spec, why):
|
|
out = headscale.fail(stc(spec, "test"))
|
|
assert "Pre-switch check 'headscale' failed" in out, out
|
|
assert why in out, out
|
|
assert "NIXOS_NO_CHECK=1" in out, "no skip hint:\n" + out
|
|
return out
|
|
|
|
with subtest("a bad store-path policy fails the build"):
|
|
for path, why in [
|
|
("${buildFailures.undefinedGroup}", 'group not found: "group:missing"'),
|
|
("${buildFailures.testsFail}", "test(s) failed"),
|
|
]:
|
|
build_log = open(f"{path}/testBuildFailure.log").read()
|
|
assert why in build_log, build_log
|
|
|
|
headscale.wait_for_unit("headscale.service")
|
|
headscale.wait_until_succeeds("headscale health", timeout=timedelta(seconds=60))
|
|
|
|
with subtest("rejected switches leave the running headscale alone"):
|
|
pid = main_pid()
|
|
before = served_policy()
|
|
assert_rejected("badConfig", "server_url is missing a scheme")
|
|
put_policy("${undefinedGroup}")
|
|
assert_rejected("mutable", 'group not found: "group:missing"')
|
|
put_policy("${testsFail}")
|
|
assert_rejected("mutable", "test(s) failed")
|
|
out = assert_rejected("policyOnly", "test(s) failed")
|
|
assert "checking the new configuration" not in out, out
|
|
# Checked as the headscale user, who cannot read this file.
|
|
put_policy("${valid}", "600")
|
|
assert_rejected("mutable", "permission denied")
|
|
assert main_pid() == pid, "headscale was restarted"
|
|
assert served_policy() == before, "old policy no longer served"
|
|
headscale.succeed("headscale health")
|
|
|
|
with subtest("a passing switch check"):
|
|
put_policy("${valid}")
|
|
headscale.succeed(stc("mutable", "check"))
|
|
|
|
with subtest("skips"):
|
|
out = headscale.succeed("${toplevel}/bin/switch-to-configuration check 2>&1")
|
|
assert "unit unchanged" in out, out
|
|
out = headscale.succeed(stc("newUser", "check"))
|
|
assert "does not exist yet" in out, out
|
|
|
|
with subtest("escape hatches"):
|
|
headscale.succeed(stc("unchecked", "check"))
|
|
headscale.succeed("NIXOS_NO_CHECK=1 " + stc("badConfig", "check"))
|
|
|
|
with subtest("a valid switch goes through and applies the policy"):
|
|
pid = main_pid()
|
|
headscale.succeed(stc("testsPass", "test"))
|
|
headscale.wait_until_succeeds("headscale health", timeout=timedelta(seconds=60))
|
|
assert main_pid() != pid, "headscale was not restarted"
|
|
want = headscale.succeed("${jq} -S . ${testsPass}")
|
|
assert served_policy() == want, "new policy not served"
|
|
'';
|
|
}
|