diff options
Diffstat (limited to 'configs/wiregrill.nix')
| -rw-r--r-- | configs/wiregrill.nix | 107 |
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" ]; + }; + }) + ])) + ]) |
