---
title: Containers with added capabilities
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > Code Security > Infrastructure as Code (IaC)
  Security > IaC Security Rules > Containers with added capabilities
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# Containers with added capabilities

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com, us2.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md). ({% placeholder "user-datadog-site-name" /%}).
{% /alert %}

{% /callout %}

## Metadata{% #metadata %}

**Id:** `terraform-kubernetes-container-with-added-capabilities` 

**Provider:** Kubernetes

**Platform:** Terraform

**Severity:** Medium

**Category:** Insecure Configurations

#### Learn More{% #learn-more %}

- [Provider Reference](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/pod#capabilities-1)

### Description{% #description %}

Containers should not include added capabilities. This rule checks both `container` and `init_container` entries and ensures `security_context.capabilities.add` adds no capability. If the `add` list contains at least one capability, or is a value that cannot be resolved (such as a variable), the rule flags the resource with its location. An empty list (`add = []`) is not reported.

## Compliant Code Examples{% #compliant-code-examples %}

```terraform

resource "kubernetes_pod" "negative3" {
  metadata {
    name = "terraform-example"
  }

  spec {

    container =  [
      {
        image = "nginx:1.7.9"
        name  = "example"

        env = {
          name  = "environment"
          value = "test"
        }

        port = {
          container_port = 8080
        }

        liveness_probe = {
          http_get = {
            path = "/nginx_status"
            port = 80

            http_header = {
              name  = "X-Custom-Header"
              value = "Awesome"
            }
          }

          initial_delay_seconds = 3
          period_seconds        = 3
        }
      },

      {
        image = "nginx:1.7.9"
        name  = "example2"

        env = {
          name  = "environment"
          value = "test"
        }

        port = {
          container_port = 8080
        }

        liveness_probe = {
          http_get = {
            path = "/nginx_status"
            port = 80

            http_header = {
              name  = "X-Custom-Header"
              value = "Awesome"
            }
          }

          initial_delay_seconds = 3
          period_seconds        = 3
        }
      }
    ]

    dns_config {
      nameservers = ["1.1.1.1", "8.8.8.8", "9.9.9.9"]
      searches    = ["example.com"]

      option {
        name  = "ndots"
        value = 1
      }

      option {
        name = "use-vc"
      }
    }

    dns_policy = "None"
  }
}



resource "kubernetes_pod" "negative4" {
  metadata {
    name = "terraform-example"
  }

  spec {
    container {
      image = "nginx:1.7.9"
      name  = "example"

      env {
        name  = "environment"
        value = "test"
      }

      port {
        container_port = 8080
      }

      liveness_probe {
        http_get {
          path = "/nginx_status"
          port = 80

          http_header {
            name  = "X-Custom-Header"
            value = "Awesome"
          }
        }

        initial_delay_seconds = 3
        period_seconds        = 3
      }
    }


    dns_config {
      nameservers = ["1.1.1.1", "8.8.8.8", "9.9.9.9"]
      searches    = ["example.com"]

      option {
        name  = "ndots"
        value = 1
      }

      option {
        name = "use-vc"
      }
    }

    dns_policy = "None"
  }
}
```

```terraform
# ✅ Empty `add` lists add no Linux capability
resource "kubernetes_deployment" "empty_add" {
  metadata {
    name      = "tunnel"
    namespace = "default"
  }
  spec {
    replicas = 1
    selector {
      match_labels = { app = "tunnel" }
    }
    template {
      metadata {
        labels = { app = "tunnel" }
      }
      spec {
        init_container {
          name  = "init"
          image = "example.com/init:1.0.0"
          security_context {
            capabilities {
              add  = []
              drop = ["ALL"]
            }
          }
        }
        container {
          name  = "tunnel"
          image = "example.com/tunnel:1.0.0"
          security_context {
            allow_privilege_escalation = false
            privileged                 = false
            capabilities {
              add  = []
              drop = ["NET_RAW"]
            }
          }
        }
      }
    }
  }
}
```

## Non-Compliant Code Examples{% #non-compliant-code-examples %}

```terraform

resource "kubernetes_pod" "positive1" {
  metadata {
    name = "terraform-example"
  }

  spec {
    container = [
     {
      image = "nginx:1.7.9"
      name  = "example22"

      security_context = {
        capabilities = {
          add = ["NET_BIND_SERVICE"]
        }
      }

      env = {
        name  = "environment"
        value = "test"
      }

      port = {
        container_port = 8080
      }

      liveness_probe = {
        http_get = {
          path = "/nginx_status"
          port = 80

          http_header = {
            name  = "X-Custom-Header"
            value = "Awesome"
          }
        }

        initial_delay_seconds = 3
        period_seconds        = 3
      }
     }
     ,
     {
      image = "nginx:1.7.9"
      name  = "example22222"

       security_context = {
        capabilities = {
          add = ["NET_BIND_SERVICE"]
        }
      }

      env = {
        name  = "environment"
        value = "test"
      }

      port = {
        container_port = 8080
      }

      liveness_probe = {
        http_get = {
          path = "/nginx_status"
          port = 80

          http_header = {
            name  = "X-Custom-Header"
            value = "Awesome"
          }
        }

        initial_delay_seconds = 3
        period_seconds        = 3
      }
     }
   ]


    dns_config {
      nameservers = ["1.1.1.1", "8.8.8.8", "9.9.9.9"]
      searches    = ["example.com"]

      option {
        name  = "ndots"
        value = 1
      }

      option {
        name = "use-vc"
      }
    }

    dns_policy = "None"
  }
}



resource "kubernetes_pod" "positive2" {
  metadata {
    name = "terraform-example"
  }

  spec {
    container {
      image = "nginx:1.7.9"
      name  = "example"

      security_context {
        capabilities {
          add = ["NET_BIND_SERVICE"]
        }
      }

      env {
        name  = "environment"
        value = "test"
      }

      port {
        container_port = 8080
      }

      liveness_probe {
        http_get {
          path = "/nginx_status"
          port = 80

          http_header {
            name  = "X-Custom-Header"
            value = "Awesome"
          }
        }

        initial_delay_seconds = 3
        period_seconds        = 3
      }
    }


    dns_config {
      nameservers = ["1.1.1.1", "8.8.8.8", "9.9.9.9"]
      searches    = ["example.com"]

      option {
        name  = "ndots"
        value = 1
      }

      option {
        name = "use-vc"
      }
    }

    dns_policy = "None"
  }
}
```

```terraform
variable "extra_caps" {
  type = list(string)
}

resource "kubernetes_deployment" "added" {
  metadata {
    name      = "tunnel"
    namespace = "default"
  }
  spec {
    replicas = 1
    selector {
      match_labels = { app = "tunnel" }
    }
    template {
      metadata {
        labels = { app = "tunnel" }
      }
      spec {
        init_container {
          name  = "init"
          image = "example.com/init:1.0.0"
          security_context {
            capabilities {
              add  = ["SYS_ADMIN"]
              drop = ["ALL"]
            }
          }
        }
        container {
          name  = "tunnel"
          image = "example.com/tunnel:1.0.0"
          security_context {
            capabilities {
              add  = ["NET_ADMIN"]
              drop = ["NET_RAW"]
            }
          }
        }
      }
    }
  }
}

# ❌ Unresolved list: the added capabilities are unknown
resource "kubernetes_pod" "unresolved" {
  metadata {
    name = "unresolved"
  }
  spec {
    container {
      name  = "app"
      image = "example.com/app:1.0.0"
      security_context {
        capabilities {
          add = var.extra_caps
        }
      }
    }
  }
}
```
