sshd can use outputRef for authorized_keys

This commit is contained in:
Daniel Barlow 2024-08-25 16:35:50 +01:00
parent 83ca86fe42
commit c7164a6f4a
3 changed files with 17 additions and 17 deletions

View File

@ -72,15 +72,6 @@ in rec {
dependencies = [ services.wan-address-for-secrets ]; dependencies = [ services.wan-address-for-secrets ];
}; };
services.ssh-keys = longrun {
name = "write-ssh-keys";
run = ''
mkdir -p /run/authorized_keys
exec ${pkgs.watch-ssh-keys}/bin/watch-ssh-keys -d /run/authorized_keys ${services.secrets} ssh/authorizedKeys
'';
dependencies = [ services.secrets ] ;
};
services.wwan = svc.wwan.huawei-e3372.build { services.wwan = svc.wwan.huawei-e3372.build {
apn = "data.uk"; apn = "data.uk";
username = "user"; username = "user";
@ -188,9 +179,7 @@ in rec {
}; };
services.sshd = svc.ssh.build { services.sshd = svc.ssh.build {
authorizedKeys = { authorizedKeys = outputRef config.services.secrets "ssh/authorizedKeys";
root = rsecrets.root.openssh.authorizedKeys.keys;
};
}; };
services.lns-address = let services.lns-address = let

View File

@ -43,7 +43,7 @@ in {
description = "Allow remote hosts to connect to local forwarded ports (by default they are bound to loopback)"; description = "Allow remote hosts to connect to local forwarded ports (by default they are bound to loopback)";
}; };
authorizedKeys = mkOption { authorizedKeys = mkOption {
type = types.nullOr (types.attrsOf (types.listOf types.nonEmptyStr)); type = types.nullOr (liminix.lib.types.replacable (types.attrsOf (types.listOf types.nonEmptyStr)));
example = { example = {
root = ["ssh-rsa AAAAB3N...aZaZ"]; root = ["ssh-rsa AAAAB3N...aZaZ"];
alice = ["ssh-rsa AAAAB3N...qS4r"]; alice = ["ssh-rsa AAAAB3N...qS4r"];

View File

@ -2,6 +2,7 @@
liminix liminix
, dropbear , dropbear
, lib , lib
, watch-ssh-keys
}: }:
{ {
address, address,
@ -17,9 +18,10 @@
}: }:
let let
name = "sshd"; name = "sshd";
inherit (builtins) toString; inherit (builtins) toString typeOf;
inherit (liminix.services) longrun; inherit (liminix.services) longrun;
inherit (lib) concatStringsSep mapAttrs mapAttrsToList; inherit (lib) concatStringsSep mapAttrs mapAttrsToList;
keydir = "/run/${name}/authorized_keys";
options = options =
[ [
"-e" # pass environment to child "-e" # pass environment to child
@ -34,18 +36,26 @@ let
(lib.optional (! allowLocalPortForward) "-j") ++ (lib.optional (! allowLocalPortForward) "-j") ++
(lib.optional (! allowRemotePortForward) "-k") ++ (lib.optional (! allowRemotePortForward) "-k") ++
(lib.optional (! allowRemoteConnectionToForwardedPorts) "-a") ++ (lib.optional (! allowRemoteConnectionToForwardedPorts) "-a") ++
(lib.optionals (authorizedKeys != null) (lib.optionals (authorizedKeys != null) ["-U" "${keydir}/%n"]) ++
["-U" "/run/${name}/authorized_keys/%n"]) ++
[(if address != null [(if address != null
then "-p ${address}:${toString port}" then "-p ${address}:${toString port}"
else "-p ${toString port}")] ++ else "-p ${toString port}")] ++
[extraConfig]; [extraConfig];
isKeyservice = typeOf authorizedKeys == "lambda";
authKeysConcat = authKeysConcat =
if authorizedKeys != null if authorizedKeys != null && !isKeyservice
then mapAttrs then mapAttrs
(n : v : concatStringsSep "\\n" v) (n : v : concatStringsSep "\\n" v)
authorizedKeys authorizedKeys
else {}; else {};
keyservice = longrun {
name = "${name}-watch-keys";
run = ''
mkdir -p ${keydir}
exec ${watch-ssh-keys}/bin/watch-ssh-keys -d ${keydir} ${authorizedKeys "service"} ${authorizedKeys "path"}
'';
dependencies = [ (authorizedKeys "service") ] ;
};
in in
longrun { longrun {
inherit name; inherit name;
@ -65,4 +75,5 @@ longrun {
. /etc/profile # sets PATH but do we need this? it's the same file as ashrc . /etc/profile # sets PATH but do we need this? it's the same file as ashrc
exec env -i ENV=/etc/ashrc PATH=$PATH ${dropbear}/bin/dropbear ${concatStringsSep " " options} exec env -i ENV=/etc/ashrc PATH=$PATH ${dropbear}/bin/dropbear ${concatStringsSep " " options}
''; '';
dependencies = lib.optional isKeyservice keyservice;
} }