For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/iac_security/iac_rules/terraform-azure-security-group-is-not-configured.md. A documentation index is available at /llms.txt.
This product is not supported for your selected Datadog site. ().

Metadata

Id: terraform-azure-security-group-is-not-configured

Provider: Azure

Platform: Terraform

Severity: High

Category: Insecure Configurations

Learn More

Description

Network Security Groups (NSGs) in Azure provide filtering of network traffic to and from Azure resources within an Azure virtual network. When a subnet is not configured with an NSG, it lacks essential security controls that filter inbound and outbound traffic, potentially exposing resources to unauthorized access and network-based attacks.

A subnet is protected in one of two ways, and the rule checks both:

  • For subnets defined inline in an azurerm_virtual_network, the security_group attribute must be set to a non-empty NSG reference.
  • For standalone azurerm_subnet resources, an azurerm_subnet_network_security_group_association must reference the subnet.

Subnets that Azure doesn’t allow an NSG on (GatewaySubnet, AzureFirewallSubnet, AzureFirewallManagementSubnet and RouteServerSubnet) are not reported. Without this protection, workloads running in these subnets may be accessible from unwanted network sources, increasing the attack surface.

Secure example:

resource "azurerm_virtual_network" "example" {
  name                = "example-network"
  address_space       = ["10.1.2.0/24"]
  location            = "West US"
  resource_group_name = "example-rg"

  subnet {
    name           = "subnet1"
    address_prefix = "10.1.2.0/25"
    security_group = "securitygroup-id"
  }
}

resource "azurerm_subnet" "example" {
  name                 = "subnet2"
  resource_group_name  = "example-rg"
  virtual_network_name = "example-network"
  address_prefixes     = ["10.1.2.128/25"]
}

resource "azurerm_subnet_network_security_group_association" "example" {
  subnet_id                 = azurerm_subnet.example.id
  network_security_group_id = "securitygroup-id"
}

Vulnerable example:

resource "azurerm_virtual_network" "example" {
  name                = "example-network"
  address_space       = ["10.1.2.0/24"]
  location            = "West US"
  resource_group_name = "example-rg"

  subnet {
    name           = "subnet1"
    address_prefix = "10.1.2.0/25"
    // Missing security_group attribute
  }
}

// No azurerm_subnet_network_security_group_association references this subnet
resource "azurerm_subnet" "example" {
  name                 = "subnet2"
  resource_group_name  = "example-rg"
  virtual_network_name = "example-network"
  address_prefixes     = ["10.1.2.128/25"]
}

Compliant Code Examples

#this code is a correct code for which the query should not find any result
resource "azurerm_virtual_network" "negative1" {
  name                = "test-network"
  address_space       = ["10.1.2.0/24"]
  location            = "West US"
  resource_group_name = "rg"

  subnet {
    name           = "subnet1"
    address_prefix = "10.1.2.0/25"
    security_group = "a"
  }
}

resource "azurerm_subnet" "negative2" {
  name                 = "subnet2"
  resource_group_name  = "rg"
  virtual_network_name = "test-network"
  address_prefixes     = ["10.1.2.0/25"]
}

resource "azurerm_subnet_network_security_group_association" "negative2" {
  subnet_id                 = azurerm_subnet.negative2.id
  network_security_group_id = "nsg-id"
}

resource "azurerm_subnet" "negative3" {
  name                 = "GatewaySubnet"
  resource_group_name  = "rg"
  virtual_network_name = "test-network"
  address_prefixes     = ["10.1.3.0/27"]
}

resource "azurerm_subnet" "negative4" {
  count                = 2
  name                 = "subnet4-${count.index}"
  resource_group_name  = "rg"
  virtual_network_name = "test-network"
  address_prefixes     = ["10.1.4.0/27"]
}

resource "azurerm_subnet_network_security_group_association" "negative4" {
  count                     = 2
  subnet_id                 = azurerm_subnet.negative4[count.index].id
  network_security_group_id = "nsg-id"
}

resource "azurerm_subnet" "negative5" {
  for_each             = toset(["a", "b"])
  name                 = each.key
  resource_group_name  = "rg"
  virtual_network_name = "test-network"
  address_prefixes     = ["10.1.5.0/27"]
}

resource "azurerm_subnet_network_security_group_association" "negative5" {
  for_each                  = azurerm_subnet.negative5
  subnet_id                 = each.value.id
  network_security_group_id = "nsg-id"
}

Non-Compliant Code Examples

#this is a problematic code where the query should report a result(s)
resource "azurerm_virtual_network" "positive1" {
  name                = "test-network"
  address_space       = ["10.1.2.0/24"]
  location            = "West US"
  resource_group_name = "rg"

  subnet {
    name           = "subnet1"
    address_prefix = "10.1.2.0/25"
  }
}

resource "azurerm_virtual_network" "positive2" {
  name                = "test-network"
  address_space       = ["10.1.2.0/24"]
  location            = "West US"
  resource_group_name = "rg"

  subnet {
    name           = "subnet1"
    address_prefix = "10.1.2.0/25"
    security_group = ""
  }
}

resource "azurerm_subnet" "positive3" {
  name                 = "subnet3"
  resource_group_name  = "rg"
  virtual_network_name = "test-network"
  address_prefixes     = ["10.1.2.0/25"]
}