|
#!/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 |