Skip to content

Instantly share code, notes, and snippets.

@rayjanoka
Created May 10, 2025 00:56
Show Gist options
  • Save rayjanoka/49035e2360a2de5ed78285e7922ecd29 to your computer and use it in GitHub Desktop.
Save rayjanoka/49035e2360a2de5ed78285e7922ecd29 to your computer and use it in GitHub Desktop.
Test TXT Records - Show Issue with content vs content.exact
#!/bin/bash
# Cloudflare DNS TXT Record Test Script
# This script tests Cloudflare's ability to store and retrieve TXT records with various content formats
# Configuration - Replace these with your actual values
CLOUDFLARE_API_TOKEN="${CLOUDFLARE_API_TOKEN:-YOUR_API_TOKEN}"
CLOUDFLARE_ZONE_ID="${CLOUDFLARE_ZONE_ID:-YOUR_ZONE_ID}"
DNS_RECORD_NAME="test-txt-registry.asdf.com"
# Color codes for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color
# Test values to try
declare -a TEST_VALUES=(
"heritage"
"heritage=hfvm"
"heritage=hfvm,hfvm/owner=dev"
"heritage=hfvm,hfvm/owner=dev,hfvm/resource=user"
)
# Function to create a TXT record
create_txt_record() {
local content="$1"
echo -e "${YELLOW}Creating TXT record with content: ${content}${NC}" >&2
# Prepare the JSON payload
payload=$(cat <<EOF
{
"type": "TXT",
"name": "${DNS_RECORD_NAME}",
"content": "${content}",
"ttl": 120
}
EOF
)
# Make the API call to create the record
response=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE_ID}/dns_records" \
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
-H "Content-Type: application/json" \
--data "${payload}")
# Check if the record was created successfully
success=$(echo "$response" | grep -o '"success":true')
if [ -n "$success" ]; then
record_id=$(echo "$response" | grep -o '"id":"[^"]*' | cut -d'"' -f4)
echo -e "${GREEN}✓ TXT record created successfully. ID: ${record_id}${NC}" >&2
echo "$record_id"
else
echo -e "${RED}✗ Failed to create TXT record${NC}" >&2
echo "$response" >&2
echo "" >&2
return 1
fi
}
# Function to retrieve a TXT record
get_txt_record() {
local content="$1"
local content_key="$2"
echo -e "${YELLOW}Retrieving TXT record with ${content_key} filter: ${content}${NC}" >&2
# Make the API call to get the record
response=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE_ID}/dns_records?type=TXT&name=${DNS_RECORD_NAME}&${content_key}=${content}" \
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
-H "Content-Type: application/json")
# Check if the record was retrieved successfully
count=$(echo "$response" | grep -o '"count":[0-9]*' | cut -d':' -f2)
if [ "$count" -gt 0 ]; then
echo -e "${GREEN}✓ TXT record retrieved successfully using ${content_key}${NC}" >&2
retrieved_content=$(echo "$response" | grep -o '"content":"[^"]*' | cut -d'"' -f4)
echo -e "Retrieved content: ${retrieved_content}" >&2
return 0
else
echo -e "${RED}✗ Failed to retrieve TXT record with ${content_key} filter${NC}" >&2
echo "$response" >&2
echo "" >&2
return 1
fi
}
# Function to delete a TXT record
delete_txt_record() {
local record_id="$1"
if [ -z "$record_id" ]; then
echo -e "${RED}Error: No record ID provided for deletion${NC}" >&2
return 1
fi
# Make the API call to delete the record (silently)
response=$(curl -s -X DELETE "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE_ID}/dns_records/${record_id}" \
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
-H "Content-Type: application/json")
# Check if the record was deleted successfully
success=$(echo "$response" | grep -o '"success":true')
if [ -n "$success" ]; then
# Success - don't log anything
return 0
else
echo -e "${RED}✗ Failed to delete TXT record with ID: ${record_id}${NC}" >&2
echo "$response" >&2
return 1
fi
}
# Function to list all TXT records for the domain
list_txt_records() {
echo -e "${YELLOW}Listing all TXT records for ${DNS_RECORD_NAME}${NC}" >&2
response=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE_ID}/dns_records?type=TXT&name=${DNS_RECORD_NAME}" \
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
-H "Content-Type: application/json")
echo "$response" | jq .
}
# Main test function
run_tests() {
echo "===================================================="
echo " Cloudflare DNS TXT Record Test"
echo " Testing record: ${DNS_RECORD_NAME}"
echo " Testing content vs content.exact filtering"
echo "===================================================="
# Print summary of test values
echo -e "\n${YELLOW}Content values to be tested:${NC}"
for i in "${!TEST_VALUES[@]}"; do
echo -e " ${i+1}. ${TEST_VALUES[$i]}"
done
echo -e "\n"
# Test each value
for value in "${TEST_VALUES[@]}"; do
echo -e "\n\n${YELLOW}===== Testing \"content\" value: ${value} =====${NC}"
# Create the TXT record
record_id=$(create_txt_record "$value")
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to create record, skipping test for: ${value}${NC}"
continue
fi
# Try to retrieve the record using content filter
echo -e "\n${YELLOW}--- CONTENT FILTER TEST ---${NC}"
get_txt_record "$value" "content"
content_result=$?
# Try to retrieve the record using content.exact filter
echo -e "\n${YELLOW}--- CONTENT.EXACT FILTER TEST ---${NC}"
get_txt_record "$value" "content.exact"
content_exact_result=$?
# Compare results
if [ $content_result -eq 0 ] && [ $content_exact_result -eq 0 ]; then
echo -e "${GREEN}✓ Both content and content.exact filters worked for: ${value}${NC}"
elif [ $content_result -eq 0 ] && [ $content_exact_result -ne 0 ]; then
echo -e "${YELLOW}⚠ Only content filter worked for: ${value}${NC}"
elif [ $content_result -ne 0 ] && [ $content_exact_result -eq 0 ]; then
echo -e "${YELLOW}⚠ Only content.exact filter worked for: ${value}${NC}"
else
echo -e "${RED}✗ Neither filter worked for: ${value}${NC}"
fi
# Delete the record
delete_txt_record "$record_id"
echo -e "\n${YELLOW}===== Completed testing: ${value} =====${NC}"
done
echo -e "\n===================================================="
echo " Test completed"
echo "===================================================="
}
# Check if API token and Zone ID are set
if [ "$CLOUDFLARE_API_TOKEN" = "YOUR_API_TOKEN" ] || [ "$CLOUDFLARE_ZONE_ID" = "YOUR_ZONE_ID" ]; then
echo -e "${RED}Error: Please set your Cloudflare API token and Zone ID in the script or as environment variables.${NC}"
echo -e "${YELLOW}You can run the script with:${NC}"
echo -e "CLOUDFLARE_API_TOKEN=your_token CLOUDFLARE_ZONE_ID=your_zone_id ./test-txt-record.sh"
exit 1
fi
# Run the tests
run_tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment