Cloudflare API Authentication
Overview
This document explains the Cloudflare API authentication methods used in the Comserv2 application and how to resolve common authentication issues.
Issue
The Cloudflare API integration may fail with one of the following errors:
- IP Restriction Error: "Cannot use the access token from location: [IP address]" (Error code: 9109)
- Authentication Error: "Authentication error" (Error code: 10000)
- Unknown API Key Error: "Unknown X-Auth-Key or X-Auth-Email" (Error code: 9103)
These errors occur when:
- The API token has IP address restrictions that prevent it from being used from the server
- The API token doesn't have the necessary permissions to access the requested resources
- The API key is invalid or has expired
Solution
The Comserv2 application has been updated to handle these authentication issues in the following ways:
- Enhanced Error Handling: Improved error messages for authentication failures with specific guidance based on the error type
- Fallback Mechanism: When API authentication fails, the system will use cached or mock DNS records to ensure the application continues to function
- Hardcoded Zone IDs: For known domains, the system uses hardcoded zone IDs to reduce dependency on API authentication
Fallback Mechanism
When Cloudflare API authentication fails, the system will:
- First try to use cached DNS records if available
- If no cached records are available, use mock DNS records with realistic values
- Log the authentication failure for troubleshooting
This ensures that the application continues to function even when the Cloudflare API is unavailable or authentication fails.
Creating a New API Token
To create a new Cloudflare API token without IP restrictions:
- Log in to your Cloudflare account at https://dash.cloudflare.com/
- Click on your profile icon in the top right corner and select "My Profile"
- In the left sidebar, click on "API Tokens"
- Click the "Create Token" button
- Select the "Edit zone DNS" template or create a custom token with the following permissions:
- Zone > DNS > Edit
- Zone > Zone > Read
- Under "Zone Resources", select the specific zones (domains) you want to manage
- Leave the "Client IP Address Filtering" section blank to allow access from any IP address
- Click "Continue to summary" and then "Create Token"
- Copy the generated token and update the
cloudflare_config.json file
Troubleshooting
Testing API Authentication
You can test the Cloudflare API authentication using the provided test scripts:
# Test API token authentication
perl /home/shanta/PycharmProjects/comserv2/Comserv/script/test_cloudflare_token.pl
# Test domain-specific operations
perl /home/shanta/PycharmProjects/comserv2/Comserv/script/test_cloudflare_domain.pl
Common Issues and Solutions
- IP Restriction Error (Code: 9109)
-
The API token has IP address restrictions that prevent it from being used from the server.
Solution: Create a new API token without IP restrictions or add the server's IP address to the allowed list.
- Authentication Error (Code: 10000)
-
The API token is invalid, has expired, or doesn't have the necessary permissions.
Solution: Create a new API token with the correct permissions.
- Unknown API Key Error (Code: 9103)
-
The API key is invalid or not recognized by Cloudflare.
Solution: Use an API token instead of an API key, as API keys are being deprecated by Cloudflare.