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

133 lines
4.3 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 = [
"api-keys/data/backblaze/Chaos-Backups"
"api-keys/data/backblaze/Chaos-Photos"
"api-keys/data/backblaze/Chaos-Music"
"api-keys/data/backblaze/Chaos-Personal"
"api-keys/data/backblaze/Chaos-Public"
"api-keys/data/backblaze/Chaos-Media"
2024-04-28 11:54:10 +01:00
"api-keys/data/backblaze/Chaos-Notes"
"api-keys/data/backblaze/Chaos-Uploads"
2023-09-18 03:56:58 +01:00
"api-keys/data/backblaze/Phoenix-Cryptidz-Storage"
"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"
2023-09-18 03:56:58 +01:00
"private-public-keys/data/rclone/Chaos-Media-Crypt"
"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
webdav_main_htpasswd = {
user = "storage";
group = "storage";
fetchScript = ''
simple_get_htpasswd "/api-keys/storage/webdav/Main" "$secretFile"
2022-11-15 14:52:49 +00:00
'';
};
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
simple_get_replace_b2 "/api-keys/backblaze/Chaos-Media" "B2_CHAOS_MEDIA" "$secretFile"
simple_get_replace_b2 "/api-keys/backblaze/Chaos-Backups" "B2_CHAOS_BACKUPS" "$secretFile"
simple_get_replace_b2 "/api-keys/backblaze/Chaos-Photos" "B2_CHAOS_PHOTOS" "$secretFile"
simple_get_replace_b2 "/api-keys/backblaze/Chaos-Music" "B2_CHAOS_MUSIC" "$secretFile"
simple_get_replace_b2 "/api-keys/backblaze/Chaos-Personal" "B2_CHAOS_PERSONAL" "$secretFile"
simple_get_replace_b2 "/api-keys/backblaze/Chaos-Public" "B2_CHAOS_PUBLIC" "$secretFile"
2024-04-28 11:54:10 +01:00
simple_get_replace_b2 "/api-keys/backblaze/Chaos-Notes" "B2_CHAOS_NOTES" "$secretFile"
simple_get_replace_b2 "/api-keys/backblaze/Chaos-Uploads" "B2_CHAOS_UPLOADS" "$secretFile"
simple_get_replace_b2 "/api-keys/backblaze/Phoenix-Cryptidz-Storage" "B2_PHOENIX_CRYPTIDZ_STORAGE" "$secretFile"
2022-11-15 14:52:49 +00:00
simple_get_replace_crypt "/private-public-keys/rclone/Chaos-Media-Crypt" "STORAGE_MEDIA_CRYPT" "$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
}