Skip to content

nutanix_network_security_policy_v2 : Failed to add a rule on an existing security policy. #1052

@aharlaut

Description

@aharlaut

Nutanix Cluster Information

  • Nutanix Cluster (Prism Element / AOS) : 7.3.0.7
  • Nutanix Prism Central : pc.7.3.0.6

Terraform Version

Terraform v1.14.2 on windows_amd64
provider registry.terraform.io/nutanix/nutanix v2.3.4

Affected ressources :

  • nutanix_network_security_policy_v2

Terraform Configuration Files

terraform {
  required_version = ">= 1.14"

  required_providers {
    nutanix = {
      source  = "nutanix/nutanix"
      version = "2.3.4"
    }
  }
}

variable "nutanix_endpoint" {
  description = "Nutanix Prism Central endpoint (FQDN or IP)"
  type        = string
  sensitive   = true
}

variable "nutanix_username" {
  description = "Nutanix Prism Central username"
  type        = string
  sensitive   = true
}

variable "nutanix_password" {
  description = "Nutanix Prism Central password"
  type        = string
  sensitive   = true
}

variable "nutanix_insecure" {
  description = "Allow insecure TLS connections"
  type        = bool
  default     = true
}

variable "vpc_name" {
  description = "Name of the VPC to scope the security policy to"
  type        = string
  default     = "your-vpc-name"
}


provider "nutanix" {
  username  = var.nutanix_username
  password  = var.nutanix_password
  endpoint  = var.nutanix_endpoint
  insecure  = var.nutanix_insecure
  wait_timeout = 60
}

data "nutanix_vpcs_v2" "vpc" {
  filter = "name eq '${var.vpc_name}'"
}

# Address groups
resource "nutanix_address_groups_v2" "admin_network" {
  name        = "AdminNetwork"
  description = "Administrator network addresses"

  ipv4_addresses {
    value         = "10.0.1.0"
    prefix_length = 24
  }
}

# Categories
# Category for web servers
resource "nutanix_category_v2" "webservers" {
  key         = "AppType"
  value       = "WebServer"
  description = "Web server applications"
}

# Category for database servers
resource "nutanix_category_v2" "database" {
  key         = "AppType"
  value       = "Database"
  description = "Database server applications"
}

# Services groups
# HTTPS service
resource "nutanix_service_groups_v2" "https" {
  name        = "TEST_HTTPS"
  description = "HTTPS traffic"

  tcp_services {
    start_port = "443"
    end_port   = "443"
  }
}

# SSH service
resource "nutanix_service_groups_v2" "ssh" {
  name        = "TEST_SSH"
  description = "SSH traffic"

  tcp_services {
    start_port = "22"
    end_port   = "22"
  }
}

# MySQL service
resource "nutanix_service_groups_v2" "mysql" {
  name        = "TEST_MySQL"
  description = "MySQL database traffic "

  tcp_services {
    start_port = "3306"
    end_port   = "3306"
  }
}

# Security policies

resource "nutanix_network_security_policy_v2" "simple_security_policy" {
  name        = "simple_security_policy"
  description = "Simple security policy "
  type        = "APPLICATION"
  state       = "MONITOR"  

  is_ipv6_traffic_allowed = false
  is_hitlog_enabled       = true

  vpc_reference = [data.nutanix_vpcs_v2.vpc.vpcs[0].ext_id]
  scope         = "VPC_LIST"

  # =========================================================================
  # RULE 1: Allow admins to access web servers via HTTPS
  # =========================================================================
  rules {
    description = "Admin to WebServers HTTPS"
    type        = "APPLICATION"

    spec {
      application_rule_spec {
        # Destination: Web servers (using category)
        secured_group_category_references = [
          nutanix_category_v2.webservers.id
        ]

        # Source: Admin network (using IP address group)
        src_address_group_references = [
          nutanix_address_groups_v2.admin_network.ext_id
        ]

        # Service: HTTPS
        service_group_references = [
          nutanix_service_groups_v2.https.ext_id
        ]
      }
    }
  }

  # =========================================================================
  # RULE 2: Allow web servers to access database via MySQL
  # =========================================================================
  rules {
    description = "WebServers to Database MySQL"
    type        = "APPLICATION"

    spec {
      application_rule_spec {
        # Destination: Database servers (using category)
        secured_group_category_references = [
          nutanix_category_v2.database.id
        ]

        # Source: Web servers (using category)
        src_category_references = [
          nutanix_category_v2.webservers.id
        ]

        # Service: MySQL
        service_group_references = [
          nutanix_service_groups_v2.mysql.ext_id
        ]
      }
    }
  }
  # =========================================================================
  # RULE 3: Allow admin to msqsl 
  # =========================================================================

  # rules {
  #   description = "Admin to MSSQL"
  #   type        = "APPLICATION"

  #   spec {
  #     application_rule_spec {
  #       # Destination: database (using category)
  #       secured_group_category_references = [
  #         nutanix_category_v2.database.id
  #       ]

  #       # Source: admin network (using address group)
  #       src_address_group_references = [
  #         nutanix_address_groups_v2.admin_network.ext_id
  #       ]

  #       # Service: MYSQL
  #       service_group_references = [
  #         nutanix_service_groups_v2.mysql.ext_id
  #       ]
  #     }
  #   }
  # }



  # Intra-Group Rule 1: Deny traffic between webserver categories
  rules {
    description = "Deny intra-group traffic between WebServers"
    type        = "INTRA_GROUP"
    
    spec {
      intra_entity_group_rule_spec {
        secured_group_category_references = [
          nutanix_category_v2.webservers.id
        ]
        secured_group_action = "DENY"
      }
    }
  }
  
  # Intra-Group Rule 2: Deny traffic between database categories
  rules {
    description = "Deny intra-group traffic between database VMs"
    type        = "INTRA_GROUP"
    
    spec {
      intra_entity_group_rule_spec {
        secured_group_category_references = [
         nutanix_category_v2.database.id
        ]
        secured_group_action = "DENY"
      }
    }
  }
}

Debug Output

https://gist.github.com/aharlaut/4261439bc56b8d2b9988c5e03f66af75

Panic Output

Expected Behavior

New rules added on a existing security policy should work.

Actual Behavior

New rules added on a existing security policy is not working.
We have to delete first the security policy (using taint or destroy) and then reapply the security policy

Steps to Reproduce

Error 1 :

  1. terraform apply
  2. Uncomment the rule 3 on the security policy
  3. terraform apply with errors

Error 2 :

  1. terraform apply
  2. Uncomment the rule 3 on the security policy
  3. terraform apply with errors
  4. terraform apply without any changes

After a second apply without any modification the apply shows no changes.
The state is updated with 3 rules but the third rule is not configured on nutanix flow.

Please see the gist for more information
https://gist.github.com/aharlaut/4261439bc56b8d2b9988c5e03f66af75

Important Factors

Metadata

Metadata

Labels

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions