Azure-Samples / azure-files-samples

This repository contains supporting code (PowerShell modules/scripts, ARM templates, etc.) for deploying, configuring, and using Azure Files.
https://docs.microsoft.com/azure/storage/files/storage-files-introduction
MIT License
226 stars 192 forks source link

AzFileDiagnostics.ps1 fails when dns lookup returns multiple IP addresses #149

Open a-teece opened 1 year ago

a-teece commented 1 year ago

It seems that a standard/vanilla configuration of an Azure File Share returns 3 IP addresses. However the script fails with the following error because it tries to connect to a single host which is all 3 addresses (separated by a space), which obviously fails.

[OK]: Storage Account Name enatefiles.file.core.windows.net is resolved to xxx.xxx.xxx.xxx yyy.yyy.yyy.yyy zzz.zzz.zzz.zzz

======Validate port 445 reachability over Storage Account IP xxx.xxx.xxx.xxx yyy.yyy.yyy.yyy zzz.zzz.zzz.zzz

[ERROR]: Connection attempt fails with iteration(0 + 1) of 3 with the error --- No such host is known [ERROR]: Connectoin Failure with error -2147467259

[ERROR]: Connection attempt fails with iteration(1 + 1) of 3 with the error --- No such host is known [ERROR]: Connectoin Failure with error -2147467259

[ERROR]: Connection attempt fails with iteration(2 + 1) of 3 with the error --- No such host is known [ERROR]: Connectoin Failure with error -2147467259

[ERROR]: Last connection exception is: ---No such host is known

[ERROR]: Port 445 is not reachable from this client, Exit the validation and please verify the network

jcampbellburns commented 1 year ago

For anyone reviewing AzFileDiagnostics.ps1 as well as anyone else running into this issue, the issue occurs at lines 1808 thru 1811. On 1808, the script performs a lookup which is expected to return multiple addresses and filters out anything where .AddressFamily is not "InterNetwork". The script apparently assumes that this will return a single result and treats it as such without checking.

On 1810 and 1811, $result, which the coder expected to be an array of System.Net.IPAddress with a single element, contains an array of System.Net.IPAddress with more than one element. When calling System.Net.IPAddress[].IPAddressToString (which I cannot find anywhere in .Net documentation, btw), it apparently converts each element into a string and, if there's more than a single element, concatenates them into a single string separated by " ". This is then returned and other code then refers to the whole thing as a connection endpoint. The coder assumes that this would be an ip address but since it doesn't parse as one (due to being multiple addresses separated by a space), the connection endpoint is treated as a dns name and then the dns lookup fails.

The developer of this script works for Azure and should know that an Azure resource may have more than one address.

Workaround: I was able to run the script by changing both calls to $result.IPAddressToString (on line 1810 and 1811) to $result[0].IPAddressToString to use the first address only. Note: This will disregard any additional addresses which are resolved rather than testing them.