Support multiple domains

This commit is contained in:
Mike Heier 2022-08-20 14:09:55 -04:00
parent 6d06fd8262
commit a6cd81e5f3
3 changed files with 55 additions and 36 deletions

View File

@ -3,7 +3,7 @@ CLIENT_ID=<YOUR_CLIENT_ID_GUID>
CLIENT_SECRET=<YOUR_CLIENT_SECRET>
TENANT_ID=<YOUR_TENANT_ID_GUID>
RESOURCE_GROUP_NAME=<YOUR_RESOURCE_GROUP_NAME>
ZONE_NAME=<YOUR_DNS_ZONE_NAME>
RESOURCE_GROUP_NAMES="<YOUR_RESOURCE_GROUP_NAMES_SPACE_DELIMITED>"
ZONE_NAMES="<YOUR_DNS_ZONE_NAMES_SPACE_DELIMITED>"
RECORD_TYPE=A
RELATIVE_RECORD_SET_NAME=@

View File

@ -6,10 +6,18 @@ A utility that can be used to keep your Azure DNS pointed to your on-premises/ho
## Features
- Includes one template based on this [article](https://blog.rocketseat.com.br/como-fazer-um-bom-readme);
- Supports updating multiple DNS Zones at once
- Can be run on a schedule
- Can be run as a Docker container
## Prerequisites
The following steps should be completed before this tool will become functional:
- Create a new Azure DNS Zone on https://portal.azure.com
- Purchase a domain and configure it's nameservers to point to Azure DNS
- Create an ```A``` record with value ```@```
- Configure the Settings as described below in the .env file
## Settings
These settings must be configured in a .env file in the same directory as the script.
@ -20,9 +28,12 @@ CLIENT_ID=<YOUR_CLIENT_ID_GUID>
CLIENT_SECRET=<YOUR_CLIENT_SECRET>
TENANT_ID=<YOUR_TENANT_ID_GUID>
RESOURCE_GROUP_NAME=<YOUR_RESOURCE_GROUP_NAME>
ZONE_NAME=<YOUR_DNS_ZONE_NAME>
RESOURCE_GROUP_NAMES="<YOUR_RESOURCE_GROUP_NAMES_SPACE_DELIMITED>"
ZONE_NAMES="<YOUR_DNS_ZONE_NAMES_SPACE_DELIMITED>"
RECORD_TYPE=A
RELATIVE_RECORD_SET_NAME=@
```
## Usage
To run, simply configure the .env file and run the update-dns.sh script.

View File

@ -18,7 +18,7 @@ source .env
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" \
https://login.microsoftonline.com/9da185bf-c68b-4143-af97-f6744a41c9db/oauth2/token | jq --raw-output '.access_token'
https://login.microsoftonline.com/$TENANT_ID/oauth2/token | jq --raw-output '.access_token'
}
#
@ -26,9 +26,9 @@ get_token() {
#
get_recordset() {
curl -s -X GET \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Authorization: Bearer $access_token" \
-H "Content-Type: application/json" \
https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP_NAME/providers/Microsoft.Network/dnsZones/$ZONE_NAME/$RECORD_TYPE/$RELATIVE_RECORD_SET_NAME?api-version=2018-05-01 \
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 \
| jq '.'
}
@ -43,7 +43,7 @@ get_recordset_address() {
# Updates and formats the record set to be used in the PUT request body
#
update_recordset() {
echo $AZURE_RECORDSET | jq -r ".properties.ARecords[].ipv4Address = \"$1\" | del(.name,.type,.id,.properties.fqdn,.properties.provisioningState) | @text"
echo $azure_recordset | jq -r ".properties.ARecords[].ipv4Address = \"$1\" | del(.name,.type,.id,.properties.fqdn,.properties.provisioningState) | @text"
}
#
@ -51,42 +51,50 @@ update_recordset() {
#
put_recordset() {
curl -s -X PUT \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Authorization: Bearer $access_token" \
-H "Content-Type: application/json" \
--data-raw "$1" \
https://management.azure.com/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP_NAME/providers/Microsoft.Network/dnsZones/$ZONE_NAME/$RECORD_TYPE/$RELATIVE_RECORD_SET_NAME?api-version=2018-05-01
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
}
# Get an Accesss Token
ACCESS_TOKEN=$(get_token)
# echo $ACCESS_TOKEN
#access_token=$(get_token)
# echo $access_token
resource_group_list=($RESOURCE_GROUP_NAMES)
zone_list=($ZONE_NAMES)
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)
echo "Azure Record Set: $AZURE_RECORDSET"
azure_recordset=$(get_recordset)
echo "Azure Record Set: $azure_recordset"
# Parse the Record Set
AZURE_IP_ADDRESS=$(get_recordset_address)
echo "Azure IP: $AZURE_IP_ADDRESS"
azure_ip_address=$(get_recordset_address $azure_recordset)
echo "Azure IP: $azure_ip_address"
# Get the currently assigned Public IP
ISP_IP_ADDRESS=$(dig +short myip.opendns.com @resolver1.opendns.com)
echo "ISP IP: $ISP_IP_ADDRESS"
isp_ip_address=$(dig +short myip.opendns.com @resolver1.opendns.com)
echo "ISP IP: $isp_ip_address"
# 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
if [ $azure_ip_address != $isp_ip_address ]; then
echo "IP ADDRESSES DON'T MATCH. UPDATING..."
# Prepare the request content
AZURE_RECORDSET_UPDATE=$(update_recordset $ISP_IP_ADDRESS)
echo $AZURE_RECORDSET_UPDATE
azure_recordset_UPDATE=$(update_recordset $isp_ip_address)
echo $azure_recordset_UPDATE
# Perform the Update
AZURE_PUT_RESPONSE=$(put_recordset $AZURE_RECORDSET_UPDATE)
AZURE_PUT_RESPONSE=$(put_recordset $azure_recordset_UPDATE)
echo "Azure PUT Response: $AZURE_PUT_RESPONSE"
echo "Azure IP Address updated successfully"
else
echo "IP Address is up to date."
fi
done