nixfiles/hosts/hetzner-arm/containers/storage/secrets.nix

115 lines
2.9 KiB
Nix
Raw Normal View History

{pkgs, ...}: {
2022-11-15 14:52:49 +00:00
services.secrets = {
enable = true;
2023-09-18 03:56:58 +01:00
vaultLogin = {
enable = true;
loginUsername = "hetzner-arm-container-storage";
2023-09-18 03:56:58 +01:00
};
requiredVaultPaths = [
2024-11-08 20:50:43 +00:00
"api-keys/data/backblaze/Backblaze"
#"api-keys/data/storage/webdav/Main"
"api-keys/data/storage/webdav/Public"
"api-keys/data/storage/webdav/Uploads"
2024-04-26 19:55:22 +01:00
"api-keys/data/storage/webdav/Notes"
"private-public-keys/data/rclone/Chaos-Notes-Crypt"
2023-09-18 03:56:58 +01:00
];
packages = with pkgs; [
2022-11-15 14:52:49 +00:00
# for music & mail passwd files
apacheHttpd
# for rclone obscure config file
rclone
];
extraFunctions = ''
2022-11-29 10:08:44 +00:00
replace_slash_for_sed() {
sed "s#/#\\\/#"
}
2022-11-15 14:52:49 +00:00
simple_get_obscure() {
rclone obscure "$(simple_get "$@")"
2022-11-02 11:32:03 +00:00
}
2022-11-15 14:52:49 +00:00
simple_get_replace_b2() {
2022-11-29 10:08:44 +00:00
api_account=$(simple_get "$1" .keyID | replace_slash_for_sed)
api_key=$(simple_get "$1" .applicationKey | replace_slash_for_sed)
2022-11-15 14:52:49 +00:00
replace_account=''${2}_ACCOUNT
replace_key=''${2}_KEY
sed -i "s/$replace_account/$api_account/" "$3"
sed -i "s/$replace_key/$api_key/" "$3"
2022-11-02 11:32:03 +00:00
}
2022-11-29 10:08:44 +00:00
simple_get_replace_crypt() {
password=$(simple_get_obscure "$1" .password)
salt=$(simple_get_obscure "$1" .salt)
2022-11-29 10:08:44 +00:00
replace_password=''${2}_PASSWORD
replace_salt=''${2}_SALT
2022-11-29 10:08:44 +00:00
sed -i "s/$replace_password/$password/" "$3"
sed -i "s/$replace_salt/$salt/" "$3"
}
simple_get_htpasswd() {
if [ -f "$2" ]; then
rm "$2"
fi
touch "$2"
data=$(kv_get "$1" | base64)
for username in $(echo "$data" | base64 -d | jq -r ".data.data | keys | .[]"); do
password=$(echo "$data" | base64 -d | jq -r ".data.data.\"$username\"")
htpasswd -b "$2" "$username" "$password" 2>/dev/null
done
}
2022-11-15 14:52:49 +00:00
'';
secrets = {
vault_password = {
manual = true;
};
2022-11-15 14:52:49 +00:00
2024-11-08 21:01:43 +00:00
#webdav_main_htpasswd = {
# user = "storage";
# group = "storage";
# fetchScript = ''
# simple_get_htpasswd "/api-keys/storage/webdav/Main" "$secretFile"
# '';
#};
webdav_uploads_htpasswd = {
user = "storage";
group = "storage";
fetchScript = ''
simple_get_htpasswd "/api-keys/storage/webdav/Uploads" "$secretFile"
2022-11-20 10:34:55 +00:00
'';
};
2024-04-26 19:55:22 +01:00
webdav_notes_htpasswd = {
user = "storage";
group = "storage";
fetchScript = ''
simple_get_htpasswd "/api-keys/storage/webdav/Notes" "$secretFile"
'';
};
2022-11-15 14:52:49 +00:00
rclone_config = {
user = "storage";
group = "storage";
fetchScript = ''
2024-02-08 20:21:35 +00:00
cp ${./data/rclone_config.template} "$secretFile"
2022-11-15 14:52:49 +00:00
2024-11-08 20:50:43 +00:00
simple_get_replace_b2 "/api-keys/backblaze/Backblaze" "B2" "$secretFile"
2024-04-26 19:55:22 +01:00
simple_get_replace_crypt "/private-public-keys/rclone/Chaos-Notes-Crypt" "STORAGE_NOTES_CRYPT" "$secretFile"
2022-11-15 14:52:49 +00:00
'';
};
};
};
2022-11-02 11:32:03 +00:00
}