Skip to content

Instantly share code, notes, and snippets.

@shagamemnon
Created January 21, 2020 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shagamemnon/f3aecce00e192cfd9282dc7dc2bd1ee8 to your computer and use it in GitHub Desktop.
Save shagamemnon/f3aecce00e192cfd9282dc7dc2bd1ee8 to your computer and use it in GitHub Desktop.
Enable log file retention for a list of Cloudflare (Enterprise) zones.
#!/bin/sh
# Enable log retention for a list of Cloudflare zones
AUTH_EMAIL="xxxx@example.com" # the account you use to
AUTH_KEY="xxxxxxxxxxxx" # https://dash.cloudflare.com/profile/api-tokens --> "Global API Key"
ZONE_NAMES="franktaylor.io cfiq.io valleynet.io" # space-separated list of zone names
for ZONE in $ZONE_NAMES
do
printf "\n"
ZONE_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$ZONE" -H "X-Auth-Email: $AUTH_EMAIL" -H "X-Auth-Key: $AUTH_KEY" -H "Content-Type: application/json" | sed 's/,/\n/g' | awk -F'"' '/id/{print $6}' | head -1)
get_log_retention_setting=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/logs/control/retention/flag" -H "X-Auth-Email: $AUTH_EMAIL" -H "X-Auth-Key: $AUTH_KEY" -H "Content-Type: application/json")
current_log_retention_setting=$(echo "$get_log_retention_setting")
if [[ -z ${current_log_retention_setting##*'"success":true'*} ]]; then
printf "Log retention already enabled for $ZONE\n:"
printf "$current_log_retention_setting\n\n"
else
update=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/logs/control/retention/flag" -H "X-Auth-Email: $AUTH_EMAIL" -H "X-Auth-Key: $AUTH_KEY" -H "Content-Type: application/json" --data "{\"flag\":true}")
printf "$update\n"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment