Search code examples
azureazure-web-app-serviceazure-application-insightscidr

IP Address Micro Application Insights


We have an Azure web app that we use for dev\testing. I noticed in Application Insights that it is getting pinged like 500-700 times per minute. I tried blocking the IP in Networking and of course being no network expert didn't realize it will just keep rolling to the next one.

Question 1 is - How do I block by group of IP's used?

Question 2 if that doesn't work - How do I block the "U.K." as I only need in the US currently for dev\testing. I prefer question one so that I can use for my prod. version also and as needed.

enter image description here

enter image description here


Solution

  • Question 1 is - How do I block by group of IP's used?

    Create a new text file and store all the IP Addresses which you want to allow or block and each separated with a Comma (,) as shown below:

    enter image description here

    Paste this code in an PowerShell File to read the above text file:

    Param``(

    [``Parameter``(``Mandatory = $true``)]

    [string] $ResourceGroupName``,

    [``Parameter``(``Mandatory = $true``)]

    [string] $WebAppName``,

    [``Parameter``(``Mandatory = $true``)]

    [string] $IPAddressSourceFileName

    )

    #Step1 - Get All IP Addresses from the File

    $SourceIPAddresses = (``Get-Content $IPAddressSourceFileName``).Trim() | ConvertFrom-Csv

    #Step2 - Get All existing IP Addresses from the Config of App Service

    $APIVersion = ((``Get-AzResourceProvider -ProviderNamespace Microsoft.Web).ResourceTypes | Where-Object ResourceTypeName -eq sites).ApiVersions[0]

    $config = (``Get-AzResource -ResourceType Microsoft.Web/sites/config -Name $WebAppName -ResourceGroupName $ResourceGroupName -ApiVersion $APIVersion``)

    #Step3 - Prepare the new IP Addresses list from that IPAddressList file and collect all the new ones into the $IpSecurityRestrictions collection

    foreach``(``$item in $SourceIPAddresses``){

    $Rule``=``$config``.Properties.ipSecurityRestrictions | Where-Object { $_``.ipAddress -eq $item``.IPAddress}

    if``(``$null -ne $Rule``)

    {

    Write-Host -ForegroundColor Green 'No Action on the IP:' $item``.ipAddress

    }

    else

    {

    $config``.Properties.ipSecurityRestrictions+=``$item

    }

    }

    #Step4 - Finally update the new IP Addresses to Azure App Service

    Set-AzResource -ResourceId $config``.ResourceId -Properties $config``.Properties -ApiVersion $APIVersion -Force

    Run the above PowerShell Script from VS Code > Terminal > Run this following command:

    .\ReadIPAddresses.ps1 azdevops-rg-eus-dev azuredevops-wapp1-eus-dev IPAddresses.txt
    

    After running this command, you can see all the IP Addresses will be added to the Access Restrictions blade as shown here:

    enter image description here