1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
{ config, lib, pkgs, ... }: let
cfg.host = config.krebs.build.host;
nets =
lib.optional (cfg.host.nets?mycelium) cfg.host.nets.mycelium ++
lib.optional (cfg.host.nets?retiolum) cfg.host.nets.retiolum ++
lib.optional (cfg.host.nets?wiregrill) cfg.host.nets.wiregrill;
cfg.ssh.port = 11423;
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";
};
};
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" ];
};
})
]
|