Files
homelab/edge-cache/configure-token
T

30 lines
855 B
Bash
Executable File

#!/bin/sh
set -eu
EDGE_CACHE_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
EDGE_CACHE_TOKEN_FILE="$EDGE_CACHE_DIR/secrets/cloudflare_cache_api_token"
EDGE_CACHE_TOKEN_TMP="$EDGE_CACHE_TOKEN_FILE.tmp.$$"
if [ ! -t 0 ]; then
echo "Refusing to read a Cloudflare token from a non-interactive stdin." >&2
exit 1
fi
printf 'Cloudflare Cache Rules API token: ' > /dev/tty
stty -echo < /dev/tty
IFS= read -r EDGE_CACHE_TOKEN < /dev/tty || true
stty echo < /dev/tty
printf '\n' > /dev/tty
if [ "${#EDGE_CACHE_TOKEN}" -lt 20 ]; then
echo "Token is missing or unexpectedly short." >&2
exit 1
fi
umask 077
printf '%s\n' "$EDGE_CACHE_TOKEN" > "$EDGE_CACHE_TOKEN_TMP"
mv "$EDGE_CACHE_TOKEN_TMP" "$EDGE_CACHE_TOKEN_FILE"
unset EDGE_CACHE_TOKEN
echo "Token installed with mode 600. The running controller will sync the managed Cache Rule automatically."