diff --git a/.env-sample b/.env-sample index 7e349f4..19f2848 100644 --- a/.env-sample +++ b/.env-sample @@ -3,7 +3,7 @@ CLIENT_ID= CLIENT_SECRET= TENANT_ID= -RESOURCE_GROUP_NAME= -ZONE_NAME= +RESOURCE_GROUP_NAMES="" +ZONE_NAMES="" RECORD_TYPE=A RELATIVE_RECORD_SET_NAME=@ \ No newline at end of file diff --git a/README.md b/README.md index f644e99..a2ea703 100644 --- a/README.md +++ b/README.md @@ -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= CLIENT_SECRET= TENANT_ID= -RESOURCE_GROUP_NAME= -ZONE_NAME= +RESOURCE_GROUP_NAMES="" +ZONE_NAMES="" RECORD_TYPE=A RELATIVE_RECORD_SET_NAME=@ ``` +## Usage + +To run, simply configure the .env file and run the update-dns.sh script. \ No newline at end of file diff --git a/update-dns.sh b/update-dns.sh index 7c5ff7a..0b9209b 100755 --- a/update-dns.sh +++ b/update-dns.sh @@ -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 -# Get the Record Set -AZURE_RECORDSET=$(get_recordset) -echo "Azure Record Set: $AZURE_RECORDSET" +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) -# Parse the Record Set -AZURE_IP_ADDRESS=$(get_recordset_address) -echo "Azure IP: $AZURE_IP_ADDRESS" + # Get the Record Set + azure_recordset=$(get_recordset) + echo "Azure Record Set: $azure_recordset" -# Get the currently assigned Public IP -ISP_IP_ADDRESS=$(dig +short myip.opendns.com @resolver1.opendns.com) -echo "ISP IP: $ISP_IP_ADDRESS" + # Parse the Record Set + azure_ip_address=$(get_recordset_address $azure_recordset) + echo "Azure IP: $azure_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 - echo "IP ADDRESSES DON'T MATCH. UPDATING..." + # Get the currently assigned Public IP + isp_ip_address=$(dig +short myip.opendns.com @resolver1.opendns.com) + echo "ISP IP: $isp_ip_address" - # Prepare the request content - AZURE_RECORDSET_UPDATE=$(update_recordset $ISP_IP_ADDRESS) - echo $AZURE_RECORDSET_UPDATE + # 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 + echo "IP ADDRESSES DON'T MATCH. UPDATING..." - # Perform the Update - AZURE_PUT_RESPONSE=$(put_recordset $AZURE_RECORDSET_UPDATE) - echo "Azure PUT Response: $AZURE_PUT_RESPONSE" + # Prepare the request content + azure_recordset_UPDATE=$(update_recordset $isp_ip_address) + echo $azure_recordset_UPDATE - echo "Azure IP Address updated successfully" -else - echo "IP Address is up to date." -fi \ No newline at end of file + # Perform the 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 \ No newline at end of file