diff options
| author | tv <tv@krebsco.de> | 2026-12-01 03:02:09 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-12-03 04:11:14 +0000 |
| commit | 8c37bb9d27479b5a3dcddb9d677fc80b79db048a (patch) | |
| tree | cfcef2fdaeea5bb0d555514d59fa942cffb9832e /configs/sshd.nix | |
| parent | ef7ef13a0b1b1f2d12b2092733ff0e4dffd0abd3 (diff) | |
sshd: take the host key from enstate where it is enabled
As application/x-openssh-private-key; algorithm=ed25519, a type of
hrm's own with a validator, from the host's entry in the passage store
unless the host names another, instead of krebs.build.host.ssh.privkey.
sshd reads the key itself, not by LoadCredential=, so it gets ordered
after enstate.service here, and enstate reloads it, which reads the key
again, when it changes. A generated key, after enstate's, keeps sshd
up while enstate has none: on a first boot, with lost state, or after a
rollback it can't serve.
Diffstat (limited to 'configs/sshd.nix')
| -rw-r--r-- | configs/sshd.nix | 117 |
1 files changed, 89 insertions, 28 deletions
diff --git a/configs/sshd.nix b/configs/sshd.nix index 1f7ea5e2..dcd72974 100644 --- a/configs/sshd.nix +++ b/configs/sshd.nix @@ -1,4 +1,4 @@ -{ config, lib, ... }: let +{ config, lib, pkgs, ... }: let cfg.host = config.krebs.build.host; nets = lib.optional (cfg.host.nets?mycelium) cfg.host.nets.mycelium ++ @@ -6,36 +6,97 @@ lib.optional (cfg.host.nets?wiregrill) cfg.host.nets.wiregrill; cfg.ssh.port = 11423; -in { - services.openssh = { - enable = true; - }; - hrm.nftables.input.internet.tcp = [ "ssh" ]; - hrm.nftables.tables.nat.chains = { - # Port 22 is only reachable on the host's own network addresses; on - # other addresses, port 22 goes nowhere and cfg.ssh.port leads to sshd. - prerouting.rules = { - ssh-own-addresses = { - order = 900; - daddr = - map (net: net.ip4.addr) (builtins.filter (net: net.ip4 != null) nets) ++ - map (net: net.ip6.addr) (builtins.filter (net: net.ip6 != null) nets); - tcp.dport = [ "ssh" ]; - verdict = "accept"; - }; - ssh-hide = { - tcp.dport = [ "ssh" ]; - verdict = "redirect to :0"; + + keyType = "application/x-openssh-private-key; algorithm=ed25519"; + keySlot = config.enstate.channels.secrets.slots."ssh.id_ed25519"; + fallbackKey = "/etc/ssh/ssh_host_ed25519_key"; + keyValidator = pkgs.writers.writeDash "validate-openssh-ed25519-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-openssh-private-key.jq" '' + "-----BEGIN OPENSSH PRIVATE KEY-----" as $begin | + "-----END OPENSSH PRIVATE KEY-----" as $end | + split("\n") | if .[-1] == "" then .[:-1] else . end | + if length == 0 then "is empty" + elif .[0] != $begin then "does not start with \($begin)" + elif .[-1] != $end then "does not end with \($end)" + elif length < 3 then "has no key between its first and last line" + else empty end | + { address: $ARGS.named.address, message: "the key \(.)" } + ''} --arg address "$address" "$1"/channels/"$address" + done + ''; +in lib.mkMerge [ + { + services.openssh = { + enable = true; + }; + hrm.nftables.input.internet.tcp = [ "ssh" ]; + hrm.nftables.tables.nat.chains = { + # Port 22 is only reachable on the host's own network addresses; on + # other addresses, port 22 goes nowhere and cfg.ssh.port leads to sshd. + prerouting.rules = { + ssh-own-addresses = { + order = 900; + daddr = + map (net: net.ip4.addr) (builtins.filter (net: net.ip4 != null) nets) ++ + map (net: net.ip6.addr) (builtins.filter (net: net.ip6 != null) nets); + tcp.dport = [ "ssh" ]; + verdict = "accept"; + }; + ssh-hide = { + tcp.dport = [ "ssh" ]; + verdict = "redirect to :0"; + }; + ssh-port = { + tcp.dport = [ cfg.ssh.port ]; + verdict = "redirect to :22"; + }; }; - ssh-port = { + output.rules.ssh-port = { + oifname = "lo"; tcp.dport = [ cfg.ssh.port ]; verdict = "redirect to :22"; }; }; - output.rules.ssh-port = { - oifname = "lo"; - tcp.dport = [ cfg.ssh.port ]; - verdict = "redirect to :22"; + } + # sshd reads its host key itself, so it gets ordered after enstate.service + # here, and reloads, which reads the key again, when it changes. + (lib.mkIf config.enstate.enable { + enstate.types.${keyType}.validator = keyValidator; + enstate.channels.secrets.slots."ssh.id_ed25519" = { + type = keyType; + secret = true; + source = lib.mkDefault { secrets.key = "ssh.id_ed25519"; }; + reactions.reload = + lib.optional (!config.services.openssh.startWhenNeeded) "sshd.service"; + }; + # enstate's key first: sshd uses it whenever it is there. The generated + # one is for while enstate has none; sshd skips a key it can't load. + # Not by generateHostKeys, which would write a key to enstate's path. + services.openssh.generateHostKeys = false; + services.openssh.hostKeys = lib.mkForce [ + { type = "ed25519"; path = keySlot.path; } + { type = "ed25519"; path = fallbackKey; } + ]; + systemd.services.sshd-fallback-key = { + description = "SSH fallback host key generation"; + wantedBy = [ "multi-user.target" ]; + unitConfig.ConditionFileNotEmpty = "!${fallbackKey}"; + serviceConfig.Type = "oneshot"; + script = '' + rm -f ${fallbackKey} ${fallbackKey}.pub + ${config.services.openssh.package}/bin/ssh-keygen -t ed25519 -N "" -f ${fallbackKey} + ''; + }; + # Only the unit there is: sshd, or sshd@ with startWhenNeeded. + systemd.services.${if config.services.openssh.startWhenNeeded then "sshd@" else "sshd"} = { + after = [ "enstate.service" "sshd-fallback-key.service" ]; + wants = [ "enstate.service" "sshd-fallback-key.service" ]; }; - }; -} + }) +] |
