Automatic DNS Cleanup #236
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: "Automatic DNS Cleanup" | |
on: | |
schedule: | |
- cron: '0 0 * * *' # At midnight, daily | |
jobs: | |
DNS_Cleanup: | |
runs-on: "ubuntu-latest" | |
steps: | |
- name: "Run Cleanup via Bash" | |
shell: "bash" | |
run: | | |
- name: "Run Cleanup via Bash" | |
shell: "bash" | |
run: | | |
date=$(date) | |
authemail="${{ secrets.LF_DNS_AUTH_EMAIL }}" | |
apitoken="${{ secrets.LF_DNS_API }}" | |
#Get DNS Record IDs | |
DNSIDs=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/08be24924fc30f320e7329020986bad2/dns_records?per_page=1000" -H "X-Auth-Email: $authemail" -H "Authorization: Bearer $apitoken" -H "Content-Type: application/json" | jq -r '.result[] | .id') | |
printf "\nFound $(echo -n "$DNSIDs" | grep -c '^') IDs to process\n" | |
for id in $DNSIDs | |
do | |
#printf "\nChecking DNS Record with ID:$id\n" | |
DNSRecord=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/08be24924fc30f320e7329020986bad2/dns_records/$id" -H "X-Auth-Email: $authemail" -H "Authorization: Bearer $apitoken" -H "Content-Type: application/json") | |
DNSContent=$(echo $DNSRecord | jq -r '.result | .content') | |
DNSName=$(echo $DNSRecord | jq -r '.result | .name') | |
#Logic for Naming Convention goes here. We want to leave our other DNS records alone. Crazy {^^} converts to upper. It's a happy thing. | |
if [[ "${DNSContent}" == *".instruqt.io"* ]] || [[ "${DNSName}" == *-ctf* ]]; then | |
#printf "Record Matches Training Naming Convention\n" | |
DNSDateExp=$(date -d "$(echo $DNSRecord | jq -r '.result | .created_on')+7days") | |
printf "DNS Name:$DNSContent DNS Expiry Date:$DNSDateExp Current Date:$date\n" | |
#Compare Dates | |
if [ $(date -d "$date" +%s) -gt $(date -d "$DNSDateExp" +%s) ]; then | |
printf "Record $DNSContent is older than 7 days. Deleting record.\n" | |
DeleteRecord=$(curl -s -X DELETE "https://api.cloudflare.com/client/v4/zones/08be24924fc30f320e7329020986bad2/dns_records/$id" -H "X-Auth-Email: $authemail" -H "Authorization: Bearer $apitoken" -H "Content-Type: application/json") | |
fi | |
fi | |
done |