2019-11-27 05:04:27 -08:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
2020-01-12 11:52:38 -08:00
|
|
|
cfg = config.nix-bitcoin;
|
2019-11-27 05:04:27 -08:00
|
|
|
setupSecrets = concatStrings (mapAttrsToList (n: v: ''
|
2020-01-12 11:52:38 -08:00
|
|
|
setupSecret ${n} ${v.user} ${v.group} ${v.permissions} }
|
|
|
|
'') cfg.secrets);
|
2019-11-27 05:04:27 -08:00
|
|
|
in
|
|
|
|
{
|
2020-01-12 11:52:38 -08:00
|
|
|
options.nix-bitcoin = {
|
2020-01-12 11:52:39 -08:00
|
|
|
secretsDir = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/etc/nix-bitcoin-secrets";
|
|
|
|
description = "Directory to store secrets";
|
|
|
|
};
|
|
|
|
|
2020-02-26 08:11:19 -08:00
|
|
|
deployment.secretsDir = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
description = ''
|
2020-08-04 06:32:06 -07:00
|
|
|
Directory of local secrets that are transferred to the nix-bitcoin node on deployment
|
2020-02-26 08:11:19 -08:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-01-12 11:52:38 -08:00
|
|
|
secrets = mkOption {
|
|
|
|
default = {};
|
|
|
|
type = with types; attrsOf (submodule (
|
|
|
|
{ config, ... }: {
|
|
|
|
options = {
|
|
|
|
user = mkOption {
|
|
|
|
type = str;
|
|
|
|
default = "root";
|
|
|
|
};
|
|
|
|
group = mkOption {
|
|
|
|
type = str;
|
|
|
|
default = config.user;
|
|
|
|
};
|
|
|
|
permissions = mkOption {
|
|
|
|
type = str;
|
|
|
|
default = "0440";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
));
|
|
|
|
};
|
|
|
|
|
|
|
|
setup-secrets = mkEnableOption "Set permissions for secrets generated by 'generate-secrets.sh'";
|
|
|
|
};
|
2019-11-27 05:04:27 -08:00
|
|
|
|
2020-01-12 11:52:38 -08:00
|
|
|
config = mkIf cfg.setup-secrets {
|
2019-11-27 05:04:27 -08:00
|
|
|
systemd.targets.nix-bitcoin-secrets = {
|
|
|
|
requires = [ "setup-secrets.service" ];
|
|
|
|
after = [ "setup-secrets.service" ];
|
|
|
|
};
|
|
|
|
|
|
|
|
# Operation of this service:
|
|
|
|
# - Set owner and permissions for all used secrets
|
|
|
|
# - Make all other secrets accessible to root only
|
2020-01-12 11:52:38 -08:00
|
|
|
# For all steps make sure that no secrets are copied to the nix store.
|
2019-11-27 05:04:27 -08:00
|
|
|
#
|
|
|
|
systemd.services.setup-secrets = {
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
2020-02-26 11:37:47 -08:00
|
|
|
};
|
2019-11-27 05:04:27 -08:00
|
|
|
script = ''
|
|
|
|
setupSecret() {
|
|
|
|
file="$1"
|
|
|
|
user="$2"
|
|
|
|
group="$3"
|
|
|
|
permissions="$4"
|
|
|
|
if [[ ! -e $file ]]; then
|
2020-01-12 11:52:38 -08:00
|
|
|
echo "Error: Secret file '$file' is missing"
|
|
|
|
exit 1
|
2019-11-27 05:04:27 -08:00
|
|
|
fi
|
|
|
|
chown "$user:$group" "$file"
|
|
|
|
chmod "$permissions" "$file"
|
|
|
|
processedFiles+=("$file")
|
|
|
|
}
|
|
|
|
|
2020-01-12 11:52:39 -08:00
|
|
|
dir="${cfg.secretsDir}"
|
2019-11-27 05:04:27 -08:00
|
|
|
if [[ ! -e $dir ]]; then
|
|
|
|
echo "Error: Secrets dir '$dir' is missing"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
chown root: "$dir"
|
|
|
|
cd "$dir"
|
|
|
|
|
|
|
|
processedFiles=()
|
|
|
|
${setupSecrets}
|
|
|
|
|
|
|
|
# Make all other files accessible to root only
|
|
|
|
unprocessedFiles=$(comm -23 <(printf '%s\n' *) <(printf '%s\n' "''${processedFiles[@]}" | sort))
|
|
|
|
IFS=$'\n'
|
|
|
|
chown root: $unprocessedFiles
|
|
|
|
chmod 0440 $unprocessedFiles
|
|
|
|
|
|
|
|
# Now make the secrets dir accessible to other users
|
|
|
|
chmod 0751 "$dir"
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|