router.update.repository names a public repository whose tags vX.Y.Z are releases. router-update check (daily, and from the Updates page) looks for a newer one and the Overview says so; router-update --git builds the router from a tag like a bundle update: the settings are migrated, the new version must be confirmed, the nixpkgs pin stays. The flake of such a router comes from the release's config-flake.nix. The global flake registry is off. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
455 lines
20 KiB
Nix
455 lines
20 KiB
Nix
# Updating an installed router to a newer nixos-router.
|
|
#
|
|
# "Versions" are copies of this repository evaluated as flakes of their own
|
|
# (a different source path, VERSION, commit time, and for version 2 a settings
|
|
# migration), so the whole path runs for real: a builder signs releases as
|
|
# binary caches (router-release), and the router takes them, with these rules:
|
|
#
|
|
# - signed with another key, or not signed at all: refused,
|
|
# - not newer than the installed version: refused,
|
|
# - upload in the web UI: imported, the configuration flake points at the new
|
|
# source, settings.json is migrated, the router is rebuilt offline and the
|
|
# update waits for confirmation; unconfirmed, everything goes back,
|
|
# - the same update pulled from a server and confirmed in the web UI: the
|
|
# router runs the new version and rebuilds to exactly itself.
|
|
{ self, pkgs }:
|
|
let
|
|
inherit (pkgs) lib;
|
|
common = import ./common.nix;
|
|
keys = ./update-keys;
|
|
|
|
# A version of the repository: its source (with the test key as update key)
|
|
# evaluated as a flake that has been told its commit time.
|
|
mkVersion =
|
|
{
|
|
name,
|
|
version,
|
|
serial,
|
|
extra ? "",
|
|
}:
|
|
let
|
|
src = pkgs.runCommand "nixos-router-${name}" { } ''
|
|
cp -r ${self.outPath} $out
|
|
chmod -R u+w $out
|
|
cp ${keys}/good.pub $out/keys/update.pub
|
|
printf '%s\n' ${version} > $out/VERSION
|
|
${extra}
|
|
'';
|
|
flake = (import "${src}/flake.nix").outputs {
|
|
self = flake // {
|
|
outPath = src;
|
|
lastModified = serial;
|
|
rev = name;
|
|
};
|
|
inherit (self.inputs) nixpkgs;
|
|
};
|
|
in
|
|
flake // { inherit src; };
|
|
|
|
v1 = mkVersion {
|
|
name = "v1";
|
|
version = "1.0.0";
|
|
serial = 1000;
|
|
# Settings version 0: the real migrations are not part of this test.
|
|
extra = "rm $out/migrations/*.jq";
|
|
};
|
|
v2 = mkVersion {
|
|
name = "v2";
|
|
version = "2.0.0";
|
|
serial = 2000;
|
|
# Settings version 1: the time zone becomes explicit.
|
|
extra = ''
|
|
rm $out/migrations/*.jq
|
|
echo '.system.timeZone = "Europe/Berlin"' > $out/migrations/1.jq
|
|
'';
|
|
};
|
|
|
|
# Version 3 is only a source tree: the builder serves it from a git repository.
|
|
v3 = mkVersion {
|
|
name = "v3";
|
|
version = "3.0.0";
|
|
serial = 3000;
|
|
extra = ''
|
|
rm $out/migrations/*.jq
|
|
echo '.system.timeZone = "Europe/Berlin"' > $out/migrations/1.jq
|
|
echo '.system.timeZone = "Europe/Paris"' > $out/migrations/2.jq
|
|
'';
|
|
};
|
|
|
|
release =
|
|
v:
|
|
v.lib.mkRelease {
|
|
inherit pkgs;
|
|
# The router has all it needs to rebuild; keep the bundles small.
|
|
includeReference = false;
|
|
};
|
|
release1 = release v1;
|
|
release2 = release v2;
|
|
tool =
|
|
v: r:
|
|
"${
|
|
v.lib.mkReleaseTool {
|
|
inherit pkgs;
|
|
release = r;
|
|
}
|
|
}/bin/router-release";
|
|
|
|
settings = pkgs.writeText "settings.json" (
|
|
builtins.toJSON {
|
|
system = {
|
|
hostName = "router";
|
|
stateVersion = "26.11";
|
|
};
|
|
router = {
|
|
update.channel = "26.05";
|
|
wan.interface = "eth1";
|
|
lans.main = {
|
|
interface = "eth2";
|
|
internet = true;
|
|
ipv4.address = "192.168.10.1";
|
|
dhcp.pool = {
|
|
start = "192.168.10.100";
|
|
end = "192.168.10.199";
|
|
};
|
|
};
|
|
};
|
|
}
|
|
);
|
|
|
|
# Only so the test driver can talk to the installed system; and a short
|
|
# rollback timeout, so that an unconfirmed update comes back soon. The
|
|
# channel is a server on the builder, and a newer nixpkgs a copy of the
|
|
# installed one (see apply.nix): what is tested is that an update keeps it.
|
|
local = pkgs.writeText "local.nix" ''
|
|
{ modulesPath, pkgs, ... }:
|
|
{
|
|
imports = [ (modulesPath + "/testing/test-instrumentation.nix") ];
|
|
router.rollback.timeout = "90s";
|
|
routerSettings.upgrade = {
|
|
channelsUrl = "http://192.168.10.60:8080";
|
|
nixpkgsRef = "path:/var/lib/newer-nixpkgs?lastModified=${toString self.inputs.nixpkgs.lastModified}&rev=%rev%";
|
|
};
|
|
environment.systemPackages = [ pkgs.jq ];
|
|
}
|
|
'';
|
|
|
|
configDir = v1.lib.mkConfigDir { inherit pkgs settings local; };
|
|
toplevel =
|
|
(v1.lib.mkRouterSystem {
|
|
settings = "${configDir}/settings.json";
|
|
modules = [ "${configDir}/local.nix" ];
|
|
}).config.system.build.toplevel;
|
|
|
|
sharedDisk = {
|
|
virtualisation.diskImage = "./target.qcow2";
|
|
virtualisation.diskSize = 8192;
|
|
};
|
|
builderIp = "192.168.10.60";
|
|
in
|
|
pkgs.testers.runNixOSTest {
|
|
name = "router-update";
|
|
|
|
nodes = {
|
|
installer =
|
|
{ lib, ... }:
|
|
{
|
|
imports = [
|
|
sharedDisk
|
|
self.nixosModules.installer
|
|
common.tools
|
|
];
|
|
router.installer.system = toplevel;
|
|
virtualisation.memorySize = 2048;
|
|
virtualisation.fileSystems."/".autoFormat = true;
|
|
virtualisation.emptyDiskImages = [ 1024 ];
|
|
virtualisation.rootDevice = "/dev/vdb";
|
|
nix.settings.substituters = lib.mkForce [ ];
|
|
};
|
|
|
|
target = {
|
|
imports = [ sharedDisk ];
|
|
virtualisation.memorySize = 4096;
|
|
virtualisation.vlans = [
|
|
1
|
|
2
|
|
];
|
|
virtualisation.useBootLoader = true;
|
|
virtualisation.useEFIBoot = true;
|
|
virtualisation.useDefaultFilesystems = false;
|
|
virtualisation.efi.keepVariables = false;
|
|
virtualisation.fileSystems."/" = {
|
|
device = "/dev/disk/by-label/this-is-not-real-and-will-never-be-used";
|
|
fsType = "ext4";
|
|
};
|
|
};
|
|
|
|
# Where releases are made and signed, served, and uploaded from.
|
|
builder =
|
|
{ lib, pkgs, ... }:
|
|
{
|
|
imports = [ common.tools ];
|
|
virtualisation.vlans = [ 2 ];
|
|
virtualisation.memorySize = 2048;
|
|
virtualisation.diskSize = 6144;
|
|
networking.interfaces.eth1.ipv4.addresses = lib.mkForce [
|
|
{
|
|
address = builderIp;
|
|
prefixLength = 24;
|
|
}
|
|
];
|
|
networking.firewall.enable = false;
|
|
environment.systemPackages = [
|
|
pkgs.python3
|
|
pkgs.git
|
|
];
|
|
virtualisation.additionalPaths = [
|
|
release1
|
|
release2
|
|
v3.src
|
|
];
|
|
environment.etc."channels.py".source = common.channelServer pkgs;
|
|
environment.etc."git-server.py".source = common.gitServer pkgs;
|
|
environment.etc."keys/good.sec".source = "${keys}/good.sec";
|
|
environment.etc."keys/evil.sec".source = "${keys}/evil.sec";
|
|
};
|
|
};
|
|
|
|
testScript = ''
|
|
import json
|
|
|
|
installer.start()
|
|
installer.wait_for_unit("multi-user.target")
|
|
installer.succeed("mkdir -p /root/secrets && cp -r ${configDir} /root/secrets/config")
|
|
installer.succeed("router-install --yes --secrets /root/secrets /dev/vda >&2")
|
|
installer.shutdown()
|
|
|
|
target.state_dir = installer.state_dir
|
|
target.start()
|
|
builder.start()
|
|
target.wait_for_unit("multi-user.target")
|
|
builder.wait_for_unit("multi-user.target")
|
|
target.succeed("echo hunter2hunter2 | router-webui passwd /var/lib/router/webui-password")
|
|
target.wait_for_unit("router-webui.service")
|
|
|
|
ui = "https://192.168.10.1"
|
|
|
|
def release_json(): return target.succeed("cat /etc/router-release")
|
|
def version(): return target.succeed("jq -r .version /etc/router-release").strip()
|
|
def current(): return target.succeed("readlink -f /run/current-system").strip()
|
|
def flake_source(): return target.succeed("grep -o 'path:[^\"]*' /var/lib/router/config/flake.nix").strip()
|
|
def serve(directory, port):
|
|
builder.succeed(f"systemd-run --unit=serve-{port} -p WorkingDirectory={directory} ${pkgs.python3}/bin/python3 -m http.server {port} --bind ${builderIp}")
|
|
builder.wait_for_open_port(port, "${builderIp}")
|
|
def sign(tool, key, out):
|
|
builder.succeed(f"{tool} --key /etc/keys/{key}.sec --out {out} --compression none >&2")
|
|
def update_from(port, flags=""):
|
|
return target.execute(f"router-update {flags} --url http://${builderIp}:{port} 2>&1")
|
|
def login():
|
|
builder.succeed(f"curl -sk -c /tmp/jar -o /dev/null -d password=hunter2hunter2 {ui}/login")
|
|
def csrf(path):
|
|
return builder.succeed(f"curl -sk -b /tmp/jar {ui}{path} | grep -o 'name=\"csrf\" value=\"[^\"]*' | head -1 | cut -d'\"' -f4").strip()
|
|
def csrf_from_query(path):
|
|
return builder.succeed(f"curl -sk -b /tmp/jar {ui}{path} | grep -o 'upload?csrf=[^\"]*' | head -1 | cut -d= -f2").strip()
|
|
def update_finished():
|
|
# Stops at a failed update rather than waiting for one that will not come.
|
|
target.wait_until_succeeds(
|
|
"state=$(systemctl show -p ActiveState --value router-update.service);"
|
|
" test $state = inactive -o $state = failed", timeout=300)
|
|
result = target.succeed("systemctl show -p Result --value router-update.service").strip()
|
|
assert result == "success", (result, target.succeed("journalctl -u router-update.service --no-pager | tail -30"))
|
|
|
|
v1_flake = flake_source()
|
|
|
|
with subtest("the installed router is version 1 and trusts the update key"):
|
|
assert version() == "1.0.0", release_json()
|
|
assert v1_flake == "path:${v1.src}", v1_flake
|
|
target.succeed("test -s /etc/router-update-keys")
|
|
|
|
with subtest("the packages are upgraded before any update"):
|
|
target.succeed("cp -r ${self.inputs.nixpkgs} /var/lib/newer-nixpkgs")
|
|
builder.succeed("mkdir -p /srv/channels/files/nixos-26.05 && printf %s ${self.inputs.nixpkgs.rev} > /srv/channels/files/nixos-26.05/git-revision")
|
|
builder.succeed("systemd-run --unit=channels /etc/channels.py")
|
|
builder.wait_for_open_port(8080)
|
|
# The packages are the same, so there is only something to activate when
|
|
# the settings change too (an upgrade applies what is saved).
|
|
target.succeed("sed -i 's/192.168.10.199/192.168.10.198/' /var/lib/router/config/settings.json")
|
|
target.succeed("router-upgrade preview --force >&2")
|
|
target.succeed("router-upgrade apply 5min >&2")
|
|
target.succeed("router-confirm >&2")
|
|
target.fail("systemctl is-active router-rollback.timer")
|
|
assert json.loads(target.succeed("cat /var/lib/router/config/nixpkgs-pin.json"))["rev"] == "${self.inputs.nixpkgs.rev}"
|
|
assert "newer-nixpkgs" in target.succeed("cat /var/lib/router/config/flake.lock")
|
|
target.succeed("test ! -e /var/lib/router/config.pre-update")
|
|
|
|
pin = target.succeed("cat /var/lib/router/config/nixpkgs-pin.json")
|
|
|
|
def pinned():
|
|
assert target.succeed("cat /var/lib/router/config/nixpkgs-pin.json") == pin
|
|
# The lock names the upgraded nixpkgs, not the one the release brings.
|
|
lock = json.loads(target.succeed("cat /var/lib/router/config/flake.lock"))
|
|
node = lock["nodes"][lock["nodes"]["root"]["inputs"]["nixos-router"]]["inputs"]["nixpkgs"]
|
|
assert lock["nodes"][node]["locked"]["path"] == "/var/lib/newer-nixpkgs", lock["nodes"][node]
|
|
|
|
pinned()
|
|
|
|
sign("${tool v2 release2}", "evil", "/root/evil")
|
|
serve("/root/evil", 8001)
|
|
sign("${tool v2 release2}", "good", "/root/good")
|
|
serve("/root/good", 8004)
|
|
# The good release without its signatures.
|
|
builder.succeed("cp -r /root/good /root/unsigned && sed -i '/^Sig:/d' /root/unsigned/cache/*.narinfo")
|
|
serve("/root/unsigned", 8002)
|
|
sign("${tool v1 release1}", "good", "/root/old")
|
|
serve("/root/old", 8003)
|
|
|
|
def unchanged():
|
|
assert version() == "1.0.0"
|
|
assert flake_source() == v1_flake
|
|
target.succeed("test ! -e /var/lib/router/config.pre-update")
|
|
target.fail("grep -q '\"version\"' /var/lib/router/config/settings.json")
|
|
|
|
with subtest("an update signed with another key is refused"):
|
|
rc, out = update_from(8001)
|
|
assert rc != 0 and "not signed with this router's update key" in out, out
|
|
unchanged()
|
|
|
|
with subtest("an update without signatures is refused"):
|
|
rc, out = update_from(8002)
|
|
assert rc != 0 and "not signed with this router's update key" in out, out
|
|
unchanged()
|
|
|
|
with subtest("a release that is not newer is refused"):
|
|
rc, out = update_from(8003)
|
|
assert rc != 0 and "not newer" in out, out
|
|
unchanged()
|
|
|
|
with subtest("a bundle uploaded in the web UI is verified, installed and migrated"):
|
|
login()
|
|
token = csrf_from_query("/update")
|
|
assert token
|
|
assert "Install a bundle" in builder.succeed(f"curl -sk -b /tmp/jar {ui}/update")
|
|
# (-F puts the file after the other fields, as the page's form does.)
|
|
assert builder.succeed(
|
|
f"curl -sk -b /tmp/jar -o /dev/null -w '%{{http_code}}' -F bundle=@/root/good/router-update-2.0.0.bundle '{ui}/update/upload?csrf=wrong'").strip() == "403"
|
|
builder.succeed(f"curl -sk -b /tmp/jar -o /dev/null -F bundle=@/root/good/router-update-2.0.0.bundle '{ui}/update/upload?csrf={token}'")
|
|
update_finished()
|
|
assert version() == "2.0.0", release_json()
|
|
assert flake_source() == "path:${v2.src}", flake_source()
|
|
settings = target.succeed("cat /var/lib/router/config/settings.json")
|
|
assert '"version": 1' in settings and "Europe/Berlin" in settings, settings
|
|
assert target.succeed("readlink /etc/localtime").strip().endswith("Europe/Berlin")
|
|
target.succeed("systemctl is-active router-rollback.timer")
|
|
target.succeed("test -d /var/lib/router/config.pre-update")
|
|
|
|
with subtest("an update that is not confirmed is rolled back, configuration included"):
|
|
target.wait_until_succeeds("test $(jq -r .version /etc/router-release) = 1.0.0", timeout=300)
|
|
unchanged()
|
|
assert "Berlin" not in target.succeed("cat /var/lib/router/config/settings.json")
|
|
assert target.succeed("readlink /etc/localtime").strip().endswith("/UTC")
|
|
pinned()
|
|
target.wait_for_unit("router-webui.service")
|
|
|
|
with subtest("the same update, pulled from a server in the web UI, can be confirmed"):
|
|
login()
|
|
token = csrf("/update")
|
|
form = lambda path, data: builder.succeed(
|
|
f"curl -sk -b /tmp/jar -o /tmp/out -w '%{{http_code}}' --data-urlencode csrf={token} {data} {ui}{path}").strip()
|
|
assert form("/update/url", "--data-urlencode url=http://${builderIp}:8004/") == "303"
|
|
assert "Available: <strong>2.0.0</strong>" in (form("/update/check", "") and builder.succeed("cat /tmp/out"))
|
|
assert form("/update/install", "") == "303"
|
|
update_finished()
|
|
assert version() == "2.0.0"
|
|
target.succeed("systemctl is-active router-rollback.timer")
|
|
login()
|
|
token = csrf("/")
|
|
builder.succeed(f"curl -sk -b /tmp/jar -o /dev/null --data-urlencode csrf={token} {ui}/confirm")
|
|
target.wait_until_fails("systemctl is-active router-rollback.timer", timeout=60)
|
|
target.wait_until_succeeds("test $(systemctl show -p ActiveState --value router-confirm.service) = inactive")
|
|
target.succeed("test ! -e /var/lib/router/config.pre-update")
|
|
assert target.succeed("readlink -f /nix/var/nix/profiles/system").strip() == current()
|
|
assert target.succeed("readlink /etc/localtime").strip().endswith("Europe/Berlin")
|
|
|
|
with subtest("the updated router keeps the upgraded packages and rebuilds to exactly itself, offline"):
|
|
pinned()
|
|
assert flake_source() == "path:${v2.src}", flake_source()
|
|
out = target.succeed("router-apply 2>&1")
|
|
assert "no changes" in out, out
|
|
|
|
with subtest("the old release is not accepted any more"):
|
|
rc, out = update_from(8003)
|
|
assert rc != 0 and "not newer" in out, out
|
|
assert version() == "2.0.0"
|
|
|
|
with subtest("a release in a git repository is found, built and installed like a bundle"):
|
|
repo = "http://${builderIp}:8005/router.git"
|
|
builder.succeed("cp -r ${v3.src} /root/v3 && chmod -R u+w /root/v3")
|
|
builder.succeed("cd /root/v3 && git init -q -b main && git add -A"
|
|
" && git -c user.name=test -c user.email=test@example.org commit -q -m 'version 3' && git tag v3.0.0")
|
|
commit = builder.succeed("git -C /root/v3 rev-parse HEAD").strip()
|
|
builder.succeed("mkdir -p /srv/git && git clone -q --bare /root/v3 /srv/git/router.git")
|
|
builder.succeed("systemd-run --unit=git-server /etc/git-server.py /srv/git 8005")
|
|
builder.wait_for_open_port(8005, "${builderIp}")
|
|
|
|
# Nothing to look at until a repository is named.
|
|
assert "no repository" in target.succeed("router-update check 2>&1")
|
|
login()
|
|
token = csrf("/update")
|
|
form = lambda path, data: builder.succeed(
|
|
f"curl -sk -b /tmp/jar -o /tmp/out -w '%{{http_code}}' --data-urlencode csrf={token} {data} {ui}{path}").strip()
|
|
assert form("/update/repository", f"--data-urlencode repository={repo}/") == "303"
|
|
assert form("/update/gitcheck", "") == "303"
|
|
target.wait_until_succeeds(
|
|
"test $(systemctl show -p ActiveState --value router-update-check.service) = inactive"
|
|
" && test $(systemctl show -p Result --value router-update-check.service) = success", timeout=120)
|
|
check = json.loads(target.succeed("cat /var/lib/router/update/check.json"))
|
|
assert check["newer"] is True and check["newest"] == {"tag": "v3.0.0", "version": "3.0.0", "rev": commit} and check["installed"] == "2.0.0", check
|
|
assert "nixos-router 3.0.0 is out" in builder.succeed(f"curl -sk -b /tmp/jar {ui}/")
|
|
assert "Install 3.0.0" in builder.succeed(f"curl -sk -b /tmp/jar {ui}/update")
|
|
|
|
# Only tags that are versions are releases; a newer tag of another kind is ignored.
|
|
builder.succeed("cd /root/v3 && git tag nightly && git tag v4.0 && git push -q /srv/git/router.git nightly v4.0")
|
|
target.succeed("router-update check >&2")
|
|
assert json.loads(target.succeed("cat /var/lib/router/update/check.json"))["newest"]["tag"] == "v3.0.0"
|
|
|
|
rc, out = target.execute("router-update --git v9.9.9 2>&1")
|
|
assert rc != 0 and "no such release tag" in out, out
|
|
rc, out = target.execute("router-update --git 'v3.0.0; reboot' 2>&1")
|
|
assert rc != 0 and "not a release tag" in out, out
|
|
assert version() == "2.0.0"
|
|
|
|
login()
|
|
token = csrf("/update")
|
|
assert form("/update/gitinstall", "--data-urlencode tag=v3.0.0") == "303"
|
|
update_finished()
|
|
assert version() == "3.0.0", release_json()
|
|
flake = target.succeed("cat /var/lib/router/config/flake.nix")
|
|
assert f"git+{repo}?ref=refs/tags/v3.0.0&rev={commit}" in flake, flake
|
|
lock = json.loads(target.succeed("cat /var/lib/router/config/flake.lock"))
|
|
locked = lock["nodes"][lock["nodes"]["root"]["inputs"]["nixos-router"]]["locked"]
|
|
assert locked["type"] == "git" and locked["rev"] == commit, locked
|
|
settings = target.succeed("cat /var/lib/router/config/settings.json")
|
|
assert '"version": 2' in settings and "Europe/Paris" in settings, settings
|
|
assert "router-update-check" not in settings
|
|
pinned()
|
|
target.succeed("systemctl is-active router-rollback.timer")
|
|
login()
|
|
token = csrf("/")
|
|
builder.succeed(f"curl -sk -b /tmp/jar -o /dev/null --data-urlencode csrf={token} {ui}/confirm")
|
|
target.wait_until_fails("systemctl is-active router-rollback.timer", timeout=60)
|
|
target.wait_until_succeeds("test $(systemctl show -p ActiveState --value router-confirm.service) = inactive")
|
|
assert target.succeed("readlink /etc/localtime").strip().endswith("Europe/Paris")
|
|
|
|
with subtest("the router built from git rebuilds to exactly itself without the repository, and is up to date"):
|
|
builder.succeed("systemctl stop git-server")
|
|
out = target.succeed("router-apply 2>&1")
|
|
assert "no changes" in out, out
|
|
builder.succeed("systemd-run --unit=git-server-again /etc/git-server.py /srv/git 8005")
|
|
builder.wait_for_open_port(8005, "${builderIp}")
|
|
target.succeed("router-update check >&2")
|
|
check = json.loads(target.succeed("cat /var/lib/router/update/check.json"))
|
|
assert check["newer"] is False and check["installed"] == "3.0.0", check
|
|
rc, out = target.execute("router-update --git latest 2>&1")
|
|
assert rc != 0 and "not newer" in out, out
|
|
'';
|
|
}
|