2022-07-14 00:39:22 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# set -x
|
|
|
|
set -e
|
|
|
|
|
|
|
|
#
|
|
|
|
# Source the environment variables
|
|
|
|
#
|
2022-08-21 12:50:49 +00:00
|
|
|
if [[ -f .env ]]; then
|
|
|
|
source .env
|
|
|
|
elif [[ -f /etc/azure-dns-update/.env ]]; then
|
|
|
|
source /etc/azure-dns-update/.env
|
|
|
|
else
|
|
|
|
#if [[ ! -f .env ]]; then
|
2022-07-14 00:39:22 +00:00
|
|
|
echo "MISSING .env FILE!"
|
|
|
|
exit -1
|
|
|
|
fi
|
2022-08-21 13:53:57 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Logging
|
|
|
|
#
|
|
|
|
write_log() {
|
|
|
|
logLevel='DEBUG'
|
|
|
|
if [[ ! -z $2 ]]; then
|
|
|
|
echo "$1: $2"
|
|
|
|
else
|
|
|
|
echo "logLevel: $1"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
write_message() {
|
|
|
|
write_log 'INFO' "$1"
|
|
|
|
}
|
|
|
|
|
2022-07-14 00:39:22 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# CURL to get the bearer token for the following calls
|
|
|
|
#
|
|
|
|
get_token() {
|
|
|
|
curl -s -X POST \
|
|
|
|
-d "grant_type=client_credentials&client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&resource=https%3A%2F%2Fmanagement.azure.com%2F" \
|
2022-08-20 18:09:55 +00:00
|
|
|
https://login.microsoftonline.com/$TENANT_ID/oauth2/token | jq --raw-output '.access_token'
|
2022-07-14 00:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# CURL to get the recordset
|
|
|
|
#
|
|
|
|
get_recordset() {
|
|
|
|
curl -s -X GET \
|
2022-08-20 18:09:55 +00:00
|
|
|
-H "Authorization: Bearer $access_token" \
|
2022-07-14 00:39:22 +00:00
|
|
|
-H "Content-Type: application/json" \
|
2022-08-20 18:09:55 +00:00
|
|
|
https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$resource_group/providers/Microsoft.Network/dnsZones/$zone_name/$RECORD_TYPE/$RELATIVE_RECORD_SET_NAME?api-version=2018-05-01 \
|
2022-07-14 00:39:22 +00:00
|
|
|
| jq '.'
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Parses the IPv4 IP Address from the Record Set response
|
|
|
|
#
|
|
|
|
get_recordset_address() {
|
|
|
|
get_recordset | jq --raw-output '.properties.ARecords[].ipv4Address'
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Updates and formats the record set to be used in the PUT request body
|
|
|
|
#
|
|
|
|
update_recordset() {
|
2022-08-20 18:09:55 +00:00
|
|
|
echo $azure_recordset | jq -r ".properties.ARecords[].ipv4Address = \"$1\" | del(.name,.type,.id,.properties.fqdn,.properties.provisioningState) | @text"
|
2022-07-14 00:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# CURL to PUT the updated record set
|
|
|
|
#
|
|
|
|
put_recordset() {
|
|
|
|
curl -s -X PUT \
|
2022-08-20 18:09:55 +00:00
|
|
|
-H "Authorization: Bearer $access_token" \
|
2022-07-14 00:39:22 +00:00
|
|
|
-H "Content-Type: application/json" \
|
|
|
|
--data-raw "$1" \
|
2022-08-20 18:09:55 +00:00
|
|
|
https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$resource_group/providers/Microsoft.Network/dnsZones/$zone_name/$RECORD_TYPE/$RELATIVE_RECORD_SET_NAME?api-version=2018-05-01
|
2022-07-14 00:39:22 +00:00
|
|
|
}
|
|
|
|
|
2022-08-20 18:09:55 +00:00
|
|
|
resource_group_list=($RESOURCE_GROUP_NAMES)
|
|
|
|
zone_list=($ZONE_NAMES)
|
2022-08-21 13:53:57 +00:00
|
|
|
|
|
|
|
write_message "======================================="
|
|
|
|
write_message "======================================="
|
2022-08-20 18:09:55 +00:00
|
|
|
for i in "${!resource_group_list[@]}"; do
|
|
|
|
resource_group="${resource_group_list[i]}"
|
|
|
|
zone_name="${zone_list[i]}"
|
|
|
|
access_token=$(get_token)
|
|
|
|
|
|
|
|
# Get the Record Set
|
|
|
|
azure_recordset=$(get_recordset)
|
2022-08-21 13:53:57 +00:00
|
|
|
write_message "Azure Record Set: $zone_name"
|
2022-08-20 18:09:55 +00:00
|
|
|
|
|
|
|
# Parse the Record Set
|
|
|
|
azure_ip_address=$(get_recordset_address $azure_recordset)
|
2022-08-21 13:53:57 +00:00
|
|
|
write_message "Azure IP: $azure_ip_address"
|
2022-08-20 18:09:55 +00:00
|
|
|
|
|
|
|
# Get the currently assigned Public IP
|
|
|
|
isp_ip_address=$(dig +short myip.opendns.com @resolver1.opendns.com)
|
2022-08-21 13:53:57 +00:00
|
|
|
write_message "ISP IP: $isp_ip_address"
|
2022-08-20 18:09:55 +00:00
|
|
|
|
|
|
|
# If the currently assigned public IP doesn't match the configured IP address in Azure...
|
|
|
|
# update the Azure record set.
|
|
|
|
if [ $azure_ip_address != $isp_ip_address ]; then
|
2022-08-21 13:53:57 +00:00
|
|
|
write_message "IP ADDRESSES DON'T MATCH. UPDATING..."
|
2022-08-20 18:09:55 +00:00
|
|
|
|
|
|
|
# Prepare the request content
|
|
|
|
azure_recordset_UPDATE=$(update_recordset $isp_ip_address)
|
2022-08-21 13:53:57 +00:00
|
|
|
write_message $azure_recordset_UPDATE
|
2022-08-20 18:09:55 +00:00
|
|
|
|
|
|
|
# Perform the Update
|
|
|
|
AZURE_PUT_RESPONSE=$(put_recordset $azure_recordset_UPDATE)
|
2022-08-21 13:53:57 +00:00
|
|
|
write_message "Azure PUT Response: $AZURE_PUT_RESPONSE"
|
2022-08-20 18:09:55 +00:00
|
|
|
|
2022-08-21 13:53:57 +00:00
|
|
|
write_message "======================================="
|
|
|
|
write_message $'Azure IP Address updated successfully\n'
|
|
|
|
write_message "======================================="
|
2022-08-20 18:09:55 +00:00
|
|
|
else
|
2022-08-21 13:53:57 +00:00
|
|
|
write_message "======================================="
|
|
|
|
write_message $'IP Address is up to date.\n'
|
|
|
|
write_message "======================================="
|
2022-08-20 18:09:55 +00:00
|
|
|
fi
|
2022-08-21 12:50:49 +00:00
|
|
|
done
|
2022-08-21 13:53:57 +00:00
|
|
|
|
|
|
|
write_message "======================================="
|
|
|
|
write_message "======================================="
|