-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelete_cert_from_cf.sh
48 lines (38 loc) · 1.17 KB
/
delete_cert_from_cf.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/bin/bash
# Load configuration
CONFIG_FILE="config/CF_api.conf"
CERT_ID=""
while getopts "c:z:t:i:" opt; do
case ${opt} in
c) CONFIG_FILE=$OPTARG ;;
z) ZONEID=$OPTARG ;;
t) BEARER_TOKEN=$OPTARG ;;
i) CERT_ID=$OPTARG ;;
\?) echo "Usage: $0 [-c CONFIG_FILE] [-z ZONEID] [-t BEARER_TOKEN] [-i CERT_ID]"
exit 1 ;;
esac
done
source $CONFIG_FILE
# Validate required parameters
if [ -z "$ZONEID" ] || [ -z "$BEARER_TOKEN" ] || [ -z "$CERT_ID" ]; then
echo "ZONEID, BEARER_TOKEN, and CERT_ID are required parameters."
exit 1
fi
# Logging setup
LOGFILE="logs/delete_cert_from_cf.log"
mkdir -p logs
log() {
echo "$(date +"%Y-%m-%d %H:%M:%S") [INFO] $1" | tee -a $LOGFILE
}
error() {
echo "$(date +"%Y-%m-%d %H:%M:%S") [ERROR] $1" | tee -a $LOGFILE >&2
exit 1
}
log "Deleting certificate $CERT_ID from Cloudflare..."
DELETE_RESPONSE=$(curl -s -X DELETE "https://api.cloudflare.com/client/v4/zones/$ZONEID/origin_tls_client_auth/ca/$CERT_ID" \
-H "Authorization: Bearer $BEARER_TOKEN")
if echo $DELETE_RESPONSE | grep -q "\"success\":true"; then
log "Certificate deleted successfully."
else
error "Failed to delete certificate: $DELETE_RESPONSE"
fi