Skip to main content

Kubernetes

A Continuous Compliance Framework plugin that collects Kubernetes resources and evaluates OPA/Rego policies against them.

Supports multiple authentication methods (EKS via AWS STS, kubeconfig for any cluster) and concurrent multi-cluster collection.

Breaking change in this release. The plugin now implements the RunnerV2 interface and evaluates policies per individual resource rather than per cluster. The Rego input schema has moved from v1 (input.clusters[...]) to v2 (input.main + input.context). Existing policies that read input.clusters[...] must be rewritten. Companion policy repos (plugin-k8s-policies, plugin-k8s-opres-policies) will be updated in a coordinated follow-up.

This page is for operators who run the plugin-k8s plugin with the CCF Agent, and for developers who write policies for it. It documents v0.2.4 and is generated from the plugin's README at v0.2.4.

Configuration​

Plugin Configuration​

The plugin receives configuration as flat string fields from the CCF agent. All structured values are JSON-encoded strings.

FieldRequiredDescription
clustersYesJSON array of cluster connection configs
resourcesYesJSON array of Kubernetes resource types to collect (e.g. "nodes", "pods", "deployments")
main_resourcesNoJSON array — subset of resources that produces per-instance subjects and evidence. Defaults to all of resources. Types in resources but not in main_resources are collected as policy context only.
identity_labelsNoJSON object mapping identity-label key → ordered list of metadata.labels keys to try. Default: {"app_name": ["app.kubernetes.io/name", "app"]}. First match wins; falls back to metadata.name when none are present.
namespace_includeNoJSON array of namespaces to include (empty = all)
namespace_excludeNoJSON array of namespaces to exclude
policy_labelsNoJSON object of key-value labels added to evidence metadata
policy_inputNoJSON object of custom fields merged into the Rego input document. Reserved keys: schema_version, source, main, context, subject, fleet.

Cluster Configuration​

Each entry in clusters has:

FieldRequiredProviderDescription
nameYesallUnique display name for this cluster
providerNoall"eks" (default) or "kubeconfig"
regionYeseksAWS region
cluster_nameYeseksEKS cluster name
role_arnNoeksIAM role ARN to assume before authenticating
kubeconfigNokubeconfigPath to kubeconfig file (default: ~/.kube/config)
contextNokubeconfigKubeconfig context to use (default: current-context)

Configuration Examples​

Single EKS cluster — evidence per pod, nodes as context​

{
"clusters": "[{\"name\":\"prod\",\"region\":\"us-east-1\",\"cluster_name\":\"prod-eks\"}]",
"resources": "[\"nodes\",\"pods\"]",
"main_resources": "[\"pods\"]",
"namespace_exclude": "[\"kube-system\",\"kube-public\"]",
"policy_labels": "{\"team\":\"platform\",\"environment\":\"production\"}",
"policy_input": "{\"expected_azs\":[\"us-east-1a\",\"us-east-1b\",\"us-east-1c\"]}"
}

Custom identity label​

{
"clusters": "[{\"name\":\"prod\",\"region\":\"us-east-1\",\"cluster_name\":\"prod-eks\"}]",
"resources": "[\"pods\"]",
"identity_labels": "{\"app_name\":[\"app.company.io/service\",\"app.kubernetes.io/name\"],\"team\":[\"team.company.io/owner\"]}"
}

EKS with cross-account role assumption​

{
"clusters": "[{\"name\":\"prod\",\"region\":\"us-east-1\",\"cluster_name\":\"prod-eks\",\"role_arn\":\"arn:aws:iam::123456789012:role/eks-readonly\"}]",
"resources": "[\"nodes\",\"pods\"]",
"policy_input": "{\"expected_azs\":[\"us-east-1a\",\"us-east-1b\"]}"
}

Local cluster via kubeconfig (kind, minikube, k3s)​

{
"clusters": "[{\"name\":\"local-dev\",\"provider\":\"kubeconfig\"}]",
"resources": "[\"pods\",\"services\",\"deployments\"]",
"namespace_include": "[\"default\",\"app\"]"
}

Multi-cluster (mixed providers)​

{
"clusters": "[{\"name\":\"prod-east\",\"region\":\"us-east-1\",\"cluster_name\":\"prod-east-eks\"},{\"name\":\"prod-west\",\"region\":\"us-west-2\",\"cluster_name\":\"prod-west-eks\"},{\"name\":\"dev\",\"provider\":\"kubeconfig\",\"context\":\"kind-dev\"}]",
"resources": "[\"nodes\",\"pods\"]",
"main_resources": "[\"pods\"]",
"namespace_exclude": "[\"kube-system\"]",
"policy_input": "{\"expected_azs\":[\"us-east-1a\",\"us-east-1b\",\"us-west-2a\",\"us-west-2b\"]}"
}

Evidence collected​

Subjects and evidence model​

During Init, the plugin registers one SubjectTemplate per entry in main_resources (e.g. k8s-pods, k8s-nodes). Each template has the identity label keys:

  • cluster_name
  • namespace (only for namespaced resources)
  • app_name (resolved from metadata.labels via identity_labels; falls back to metadata.name)
  • name

