Review effective AppArmor protection for containers

Check node support and the effective profile, then apply the required AppArmor restrictions.

Description

If a Terraform-managed Linux Pod has no AppArmor annotation, check which profile the runtime applies to its container.

Potential impact

A missing profile may permit unnecessary file access. An overly restrictive profile can stop the application from working.

Remediation

Use RuntimeDefault or a reviewed Localhost profile on AppArmor-enabled nodes. For new configurations, use a Kubernetes API and provider that support securityContext.appArmorProfile. Check the effective profile and denial logs after startup.

Examples

The pre-1.30 annotation requests k8s-apparmor-example-allow-write for the example container. Load that profile on every eligible node first. For deployment, use a maintained image and test the file operations the service needs.

Before

hcl
resource "kubernetes_pod" "pod" {
  metadata {
    name = "terraform-example"
  }

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

After

hcl
resource "kubernetes_pod" "pod" {
  metadata {
    name = "terraform-example"
    annotations = {
      "container.apparmor.security.beta.kubernetes.io/example" = "localhost/k8s-apparmor-example-allow-write"
    }
  }

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

References