Azure/Azure Networking

Azure 네트워크 보안 그룹 일괄 업데이트(추가/삭제) 스크립트

서희 아빠 2020. 6. 10. 10:56

Azure에서는 서브넷 또는 VM의 네트워크 인터페이스 레벨에서 네트워크보안그룹(이하 NSG)를 설정할 수 있습니다.

일반적으로 서브넷에 바인딩해서 서브넷 단위로 관리하기도 하고, 같은 서브넷 이라도 VM별로 설정을 위해

각 NIC별로 할당을 하기도 합니다. 또는 서비스별로 predefined 되어 있는 서브넷에 자동으로 생성을 하는 경우도 있습니다.(Azure Firewall, Azure Bastion, AAD DS등)

네트워크 인/아웃에 대한 트래픽 필터링 목적으로 쓰이며, 가장 기본적인 네트워크 보안 레벨에서 필수로 설정하게 됩니다.

인바운드와 아웃바운드 각각 설정을 하도록 되어 있으며, 100~4096까지 규칙을 설정할 수 있습니다. 보다 자세한 내용은 하기 docs 사이트에 잘 정리되어 있으니 한번쯤 읽어 보시면 좋을 것 같습니다.

네트워크 보안 그룹

https://docs.microsoft.com/ko-kr/azure/virtual-network/security-overview

간단하게 Powershell의 AZ모듈을 이용해서 대량의 NSG를 업데이트 하는 스크립트를 공유 드립니다.

아무래도 대규모 단위의 시스템이라면, NSG는 휴먼에러가 발생할 가능성이 매우 높기 때문에 스크립트로 작업을 권장 합니다. 

#NSG Rule 추가
param(
[Parameter(Mandatory=$True)]
[string]
$subscriptionId,
[string]
$resourceGroupName,
[string]
$nsgName,
[string]
$nsgRuleName,
[string]
$nsgRuleDescription,
[string]
$nsgRuleAccess,
[string]
$nsgRuleProtocol,
[string]
$nsgRuleDirection,
[string]
$nsgRulePriority,
[string]
$nsgRuleSourceAddressPrefix,
[string]
$nsgRuleSourcePortRange,
[string]
$nsgRuleDestinationAddressPrefix,
[string]
$nsgRuleDestinationPortRange,
$csvFilePath = "<csv파일경로>"
)

Import-Csv $csvFilePath |`
  ForEach-Object {
    $nsgName = $_."NSG Name"
    $nsgRuleName = $_."NSG Rule Name"
    $nsgRuleDescription = $_."NSG Rule Description"
    $nsgRuleAccess = $_."NSG Rule Access"
    $nsgRuleProtocol = $_."NSG Rule Protocol"
    $nsgRuleDirection = $_."NSG Rule Direction"
    $nsgRulePriority = $_."NSG Rule Priority"
    $nsgRuleSourceAddressPrefix = $_."NSG Rule Source Address Prefix"
    $nsgRuleSourcePortRange = $_."NSG Rule Source Port Range"
    $nsgRuleDestinationAddressPrefix = $_."NSG Rule Destination Address Prefix"
    $nsgRuleDestinationPortRange = $_."NSG Rule Destination Port Range"
    $resourceGroupName = $_."RG Name"
 
    #새 규칙 삽입  
    $nsgRuleNameValue = Get-AzNetworkSecurityGroup -Name $nsgName -ResourceGroupName $resourceGroupName | Get-AzNetworkSecurityRuleConfig -Name $nsgRuleName -ErrorAction SilentlyContinue
    
    if($nsgRuleNameValue.Name -match $nsgRuleName){
       Write-Host "이 규칙은(" $nsgRuleNameValue.Name ") 이미 있습니다."
       }
    else{
         Get-AzNetworkSecurityGroup -Name $nsgName -ResourceGroupName $resourceGroupName | Add-AzNetworkSecurityRuleConfig -Name $nsgRuleName -Description $nsgRuleDescription -Access $nsgRuleAccess -Protocol $nsgRuleProtocol -Direction $nsgRuleDirection -Priority $nsgRulePriority -SourceAddressPrefix $nsgRuleSourceAddressPrefix.split(";") -SourcePortRange $nsgRuleSourcePortRange -DestinationAddressPrefix $nsgRuleDestinationAddressPrefix -DestinationPortRange $nsgRuleDestinationPortRange -Verbose | Set-AzNetworkSecurityGroup -Verbose
       }
    }

csv파일 경로만 스크립트에 지정해 주시면 됩니다. csv의 각 컬럼은 $_. 뒤의 내용으로 붙여넣으면 됩니다.

NSG Name,NSG Rule Name, ...이런식으로요

삭제도 동일하게 가능합니다.

#NSG Rule 삭제
param(
[Parameter(Mandatory=$True)]
[string]
$subscriptionId,
[string]
$resourceGroupName,
 
[string]
$nsgName,
 
[string]
$nsgRuleName,
[string]
$nsgRuleDescription,
[string]
$nsgRuleAccess,
[string]
$nsgRuleProtocol,
[string]
$nsgRuleDirection,
[string]
$nsgRulePriority,
[string]
$nsgRuleSourceAddressPrefix,
[string]
$nsgRuleSourcePortRange,
[string]
$nsgRuleDestinationAddressPrefix,
[string]
$nsgRuleDestinationPortRange,
$csvFilePath = "<csv파일경로>"
)
Import-Csv $csvFilePath |`
  ForEach-Object {
    $nsgName = $_."NSG Name"
    $nsgRuleName = $_."NSG Rule Name"
    $nsgRuleDescription = $_."NSG Rule Description"
    $nsgRuleAccess = $_."NSG Rule Access"
    $nsgRuleProtocol = $_."NSG Rule Protocol"
    $nsgRuleDirection = $_."NSG Rule Direction"
    $nsgRulePriority = $_."NSG Rule Priority"
    $nsgRuleSourceAddressPrefix = $_."NSG Rule Source Address Prefix"
    $nsgRuleSourcePortRange = $_."NSG Rule Source Port Range"
    $nsgRuleDestinationAddressPrefix = $_."NSG Rule Destination Address Prefix"
    $nsgRuleDestinationPortRange = $_."NSG Rule Destination Port Range"
    $resourceGroupName= $_."RG Name"
   
    $nsgRuleNameValue = Get-AzNetworkSecurityGroup -Name $nsgName -ResourceGroupName $resourceGroupName | Get-AzNetworkSecurityRuleConfig -Name $nsgRuleName -ErrorAction SilentlyContinue

       Get-AzNetworkSecurityGroup -Name $nsgName -ResourceGroupName $resourceGroupName | remove-AzNetworkSecurityRuleConfig -Name $nsgRuleName -Verbose | Set-AzNetworkSecurityGroup -Verbose
    }

NSG를 설정하다보면 아시겠지만, ASG를 적극적으로 활용하면 IP를 일일이 기억하지 않아도 되고 직관적이라 ASG도 추가해서 관리하는 부분도 해 보시면 좋을 것 같습니다.

enjoy azure.