For cluster-scoped resources such as nodes, the emitted evidence labels still include namespace with an empty string so label contracts stay stable, but the corresponding SubjectTemplate omits namespace from its identity keys and rendering.

During Eval, every concrete Kubernetes resource instance that matches a main_resources type becomes its own subject and receives its own evidence for every configured policy path. Policies are invoked once per (resource instance, policy path) pair. Evidence is batched per cluster, and a batch is sent with CreateEvidence when it reaches 100 evidences or when the policy-evaluation data it holds reaches a byte limit, whichever comes first.

Each evidence carries a JSON copy of the Rego input it was evaluated against, and that input includes every collected resource in the fleet (input.context and input.fleet). With large clusters a single input can be tens of MiB, so the byte limit is what keeps memory bounded.

Environment variables​

VariableDefaultDescription
CCF_K8S_EVIDENCE_BATCH_MAX_BYTES67108864 (64 MiB)Maximum total size, in bytes, of the policy-evaluation input and policy data held in one evidence batch. Lower it if the plugin runs out of memory. Invalid or non-positive values fall back to the default with a warning.

The agent passes its environment through to plugin processes, so set this on the agent.

Policy input​

The plugin implements input schema v2 only: a policy runs once per resource instance and policy path, and receives one document as input with the resource under evaluation in input.main and the whole cluster snapshot in input.context. There is no input.clusters, the shape of schema v1. Only resource types listed in main_resources (all of resources when it is not set) are evaluated; the others are collected and appear in input.context.resources and input.fleet as context only. Fields are listed by their path inside the document. Resources are the unstructured objects the Kubernetes API returns, so only their top level is described here; the keys of input.context.resources are the resource names from the resources setting in lower case. A field marked as left out is missing from the document, not null. A policy sees the data of the cluster being evaluated in input.context, and the data of every cluster in input.fleet. Every evidence carries a JSON copy of this input, which includes every collected resource, so the document can be large. Your own policy data from the agent configuration is in data.<key>, not in input. Custom fields from the policy_input setting are merged into the top level of the document. That setting is a JSON object, and the plugin refuses to start when it holds one of the reserved keys schema_version, source, main, subject, context or fleet, so a custom field can never replace what the plugin sets.

FieldTypeDescription
schema_versionstringAlways v2.
sourcestringAlways plugin-kubernetes.
mainobjectThe Kubernetes resource under evaluation, as the API returns it (apiVersion, kind, metadata, spec, status and the rest).
subjectobjectIdentity of the resource under evaluation. The fields below are inside it.
subject.cluster_namestringName of the cluster, the name set for it in clusters.
subject.resource_typestringResource type, as listed in main_resources, such as pods.
subject.namestringThe resource's metadata.name.
subject.namespacestringThe resource's metadata.namespace. Left out for cluster-scoped resources.
subject.identifierstringStable identifier of the resource's subject, k8s-<resource type>/<cluster>/[<namespace>/]<app name>/<name>, each part lower case with characters other than letters, digits and - replaced.
subject.identity_labelsobject (string to string)One entry per key of identity_labels (app_name by default). The value is the first of the key's metadata.labels that the resource has, looked up in the resource's labels, its pod template labels and its selector matchLabels; the resource's metadata.name when none match.
contextobjectThe cluster the resource belongs to, with its snapshot.
context.clusterobjectCluster the resource belongs to, with name, region and provider (eks or kubeconfig). region is empty for a kubeconfig cluster that has none.
context.resourcesobject (string to array of objects)Every collected resource of this cluster, keyed by resource type. Includes the resource in main and the types that are in resources only.
fleetobjectSnapshot of all clusters collected in this run.
fleet.clustersobjectKeyed by cluster name. Each entry has cluster (the same name, region and provider object as context.cluster) and resources (the same map as context.resources).
<custom key>anyA field you set in the policy_input setting, merged at the top level. Not a reserved key.

An example input:

{
"schema_version": "v2",
"source": "plugin-kubernetes",
"main": {
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "web-7d9f8c6b5-abcde",
"namespace": "shop",
"uid": "11111111-2222-3333-4444-555555555555",
"labels": {
"app.kubernetes.io/name": "web"
}
},
"spec": {
"nodeName": "ip-10-0-1-23.example.internal",
"containers": [
{
"name": "web",
"image": "registry.example.com/shop/web:1.4.2"
}
]
},
"status": {
"phase": "Running"
}
},
"subject": {
"cluster_name": "prod",
"resource_type": "pods",
"name": "web-7d9f8c6b5-abcde",
"namespace": "shop",
"identifier": "k8s-pods/prod/shop/web/web-7d9f8c6b5-abcde",
"identity_labels": {
"app_name": "web"
}
},
"context": {
"cluster": {
"name": "prod",
"region": "us-east-1",
"provider": "eks"
},
"resources": {
"nodes": [
{
"apiVersion": "v1",
"kind": "Node",
"metadata": {
"name": "ip-10-0-1-23.example.internal"
}
}
],
"pods": [
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "web-7d9f8c6b5-abcde",
"namespace": "shop"
}
}
]
}
},
"fleet": {
"clusters": {
"prod": {
"cluster": {
"name": "prod",
"region": "us-east-1",
"provider": "eks"
},
"resources": {
"nodes": [
{
"apiVersion": "v1",
"kind": "Node",
"metadata": {
"name": "ip-10-0-1-23.example.internal"
}
}
],
"pods": [
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "web-7d9f8c6b5-abcde",
"namespace": "shop"
}
}
]
}
}
}
},
"expected_azs": [
"us-east-1a",
"us-east-1b"
]
}

