diff --git a/deploy/scripts/cf-cache-rules-apply.sh b/deploy/scripts/cf-cache-rules-apply.sh index 8535199..cc963d5 100755 --- a/deploy/scripts/cf-cache-rules-apply.sh +++ b/deploy/scripts/cf-cache-rules-apply.sh @@ -4,7 +4,14 @@ # Usage: CF_API_TOKEN= bash deploy/scripts/cf-cache-rules-apply.sh # # Requires: CF_API_TOKEN with Cache Rules write permission for andebol.net -# and newsproject.bitreport.pt zones. +# and bitreport.pt zones. +# +# Notes discovered during BIT-21922: +# - newsproject.bitreport.pt is a subdomain of zone bitreport.pt (not its own zone) +# - CF API endpoint is /entrypoint (singular), not /entrypoints +# - starts_with operator not supported on Free plan; use contains +# - edge_ttl override_origin required to cache HTML (origin_cache_control alone is insufficient) +# - Verification must use GET requests (not HEAD/-I); rule expression matches GET only set -euo pipefail : "${CF_API_TOKEN:?CF_API_TOKEN must be set (Cloudflare API token with Cache Rules write)}" @@ -31,18 +38,6 @@ upsert_cache_rule() { local zone_name=$3 local rule_name="News platform — cache HTML (respect origin)" - # Fetch existing entrypoint ruleset - local existing - existing=$(curl -fsS "$CF_API/zones/$zone_id/rulesets/phases/http_request_cache_settings/entrypoints" \ - -H "Authorization: Bearer $CF_API_TOKEN" 2>/dev/null || echo '{"result":{}}') - local ruleset_id - ruleset_id=$(echo "$existing" | python3 -c " -import sys, json -d = json.load(sys.stdin) -r = d.get('result', {}) -print(r.get('id', '')) -" 2>/dev/null || echo "") - local new_rule new_rule=$(python3 -c " import json, sys @@ -50,7 +45,7 @@ rule = { 'action': 'set_cache_settings', 'action_parameters': { 'cache': True, - 'origin_cache_control': {'enabled': True} + 'edge_ttl': {'mode': 'override_origin', 'default': 60} }, 'expression': sys.argv[1], 'description': sys.argv[2], @@ -59,67 +54,82 @@ rule = { print(json.dumps(rule)) " "$expr" "$rule_name") + # Fetch existing entrypoint ruleset (returns 200 with result:null if none exists) + local existing + existing=$(curl -sS "$CF_API/zones/$zone_id/rulesets/phases/http_request_cache_settings/entrypoint" \ + -H "Authorization: Bearer $CF_API_TOKEN" 2>/dev/null || echo '{"result":null}') + local ruleset_id + ruleset_id=$(echo "$existing" | python3 -c " +import sys, json +d = json.load(sys.stdin) +r = d.get('result') or {} +print(r.get('id', '')) +" 2>/dev/null || echo "") + + local payload if [ -n "$ruleset_id" ]; then - # Append the new rule to the existing ruleset + # Update existing ruleset: dedup by description then append local existing_rules existing_rules=$(echo "$existing" | python3 -c " import sys, json d = json.load(sys.stdin) -print(json.dumps(d.get('result', {}).get('rules', []))) +print(json.dumps((d.get('result') or {}).get('rules', []))) ") - local payload payload=$(python3 -c " import json, sys existing = json.loads(sys.argv[1]) new_rule = json.loads(sys.argv[2]) -# Remove any prior rule with same description to avoid duplicates rules = [r for r in existing if r.get('description') != new_rule['description']] rules.append(new_rule) print(json.dumps({'rules': rules})) " "$existing_rules" "$new_rule") - curl -fsS -X PATCH "$CF_API/zones/$zone_id/rulesets/$ruleset_id" \ + curl -fsS -X PUT "$CF_API/zones/$zone_id/rulesets/$ruleset_id" \ -H "Authorization: Bearer $CF_API_TOKEN" \ -H "Content-Type: application/json" \ - -d "$payload" | python3 -m json.tool + --data-raw "$payload" | python3 -m json.tool else - curl -fsS -X PUT "$CF_API/zones/$zone_id/rulesets/phases/http_request_cache_settings/entrypoints" \ + # Create new entrypoint ruleset + payload=$(python3 -c " +import json, sys +print(json.dumps({'rules': [json.loads(sys.argv[1])]})) +" "$new_rule") + curl -fsS -X PUT "$CF_API/zones/$zone_id/rulesets/phases/http_request_cache_settings/entrypoint" \ -H "Authorization: Bearer $CF_API_TOKEN" \ -H "Content-Type: application/json" \ - -d "$(python3 -c "import json,sys; print(json.dumps({'rules': [json.loads(sys.argv[1])]}))" "$new_rule")" \ - | python3 -m json.tool + --data-raw "$payload" | python3 -m json.tool fi echo "Cache rule applied for $zone_name (zone $zone_id)" } echo "==> Resolving zone IDs..." ZONE_ANDEBOL=$(get_zone_id "andebol.net") -echo " andebol.net : $ZONE_ANDEBOL" -ZONE_NEWS=$(get_zone_id "newsproject.bitreport.pt") -echo " newsproject.bitreport.pt : $ZONE_NEWS" +echo " andebol.net : $ZONE_ANDEBOL" +ZONE_NEWS=$(get_zone_id "bitreport.pt") +echo " newsproject.bitreport.pt: $ZONE_NEWS (zone: bitreport.pt)" echo "==> Applying cache rule for andebol.net..." -EXPR_ANDEBOL='(http.request.method eq "GET") and (http.host in {"andebol.net" "www.andebol.net"}) and not (http.request.uri.path contains "/wp-admin") and not (http.request.uri.path contains "/wp-login.php") and not (http.request.uri.path starts_with "/wp-json/") and not (http.request.uri.path starts_with "/api/")' +EXPR_ANDEBOL='(http.request.method eq "GET") and (http.host in {"andebol.net" "www.andebol.net"}) and not (http.request.uri.path contains "/wp-admin") and not (http.request.uri.path contains "/wp-login.php") and not (http.request.uri.path contains "/wp-json/") and not (http.request.uri.path contains "/api/")' upsert_cache_rule "$ZONE_ANDEBOL" "$EXPR_ANDEBOL" "andebol.net" echo "==> Applying cache rule for newsproject.bitreport.pt..." -EXPR_NEWS='(http.request.method eq "GET") and (http.host eq "newsproject.bitreport.pt") and not (http.request.uri.path contains "/wp-admin") and not (http.request.uri.path contains "/wp-login.php") and not (http.request.uri.path starts_with "/wp-json/") and not (http.request.uri.path starts_with "/api/")' +EXPR_NEWS='(http.request.method eq "GET") and (http.host eq "newsproject.bitreport.pt") and not (http.request.uri.path contains "/wp-admin") and not (http.request.uri.path contains "/wp-login.php") and not (http.request.uri.path contains "/wp-json/") and not (http.request.uri.path contains "/api/")' upsert_cache_rule "$ZONE_NEWS" "$EXPR_NEWS" "newsproject.bitreport.pt" echo "" -echo "==> Waiting 10 s for rule propagation..." -sleep 10 +echo "==> Waiting 30 s for rule propagation..." +sleep 30 -echo "==> Verification — double curl per zone:" +echo "==> Verification — double GET per zone (rule matches GET only, not HEAD):" for HOST in "https://andebol.net/" "https://newsproject.bitreport.pt/"; do echo "" echo "--- $HOST ---" for i in 1 2; do - echo -n " pass $i: " - curl -fsSI "$HOST" --max-time 15 \ - -w "cf-cache-status=%{header{cf-cache-status}} TTFB=%{time_starttransfer}s\n" \ - -o /dev/null 2>/dev/null + CF=$(curl -fsS "$HOST" --max-time 15 -D - -o /dev/null 2>/dev/null | grep -i "cf-cache-status:" | tr -d '\r') + AGE=$(curl -fsS "$HOST" --max-time 15 -D - -o /dev/null 2>/dev/null | grep -i "^age:" | tr -d '\r') + TTFB=$(curl -fsS "$HOST" --max-time 15 -D - -o /dev/null -w "%{time_starttransfer}" 2>/dev/null | tail -1) + echo " pass $i: $CF | $AGE | TTFB=${TTFB}s" done done echo "" -echo "Done. Expected: cf-cache-status=HIT (or STALE/REVALIDATED) on pass 2." -echo "If still DYNAMIC, wait 30 s and re-run the verification block only." +echo "Done. Expected: cf-cache-status=HIT on pass 2." +echo "If still MISS/DYNAMIC, wait 60 s and re-run the verification block."