Compare commits

...

7 Commits

Author SHA1 Message Date
49e7cd13b6 formatting and cleanup 2022-06-01 18:07:05 +05:30
bb80e40ce9 Completed 2022-06-01 01:56:44 +05:30
254615eda6 added DNS export files to gitignore 2022-05-31 23:06:03 +05:30
48cfb3ee87 getAllDNS, deleteARecord and some main file logics 2022-05-28 02:06:26 +05:30
790a38a970 verify access and list zones 2022-05-27 01:47:43 +05:30
81cd95924e Get credentials 2022-05-27 00:12:23 +05:30
fbecb310a5 fixed typo 2022-05-27 00:11:06 +05:30
4 changed files with 136 additions and 3 deletions

4
.gitignore vendored
View File

@ -168,4 +168,6 @@ cython_debug/
# Project setup
config.json
config.json
response-example*
dns-export*

56
apicalls.py Normal file
View File

@ -0,0 +1,56 @@
import requests
def getHeaders(credentials):
headers = {'Authorization': f'Bearer {credentials["api_key"]}', 'X-Auth-Email': f'{credentials["email"]}',
'Content-Type': 'application/json'}
return headers
def errorChecker(r):
if r.status_code == 200:
return False
elif r.status_code == 429:
print(f'\n\n\n{"="*80}It looks like you have reached your rate limit to access CloudFlare APIs\nPlease try after 5 minutes.{"="*80}')
else:
print(f'Error {r.status_code}.\nDetails:\n{r.json()}')
assert False
def verifyCredentials(credentials):
headers = getHeaders(credentials)
r = requests.get('https://api.cloudflare.com/client/v4/user/tokens/verify', headers=headers)
errorChecker(r)
return r
def listAllZones(credentials):
zones = requests.get(
'https://api.cloudflare.com/client/v4/zones',
headers= getHeaders(credentials)
)
errorChecker(zones)
return zones
def getAllDNS(credentials, id):
dns_records = requests.get(
f'https://api.cloudflare.com/client/v4/zones/{id}/dns_records',
headers=getHeaders(credentials)
)
errorChecker(dns_records)
return dns_records
def exportBindDNS(credentials, id):
export_response = requests.get(
f'https://api.cloudflare.com/client/v4/zones/{id}/dns_records/export',
headers=getHeaders(credentials)
)
errorChecker(export_response)
return export_response
def deleteARecord(credentials, zone_id, dns_id):
delete_response = requests.delete(
f'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{dns_id}',
headers=getHeaders(credentials)
)
errorChecker(delete_response)
return delete_response

76
app.py
View File

@ -0,0 +1,76 @@
import json
import uuid
import datetime
from fileinput import filename
from sys import argv
from pathlib import Path
from apicalls import *
# Get API key and Email address
def getCredentials():
api_key = input("Enter the Cloudflare API key: ")
email_address = input("Enter the Cloudflare Email address: ")
return {"api_key": api_key, "email": email_address}
# Get credentials
if len(argv) > 1 and 'loadconfig' in argv:
if Path("config.json").exists():
configfile = open("config.json")
config = json.load(configfile)
credentials = {'api_key': config['API_Key'], 'email': config['Email']}
else:
print('config.json was not found. Please enter the credentials manually')
credentials = getCredentials()
else:
credentials = getCredentials()
zones = listAllZones(credentials).json()["result"]
print('Here are the domains in your account')
zone_index = 0
zone_list = {}
for zone in zones:
print(f'{zone_index + 1}: {zone["name"]}')
zone_list[zone['name']] = {'id': zone['id'], 'name': zone['name'], 'owner': zone['owner']['email'], 'account_of': zone['account']['name']}
domainToDelete = input('Please enter the domain name to delete the DNS records from:\n')
if domainToDelete in zone_list:
zone_id = zone_list[domainToDelete]['id']
zone_owner = zone_list[domainToDelete]['owner']
confirmation = input(f'Are you sure to delete all the DNS records from the domain {domainToDelete}, owned by {zone_owner}?\nEnter YES to continue: ')
if confirmation == 'YES':
# Get Bind DNS
bind_DNS = exportBindDNS(credentials, zone_id).text
# Write to a file
file_name = f'dns-export-{str(uuid.uuid4())[0:8]}-{domainToDelete}-{datetime.datetime.now().strftime("%d-%b-%Y-%H-%M-%S")}.txt'
print(f'Backing up DNS records to {file_name}')
bind_export_file = open(file_name, 'w')
bind_export_file.write(bind_DNS)
bind_export_file.close()
print('Successfully backed up DNS records in BIND format.')
# get DNS Records
dns_records = getAllDNS(credentials, zone_id).json()["result"]
# Delete DNS Records
print('Starting Deletion of DNS records')
for record in dns_records:
dns_id = record['id']
dns_type = record['type']
dns_name = record['name']
dns_content = record['content']
print(f'Deleting:\n{dns_type}\t{dns_name}\t{dns_content}...')
delete_response = deleteARecord(credentials, zone_id, dns_id)
if delete_response.json()['result']['id'] == dns_id:
print(f'Successfully Deleted {dns_id}')
else:
print('Something is wrong, abroading...')
assert False
else:
print('Aborted, bye...')
else:
print('We don\'t have access to the domain name you have entered. Please make sure the API key you have provided has access to the domain name you have entereed.')

View File

@ -1,5 +1,4 @@
{
"saveCredentials": true,
"API_KEY": "your api key goes here",
"API_Key": "your api key goes here",
"Email": "your cloudflare email address"
}