Skip to content

Instantly share code, notes, and snippets.

@anapsix
Created May 13, 2020 18:57
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 anapsix/3d91a58678f3da274b225616da378bf7 to your computer and use it in GitHub Desktop.
Save anapsix/3d91a58678f3da274b225616da378bf7 to your computer and use it in GitHub Desktop.
Generate Terraform statements for cloudflare_filter, and cloudflare_firewall_rule
##############################################################################
# this JQ script parses Cloudflare API call listing Firewall Rules
# and generates cloudflare_filter_and cloudflare_firewall_rule
##############################################################################
# Copyright (c) 2020 Anastas Dancha (@anapsix)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
##############################################################################
## Step 1: create bash alias
#
# list_firewall_rules() {
# local _zone="${1}"
# curl \
# -sS \
# -X GET "https://api.cloudflare.com/client/v4/zones/${_zone}/firewall/rules" \
# -H "X-Auth-Email: ${CF_EMAIL}" \
# -H "X-Auth-Key: ${CF_TOKEN}"
# }
#
##############################################################################
## Step 2: run with following
#
# list_firewall_rules ${ZONE_ID} | jq -r -f ./generate_firewall_rules.jq
#
##############################################################################
# NOTE: you'll need CF_EMAIL and CF_TOKEN exported as environment variables
##############################################################################
.result | sort_by(.priority) | .[] | "## \(.description)
resource \"cloudflare_filter\" \"filter_\(.filter.id)\" {
zone_id = var.zone_id
description = \"\"
expression = <<-EOT
\(.filter.expression)
EOT
}
resource \"cloudflare_firewall_rule\" \"firewall_rule_\(.id)\" {
zone_id = var.zone_id
description = \"\(.description)\"
filter_id = \"\(.filter.id)\"
action = \"\(.action)\"
priority = \(.priority)
paused = \(.paused)
}
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment