summaryrefslogtreecommitdiffstats
path: root/configs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-12-02 22:22:09 +0000
committertv <tv@krebsco.de>2026-12-02 22:22:09 +0000
commit0173a4e2b0b31db7b42842a414dca5b2b15ebc2c (patch)
tree33b82293d8645796671836b88297f2c1a41d200d /configs
parent2d6af414e4308499515109374e1c7046af4ddf51 (diff)
wiregrill: take the key from enstate where it is enabled
As application/x-wireguard-private-key, a type of hrm's own with a validator, from the host's entry in the passage store unless the host names another. With networkd, the key is systemd-networkd's credential, so enstate's module orders it after enstate.service, and enstate restarts systemd-networkd when the key changes; the scripted unit reads the key itself, so it gets the ordering here, and enstate restarts it. Without the key, systemd won't start networkd at all, taking every interface down; a placeholder key as networkd's fallback credential leaves only wiregrill down until the key comes.
Diffstat (limited to 'configs')
-rw-r--r--configs/wiregrill.nix107
1 files changed, 78 insertions, 29 deletions
diff --git a/configs/wiregrill.nix b/configs/wiregrill.nix
index ee730615..31fe5207 100644
--- a/configs/wiregrill.nix
+++ b/configs/wiregrill.nix
@@ -4,33 +4,82 @@
net = config.krebs.build.host.nets.wiregrill or null;
};
toCidrNotation = ip: "${ip.addr}/${toString ip.prefixLength}";
+ keyType = "application/x-wireguard-private-key";
+ keySlot = config.enstate.channels.secrets.slots."wiregrill.key";
+ keyValidator = pkgs.writers.writeDash "validate-wireguard-private-key" ''
+ set -efu
+ # Not in a pipe, where set -e would miss a failure of jq.
+ addresses=$(${pkgs.jq}/bin/jq -r -f ${pkgs.writeJq "slots-of-type.jq" ''
+ .slots | to_entries[] | select(.value.type == $ARGS.named.type) | .key
+ ''} --arg type ${lib.escapeShellArg keyType} "$1"/invocation.json)
+ for address in $addresses; do
+ ${pkgs.jq}/bin/jq -Rsc -f ${pkgs.writeJq "check-wireguard-private-key.jq" ''
+ if rtrimstr("\n") | test("^[A-Za-z0-9+/]{42}[AEIMQUYcgkosw048]=$") then empty
+ else { address: $ARGS.named.address, message: "the key is not 32 bytes in base64" } end
+ ''} --arg address "$address" "$1"/channels/"$address"
+ done
+ '';
in
- lib.mkIf cfg.enable {
- networking.wireguard.interfaces.wiregrill = {
- ips =
- lib.optional (cfg.net.ip4 != null) (toCidrNotation cfg.net.ip4) ++
- lib.optional (cfg.net.ip6 != null) (toCidrNotation cfg.net.ip6);
- listenPort = 51820;
- privateKeyFile = "${config.krebs.secret.directory}/wiregrill.key";
- allowedIPsAsRoutes = true;
- peers = lib.mapAttrsToList
- (_: host: {
- allowedIPs = host.nets.wiregrill.wireguard.subnets;
- endpoint =
- lib.mkIf (host.nets.wiregrill.via != null) (host.nets.wiregrill.via.ip4.addr + ":${toString host.nets.wiregrill.wireguard.port}");
- persistentKeepalive = lib.mkIf (host.nets.wiregrill.via != null) 61;
- publicKey =
- lib.replaceStrings ["\n"] [""] host.nets.wiregrill.wireguard.pubkey;
- })
- (lib.filterAttrs (_: h: lib.hasAttr "wiregrill" h.nets) config.krebs.hosts);
- };
- systemd.network.networks.wiregrill = {
- matchConfig.Name = "wiregrill";
- address =
- lib.optional (cfg.net.ip4 != null) (toCidrNotation cfg.net.ip4) ++
- lib.optional (cfg.net.ip6 != null) (toCidrNotation cfg.net.ip6);
- };
- tv.iptables.extra.filter.INPUT = [
- "-p udp --dport ${toString cfg.net.wireguard.port} -j ACCEPT"
- ];
- }
+ lib.mkIf cfg.enable (lib.mkMerge [
+ {
+ networking.wireguard.interfaces.wiregrill = {
+ ips =
+ lib.optional (cfg.net.ip4 != null) (toCidrNotation cfg.net.ip4) ++
+ lib.optional (cfg.net.ip6 != null) (toCidrNotation cfg.net.ip6);
+ listenPort = 51820;
+ privateKeyFile =
+ if config.enstate.enable then keySlot.path
+ else "${config.krebs.secret.directory}/wiregrill.key";
+ allowedIPsAsRoutes = true;
+ peers = lib.mapAttrsToList
+ (_: host: {
+ allowedIPs = host.nets.wiregrill.wireguard.subnets;
+ endpoint =
+ lib.mkIf (host.nets.wiregrill.via != null) (host.nets.wiregrill.via.ip4.addr + ":${toString host.nets.wiregrill.wireguard.port}");
+ persistentKeepalive = lib.mkIf (host.nets.wiregrill.via != null) 61;
+ publicKey =
+ lib.replaceStrings ["\n"] [""] host.nets.wiregrill.wireguard.pubkey;
+ })
+ (lib.filterAttrs (_: h: lib.hasAttr "wiregrill" h.nets) config.krebs.hosts);
+ };
+ systemd.network.networks.wiregrill = {
+ matchConfig.Name = "wiregrill";
+ address =
+ lib.optional (cfg.net.ip4 != null) (toCidrNotation cfg.net.ip4) ++
+ lib.optional (cfg.net.ip6 != null) (toCidrNotation cfg.net.ip6);
+ };
+ tv.iptables.extra.filter.INPUT = [
+ "-p udp --dport ${toString cfg.net.wireguard.port} -j ACCEPT"
+ ];
+ }
+ # With networkd, the key is systemd-networkd's credential, which orders
+ # it after enstate.service by itself; the scripted unit reads it itself.
+ (lib.mkIf config.enstate.enable (lib.mkMerge [
+ {
+ enstate.types.${keyType}.validator = keyValidator;
+ enstate.channels.secrets.slots."wiregrill.key" = {
+ type = keyType;
+ secret = true;
+ source = lib.mkDefault { secrets.key = "wiregrill.key"; };
+ reactions.restart =
+ if config.networking.wireguard.useNetworkd
+ then [ "systemd-networkd.service" ]
+ else [ "wireguard-wiregrill.service" ];
+ };
+ }
+ # Without the key, systemd wouldn't start networkd at all, and every
+ # interface would stay down. With a key that isn't one, only
+ # wiregrill does.
+ (lib.mkIf config.networking.wireguard.useNetworkd {
+ systemd.services.systemd-networkd.serviceConfig.SetCredential = [
+ "wireguard-wiregrill-private-key:none"
+ ];
+ })
+ (lib.mkIf (!config.networking.wireguard.useNetworkd) {
+ systemd.services.wireguard-wiregrill = {
+ after = [ "enstate.service" ];
+ wants = [ "enstate.service" ];
+ };
+ })
+ ]))
+ ])
[cgit] Unable to lock slot /tmp/cgit/f7200000.lock: Permission denied (13)