github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/rules/kubernetes/policies/general/capabilities_no_drop_all.rego (about)

     1  # METADATA
     2  # title: "Default capabilities: some containers do not drop all"
     3  # description: "The container should drop all default capabilities and add only those that are needed for its execution."
     4  # scope: package
     5  # schemas:
     6  # - input: schema["kubernetes"]
     7  # related_resources:
     8  # - https://kubesec.io/basics/containers-securitycontext-capabilities-drop-index-all/
     9  # custom:
    10  #   id: KSV003
    11  #   avd_id: AVD-KSV-0003
    12  #   severity: LOW
    13  #   short_code: drop-default-capabilities
    14  #   recommended_action: "Add 'ALL' to containers[].securityContext.capabilities.drop."
    15  #   input:
    16  #     selector:
    17  #     - type: kubernetes
    18  package builtin.kubernetes.KSV003
    19  
    20  import data.lib.kubernetes
    21  
    22  default checkCapsDropAll = false
    23  
    24  # Get all containers which include 'ALL' in security.capabilities.drop
    25  getCapsDropAllContainers[container] {
    26  	allContainers := kubernetes.containers[_]
    27  	lower(allContainers.securityContext.capabilities.drop[_]) == "all"
    28  	container := allContainers.name
    29  }
    30  
    31  # Get all containers which don't include 'ALL' in security.capabilities.drop
    32  getCapsNoDropAllContainers[container] {
    33  	container := kubernetes.containers[_]
    34  	not getCapsDropAllContainers[container.name]
    35  }
    36  
    37  deny[res] {
    38  	container := getCapsNoDropAllContainers[_]
    39  	msg := kubernetes.format(sprintf("Container '%s' of %s '%s' should add 'ALL' to 'securityContext.capabilities.drop'", [container.name, kubernetes.kind, kubernetes.name]))
    40  	res := result.new(msg, container)
    41  }