Skip to content

Instantly share code, notes, and snippets.

@henrycunh
Last active December 29, 2021 16:07
Show Gist options
  • Save henrycunh/628a9b80230adab04247350d5f78d6ca to your computer and use it in GitHub Desktop.
Save henrycunh/628a9b80230adab04247350d5f78d6ca to your computer and use it in GitHub Desktop.
Deleting Cloudflare Access applications
#!/bin/bash
# get list of applications from arguments
APPLICATIONS=$@
# if no applications are specified, throw error
if [ -z "$APPLICATIONS" ]; then
echo "[error] No applications specified."
exit 1
fi
# verify if needed environment variables are set
if [ -z "$CF_API_KEY" ]; then
echo "[error] CF_API_KEY environment variable is not set."
exit 1
fi
if [ -z "$CF_EMAIL" ]; then
echo "[error] CF_EMAIL environment variable is not set."
exit 1
fi
if [ -z "$CF_ZONE_ID" ]; then
echo "[error] CF_ZONE_ID environment variable is not set."
exit 1
fi
# get list of applications from cloudflare access api
CF_APPLICATIONS=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/access/apps" \
-H "X-Auth-Email: $CF_EMAIL" \
-H "X-Auth-Key: $CF_API_KEY" \
-H "Content-Type: application/json" \
| jq -r '.result[] | .id + "/" + .name'
)
# if flag --ignore is set, ignore the applications specified
if [ "$1" == "--ignore" ]; then
echo "[info] Deleting all applications except:"
shift
APPLICATIONS=$@
# loop through preserved applications and print them
for application in $APPLICATIONS; do
echo "[info] - $application"
done
# filter applications to be deleted, removing the ignored applications
for application in $CF_APPLICATIONS; do
APPLICATIONS_TO_DELETE="$CF_APPLICATIONS"
for preserved_application in $APPLICATIONS; do
APPLICATIONS_TO_DELETE=$(echo "$APPLICATIONS_TO_DELETE" | grep -v "$preserved_application")
done
APPLICATIONS=$APPLICATIONS_TO_DELETE
done
else
APPLICATIONS_TO_DELETE=""
for application in $APPLICATIONS; do
application_data=$(echo "$CF_APPLICATIONS" | grep "$application")
APPLICATIONS_TO_DELETE="$APPLICATIONS_TO_DELETE $application_data"
# # add application to list of applications to delete if it was passed on argument
# APPLICATIONS_TO_DELETE="$APPLICATIONS_TO_DELETE $(echo "$CF_APPLICATIONS" | grep "$application")"
done
APPLICATIONS=$APPLICATIONS_TO_DELETE
fi
# present a prompt to the user to confirm deletion
echo "[info] Are you sure you want to delete the following applications? (yes/no)"
# loop through all applications and print them
for application in $APPLICATIONS; do
# if the application is not in the preserved list, print it
app_name=$(echo $application | cut -d "/" -f 2)
app_id=$(echo $application | cut -d "/" -f 1)
if [[ ! " ${APPLICATIONS[@]} " =~ " ${app_name} " ]]; then
echo "[info] - $app_name ($app_id)"
fi
done
read -p "[info] Type 'yes' to continue: " -r
# if user does not type 'yes', exit
if [[ ! $REPLY =~ ^[Yy]es$ ]]; then
echo "[info] Exiting..."
exit 1
fi
# loop through all applications and delete them
for application in $CF_APPLICATIONS; do
# if the application is not in the preserved list, delete it
app_name=$(echo $application | cut -d "/" -f 2)
app_id=$(echo $application | cut -d "/" -f 1)
if [[ ! " ${APPLICATIONS[@]} " =~ " ${app_name} " ]]; then
echo "[info] Deleting $app_name ($app_id)..."
delete_response=$(curl -s -X DELETE "https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/access/apps/$app_id" \
-H "X-Auth-Email: $CF_EMAIL" \
-H "X-Auth-Key: $CF_API_KEY" \
-H "Content-Type: application/json"
)
delete_status=$(echo $delete_response | jq -r '.result.success')
if [ "$delete_status" == "true" ]; then
echo "[info] - $app_name ($app_id) deleted"
else
echo "[error] - $app_name ($app_id) failed to delete"
delete_errors=$(echo $delete_response | jq -r '.result.errors[] | .code + ": " + .message')
echo "[error] - $delete_errors"
fi
fi
done

Why?

This script is useful for cleaning up environments, or, if anything goes wrong with your automation and you need to clean pages of applications, here's what you need.

Installation

You can get the URL of the raw contents of the script and make it executable:

curl -s $url > delete-apps.sh
chmod u+x delete-apps.sh

Usage

Deleting all specified applications

./delete-apps.sh "my-app" "my-app-staging"

Deleting all apps but specified ones

./delete-apps.sh --ignore "my-app"

Environment Variables

# Cloudflare Zone ID
export CF_ZONE_ID=
# Cloudflare Account Email
export CF_EMAIL=
# Cloudflare Account API KEY
export CF_API_KEY=
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment