From 5cde0f368ccce0fcdaa260a9e32953fc79441680 Mon Sep 17 00:00:00 2001 From: nicholasphillips Date: Thu, 6 Feb 2025 13:23:05 +0000 Subject: [PATCH 1/3] Change to use Cloudflare API --- Dockerfile | 9 +++++---- k8s-tools.sh | 43 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index 757cf77..60d2c0e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,9 @@ -FROM node:20 +FROM ubuntu:22.04 -RUN npm install -g cloudflare-cli -RUN apt-get update -RUN apt-get install -y jq +RUN apt-get update && apt-get install -y build-essential software-properties-common openssl \ + zip unzip --no-install-recommends \ + && apt-get install -y curl jq \ + && apt-get auto-remove && rm -rf /var/lib/apt/lists/* RUN curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/$(dpkg --print-architecture)/kubectl" RUN chmod +x ./kubectl RUN mv ./kubectl /usr/local/bin/kubectl diff --git a/k8s-tools.sh b/k8s-tools.sh index f2103b1..539790a 100644 --- a/k8s-tools.sh +++ b/k8s-tools.sh @@ -1,11 +1,12 @@ #!/bin/bash -echo cloudflare-cli: k8s-tools v0.0.17 +echo cloudflare-cli: k8s-tools v0.0.18 bad=0 if [ -z "$action" ]; then echo "variable 'action' is not set"; bad=1; fi if [ -z "$subdomain" ]; then echo "variable 'subdomain' is not set"; bad=1; fi if [ -z "$use_proxy" ]; then echo "variable 'use_proxy' is not set"; bad=1; fi +if [ -z "$CF_ZONE_ID" ]; then echo "variable 'CF_ZONE_ID' is not set"; bad=1; fi if [ -z "$CF_API_KEY" ]; then echo "variable 'CF_API_KEY' is not set"; bad=1; fi if [ -z "$CF_API_EMAIL" ]; then echo "variable 'CF_API_EMAIL' is not set"; bad=1; fi if [ -z "$CF_API_DOMAIN" ]; then echo "variable 'CF_API_DOMAIN' is not set"; bad=1; fi @@ -54,7 +55,6 @@ if [ $action = "create" ]; then if [ -z "$resource" ]; then echo "no service data returned"; exit 1; fi echo got service info fi - if [ $record_type = "A" ] then echo getting external IP... @@ -79,16 +79,45 @@ if [ $action = "create" ]; then fi echo deleting any existing record... - cfcli -e $CF_API_EMAIL -k $CF_API_KEY -d $CF_API_DOMAIN -a -t $record_type rm $subdomain - echo adding... - cfcli -e $CF_API_EMAIL -k $CF_API_KEY -d $CF_API_DOMAIN -t $record_type add $subdomain $dns_record_value --activate $use_proxy + echo looking up existing record to delete... + cloudflare_record_id=$(curl https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records?search=$subdomain \ + -H "X-Auth-Email: $CF_API_EMAIL" \ + -H "X-Auth-Key: $CF_API_KEY" | jq -r ".result[] | select(.name | contains(\"$subdomain\")) | .id") + + + echo deleting... + curl https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records/$cloudflare_record_id \ + -X DELETE \ + -H "X-Auth-Email: $CF_API_EMAIL" \ + -H "X-Auth-Key: $CF_API_KEY" + + echo adding... + curl https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records \ + -H 'Content-Type: application/json' \ + -H "X-Auth-Email: $CF_API_EMAIL" \ + -H "X-Auth-Key: $CF_API_KEY" \ + -d '{ + "content": "'$dns_record_value'", + "name": "'$subdomain'", + "proxied": '$use_proxy', + "type": "'$record_type'" + }' retVal=$? fi if [ $action = "delete" ]; then bad=0 - echo deleting... - cfcli -e $CF_API_EMAIL -k $CF_API_KEY -d $CF_API_DOMAIN -a -t $record_type rm $subdomain + echo deleting... + echo looking up existing record to delete... + cloudflare_record_id=$(curl https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records?search=$subdomain \ + -H "X-Auth-Email: $CF_API_EMAIL" \ + -H "X-Auth-Key: $CF_API_KEY" | jq -r ".result[] | select(.name | contains(\"$subdomain\")) | .id") + + echo deleting... + curl https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records/$cloudflare_record_id \ + -X DELETE \ + -H "X-Auth-Email: $CF_API_EMAIL" \ + -H "X-Auth-Key: $CF_API_KEY" retVal=$? fi if [ $bad -eq 1 ]; then echo "unknown action - use create or delete"; exit 1; fi From b5b022601f4fb6fd654672b107a463bf58a1e86d Mon Sep 17 00:00:00 2001 From: James Seden Smith Date: Thu, 6 Feb 2025 14:03:21 +0000 Subject: [PATCH 2/3] Get zone id programtically --- k8s-tools.sh | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/k8s-tools.sh b/k8s-tools.sh index 539790a..84cfd5d 100644 --- a/k8s-tools.sh +++ b/k8s-tools.sh @@ -1,12 +1,11 @@ #!/bin/bash -echo cloudflare-cli: k8s-tools v0.0.18 +echo cloudflare-cli: k8s-tools v0.0.19 bad=0 if [ -z "$action" ]; then echo "variable 'action' is not set"; bad=1; fi if [ -z "$subdomain" ]; then echo "variable 'subdomain' is not set"; bad=1; fi if [ -z "$use_proxy" ]; then echo "variable 'use_proxy' is not set"; bad=1; fi -if [ -z "$CF_ZONE_ID" ]; then echo "variable 'CF_ZONE_ID' is not set"; bad=1; fi if [ -z "$CF_API_KEY" ]; then echo "variable 'CF_API_KEY' is not set"; bad=1; fi if [ -z "$CF_API_EMAIL" ]; then echo "variable 'CF_API_EMAIL' is not set"; bad=1; fi if [ -z "$CF_API_DOMAIN" ]; then echo "variable 'CF_API_DOMAIN' is not set"; bad=1; fi @@ -27,6 +26,12 @@ fi record_type=${CF_DNS_TYPE:="A"} bad=1 + +zone_id=$(curl https://api.cloudflare.com/client/v4/zones?name=$CF_API_DOMAIN \ +-H "X-Auth-Email: $CF_API_EMAIL" \ +-H "X-Auth-Key: $CF_API_KEY" | jq -r ".result[] | select(.name | contains(\"$CF_API_DOMAIN\")) | .id") +if [ -z "$zone_id" ]; then echo "zone not found"; exit 1; fi + if [ $action = "create" ]; then bad=0 @@ -81,19 +86,19 @@ if [ $action = "create" ]; then echo deleting any existing record... echo looking up existing record to delete... - cloudflare_record_id=$(curl https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records?search=$subdomain \ + cloudflare_record_id=$(curl https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records?search=$subdomain \ -H "X-Auth-Email: $CF_API_EMAIL" \ -H "X-Auth-Key: $CF_API_KEY" | jq -r ".result[] | select(.name | contains(\"$subdomain\")) | .id") - + echo deleting... - curl https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records/$cloudflare_record_id \ + curl https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$cloudflare_record_id \ -X DELETE \ -H "X-Auth-Email: $CF_API_EMAIL" \ -H "X-Auth-Key: $CF_API_KEY" echo adding... - curl https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records \ + curl https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records \ -H 'Content-Type: application/json' \ -H "X-Auth-Email: $CF_API_EMAIL" \ -H "X-Auth-Key: $CF_API_KEY" \ @@ -109,12 +114,12 @@ if [ $action = "delete" ]; then bad=0 echo deleting... echo looking up existing record to delete... - cloudflare_record_id=$(curl https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records?search=$subdomain \ + cloudflare_record_id=$(curl https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records?search=$subdomain \ -H "X-Auth-Email: $CF_API_EMAIL" \ -H "X-Auth-Key: $CF_API_KEY" | jq -r ".result[] | select(.name | contains(\"$subdomain\")) | .id") echo deleting... - curl https://api.cloudflare.com/client/v4/zones/$CF_ZONE_ID/dns_records/$cloudflare_record_id \ + curl https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$cloudflare_record_id \ -X DELETE \ -H "X-Auth-Email: $CF_API_EMAIL" \ -H "X-Auth-Key: $CF_API_KEY" From 1904f557f029f25564d98c3d75cbc8ef5602aa13 Mon Sep 17 00:00:00 2001 From: James Seden Smith Date: Mon, 10 Feb 2025 16:02:20 +0000 Subject: [PATCH 3/3] Bump Version --- k8s-tools.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k8s-tools.sh b/k8s-tools.sh index 84cfd5d..3363c90 100644 --- a/k8s-tools.sh +++ b/k8s-tools.sh @@ -1,6 +1,6 @@ #!/bin/bash -echo cloudflare-cli: k8s-tools v0.0.19 +echo cloudflare-cli: k8s-tools v0.0.20 bad=0 if [ -z "$action" ]; then echo "variable 'action' is not set"; bad=1; fi