OnCallReadyStart learning

Security and secrets Lesson 23.23 · Kubernetes Security (CKS) · 5 min read

Static analysis: kubesec and kube-linter before you apply

In plain words

Before a letter goes out, a spell checker underlines the mistakes it knows about. kubesec and KubeLinter do that for Kubernetes files: one gives each file a score with the risky settings at the top, the other lists every problem with the fix next to it. You run them before anything reaches the cluster, so the mistake is fixed while it is still just text in a review.

Catch the risky manifest before it reaches the cluster

The problem. Admission policies stop a bad object at the API server, but by then the change is merged, the pipeline has run and someone is waiting. A scanner on the manifest itself catches the same mistakes at review time: a privileged container, an image without a tag, no resource limits. The CKS names two tools for it: kubesec and KubeLinter.

kubesec: a score

kubesec scan FILE checks each object against rules with points. Risky settings are critical (negative points): privileged -30, SYS_ADMIN -30, hostNetwork / hostPID / hostIPC -9, the docker socket -9, allowPrivilegeEscalation -7, secrets in env vars -5. Hardening passes with positive points: a service account +3, AppArmor +3, and +1 each for runAsNonRoot, readOnlyRootFilesystem, capabilities drop ALL, seccomp, requests and limits. A hardening rule you did not meet is listed under advise. Score 0 or less = exit code 2.

$ kubesec scan web.yaml
[
  {
    "object": "Deployment/web.shop",
    "valid": true,
    "fileName": "web.yaml",
    "message": "Failed with a score of -30 points",
    "score": -30,
    "scoring": {
      "critical": [
        {
          "id": "Privileged",
          "selector": "containers[] .securityContext .privileged == true",
          "reason": "Privileged containers can allow almost completely unrestricted host access",
          "points": -30
        }
      ],
      "advise": [
        {
          "id": "ApparmorAny",
          ...

The output is JSON, so in a pipeline you ask it questions: kubesec scan web.yaml | jq -r '.[0].scoring.critical[].id'.

KubeLinter: findings with a fix

kube-linter lint PATH (a file or a directory) runs named checks and prints one line per finding, with the check and its remediation, then fails:

$ kube-linter lint web.yaml
KubeLinter 0.8.3

web.yaml: (object: shop/web apps/v1, Kind=Deployment) The container "web" is using an invalid container
image, "nginx:latest". Please use images that are not blocked by the `BlockList` criteria :
[".*:(latest)$" "^[^:]*$" "(.*/[^:]+)$"] (check: latest-tag, remediation: Use a container image with a
specific tag other than latest.)

web.yaml: (object: shop/web apps/v1, Kind=Deployment) container "web" is privileged (check:
privileged-container, remediation: Do not run your container as privileged unless it is required.)
...
Error: found 7 lint errors
kubesecKubeLinter
Outputa score per object, JSONa finding per problem, plain or JSON
Fails whenthe score is 0 or less (exit 2)any finding (exit 1)
Looks atone object at a timethe object and its neighbours (Service selectors, ServiceAccounts)
Silence one--rules picks rules--exclude CHECK, or the annotation ignore-check.kube-linter.io/<check>

Read the scores critically. A kubesec score above 0 is not "secure": it is a sum, and one privileged container (-30) outweighs every hardening point. KubeLinter does not know your app: an ignore annotation with a reason is better than a check that everyone learns to skip.

In an interview: "static analysis in CI finds the risky manifest before review is over; admission policies stop whatever still gets through".

On the exam: run the tool on the file you are given, read which rule or check fails, fix the manifest, and run it again until it is clean.

Why it helps

Fixing a manifest in review is cheaper than fixing a deploy that admission refused or a pod that already runs privileged. Both tools run in seconds in CI, the CKS names them in its supply chain domain, and reading their output well (a positive score is not the same as secure) is a skill reviewers use every day. They also teach which securityContext fields matter, and why.

On the blog

Commands in this lesson

kubesec kube-linter

FAQ

How does kubesec score a manifest?

Every rule that matches adds its points: risky settings such as privileged, SYS_ADMIN, host namespaces or the docker socket are critical with negative points, and hardening such as runAsNonRoot, a read-only root filesystem or dropped capabilities adds positive points. Hardening you did not set is listed under advise. A score of 0 or less exits 2.

What does KubeLinter report?

One line per finding: the file, the object, a message, the check's name and its remediation, for example latest-tag or no-read-only-root-fs. Any finding makes it exit 1, so it works directly as a CI gate. Some checks, like dangling-service, look at the other objects in the same run.

Is a positive kubesec score secure?

No. The score is a sum, and one privileged container at -30 outweighs every hardening point together. Read the critical list first: a manifest with no critical finding and a few hardening points is far better than a high score that still has a critical one.

How do I silence a KubeLinter check for one object?

With the annotation ignore-check.kube-linter.io/<check> on the object, with the reason as its value, or --exclude for the whole run. An annotation with a reason documents a deliberate choice; excluding a check for everyone hides future mistakes too.

How do static analysis and admission policies fit together?

Static analysis runs on the files in CI and reports problems while the change is still in review. Admission policies run in the API server and refuse whatever still arrives, including objects created by hand or by tools that never went through CI. You want both: early feedback and a final gate.

In an interview Mid

How would you stop insecure Kubernetes manifests from being merged?

Run static analysis in the pipeline on every manifest and rendered chart: kube-linter lint fails the job on any finding such as a privileged container or an image without a tag, and kubesec scan scores each object and fails on a score of 0 or less, with the critical findings listed first. Fix the manifest, rescan, and keep admission policies in the cluster as the second gate for anything that bypasses CI.

Also asked: What is the difference between kubesec and KubeLinter? · Why is a positive kubesec score not enough? · Where in a pipeline would you run these scanners?

Practise this lesson in the terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.