modules: secrets: centralize agenix keys

If I intend on splitting the keys depending on which host needs to have
access to it, I should have a singular spot to manage the keys.
This commit is contained in:
Bruno BELANYI 2023-04-13 16:37:42 +00:00
parent 68bf36c45c
commit 34a3f9a0d6
4 changed files with 43 additions and 11 deletions

39
keys/default.nix Normal file
View File

@ -0,0 +1,39 @@
# Populate agenix keys from a central location
let
inherit (builtins)
mapAttrs
readDir
readFile
stringLength
substring
;
removeSuffix = suffix: str:
let
sufLen = stringLength suffix;
sLen = stringLength str;
in
if sufLen <= sLen && suffix == substring (sLen - sufLen) sufLen str then
substring 0 (sLen - sufLen) str
else
str;
readKeys = dir:
let
files = readDir dir;
readNoNewlines = f: removeSuffix "\n" (readFile f);
readKey = name: readNoNewlines (dir + "/${name}");
in
mapAttrs (n: _: readKey n) files;
hosts = readKeys ./hosts;
users = readKeys ./users;
in
{
inherit
hosts
users;
all = (builtins.attrValues hosts) ++ (builtins.attrValues users);
}

1
keys/hosts/porthos Normal file
View File

@ -0,0 +1 @@
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICGzznQ3LSmBYHx6fXthgMDiTcU5i/Nvj020SbmhzAFb root@porthos

1
keys/users/ambroisie Normal file
View File

@ -0,0 +1 @@
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMIVd6Oh08iUNb1vTULbxGpevnh++wxsWW9wqhaDryIq ambroisie@agenix

View File

@ -1,16 +1,7 @@
let
# FIXME: read them from directories
ambroisie = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMIVd6Oh08iUNb1vTULbxGpevnh++wxsWW9wqhaDryIq ambroisie@agenix";
users = [
ambroisie
];
keys = import ../../keys;
porthos = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICGzznQ3LSmBYHx6fXthgMDiTcU5i/Nvj020SbmhzAFb root@porthos";
machines = [
porthos
];
all = users ++ machines;
inherit (keys) all;
in
{
"acme/dns-key.age".publicKeys = all;