Subject templates​

The subject templates this plugin registers. Each example is built from sample data.

k8s-pods​

One template for each type in main_resources (all of resources when it is not set), registered when the agent calls Init; the name is k8s- and the resource type in lower case, so main_resources of ["pods", "nodes"] registers k8s-pods and k8s-nodes. The first example is for a namespaced type, pods. Every template is a component with the same identity keys, a title of Kubernetes <type> [<namespace>/]<name> in <cluster_name>, a description of Kubernetes <type> <name> in cluster <cluster_name>[ under namespace <namespace>] and the purpose Individual Kubernetes <type> instance evaluated by the Kubernetes plugin. The namespace part is left out of the title and description when the namespace is empty. Evidence also carries resource_type and the labels from policy_labels, which are not part of the identity. Each evidence is also linked to a cluster component, k8s-cluster/<cluster name>.

Identity keyDescription
cluster_nameName of the cluster the resource belongs to.
namespaceNamespace of the resource. An empty string for cluster-scoped resources such as nodes.
app_nameApplication name, resolved through identity_labels. When you add keys to identity_labels, their values go in evidence labels but the template's identity keys stay these four.
nameThe resource's metadata.name.

Example:

{
"name": "k8s-pods",
"type": "component",
"title": "Kubernetes pods shop/web-7d9f8c6b5-abcde in prod",
"description": "Kubernetes pods web-7d9f8c6b5-abcde in cluster prod under namespace shop",
"purpose": "Individual Kubernetes pods instance evaluated by the Kubernetes plugin.",
"identityLabels": {
"cluster_name": "prod",
"namespace": "shop",
"app_name": "web",
"name": "web-7d9f8c6b5-abcde"
}
}

k8s-nodes​

The same template for a cluster-scoped type, here nodes. The namespace is empty, so the evidence label namespace is an empty string and the title and description leave the namespace out.

Identity keyDescription
cluster_nameName of the cluster the node belongs to.
namespaceAlways an empty string for nodes.
app_nameFalls back to the node's name, since nodes rarely carry an application label.
nameThe node's metadata.name.

Example:

{
"name": "k8s-nodes",
"type": "component",
"title": "Kubernetes nodes ip-10-0-1-23.example.internal in prod",
"description": "Kubernetes nodes ip-10-0-1-23.example.internal in cluster prod",
"purpose": "Individual Kubernetes nodes instance evaluated by the Kubernetes plugin.",
"identityLabels": {
"cluster_name": "prod",
"namespace": "",
"app_name": "ip-10-0-1-23.example.internal",
"name": "ip-10-0-1-23.example.internal"
}
}

Policies​

The policy bundles that work with this plugin, at their newest releases. The controls are the ones the bundle's policies list in their metadata.

BundleLatest versionControls coveredCompatible plugin lines
plugin-k8s-opres-policiesv0.2.1none listedlatest

Policy data​

Any policy_data set on this plugin's block in the agent configuration is passed to every policy evaluation and is available to Rego as data.<key> (unlike policy_input, which is merged into input):

plugins:
kubernetes:
# ...source, policies and config as usual...
policy_data:
allowed_registries:
- ghcr.io/compliance-framework

Example: writing a policy​

package compliance_framework.pod_has_node_binding

import rego.v1

violation contains {"remarks": msg} if {
input.main.kind == "Pod"
not input.main.spec.nodeName
msg := sprintf("Pod %q has no nodeName assigned", [input.main.metadata.name])
}

# Cross-reference sibling resources via input.context
violation contains {"remarks": msg} if {
input.main.kind == "Pod"
node_name := input.main.spec.nodeName
not node_exists(node_name)
msg := sprintf("Pod %q binds to unknown node %q", [input.main.metadata.name, node_name])
}

node_exists(name) if {
some n in input.context.resources.nodes
n.metadata.name == name
}

title := "Pod node binding"
description := "Every pod must bind to a known node in its cluster."

Key patterns:

  • input.main is always the single resource under evaluation.
  • input.context.resources.<type> is the full cluster snapshot; iterate with some r in ....
  • input.context.cluster gives the cluster name/region/provider for labels and messaging.
  • Filter out the main resource from peer checks with r.metadata.uid != input.main.metadata.uid.