Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
carlssonk committed Oct 2, 2024
1 parent 2f1e610 commit 3235f61
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 53 deletions.
35 changes: 27 additions & 8 deletions modules/cloudflare/cloudflare_ruleset.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
#!/bin/bash

# Function to update zone settings
response=$(curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/settings" \
# Cloudflare API endpoint
API_ENDPOINT="https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/rulesets"

# Function to create or update ruleset
method="POST"
endpoint="${API_ENDPOINT}"

# Check if ruleset already exists
existing_ruleset=$(curl -s -X GET "${API_ENDPOINT}?phase=${PHASE}" \
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
-H "Content-Type: application/json")

if echo "$existing_ruleset" | jq -e '.result[0]' > /dev/null; then
ruleset_id=$(echo "$existing_ruleset" | jq -r '.result[0].id')
method="PUT"
endpoint="${API_ENDPOINT}/${ruleset_id}"
fi

# Send request to create or update ruleset
response=$(curl -s -X $method "$endpoint" \
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
-H "Content-Type: application/json" \
--data "{
\"items\": [
{\"id\": \"ssl\", \"value\": \"${SSL}\"},
{\"id\": \"always_use_https\", \"value\": \"${ALWAYS_USE_HTTPS}\"}
]
\"name\": \"Dynamic Main Ruleset\",
\"description\": \"Dynamic ruleset for managing app settings\",
\"kind\": \"${KIND}\",
\"phase\": \"${PHASE}\",
\"rules\": ${RULESET_RULES}
}")

if echo "$response" | grep -q '"success":true'; then
echo "Successfully updated settings for zone ${ZONE_ID}"
echo "Successfully managed ruleset for zone ${ZONE_ID}"
else
echo "Failed to update settings for zone ${ZONE_ID}"
echo "Failed to manage ruleset for zone ${ZONE_ID}"
echo "Response: $response"
fi
51 changes: 8 additions & 43 deletions modules/cloudflare/cloudflare_zone_settings_override.sh
Original file line number Diff line number Diff line change
@@ -1,54 +1,19 @@
#!/bin/bash

# Cloudflare API endpoint
API_ENDPOINT="https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/rulesets"

# Function to create or update ruleset
method="POST"
endpoint="${API_ENDPOINT}"

# Check if ruleset already exists
existing_ruleset=$(curl -s -X GET "${API_ENDPOINT}?phase=${PHASE}" \
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
-H "Content-Type: application/json")

if echo "$existing_ruleset" | jq -e '.result[0]' > /dev/null; then
ruleset_id=$(echo "$existing_ruleset" | jq -r '.result[0].id')
method="PUT"
endpoint="${API_ENDPOINT}/${ruleset_id}"
fi

# Prepare the rules JSON
rules_json=$(echo $RULESET_RULES | jq -c '
[.[] | {
action: .action,
action_parameters: (
if .action_parameters.ssl != null then
{ssl: .action_parameters.ssl}
else
{}
end
),
expression: .expression,
description: .description
}]
')

# Send request to create or update ruleset
response=$(curl -s -X $method "$endpoint" \
# Function to update zone settings
response=$(curl -s -X PATCH "https://api.cloudflare.com/client/v4/zones/${ZONE_ID}/settings" \
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
-H "Content-Type: application/json" \
--data "{
\"name\": \"Dynamic Main Ruleset\",
\"description\": \"Dynamic ruleset for managing app settings\",
\"kind\": \"${KIND}\",
\"phase\": \"${PHASE}\",
\"rules\": ${rules_json}
\"items\": [
{\"id\": \"ssl\", \"value\": \"${SSL}\"},
{\"id\": \"always_use_https\", \"value\": \"${ALWAYS_USE_HTTPS}\"}
]
}")

if echo "$response" | grep -q '"success":true'; then
echo "Successfully managed ruleset for zone ${ZONE_ID}"
echo "Successfully updated settings for zone ${ZONE_ID}"
else
echo "Failed to manage ruleset for zone ${ZONE_ID}"
echo "Failed to update settings for zone ${ZONE_ID}"
echo "Response: $response"
fi
4 changes: 2 additions & 2 deletions modules/cloudflare/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ resource "null_resource" "cloudflare_zone_settings_override" {
for_each = local.apps_grouped_by_root_domain

triggers = {
cloudflare_api_token = var.cloudflare_api_token
zone_id = data.cloudflare_zone.domain[each.key].id
cloudflare_api_token = var.cloudflare_api_token
ssl = "full"
always_use_https = "on"
}
Expand Down Expand Up @@ -103,7 +103,7 @@ resource "null_resource" "cloudflare_ruleset" {
zone_id = data.cloudflare_zone.domain[each.key].id
kind = "zone"
phase = "http_config_settings"
ruleset_rules = local.ruleset_rules
ruleset_rules = jsonencode(local.ruleset_rules[each.key])
}

provisioner "local-exec" {
Expand Down

0 comments on commit 3235f61

Please sign in to comment.