mirror of
https://github.com/juanfont/headscale.git
synced 2026-11-19 11:57:07 +09:00
nix: check the configuration while building the system
check.config also runs `check config -f` on the generated file at build time, native only, under the unit's environment. Updates #1926
This commit is contained in:
1 parent
3a35f74873
commit
fcb1d42b86
4 files changed
+192
-22
No files matched your search
+32
-14
@@ -24,18 +24,31 @@ let
|
||||
# Interpolated as the config and the unit are, so a Nix path is the same store path.
|
||||
policyFile = if policyMode == "file" && policyPath != "" then "${policyPath}" else null;
|
||||
policyFileCheck = cfg.check.policy && policyFile != null;
|
||||
# The build can read a store path, and must be able to run the binary.
|
||||
buildTimePolicy =
|
||||
policyFileCheck
|
||||
&& lib.hasPrefix "${builtins.storeDir}/" policyFile
|
||||
&& pkgs.stdenv.buildPlatform.canExecute pkgs.stdenv.hostPlatform;
|
||||
# Build-time checks run the host's headscale.
|
||||
canExecute = pkgs.stdenv.buildPlatform.canExecute pkgs.stdenv.hostPlatform;
|
||||
# The build can read a store path.
|
||||
buildTimePolicy = policyFileCheck && lib.hasPrefix "${builtins.storeDir}/" policyFile && canExecute;
|
||||
preSwitchPolicy = policyFileCheck && !buildTimePolicy;
|
||||
|
||||
# Interpolated as nixpkgs renders Environment=, so a Nix path is the same store path.
|
||||
unitEnv = lib.mapAttrsToList (n: v: "${n}=${v}") (
|
||||
lib.filterAttrs (_: v: v != null) config.systemd.services.headscale.environment
|
||||
);
|
||||
|
||||
policyCheck = pkgs.runCommand "headscale-policy-check" { } ''
|
||||
${lib.getExe cfg.package} check policy -f ${lib.escapeShellArg policyFile}
|
||||
touch $out
|
||||
'';
|
||||
|
||||
# check config, not check all: the files the config names are not in the sandbox.
|
||||
configCheck = pkgs.runCommand "headscale-config-check" { } ''
|
||||
env -i ${lib.escapeShellArgs unitEnv} ${lib.getExe cfg.package} check config -f ${cfg.configFile} || {
|
||||
echo "headscale: this check sees the unit's environment, not its EnvironmentFile or %-specifiers expanded; services.headscale.check.config = false skips it" >&2
|
||||
exit 1
|
||||
}
|
||||
touch $out
|
||||
'';
|
||||
|
||||
# Its own script, for shellcheck and runtimeInputs.
|
||||
preSwitchCheck =
|
||||
let
|
||||
@@ -55,9 +68,6 @@ let
|
||||
)
|
||||
);
|
||||
declared = map (g: g.name) (lib.attrValues config.users.groups);
|
||||
unitEnv = lib.mapAttrsToList (n: v: "${n}=${toString v}") (
|
||||
lib.filterAttrs (_: v: v != null) config.systemd.services.headscale.environment
|
||||
);
|
||||
in
|
||||
pkgs.writeShellApplication {
|
||||
name = "headscale-pre-switch-check";
|
||||
@@ -163,17 +173,23 @@ in
|
||||
type = lib.types.bool;
|
||||
default = true;
|
||||
description = ''
|
||||
While building the system (native builds only), run
|
||||
`headscale check config -f` on
|
||||
{option}`services.headscale.configFile`, so a configuration headscale
|
||||
rejects fails `nixos-rebuild build`.
|
||||
|
||||
Before a switch restarts headscale, run the new package's
|
||||
`headscale check all` on the new configuration and abort the switch
|
||||
if it fails. It checks the configuration and every file it names
|
||||
(policy, DERP maps, extra records, TLS pair, existing noise and DERP
|
||||
keys) as the `headscale` user. Keys are read, never created.
|
||||
|
||||
It runs outside the unit, and only when headscale's unit changes.
|
||||
It never reads a `file:` secret, so credentials referenced with
|
||||
`file:` pass. Turning off only {option}`services.headscale.check.policy`
|
||||
does not skip the policy here. Skip all checks once with
|
||||
`NIXOS_NO_CHECK=1`.
|
||||
Both run outside the unit, with its `environment` but not its
|
||||
`EnvironmentFile`; the switch check runs only when headscale's unit
|
||||
changes. Neither reads a `file:` secret, so credentials referenced
|
||||
with `file:` pass. Turning off only
|
||||
{option}`services.headscale.check.policy` does not skip the policy
|
||||
before a switch. Skip the switch check once with `NIXOS_NO_CHECK=1`.
|
||||
'';
|
||||
};
|
||||
|
||||
@@ -224,7 +240,9 @@ in
|
||||
};
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
system.checks = lib.optional buildTimePolicy policyCheck;
|
||||
system.checks =
|
||||
lib.optional buildTimePolicy policyCheck
|
||||
++ lib.optional (cfg.check.config && canExecute) configCheck;
|
||||
|
||||
system.preSwitchChecks.headscale = lib.mkIf (
|
||||
cfg.check.config || preSwitchPolicy
|
||||
|
||||
+10
-5
@@ -67,6 +67,8 @@ let
|
||||
handGroupPolicy = "/var/lib/hshand/policy.json";
|
||||
# Readable only through the group hsread.
|
||||
groupPolicy = "/var/lib/hsread/policy.json";
|
||||
# Written broken by the test: only the switch reads named files.
|
||||
badRecords = "/var/lib/headscale/extra-records.json";
|
||||
badServerUrl = lib.mkForce "ftp://headscale";
|
||||
in
|
||||
{
|
||||
@@ -79,7 +81,8 @@ in
|
||||
# Specialisations that set more than services.headscale.
|
||||
{
|
||||
specialisation = lib.mapAttrs (_: c: { configuration = c; }) {
|
||||
unitEnv.systemd.services.headscale.environment.HEADSCALE_SERVER_URL = "ftp://headscale";
|
||||
# A bad value here would fail the build check, which sees it too.
|
||||
unitEnv.systemd.services.headscale.environment.HEADSCALE_DNS_EXTRA_RECORDS_PATH = badRecords;
|
||||
# hsnew is created by this switch, so the check leaves it out.
|
||||
supplementaryGroup = {
|
||||
services.headscale.settings.policy.path = lib.mkForce groupPolicy;
|
||||
@@ -110,12 +113,13 @@ in
|
||||
};
|
||||
users.groups.hsread = { };
|
||||
specialisation = lib.mapAttrs (_: c: { configuration.services.headscale = c; }) {
|
||||
badConfig.settings.server_url = badServerUrl;
|
||||
badRecords.settings.dns.extra_records_path = badRecords;
|
||||
mutable.settings.policy.path = lib.mkForce mutablePolicy;
|
||||
policyOnly = {
|
||||
settings.policy.path = lib.mkForce mutablePolicy;
|
||||
check.config = false;
|
||||
};
|
||||
# Builds only because check.config = false also drops the build check.
|
||||
unchecked = {
|
||||
settings.server_url = badServerUrl;
|
||||
check = {
|
||||
@@ -184,8 +188,9 @@ in
|
||||
with subtest("rejected switches leave the running headscale alone"):
|
||||
pid = main_pid()
|
||||
before = served_policy()
|
||||
assert_rejected("badConfig", "server_url is missing a scheme")
|
||||
assert_rejected("unitEnv", "server_url is missing a scheme")
|
||||
headscale.succeed("echo '{' > ${badRecords}")
|
||||
assert_rejected("badRecords", "dns.extra_records_path")
|
||||
assert_rejected("unitEnv", "dns.extra_records_path")
|
||||
put_policy("${undefinedGroup}")
|
||||
assert_rejected("mutable", 'group not found: "group:missing"')
|
||||
put_policy("${testsFail}")
|
||||
@@ -225,7 +230,7 @@ in
|
||||
|
||||
with subtest("escape hatches"):
|
||||
headscale.succeed(stc("unchecked", "check"))
|
||||
headscale.succeed("NIXOS_NO_CHECK=1 " + stc("badConfig", "check"))
|
||||
headscale.succeed("NIXOS_NO_CHECK=1 " + stc("badRecords", "check"))
|
||||
|
||||
with subtest("a valid switch goes through and applies the policy"):
|
||||
pid = main_pid()
|
||||
|
||||
@@ -39,9 +39,13 @@ in
|
||||
server = "127.0.0.1:53";
|
||||
key_name = "headscale.";
|
||||
key_alg = "hmac-sha256.";
|
||||
key = "c2VjcmV0";
|
||||
key = "file:\${CREDENTIALS_DIRECTORY}/tsig";
|
||||
};
|
||||
};
|
||||
# Read by serve, never by the checks.
|
||||
systemd.services.headscale.serviceConfig.LoadCredential = [
|
||||
"tsig:${pkgs.writeText "tsig" "c2VjcmV0"}"
|
||||
];
|
||||
};
|
||||
# All of peer1's noise goes over the kit's TLS listener, not plain :80.
|
||||
peer1 = {
|
||||
|
||||
+145
-2
@@ -26,7 +26,7 @@ let
|
||||
policy = pkgs.writeText "policy.json" "{}";
|
||||
derpMap = pkgs.writeText "derp.yaml" "regions: {}";
|
||||
|
||||
ok = settings {
|
||||
okSettings = {
|
||||
policy = {
|
||||
mode = "file";
|
||||
path = policy;
|
||||
@@ -44,7 +44,88 @@ let
|
||||
};
|
||||
};
|
||||
};
|
||||
http = settings { listen_addr = ":http"; };
|
||||
ok = settings okSettings;
|
||||
http = system {
|
||||
services.headscale = {
|
||||
settings.listen_addr = ":http";
|
||||
check.config = false;
|
||||
};
|
||||
};
|
||||
# A cross build cannot run the host's headscale.
|
||||
crossWith =
|
||||
checkConfig:
|
||||
self.inputs.nixpkgs.lib.nixosSystem {
|
||||
modules = [
|
||||
self.nixosModules.default
|
||||
{
|
||||
nixpkgs.buildPlatform = "x86_64-linux";
|
||||
nixpkgs.hostPlatform = "aarch64-linux";
|
||||
services.headscale = {
|
||||
enable = true;
|
||||
settings = lib.recursiveUpdate required okSettings;
|
||||
check.config = checkConfig;
|
||||
};
|
||||
}
|
||||
];
|
||||
};
|
||||
cross = crossWith true;
|
||||
# With check.config off, only the store policy adds a switch check.
|
||||
crossPolicy = crossWith false;
|
||||
# With check.config off, check.policy alone must find the policy headscale
|
||||
# loads: a non-empty HEADSCALE_POLICY_* of the unit over the settings.
|
||||
policyEnv =
|
||||
settings: env:
|
||||
system {
|
||||
services.headscale = {
|
||||
inherit settings;
|
||||
check.config = false;
|
||||
};
|
||||
systemd.services.headscale.environment = env;
|
||||
};
|
||||
storePolicy.policy = {
|
||||
mode = "file";
|
||||
path = policy;
|
||||
};
|
||||
mutablePolicy = "/var/lib/headscale/policy.json";
|
||||
envOnly = policyEnv { } { HEADSCALE_POLICY_PATH = policy; };
|
||||
envMutable = policyEnv storePolicy { HEADSCALE_POLICY_PATH = mutablePolicy; };
|
||||
envDatabase = policyEnv storePolicy { HEADSCALE_POLICY_MODE = "database"; };
|
||||
envFile = policyEnv {
|
||||
policy = storePolicy.policy // {
|
||||
mode = "database";
|
||||
};
|
||||
} { HEADSCALE_POLICY_MODE = "file"; };
|
||||
# Empty means unset.
|
||||
envEmpty = policyEnv storePolicy {
|
||||
HEADSCALE_POLICY_MODE = null;
|
||||
HEADSCALE_POLICY_PATH = "";
|
||||
};
|
||||
# Activation then drops every group NixOS does not declare. systemd also
|
||||
# takes a GID and a space-separated string.
|
||||
immutableUsers = system {
|
||||
users.mutableUsers = false;
|
||||
systemd.services.headscale.serviceConfig.SupplementaryGroups = [
|
||||
1234
|
||||
"a b"
|
||||
];
|
||||
};
|
||||
# The build check sees the unit's environment, as the switch check does.
|
||||
badEnv = system {
|
||||
systemd.services.headscale.environment = {
|
||||
HEADSCALE_SERVER_URL = "ftp://headscale";
|
||||
# Any path literal and derivation: the check must see the unit's values.
|
||||
HEADSCALE_DNS_EXTRA_RECORDS_PATH = ./module.nix;
|
||||
HEADSCALE_POLICY_PATH = policy;
|
||||
};
|
||||
};
|
||||
# badEnv's HEADSCALE_* Environment= values, as nixpkgs renders the unit.
|
||||
badEnvUnit = map (l: builtins.fromJSON (lib.removePrefix "Environment=" l)) (
|
||||
lib.filter (lib.hasPrefix "Environment=\"HEADSCALE_") (
|
||||
lib.splitString "\n" (
|
||||
builtins.unsafeDiscardStringContext badEnv.config.systemd.units."headscale.service".text
|
||||
)
|
||||
)
|
||||
);
|
||||
# A user's mkDefault and a plain assignment both replace the module's values.
|
||||
overrides = settings {
|
||||
prefixes.v4 = lib.mkDefault "100.96.0.0/11";
|
||||
@@ -112,6 +193,17 @@ let
|
||||
);
|
||||
|
||||
configFile = s: s.config.services.headscale.configFile;
|
||||
checkNames = s: map (d: d.name) s.config.system.checks;
|
||||
checkOf = name: s: lib.findFirst (d: d.name == name) (throw "no ${name}") s.config.system.checks;
|
||||
configCheckOf = checkOf "headscale-config-check";
|
||||
# The build checks the store policy `policy`.
|
||||
buildsPolicy =
|
||||
s:
|
||||
lib.elem "headscale-policy-check" (checkNames s)
|
||||
&& lib.hasInfix (builtins.unsafeDiscardStringContext "${policy}") (checkOf "headscale-policy-check" s)
|
||||
.buildCommand;
|
||||
switches = s: s.config.system.preSwitchChecks ? headscale;
|
||||
switchScript = s: lib.head (lib.splitString " " s.config.system.preSwitchChecks.headscale);
|
||||
binds =
|
||||
s:
|
||||
lib.elem "CAP_NET_BIND_SERVICE" s.config.systemd.services.headscale.serviceConfig.CapabilityBoundingSet;
|
||||
@@ -147,8 +239,39 @@ assert lib.assertMsg (binds bad) "bad: [::]:443 does not grant CAP_NET_BIND_SERV
|
||||
assert lib.assertMsg (
|
||||
ok.config.systemd.services.headscale.serviceConfig.CapabilityBoundingSet == [ "" ]
|
||||
) "ok: the unit keeps a capability";
|
||||
assert lib.assertMsg (
|
||||
!lib.elem "headscale-config-check" (checkNames http)
|
||||
) "http: check.config = false still checks while building";
|
||||
assert lib.assertMsg (lib.all (n: !lib.elem n (checkNames cross)) [
|
||||
"headscale-config-check"
|
||||
"headscale-policy-check"
|
||||
]) "cross: a cross build runs a headscale check";
|
||||
assert lib.assertMsg (
|
||||
crossPolicy.config.system.preSwitchChecks ? headscale
|
||||
) "crossPolicy: the store policy is not checked before a switch";
|
||||
assert lib.assertMsg (
|
||||
buildsPolicy envOnly && !switches envOnly
|
||||
) "envOnly: the unit's HEADSCALE_POLICY_PATH is not checked while building";
|
||||
assert lib.assertMsg (
|
||||
!lib.elem "headscale-policy-check" (checkNames envMutable) && switches envMutable
|
||||
) "envMutable: the settings' policy is checked instead of the unit's";
|
||||
assert lib.assertMsg (
|
||||
!lib.elem "headscale-policy-check" (checkNames envDatabase) && !switches envDatabase
|
||||
) "envDatabase: HEADSCALE_POLICY_MODE=database still checks a policy";
|
||||
assert lib.assertMsg (buildsPolicy envFile) "envFile: HEADSCALE_POLICY_MODE=file checks no policy";
|
||||
assert lib.assertMsg (buildsPolicy envEmpty) "envEmpty: empty variables replace the settings";
|
||||
assert lib.assertMsg (
|
||||
lib.length badEnvUnit == 3
|
||||
) "badEnv: unit does not render 3 HEADSCALE_* values";
|
||||
assert lib.assertMsg (lib.all (
|
||||
e: lib.hasInfix (lib.escapeShellArg e) (configCheckOf badEnv).buildCommand
|
||||
) badEnvUnit) "badEnv: the build check's environment differs from the unit's";
|
||||
pkgs.runCommand "headscale-nixos-module"
|
||||
{
|
||||
# Built as an input: a config with a file: secret passes.
|
||||
okBuild = configCheckOf ok;
|
||||
badBuild = pkgs.testers.testBuildFailure (configCheckOf bad);
|
||||
badEnvBuild = pkgs.testers.testBuildFailure (configCheckOf badEnv);
|
||||
nativeBuildInputs = [
|
||||
ok.config.services.headscale.package
|
||||
pkgs.yq-go
|
||||
@@ -178,6 +301,7 @@ pkgs.runCommand "headscale-nixos-module"
|
||||
want $ok '.listen_addr == "127.0.0.1:8080" and .disable_check_updates == true'
|
||||
want $ok '.database.type == "sqlite"'
|
||||
want $ok '.derp.urls[0] == "https://controlplane.tailscale.com/derpmap/default"'
|
||||
want $ok '.dns.challenge.provider.key == "file:''${CREDENTIALS_DIRECTORY}/tsig"'
|
||||
headscale check config -f $ok
|
||||
|
||||
bad=${configFile bad}
|
||||
@@ -194,6 +318,25 @@ pkgs.runCommand "headscale-nixos-module"
|
||||
want $badRecord '.dns.extra_records[0].typo == "A"'
|
||||
rejects $badRecord 'dns.extra_records has invalid keys: typo'
|
||||
|
||||
built() {
|
||||
grep -qF -- "$2" "$1/testBuildFailure.log" || { echo "$1: no '$2' in:"; cat "$1/testBuildFailure.log"; exit 1; }
|
||||
}
|
||||
built $badBuild 'unknown configuration key dns.magic_dsn'
|
||||
built $badBuild 'oidc.client_secret_path was removed'
|
||||
built $badBuild 'services.headscale.check.config = false skips it'
|
||||
built $badEnvBuild 'missing a scheme'
|
||||
|
||||
switch=${switchScript envMutable}
|
||||
grep -qF -- ${lib.escapeShellArg "check policy -f ${lib.escapeShellArg mutablePolicy}"} $switch ||
|
||||
{ echo "envMutable: switch check does not check ${mutablePolicy}:"; cat $switch; exit 1; }
|
||||
grep -qF 'id -Gn' ${switchScript ok} || { echo "ok: switch check drops current groups"; exit 1; }
|
||||
immutable=${switchScript immutableUsers}
|
||||
if grep -qF 'id -Gn' $immutable; then
|
||||
echo "immutableUsers: switch check keeps current groups"; exit 1
|
||||
fi
|
||||
grep -qF 'want=(headscale 1234 a b)' $immutable ||
|
||||
{ echo "immutableUsers: SupplementaryGroups not split:"; cat $immutable; exit 1; }
|
||||
|
||||
export HOME=$TMPDIR NIX_STATE_DIR=$TMPDIR/state NIX_CONF_DIR=$TMPDIR/etc NIX_LOG_DIR=$TMPDIR/log
|
||||
evaluate() {
|
||||
nix-instantiate --store dummy:// --readonly-mode --eval --strict ${evalSystem} --argstr case "$1" 2>&1
|
||||
|
||||
Reference in new issue
Block a user