id: cloudflare-rate-limiting-abuse-threshold-sentinel
namespace: company.security
description: |
Audits Cloudflare Zone security analytics via GraphQL API, detects anomalous
surges in HTTP 429 rate-limited client requests, opens an automated GitHub
SecOps abuse investigation ticket, and alerts the security response team on Slack.
inputs:
- id: zone_id
type: STRING
displayName: Cloudflare Zone ID
defaults: "023e105f4ecef8ad9ca31a8372d0c353"
- id: rate_limit_threshold
type: INT
displayName: 429 Spike Alert Threshold
defaults: 500
- id: target_repository
type: STRING
displayName: Target Security Remediation Repository
defaults: my-org/cloudflare-security-operations
triggers:
- id: check_rate_limits_hourly
type: io.kestra.plugin.core.trigger.Schedule
cron: "0 * * * *"
disabled: true
tasks:
- id: probe_rate_limit_events
type: io.kestra.plugin.scripts.python.Script
description: Interrogates Cloudflare GraphQL API for zone
httpRequestsAdaptiveGroups with edgeResponseStatus 429
containerImage: python:3.11-slim
beforeCommands:
- pip install requests kestra
script: |
import os
import requests
from datetime import datetime, timezone, timedelta
from kestra import Kestra
zone_id = "{{ inputs.zone_id }}"
token = os.environ.get("CLOUDFLARE_API_TOKEN")
threshold = int("{{ inputs.rate_limit_threshold }}")
now = datetime.now(timezone.utc)
one_hour_ago = now - timedelta(hours=1)
start_str = one_hour_ago.strftime("%Y-%m-%dT%H:%M:%SZ")
end_str = now.strftime("%Y-%m-%dT%H:%M:%SZ")
query = """
query GetRateLimitSpikes($zoneTag: String!, $start: String!, $end: String!) {
viewer {
zones(filter: {zoneTag: $zoneTag}) {
httpRequestsAdaptiveGroups(
filter: {
datetime_geq: $start,
datetime_leq: $end,
edgeResponseStatus: 429
},
limit: 10
) {
count
dimensions {
clientCountryName
}
}
}
}
}
"""
variables = {
"zoneTag": zone_id,
"start": start_str,
"end": end_str
}
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
url = "https://api.cloudflare.com/client/v4/graphql"
try:
resp = requests.post(url, headers=headers, json={"query": query, "variables": variables}, timeout=30)
data = resp.json()
except Exception as e:
print(f"GraphQL request error: {e}")
raise
zones = data.get("data", {}).get("viewer", {}).get("zones", [])
total_429s = 0
country_breakdown = []
if zones:
groups = zones[0].get("httpRequestsAdaptiveGroups", [])
for g in groups:
c = g.get("count", 0)
total_429s += c
country = g.get("dimensions", {}).get("clientCountryName", "Unknown")
country_breakdown.append(f"{country}: {c}")
is_abuse_surge = total_429s >= threshold
print(f"Zone {zone_id}: {total_429s} rate-limited 429 requests (Threshold: {threshold})")
Kestra.outputs({
"zone_id": zone_id,
"total_429_count": total_429s,
"threshold": threshold,
"is_abuse_surge": is_abuse_surge,
"country_breakdown": country_breakdown
})
- id: branch_on_rate_limit_surge
type: io.kestra.plugin.core.flow.If
description: Branches based on whether rate-limited 429 responses exceeded the
abuse threshold
condition: "{{ outputs.probe_rate_limit_events.vars.is_abuse_surge == true }}"
then:
- id: create_abuse_incident_issue
type: io.kestra.plugin.github.issues.Create
description: Logs a prioritized GitHub issue documenting Cloudflare
rate-limiting surges
oauthToken: "{{ secret('GITHUB_TOKEN') }}"
repository: "{{ inputs.target_repository }}"
title: "[Security Alert] Cloudflare 429 Rate-Limiting Surge on Zone: {{
inputs.zone_id }}"
body: |
## 🚨 Cloudflare API Abuse / Rate-Limiting Surge Detected
Automated edge security sentinel identified an elevated volume of HTTP 429 rate-limited client requests:
- **Cloudflare Zone ID:** `{{ inputs.zone_id }}`
- **Total 429 Requests (1h):** **{{ outputs.probe_rate_limit_events.vars.total_429_count }}**
- **Threshold:** `{{ inputs.rate_limit_threshold }}`
### Top Geo Distribution:
{% for item in outputs.probe_rate_limit_events.vars.country_breakdown %}
- {{ item }}
{% endfor %}
Please verify whether this reflects an active credential stuffing attack, scraping botnet, or misconfigured API partner client.
*Generated automatically by Kestra Cloudflare Security Sentinel.*
- id: alert_secops_slack
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Alerts SecOps channel on Slack with spike metrics and GitHub issue
hyperlink
url: "{{ secret('SLACK_WEBHOOK') }}"
payload: |
{{ {
"text": "🛡️ *Cloudflare Rate-Limiting Abuse Surge Alert*\n*Zone:* `" ~ inputs.zone_id ~ "`\n*429 Requests (1h):* *" ~ outputs.probe_rate_limit_events.vars.total_429_count ~ "* (Threshold: `" ~ inputs.rate_limit_threshold ~ "`)\n*Incident Ticket:* <" ~ (outputs.create_abuse_incident_issue.issueUrl ?? "#") ~ "|View GitHub Issue>"
} | toJson }}
else:
- id: log_traffic_normal
type: io.kestra.plugin.core.log.Log
message: "Cloudflare zone {{ inputs.zone_id }} rate-limited traffic is within
normal operating limits ({{
outputs.probe_rate_limit_events.vars.total_429_count }} < {{
inputs.rate_limit_threshold }})."
- id: log_audit_complete
type: io.kestra.plugin.core.log.Log
description: Final status log recording Cloudflare edge rate-limit inspection
message: "Cloudflare security audit completed for zone {{ inputs.zone_id }}.
Total 429s: {{ outputs.probe_rate_limit_events.vars.total_429_count }}."
errors:
- id: alert_flow_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Alerts Slack when Cloudflare GraphQL query or issue creation
encounters an error
url: "{{ secret('SLACK_WEBHOOK') }}"
payload: |
{{ {
"text": "⚠️ *Cloudflare Rate-Limit Sentinel Failed* in execution `" ~ execution.id ~ "`. Unable to query GraphQL API for zone `" ~ inputs.zone_id ~ "`."
} | toJson }}