2019-11-27 05:04:27 -08:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
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";
|
2022-12-18 04:13:47 -08:00
|
|
|
description = mdDoc "Directory to store secrets";
|
2020-01-12 11:52:39 -08:00
|
|
|
};
|
|
|
|
|
2021-03-10 05:08:34 -08:00
|
|
|
setupSecrets = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-12-18 04:13:47 -08:00
|
|
|
description = mdDoc ''
|
|
|
|
Set permissions for existing secrets in {option}`nix-bitcoin.secretsDir`
|
2021-10-01 02:51:55 -07:00
|
|
|
before services are started.
|
2021-03-10 05:08:34 -08:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
generateSecrets = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-12-18 04:13:47 -08:00
|
|
|
description = mdDoc ''
|
2021-10-01 02:51:55 -07:00
|
|
|
Automatically generate all required secrets before services are started.
|
2021-09-08 08:01:15 -07:00
|
|
|
Note: Make sure to create a backup of the generated secrets.
|
2021-03-10 05:08:34 -08:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-09-08 08:01:18 -07:00
|
|
|
generateSecretsCmds = mkOption {
|
|
|
|
type = types.attrsOf types.str;
|
|
|
|
default = {};
|
2022-12-18 04:13:47 -08:00
|
|
|
description = mdDoc ''
|
2021-09-08 08:01:18 -07:00
|
|
|
Bash expressions for generating secrets.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-03-10 05:08:34 -08:00
|
|
|
# Currently, this is used only by ../deployment/nixops.nix
|
2020-02-26 08:11:19 -08:00
|
|
|
deployment.secretsDir = mkOption {
|
|
|
|
type = types.path;
|
2022-12-18 04:13:47 -08:00
|
|
|
description = mdDoc ''
|
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;
|
2021-10-02 02:49:49 -07:00
|
|
|
default = "440";
|
2020-01-12 11:52:38 -08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
));
|
|
|
|
};
|
2021-03-10 05:08:36 -08:00
|
|
|
|
|
|
|
secretsSetupMethod = mkOption {
|
2022-03-10 13:48:16 -08:00
|
|
|
type = with types; nullOr str;
|
2021-12-07 19:07:25 -08:00
|
|
|
default = null;
|
2021-03-10 05:08:36 -08:00
|
|
|
};
|
2021-09-08 08:01:18 -07:00
|
|
|
|
|
|
|
generateSecretsScript = mkOption {
|
|
|
|
internal = true;
|
|
|
|
default = let
|
2022-08-22 05:57:44 -07:00
|
|
|
rpcauthSrc = pkgs.fetchurl {
|
2021-09-08 08:01:18 -07:00
|
|
|
url = "https://raw.githubusercontent.com/bitcoin/bitcoin/d6cde007db9d3e6ee93bd98a9bbfdce9bfa9b15b/share/rpcauth/rpcauth.py";
|
|
|
|
sha256 = "189mpplam6yzizssrgiyv70c9899ggh8cac76j4n7v0xqzfip07n";
|
|
|
|
};
|
|
|
|
rpcauth = pkgs.writers.writeBash "rpcauth" ''
|
|
|
|
exec ${pkgs.python3}/bin/python ${rpcauthSrc} "$@"
|
|
|
|
'';
|
|
|
|
in pkgs.writers.writeBash "generate-secrets" ''
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
export PATH=${lib.makeBinPath (with pkgs; [ coreutils gnugrep ])}
|
|
|
|
|
|
|
|
makePasswordSecret() {
|
|
|
|
# Passwords have alphabet {a-z, A-Z, 0-9} and ~119 bits of entropy
|
|
|
|
[[ -e $1 ]] || ${pkgs.pwgen}/bin/pwgen -s 20 1 > "$1"
|
|
|
|
}
|
|
|
|
makeBitcoinRPCPassword() {
|
|
|
|
user=$1
|
|
|
|
file=bitcoin-rpcpassword-$user
|
|
|
|
HMACfile=bitcoin-HMAC-$user
|
|
|
|
makePasswordSecret "$file"
|
|
|
|
if [[ $file -nt $HMACfile ]]; then
|
|
|
|
${rpcauth} $user $(cat "$file") | grep rpcauth | cut -d ':' -f 2 > "$HMACfile"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
makeCert() {
|
|
|
|
name=$1
|
|
|
|
# Add leading comma if not empty
|
|
|
|
extraAltNames=''${2:+,}''${2:-}
|
|
|
|
if [[ ! -e $name-key ]]; then
|
|
|
|
# Create new key and cert
|
|
|
|
doMakeCert "-newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes -keyout $name-key"
|
|
|
|
elif [[ ! -e $name-cert \
|
|
|
|
|| $(cat "$name-cert-alt-names" 2>/dev/null) != $extraAltNames ]]; then
|
|
|
|
# Create cert from existing key
|
|
|
|
doMakeCert "-key $name-key"
|
|
|
|
fi;
|
|
|
|
}
|
|
|
|
doMakeCert() {
|
|
|
|
# This fn uses global variables `name` and `extraAltNames`
|
|
|
|
keyOpts=$1
|
|
|
|
${pkgs.openssl}/bin/openssl req -x509 \
|
|
|
|
-sha256 -days 3650 $keyOpts -out "$name-cert" \
|
|
|
|
-subj "/CN=localhost/O=$name" \
|
|
|
|
-addext "subjectAltName=DNS:localhost,IP:127.0.0.1$extraAltNames"
|
|
|
|
echo "$extraAltNames" > "$name-cert-alt-names"
|
|
|
|
}
|
|
|
|
|
|
|
|
umask u=rw,go=
|
|
|
|
${builtins.concatStringsSep "\n" (builtins.attrValues cfg.generateSecretsCmds)}
|
|
|
|
'';
|
|
|
|
};
|
2020-01-12 11:52:38 -08:00
|
|
|
};
|
2019-11-27 05:04:27 -08:00
|
|
|
|
2021-09-08 08:01:18 -07:00
|
|
|
cfg = config.nix-bitcoin;
|
|
|
|
in {
|
|
|
|
inherit options;
|
|
|
|
|
2021-03-10 05:08:35 -08:00
|
|
|
config = {
|
2021-12-07 19:07:25 -08:00
|
|
|
assertions = [
|
|
|
|
{ assertion = cfg.secretsSetupMethod != null;
|
|
|
|
message = ''
|
|
|
|
No secrets setup method has been defined.
|
|
|
|
To fix this, choose one of the following:
|
|
|
|
|
|
|
|
- Use one of the deployment methods in ${toString ./../deployment}
|
|
|
|
|
|
|
|
- Set `nix-bitcoin.generateSecrets = true` to automatically generate secrets
|
|
|
|
|
|
|
|
- Set `nix-bitcoin.secretsSetupMethod = "manual"` if you want to manually setup secrets
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2021-03-10 05:08:35 -08:00
|
|
|
# This target is active when secrets have been setup successfully.
|
2021-03-10 05:08:36 -08:00
|
|
|
systemd.targets.nix-bitcoin-secrets = mkIf (cfg.secretsSetupMethod != "manual") {
|
2021-03-10 05:08:35 -08:00
|
|
|
# This ensures that the secrets target is always activated when switching
|
|
|
|
# configurations.
|
|
|
|
# In this way `switch-to-configuration` is guaranteed to show an error
|
|
|
|
# when activating the secrets target fails on deployment.
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
};
|
2021-03-10 05:08:34 -08:00
|
|
|
|
|
|
|
nix-bitcoin.setupSecrets = mkIf cfg.generateSecrets true;
|
2019-11-27 05:04:27 -08:00
|
|
|
|
2021-03-10 05:08:36 -08:00
|
|
|
nix-bitcoin.secretsSetupMethod = mkIf cfg.setupSecrets "setup-secrets";
|
|
|
|
|
2019-11-27 05:04:27 -08:00
|
|
|
# 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
|
|
|
#
|
2021-03-10 05:08:34 -08:00
|
|
|
systemd.services.setup-secrets = mkIf cfg.setupSecrets {
|
|
|
|
requiredBy = [ "nix-bitcoin-secrets.target" ];
|
|
|
|
before = [ "nix-bitcoin-secrets.target" ];
|
2019-11-27 05:04:27 -08:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
RemainAfterExit = true;
|
2020-02-26 11:37:47 -08:00
|
|
|
};
|
2019-11-27 05:04:27 -08:00
|
|
|
script = ''
|
2022-07-22 02:26:05 -07:00
|
|
|
# Use the same sort order for globbing and sorting as in Nix attrsets.
|
|
|
|
# Required for `comm` below.
|
|
|
|
export LC_COLLATE=C
|
|
|
|
|
2021-03-10 05:08:34 -08:00
|
|
|
${optionalString cfg.generateSecrets ''
|
|
|
|
mkdir -p "${cfg.secretsDir}"
|
|
|
|
cd "${cfg.secretsDir}"
|
|
|
|
chown root: .
|
|
|
|
chmod 0700 .
|
2021-09-08 08:01:18 -07:00
|
|
|
${cfg.generateSecretsScript}
|
2021-03-10 05:08:34 -08:00
|
|
|
''}
|
|
|
|
|
2019-11-27 05:04:27 -08:00
|
|
|
setupSecret() {
|
2021-08-15 02:28:35 -07:00
|
|
|
file="$1"
|
|
|
|
user="$2"
|
|
|
|
group="$3"
|
|
|
|
permissions="$4"
|
|
|
|
if [[ ! -e $file ]]; then
|
|
|
|
echo "Error: Secret file '$file' is missing"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
chown "$user:$group" "$file"
|
|
|
|
chmod "$permissions" "$file"
|
|
|
|
processedFiles+=("$file")
|
2019-11-27 05:04:27 -08:00
|
|
|
}
|
|
|
|
|
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=()
|
2021-03-10 05:08:34 -08:00
|
|
|
${
|
|
|
|
concatStrings (mapAttrsToList (n: v: ''
|
2022-09-05 08:45:45 -07:00
|
|
|
setupSecret ${n} ${v.user} ${v.group} ${v.permissions}
|
2021-03-10 05:08:34 -08:00
|
|
|
'') cfg.secrets)
|
|
|
|
}
|
2019-11-27 05:04:27 -08:00
|
|
|
|
|
|
|
# Make all other files accessible to root only
|
2021-10-02 02:49:49 -07:00
|
|
|
unprocessedFiles=$(
|
|
|
|
comm -23 <(shopt -s nullglob; printf '%s\n' *) <(printf '%s\n' "''${processedFiles[@]}")
|
|
|
|
)
|
2021-09-08 08:01:18 -07:00
|
|
|
if [[ $unprocessedFiles ]]; then
|
|
|
|
IFS=$'\n'
|
2022-09-05 08:45:45 -07:00
|
|
|
# shellcheck disable=SC2086
|
2021-09-08 08:01:18 -07:00
|
|
|
chown root: $unprocessedFiles
|
2022-09-05 08:45:45 -07:00
|
|
|
# shellcheck disable=SC2086
|
2021-09-08 08:01:18 -07:00
|
|
|
chmod 0440 $unprocessedFiles
|
|
|
|
fi
|
2019-11-27 05:04:27 -08:00
|
|
|
|
|
|
|
# Now make the secrets dir accessible to other users
|
|
|
|
chmod 0751 "$dir"
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|