OCI Network Firewall: Stateful Inspection, IDS/IPS, and URL Filtering with Terraform

OCI Network Firewall is a managed layer 7 firewall powered by Palo Alto Networks Panorama. It provides stateful traffic inspection, intrusion detection and prevention, URL-based content filtering, and SSL/TLS inspection for encrypted traffic. Deployed as a hub service in your VCN, it inspects traffic between subnets, between VCNs, and to and from the internet. This post covers building a production firewall policy with Terraform and routing traffic through the firewall using route tables.

Step 1: Firewall Policy and Rules

resource "oci_network_firewall_network_firewall_policy" "production_policy" {
  compartment_id = var.compartment_id
  display_name   = "production-network-firewall-policy"

  # Address Lists for rule targeting
  address_lists {
    name  = "app-tier-cidr"
    type  = "CIDR"
    addresses = [var.app_subnet_cidr]
  }

  address_lists {
    name  = "db-tier-cidr"
    type  = "CIDR"
    addresses = [var.db_subnet_cidr]
  }

  address_lists {
    name  = "trusted-admin-cidr"
    type  = "CIDR"
    addresses = [var.admin_subnet_cidr, var.vpn_client_cidr]
  }

  # Service Lists
  service_lists {
    name = "web-services"
    services {
      type             = "TCP_SERVICE"
      minimum_port     = 443
      maximum_port     = 443
    }
    services {
      type         = "TCP_SERVICE"
      minimum_port = 80
      maximum_port = 80
    }
  }

  service_lists {
    name = "oracle-db-listener"
    services {
      type         = "TCP_SERVICE"
      minimum_port = 1521
      maximum_port = 1521
    }
    services {
      type         = "TCP_SERVICE"
      minimum_port = 1522
      maximum_port = 1522
    }
  }

  # Security Rules
  security_rules {
    name   = "allow-app-to-db"
    action = "ALLOW"
    condition {
      sources      = ["app-tier-cidr"]
      destinations = ["db-tier-cidr"]
      services     = ["oracle-db-listener"]
    }
    position { after_rule = null }
  }

  security_rules {
    name   = "allow-admin-access"
    action = "ALLOW"
    condition {
      sources = ["trusted-admin-cidr"]
    }
    position { after_rule = "allow-app-to-db" }
  }

  security_rules {
    name   = "inspect-outbound-web"
    action = "INSPECT"
    inspection = "INTRUSION_DETECTION"
    condition {
      sources  = ["app-tier-cidr"]
      services = ["web-services"]
    }
    position { after_rule = "allow-admin-access" }
  }

  security_rules {
    name   = "deny-all"
    action = "DROP"
    condition { }
    position { after_rule = "inspect-outbound-web" }
  }

  defined_tags = { "Operations.Environment" = "production", "Operations.ManagedBy" = "terraform" }
}

output "policy_id" { value = oci_network_firewall_network_firewall_policy.production_policy.id }

Step 2: Deploy the Firewall

resource "oci_network_firewall_network_firewall" "hub_firewall" {
  compartment_id              = var.compartment_id
  display_name                = "hub-network-firewall"
  network_firewall_policy_id  = oci_network_firewall_network_firewall_policy.production_policy.id
  subnet_id                   = var.firewall_subnet_id
  availability_domain         = data.oci_identity_availability_domains.ads.availability_domains[0].name
  ipv4address                 = "10.0.0.10"

  defined_tags = { "Operations.Environment" = "production", "Operations.ManagedBy" = "terraform" }
}

output "firewall_ip" {
  value       = oci_network_firewall_network_firewall.hub_firewall.ipv4address
  description = "Route traffic through this IP in your VCN route tables"
}

output "firewall_id" { value = oci_network_firewall_network_firewall.hub_firewall.id }

Step 3: Route Tables to Send Traffic Through Firewall

resource "oci_core_route_table" "app_subnet_via_firewall" {
  compartment_id = var.compartment_id
  vcn_id         = var.vcn_id
  display_name   = "app-subnet-route-via-firewall"

  route_rules {
    destination       = "0.0.0.0/0"
    destination_type  = "CIDR_BLOCK"
    network_entity_id = oci_network_firewall_network_firewall.hub_firewall.id
    description       = "All app traffic inspected by Network Firewall"
  }
}

resource "oci_core_route_table" "igw_ingress_via_firewall" {
  compartment_id = var.compartment_id
  vcn_id         = var.vcn_id
  display_name   = "igw-ingress-route-via-firewall"

  route_rules {
    destination       = var.app_subnet_cidr
    destination_type  = "CIDR_BLOCK"
    network_entity_id = oci_network_firewall_network_firewall.hub_firewall.id
    description       = "Inbound internet traffic inspected before reaching app subnet"
  }
}

Step 4: Threat Detection Alarm

resource "oci_monitoring_alarm" "firewall_threats_detected" {
  compartment_id        = var.compartment_id
  display_name          = "network-firewall-threats-detected"
  is_enabled            = true
  metric_compartment_id = var.compartment_id
  namespace             = "oci_network_firewall"
  query                 = "ThreatCount[5m]{firewallId = 'FIREWALL_OCID'}.sum() > 0"
  severity              = "CRITICAL"
  pending_duration      = "PT5M"
  destinations          = [var.security_notification_topic_id]
  body                  = "Network Firewall IDS/IPS detected threats. Review Logging Analytics for detailed threat signatures and source IPs."
}

Operational Notes

Traffic must be explicitly routed through the firewall using VCN route tables. Simply deploying the firewall does not inspect any traffic. The app subnet route table must send its default route to the firewall IP. The internet gateway ingress route table must send app subnet-destined traffic to the firewall IP. Both directions must pass through the firewall for symmetric inspection, which is required for stateful TCP tracking.

The INSPECT action with INTRUSION_DETECTION logs detected threats without dropping the traffic. Change it to INTRUSION_PREVENTION to block as well as log. Start with INTRUSION_DETECTION in a new deployment to understand what the firewall would block before enabling prevention mode, as aggressive IPS signatures can block legitimate traffic and cause application failures.

Regards,
Osama

#OCI #OracleCloud #NetworkFirewall #IDSIPS #Terraform #IaC #TechBlog #Oracle #NetworkSecurity #CloudSecurity #PlatformEngineering #ZeroTrust #OracleCloudInfrastructure #PaloAlto #ThreatDetection #StatefulInspection #URLFiltering #HubAndSpoke #PolicyEnforcement #DeepPacketInspection

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.