# Garage (S3) for the apps, single node. Buckets, keys and grants are set # on every start from sops. # # asxpio-invoices asxpio rw # datacenterbingo-photos asxpio rw, bingo ro # public a website (anonymous reads through Traefik); # asxpio and public-upload write only # # Garage only takes keys in its own format (GK + 24 hex, 64 hex). The # apps' keys predate it and are hashed into that format, here and in each # app's deploy workflow: keep the two in step. { config, lib, pkgs, ... }: let garage = pkgs.garage_2; rpcSecret = config.sops.secrets."garage/rpc_secret".path; keys = [ "asxpio" "bingo" "public" ]; in { sops.secrets = { "garage/rpc_secret" = { }; } // lib.listToAttrs ( lib.concatMap (k: [ # A changed key is imported by restarting garage-bootstrap (lib.nameValuePair "garage/${k}_access_key" { restartUnits = [ "garage-bootstrap.service" ]; }) (lib.nameValuePair "garage/${k}_secret_key" { restartUnits = [ "garage-bootstrap.service" ]; }) ]) keys ); services.garage = { enable = true; package = garage; # The RPC secret is a systemd credential: mode 0440, but in a # directory only this service can enter extraEnvironment.GARAGE_ALLOW_WORLD_READABLE_SECRETS = "true"; settings = { # One node and no replica to repair from, so prefer the engine that # survives an unclean stop, sync writes, and keep metadata snapshots db_engine = "sqlite"; metadata_fsync = true; data_fsync = true; metadata_auto_snapshot_interval = "6h"; replication_factor = 1; rpc_bind_addr = "[::]:3901"; rpc_public_addr = "127.0.0.1:3901"; rpc_secret_file = "/run/credentials/garage.service/rpc_secret"; # The apps sign for us-east-1, as they did against MinIO s3_api = { s3_region = "us-east-1"; api_bind_addr = "[::]:3900"; }; # Anonymous reads: a bucket marked as a website is served here, # picked by Host: .web.garage s3_web = { bind_addr = "[::]:3902"; root_domain = ".web.garage"; index = "index.html"; }; }; }; asxpio.waitForData = [ "garage" "garage-bootstrap" ]; systemd.services.garage = { serviceConfig = { LoadCredential = [ "rpc_secret:${rpcSecret}" ]; ExecStart = lib.mkForce "${garage}/bin/garage server --single-node"; }; }; asxpio.security.ports = [ "tcp/3900-3902/garage" ]; # `garage` for root, e.g. garage status, garage bucket list environment.systemPackages = [ (pkgs.writeShellScriptBin "garage" '' export GARAGE_RPC_SECRET_FILE=${rpcSecret} exec ${garage}/bin/garage -c /etc/garage.toml "$@" '') ]; systemd.services.garage-bootstrap = { description = "Garage buckets, keys and grants"; after = [ "garage.service" ]; requires = [ "garage.service" ]; wantedBy = [ "multi-user.target" ]; path = [ garage pkgs.gnugrep pkgs.coreutils ]; environment.GARAGE_RPC_SECRET_FILE = rpcSecret; serviceConfig = { Type = "oneshot"; RemainAfterExit = true; }; script = '' set -eu g() { garage -c /etc/garage.toml "$@"; } secret() { cat "/run/secrets/garage/$1"; } key_id() { if printf '%s' "$1" | grep -Eq '^GK[0-9a-f]{24}$'; then printf '%s' "$1" else printf 'GK%s' "$(printf '%s' "$1" | sha256sum | cut -c1-24)"; fi } key_secret() { if printf '%s' "$1" | grep -Eq '^[0-9a-f]{64}$'; then printf '%s' "$1" else printf '%s' "$1" | sha256sum | cut -c1-64; fi } i=0 until g status >/dev/null 2>&1; do i=$((i + 1)) [ "$i" -le 60 ] || { echo "garage did not come up" >&2; exit 1; } sleep 1 done ensure_bucket() { g bucket info "$1" >/dev/null 2>&1 || g bucket create "$1" >/dev/null; } # ensure_key : prints the key id ensure_key() { id="$(key_id "$(secret "$2_access_key")")" sec="$(key_secret "$(secret "$2_secret_key")")" if g key info --show-secret "$id" >/dev/null 2>&1; then if ! g key info --show-secret "$id" | grep -q "$sec"; then g key delete --yes "$id" >/dev/null g key import --yes -n "$1" "$id" "$sec" >/dev/null fi else g key import --yes -n "$1" "$id" "$sec" >/dev/null fi printf '%s' "$id" } grant() { case "$3" in rw) g bucket allow --read --write "$1" --key "$2" >/dev/null g bucket deny --owner "$1" --key "$2" >/dev/null ;; ro) g bucket allow --read "$1" --key "$2" >/dev/null g bucket deny --write --owner "$1" --key "$2" >/dev/null ;; wo) g bucket allow --write "$1" --key "$2" >/dev/null g bucket deny --read --owner "$1" --key "$2" >/dev/null ;; esac } ensure_bucket asxpio-invoices ensure_bucket datacenterbingo-photos ensure_bucket public g bucket website --allow public >/dev/null asxpio="$(ensure_key asxpio asxpio)" grant asxpio-invoices "$asxpio" rw grant datacenterbingo-photos "$asxpio" rw grant public "$asxpio" wo bingo="$(ensure_key bingo bingo)" grant datacenterbingo-photos "$bingo" ro uploader="$(ensure_key public-upload public)" grant public "$uploader" wo ''; }; asxpio.backup.paths = [ "/var/lib/garage" ]; asxpio.backup.hooks = [ "/run/current-system/sw/bin/garage meta snapshot --all" ]; }