Skip to content

Commit

Permalink
Update to v3 api
Browse files Browse the repository at this point in the history
  • Loading branch information
Menkveld-24 committed Feb 22, 2023
1 parent a448f3a commit bc8aae8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# [CERTER]
# APISIX Letsencrypt certificate generator Dockerfile
# Menke 2022
# Menke 2023
FROM alpine:3.17

# Default env variables
Expand Down
32 changes: 20 additions & 12 deletions certer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# [CERTER]
# APISIX Letsencrypt certificate generator script
# Menke 2022
# Menke 2023

# Exit on any command fail
set -e
Expand All @@ -26,7 +26,7 @@ debugCurl=$([ $CURL_DEBUG == "true" ] && echo "-v" || echo "")
agreeCertbotTOS=$([ $AGREE_TOS == "true" ] && echo "--agree-tos" || echo "")
forceRenew=$([ $FORCE_RENEW == "true" ] && echo "--force-renew" || echo "")

echo "[CERTER] v1.1.0"
echo "[CERTER] v2.0.0"
echo "[CERTER] Generating certificates for: $domain"

# Validate the response of any curl
Expand Down Expand Up @@ -70,16 +70,24 @@ echo "[CERTER] Generated certificates!"

# Cleaning up and deleting old certificates
if [[ $deleteOtherCerts == "true" ]]; then
validateHTTPResonse=`curl -H "X-API-KEY: $apikey" -s $ignoreSSL $debugCurl -o /tmp/response -w "%{http_code}" -X GET "$url/apisix/admin/ssl"`
validateHTTPResonse=`curl -H "X-API-KEY: $apikey" -s $ignoreSSL $debugCurl -o /tmp/response -w "%{http_code}" -X GET "$url/apisix/admin/ssls"`

deletedCerts=0
for id in $(cat /tmp/response | jq -r ".node.nodes[]|select(.value.snis[0] == \"$domain\")|.value.id"); do
validateHTTPResponse `curl -H "X-API-KEY: $apikey" -s $ignoreSSL $debugCurl $showOutput -w "%{http_code}" -X DELETE "$url/apisix/admin/ssl/$id"`
deletedCerts=$(expr $deletedCerts + 1)
done
# Check if there are any certificates in the response
if [[ $(cat /tmp/response | jq '.total') -ne 0 ]]; then

if [[ $deletedCerts -gt 0 ]]; then
echo "[CERTER] Deleted $deletedCerts existing certificate(s)!"
deletedCerts=0
for id in $(cat /tmp/response | jq -r ".list[]|select(.value.snis[0] == \"$domain\")|.value.id"); do
validateHTTPResponse `curl -H "X-API-KEY: $apikey" -s $ignoreSSL $debugCurl $showOutput -w "%{http_code}" -X DELETE "$url/apisix/admin/ssls/$id"`
deletedCerts=$(expr $deletedCerts + 1)
done

if [[ $deletedCerts -gt 0 ]]; then
echo "[CERTER] Deleted $deletedCerts existing certificate(s)!"
else
echo "[CERTER] No certificates to clean up"
fi

# No certificates are currently installed
else
echo "[CERTER] No certificates to clean up"
fi
Expand All @@ -91,6 +99,6 @@ validateHTTPResponse `curl -H "X-API-KEY: $apikey" -s $ignoreSSL $debugCurl $sho
\"key\": \"$(cat /etc/letsencrypt/live/$domain/privkey.pem)\",
\"snis\": [\"$domain\"],
\"validity_end\": $(expr $(date +%s) + 90 \* 24 \* 60 \* 60)
}" "$url/apisix/admin/ssl"`
}" "$url/apisix/admin/ssls"`

echo "[CERTER] Uploaded certificates!"
echo "[CERTER] Uploaded certificates!"

0 comments on commit bc8aae8

Please sign in to comment.