2021-09-10 20:20:54 +02:00
|
|
|
{ config, options, lib, pkgs, ... }:
|
2020-09-01 06:37:26 +02:00
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.age;
|
2021-03-01 22:11:02 +01:00
|
|
|
|
|
|
|
# we need at least rage 0.5.0 to support ssh keys
|
|
|
|
rage =
|
|
|
|
if lib.versionOlder pkgs.rage.version "0.5.0"
|
2021-03-16 18:28:00 +01:00
|
|
|
then pkgs.callPackage ../pkgs/rage.nix { }
|
2021-03-01 22:11:02 +01:00
|
|
|
else pkgs.rage;
|
2020-09-18 20:59:01 +02:00
|
|
|
ageBin = "${rage}/bin/rage";
|
|
|
|
|
2020-09-01 06:37:26 +02:00
|
|
|
users = config.users.users;
|
|
|
|
|
2020-09-01 23:29:37 +02:00
|
|
|
identities = builtins.concatStringsSep " " (map (path: "-i ${path}") cfg.sshKeyPaths);
|
2020-09-01 23:27:54 +02:00
|
|
|
installSecret = secretType: ''
|
2021-02-25 11:23:15 +01:00
|
|
|
_truePath="${cfg.secretsMountPoint}/$_count/${secretType.name}"
|
|
|
|
echo "decrypting '${secretType.file}' to '$_truePath'..."
|
|
|
|
TMP_FILE="$_truePath.tmp"
|
|
|
|
mkdir -p "$(dirname "$_truePath")"
|
|
|
|
mkdir -p "$(dirname "${secretType.path}")"
|
2021-05-13 05:11:17 +02:00
|
|
|
(
|
|
|
|
umask u=r,g=,o=
|
|
|
|
LANG=${config.i18n.defaultLocale} ${ageBin} --decrypt ${identities} -o "$TMP_FILE" "${secretType.file}"
|
|
|
|
)
|
2020-09-03 05:49:24 +02:00
|
|
|
chmod ${secretType.mode} "$TMP_FILE"
|
|
|
|
chown ${secretType.owner}:${secretType.group} "$TMP_FILE"
|
2021-02-25 11:23:15 +01:00
|
|
|
mv -f "$TMP_FILE" "$_truePath"
|
2021-11-08 18:50:20 +01:00
|
|
|
[ "${secretType.path}" != "/run/agenix/${secretType.name}" ] && ln -sfn "/run/agenix/${secretType.name}" "${secretType.path}"
|
2020-09-01 23:27:54 +02:00
|
|
|
'';
|
2020-09-10 05:41:26 +02:00
|
|
|
|
2021-05-09 23:18:20 +02:00
|
|
|
isRootSecret = st: (st.owner == "root" || st.owner == "0") && (st.group == "root" || st.group == "0");
|
|
|
|
isNotRootSecret = st: !(isRootSecret st);
|
|
|
|
|
|
|
|
rootOwnedSecrets = builtins.filter isRootSecret (builtins.attrValues cfg.secrets);
|
2021-03-01 22:10:52 +01:00
|
|
|
installRootOwnedSecrets = builtins.concatStringsSep "\n" ([ "echo '[agenix] decrypting root secrets...'" ] ++ (map installSecret rootOwnedSecrets));
|
2020-09-10 05:41:26 +02:00
|
|
|
|
2021-05-09 23:18:20 +02:00
|
|
|
nonRootSecrets = builtins.filter isNotRootSecret (builtins.attrValues cfg.secrets);
|
2021-03-01 22:10:52 +01:00
|
|
|
installNonRootSecrets = builtins.concatStringsSep "\n" ([ "echo '[agenix] decrypting non-root secrets...'" ] ++ (map installSecret nonRootSecrets));
|
2020-09-01 06:37:26 +02:00
|
|
|
|
|
|
|
secretType = types.submodule ({ config, ... }: {
|
|
|
|
options = {
|
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = config._module.args.name;
|
|
|
|
description = ''
|
2021-11-08 18:50:20 +01:00
|
|
|
Name of the file used in /run/agenix
|
2020-09-01 06:37:26 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
file = mkOption {
|
2020-09-03 20:24:33 +02:00
|
|
|
type = types.path;
|
2020-09-01 06:37:26 +02:00
|
|
|
description = ''
|
|
|
|
Age file the secret is loaded from.
|
|
|
|
'';
|
|
|
|
};
|
2020-09-03 20:24:33 +02:00
|
|
|
path = mkOption {
|
2021-03-01 22:10:52 +01:00
|
|
|
type = types.str;
|
2021-11-08 18:50:20 +01:00
|
|
|
default = "/run/agenix/${config.name}";
|
2021-03-01 22:10:52 +01:00
|
|
|
description = ''
|
|
|
|
Path where the decrypted secret is installed.
|
|
|
|
'';
|
|
|
|
};
|
2020-09-01 06:37:26 +02:00
|
|
|
mode = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "0400";
|
|
|
|
description = ''
|
|
|
|
Permissions mode of the in octal.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
owner = mkOption {
|
|
|
|
type = types.str;
|
2021-05-09 23:18:20 +02:00
|
|
|
default = "0";
|
2020-09-01 06:37:26 +02:00
|
|
|
description = ''
|
|
|
|
User of the file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
2021-05-09 23:18:20 +02:00
|
|
|
default = users.${config.owner}.group or "0";
|
2020-09-01 06:37:26 +02:00
|
|
|
description = ''
|
|
|
|
Group of the file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
2021-03-01 22:10:52 +01:00
|
|
|
in
|
|
|
|
{
|
2020-09-01 06:37:26 +02:00
|
|
|
options.age = {
|
|
|
|
secrets = mkOption {
|
|
|
|
type = types.attrsOf secretType;
|
2021-03-01 22:10:52 +01:00
|
|
|
default = { };
|
2020-09-01 06:37:26 +02:00
|
|
|
description = ''
|
|
|
|
Attrset of secrets.
|
|
|
|
'';
|
|
|
|
};
|
2021-02-25 11:23:15 +01:00
|
|
|
secretsMountPoint = mkOption {
|
|
|
|
type = types.addCheck types.str
|
|
|
|
(s:
|
|
|
|
(builtins.match "[ \t\n]*" s) == null # non-empty
|
|
|
|
&& (builtins.match ".+/" s) == null) # without trailing slash
|
|
|
|
// { description = "${types.str.description} (with check: non-empty without trailing slash)"; };
|
2021-11-08 18:50:20 +01:00
|
|
|
default = "/run/agenix.d";
|
2021-02-25 11:23:15 +01:00
|
|
|
description = ''
|
2021-11-08 18:50:20 +01:00
|
|
|
Where secrets are created before they are symlinked to /run/agenix
|
2021-02-25 11:23:15 +01:00
|
|
|
'';
|
|
|
|
};
|
2020-09-01 06:37:26 +02:00
|
|
|
sshKeyPaths = mkOption {
|
|
|
|
type = types.listOf types.path;
|
2021-03-01 22:10:52 +01:00
|
|
|
default =
|
|
|
|
if config.services.openssh.enable then
|
|
|
|
map (e: e.path) (lib.filter (e: e.type == "rsa" || e.type == "ed25519") config.services.openssh.hostKeys)
|
|
|
|
else [ ];
|
2020-09-01 06:37:26 +02:00
|
|
|
description = ''
|
2020-09-03 20:24:33 +02:00
|
|
|
Path to SSH keys to be used as identities in age decryption.
|
2020-09-01 06:37:26 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-09-17 00:39:38 +02:00
|
|
|
config = mkIf (cfg.secrets != { }) {
|
|
|
|
assertions = [{
|
|
|
|
assertion = cfg.sshKeyPaths != [ ];
|
|
|
|
message = "age.sshKeyPaths must be set.";
|
|
|
|
}];
|
2020-09-10 05:41:26 +02:00
|
|
|
|
2021-02-25 11:23:15 +01:00
|
|
|
# Create a new directory full of secrets for symlinking (this helps
|
|
|
|
# ensure removed secrets are actually removed, or at least become
|
|
|
|
# invalid symlinks).
|
|
|
|
system.activationScripts.agenixMountSecrets = ''
|
2021-11-08 18:50:20 +01:00
|
|
|
_agenix_generation="$(basename "$(readlink /run/agenix)" || echo 0)"
|
|
|
|
(( ++_agenix_generation ))
|
|
|
|
echo "[agenix] symlinking new secrets to /run/agenix (generation $_agenix_generation)..."
|
2021-02-25 11:23:15 +01:00
|
|
|
mkdir -p "${cfg.secretsMountPoint}"
|
|
|
|
chmod 0750 "${cfg.secretsMountPoint}"
|
|
|
|
grep -q "${cfg.secretsMountPoint} ramfs" /proc/mounts || mount -t ramfs none "${cfg.secretsMountPoint}" -o nodev,nosuid,mode=0750
|
2021-11-08 18:50:20 +01:00
|
|
|
mkdir -p "${cfg.secretsMountPoint}/$_agenix_generation"
|
|
|
|
chmod 0750 "${cfg.secretsMountPoint}/$_agenix_generation"
|
|
|
|
ln -sfn "${cfg.secretsMountPoint}/$_agenix_generation" /run/agenix
|
2021-02-27 05:31:16 +01:00
|
|
|
|
|
|
|
(( _agenix_generation > 1 )) && {
|
|
|
|
echo "[agenix] removing old secrets (generation $(( _agenix_generation - 1 )))..."
|
|
|
|
rm -rf "${cfg.secretsMountPoint}/$(( _agenix_generation - 1 ))"
|
|
|
|
}
|
2021-02-25 11:23:15 +01:00
|
|
|
'';
|
|
|
|
|
2021-09-17 00:39:38 +02:00
|
|
|
# Secrets with root owner and group can be installed before users
|
|
|
|
# exist. This allows user password files to be encrypted.
|
2021-02-25 11:23:15 +01:00
|
|
|
system.activationScripts.agenixRoot = {
|
|
|
|
text = installRootOwnedSecrets;
|
|
|
|
deps = [ "agenixMountSecrets" "specialfs" ];
|
|
|
|
};
|
2021-09-17 00:39:38 +02:00
|
|
|
system.activationScripts.users.deps = [ "agenixRoot" ];
|
2021-09-10 20:20:54 +02:00
|
|
|
|
2021-02-25 11:23:15 +01:00
|
|
|
# chown the secrets mountpoint and the current generation to the keys group
|
|
|
|
# instead of leaving it root:root.
|
|
|
|
system.activationScripts.agenixChownKeys = {
|
|
|
|
text = ''
|
|
|
|
chown :keys "${cfg.secretsMountPoint}" "${cfg.secretsMountPoint}/$_agenix_generation"
|
|
|
|
'';
|
|
|
|
deps = [
|
|
|
|
"users"
|
|
|
|
"groups"
|
|
|
|
"agenixMountSecrets"
|
|
|
|
];
|
|
|
|
};
|
2021-09-10 20:20:54 +02:00
|
|
|
|
2021-02-25 11:23:15 +01:00
|
|
|
# Other secrets need to wait for users and groups to exist.
|
|
|
|
system.activationScripts.agenix = {
|
|
|
|
text = installNonRootSecrets;
|
|
|
|
deps = [
|
|
|
|
"users"
|
|
|
|
"groups"
|
|
|
|
"specialfs"
|
|
|
|
"agenixMountSecrets"
|
|
|
|
"agenixChownKeys"
|
|
|
|
];
|
|
|
|
};
|
2021-09-17 00:39:38 +02:00
|
|
|
};
|
2021-09-10 20:20:54 +02:00
|
|
|
|
2020-09-01 06:37:26 +02:00
|
|
|
}
|