Add authenticated Gateway system updates with rollback

This commit is contained in:
jhartworks committed 2026-11-24 13:46:40 +01:00
1 parent 198b1d7b78
commit 4e35a3a557
9 files changed
+423 -1

No files matched your search

+73
View File
@@ -12,6 +12,25 @@ let
&& !config.networking.useNetworkd && !config.networking.networkmanager.enable;
dhcpcdBase = builtins.head (builtins.match ".* --config ([^ ]+).*" config.systemd.services.dhcpcd.serviceConfig.ExecStart);
networkHelper = "${pkgs.python3}/bin/python3 ${./host-network.py}";
updateSocket = "/run/seiugw-update/request.sock";
updateWorker = pkgs.writeShellScript "seiugw-system-update" ''
export PATH=${lib.makeBinPath [ pkgs.coreutils pkgs.git pkgs.util-linux pkgs.systemd pkgs.curl pkgs.jq pkgs.sqlite pkgs.nix ]}:/run/current-system/sw/bin
export NIX_PATH=${escapeShellArg "nixos-config=/etc/nixos/configuration.nix:nixpkgs=${pkgs.path}"}
export SEIUGW_UPDATE_REPO=${escapeShellArg cfg.updates.checkout}
export SEIUGW_UPDATE_REMOTE=${escapeShellArg cfg.updates.remote}
export SEIUGW_UPDATE_BRANCH=${escapeShellArg cfg.updates.branch}
export SEIUGW_UPDATE_DATA=${escapeShellArg cfg.dataDir}
export SEIUGW_UPDATE_HEALTH=${escapeShellArg "http://127.0.0.1:${toString cfg.webPort}"}
${builtins.readFile ./system-update.sh}
'';
updateTrigger = pkgs.writeShellScript "seiugw-request-update" ''
if ${pkgs.systemd}/bin/systemd-run --quiet --collect --unit=seiugw-update-worker \
--property=Type=exec ${updateWorker} >&2; then
printf 'accepted\n'
else
printf 'unavailable\n'
fi
'';
in
{
options.services.seiugw = {
@@ -47,6 +66,25 @@ in
description = "Open the configured web and BACnet/IP ports in the NixOS firewall.";
};
updates = {
enable = mkEnableOption "local-root system updates requested by the Gateway administrator";
checkout = mkOption {
type = types.str;
default = "/etc/nixos/seiugw";
description = "Root-owned checkout already used by the NixOS host configuration.";
};
remote = mkOption {
type = types.str;
default = "";
description = "Trusted HTTPS Git URL used by the update worker.";
};
branch = mkOption {
type = types.str;
default = "main";
description = "Branch allowed for fast-forward gateway updates.";
};
};
bacnet = {
interface = mkOption {
type = types.str;
@@ -133,6 +171,13 @@ in
};
config = mkIf cfg.enable {
assertions = optionals cfg.updates.enable [
{
assertion = builtins.match "https://[^/@]+/[^?#]+" cfg.updates.remote != null;
message = "Gateway system updates require an HTTPS Git remote without embedded credentials.";
}
];
users.groups.seiugw = { };
users.groups.dialout = { };
users.users.seiugw = {
@@ -151,8 +196,33 @@ in
"d /var/lib/seiugw-network 0700 root root -"
] ++ optionals cfg.komari.enable [
"d ${cfg.komari.installDir} 0750 root root -"
] ++ optionals cfg.updates.enable [
"d /var/lib/seiugw-updater 0755 root root -"
];
systemd.sockets.seiugw-update = mkIf cfg.updates.enable {
description = "Local Gateway system update requests";
wantedBy = [ "sockets.target" ];
socketConfig = {
ListenStream = updateSocket;
SocketUser = "root";
SocketGroup = "seiugw";
SocketMode = "0660";
Accept = true;
RemoveOnStop = true;
};
};
systemd.services."seiugw-update@" = mkIf cfg.updates.enable {
description = "Start the independent Gateway update worker";
serviceConfig = {
ExecStart = updateTrigger;
StandardInput = "socket";
StandardOutput = "socket";
StandardError = "journal";
};
};
networking.firewall = mkIf cfg.openFirewall {
allowedTCPPorts = [ cfg.webPort cfg.opcUa.port ] ++ cfg.modbusTcpPorts;
allowedUDPPorts = [ cfg.bacnet.ipPort ];
@@ -240,6 +310,9 @@ in
Gateway__Bacnet__DeviceInstance = toString cfg.bacnet.deviceInstance;
Gateway__Bacnet__IpPort = toString cfg.bacnet.ipPort;
Gateway__OpcUa__Port = toString cfg.opcUa.port;
} // lib.optionalAttrs cfg.updates.enable {
Gateway__UpdateSocketPath = updateSocket;
Gateway__UpdateStatusPath = "/var/lib/seiugw-updater/status.json";
};
serviceConfig = {