kubesift 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kubesift/__init__.py +3 -0
- kubesift/analysis/__init__.py +3 -0
- kubesift/analysis/changes.py +165 -0
- kubesift/analysis/correlate.py +160 -0
- kubesift/analysis/describe.py +83 -0
- kubesift/analysis/engine.py +362 -0
- kubesift/analysis/llm.py +152 -0
- kubesift/checks/__init__.py +17 -0
- kubesift/checks/base.py +150 -0
- kubesift/checks/config_refs.py +84 -0
- kubesift/checks/events.py +77 -0
- kubesift/checks/hygiene.py +264 -0
- kubesift/checks/lifecycle.py +202 -0
- kubesift/checks/network.py +325 -0
- kubesift/checks/nodes.py +183 -0
- kubesift/checks/operations.py +319 -0
- kubesift/checks/pods.py +485 -0
- kubesift/checks/storage.py +72 -0
- kubesift/checks/workloads.py +201 -0
- kubesift/cli.py +874 -0
- kubesift/cluster/__init__.py +4 -0
- kubesift/cluster/client.py +141 -0
- kubesift/cluster/snapshot.py +513 -0
- kubesift/config.py +134 -0
- kubesift/cost.py +150 -0
- kubesift/drill_scenarios/01-crashloop-missing-env.yaml +24 -0
- kubesift/drill_scenarios/02-imagepull-wrong-tag.yaml +23 -0
- kubesift/drill_scenarios/03-oomkilled-low-memory.yaml +24 -0
- kubesift/drill_scenarios/04-service-selector-mismatch.yaml +34 -0
- kubesift/drill_scenarios/05-missing-probe-crashloop.yaml +24 -0
- kubesift/drill_scenarios/06-pending-insufficient-resources.yaml +26 -0
- kubesift/drill_scenarios/07-missing-secret.yaml +28 -0
- kubesift/drill_scenarios/08-pvc-missing-storageclass.yaml +38 -0
- kubesift/drill_scenarios/09-netpol-blocks-dns.yaml +14 -0
- kubesift/drill_scenarios/10a-rollout-good.yaml +27 -0
- kubesift/drill_scenarios/10b-rollout-bad.yaml +25 -0
- kubesift/drill_scenarios/__init__.py +1 -0
- kubesift/drills.py +122 -0
- kubesift/knowledge.py +51 -0
- kubesift/leader.py +145 -0
- kubesift/lint.py +89 -0
- kubesift/mcp_server.py +193 -0
- kubesift/models.py +144 -0
- kubesift/notify.py +71 -0
- kubesift/postmortem.py +152 -0
- kubesift/preflight.py +435 -0
- kubesift/redact.py +38 -0
- kubesift/remediate.py +99 -0
- kubesift/report.py +193 -0
- kubesift/runbooks.py +58 -0
- kubesift/server/__init__.py +0 -0
- kubesift/server/app.py +480 -0
- kubesift/server/static/index.html +152 -0
- kubesift/slack.py +106 -0
- kubesift/store.py +219 -0
- kubesift/timetravel.py +156 -0
- kubesift/upgrade.py +51 -0
- kubesift/util.py +174 -0
- kubesift-2.0.0.dist-info/METADATA +192 -0
- kubesift-2.0.0.dist-info/RECORD +63 -0
- kubesift-2.0.0.dist-info/WHEEL +4 -0
- kubesift-2.0.0.dist-info/entry_points.txt +3 -0
- kubesift-2.0.0.dist-info/licenses/LICENSE +202 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""References from pods to ConfigMaps, Secrets and pull secrets that don't exist."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Iterator
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from kubesift.checks.base import Context, check
|
|
9
|
+
from kubesift.models import Category, Finding, Severity
|
|
10
|
+
from kubesift.util import containers_of, meta
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _refs(pod_spec: dict[str, Any]) -> Iterator[tuple[str, str, str | None, str]]:
|
|
14
|
+
"""Yield (kind, name, key, where) for every non-optional reference."""
|
|
15
|
+
for c in containers_of(pod_spec, include_init=True):
|
|
16
|
+
cname = c.get("name", "")
|
|
17
|
+
for ef in c.get("envFrom") or []:
|
|
18
|
+
for kind, field in (("ConfigMap", "configMapRef"), ("Secret", "secretRef")):
|
|
19
|
+
ref = ef.get(field)
|
|
20
|
+
if ref and not ref.get("optional"):
|
|
21
|
+
yield kind, ref.get("name", ""), None, f"envFrom in container {cname}"
|
|
22
|
+
for env in c.get("env") or []:
|
|
23
|
+
vf = env.get("valueFrom") or {}
|
|
24
|
+
for kind, field in (("ConfigMap", "configMapKeyRef"), ("Secret", "secretKeyRef")):
|
|
25
|
+
ref = vf.get(field)
|
|
26
|
+
if ref and not ref.get("optional"):
|
|
27
|
+
yield kind, ref.get("name", ""), ref.get("key"), f"env {env.get('name')} in container {cname}"
|
|
28
|
+
for v in pod_spec.get("volumes") or []:
|
|
29
|
+
if v.get("configMap") and not v["configMap"].get("optional"):
|
|
30
|
+
yield "ConfigMap", v["configMap"].get("name", ""), None, f"volume {v.get('name')}"
|
|
31
|
+
if v.get("secret") and not v["secret"].get("optional"):
|
|
32
|
+
yield "Secret", v["secret"].get("secretName", ""), None, f"volume {v.get('name')}"
|
|
33
|
+
for src in (v.get("projected") or {}).get("sources") or []:
|
|
34
|
+
if src.get("configMap") and not src["configMap"].get("optional"):
|
|
35
|
+
yield "ConfigMap", src["configMap"].get("name", ""), None, f"projected volume {v.get('name')}"
|
|
36
|
+
if src.get("secret") and not src["secret"].get("optional"):
|
|
37
|
+
yield "Secret", src["secret"].get("name", ""), None, f"projected volume {v.get('name')}"
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@check("config-ref-missing", "Referenced ConfigMap or Secret does not exist", Category.CONFIG, Severity.CRITICAL)
|
|
41
|
+
def missing_refs(ctx: Context) -> Iterator[Finding]:
|
|
42
|
+
"""A pod references a ConfigMap/Secret (or a key in one) that is missing from its namespace."""
|
|
43
|
+
snap = ctx.snap
|
|
44
|
+
for pod in snap.all("pods"):
|
|
45
|
+
if (pod.get("status") or {}).get("phase") in ("Succeeded", "Failed"):
|
|
46
|
+
continue
|
|
47
|
+
ns = meta(pod).get("namespace")
|
|
48
|
+
problems: list[str] = []
|
|
49
|
+
for kind, name, key, where in _refs(pod.get("spec") or {}):
|
|
50
|
+
store = "configmaps" if kind == "ConfigMap" else "secrets"
|
|
51
|
+
if not snap.can_read(store):
|
|
52
|
+
continue
|
|
53
|
+
obj = snap.get(store, ns, name)
|
|
54
|
+
if obj is None:
|
|
55
|
+
problems.append(f"{kind} '{name}' (used by {where}) does not exist in namespace {ns}")
|
|
56
|
+
elif key and key not in (obj.get("keys") or []):
|
|
57
|
+
problems.append(f"{kind} '{name}' has no key '{key}' (used by {where}); keys: {', '.join(obj.get('keys') or []) or 'none'}")
|
|
58
|
+
if problems:
|
|
59
|
+
yield ctx.finding(
|
|
60
|
+
"config-ref-missing", pod, problems[0] + ".",
|
|
61
|
+
evidence=problems[:6],
|
|
62
|
+
remediation="Create the object/key in the same namespace (names are case-sensitive), "
|
|
63
|
+
"fix the reference, or mark it optional: true if the app can run without it.",
|
|
64
|
+
commands=[f"kubectl get configmaps,secrets -n {ns}"],
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
@check("image-pull-secret-missing", "imagePullSecret does not exist", Category.CONFIG, Severity.HIGH)
|
|
69
|
+
def missing_pull_secret(ctx: Context) -> Iterator[Finding]:
|
|
70
|
+
"""The pod lists an imagePullSecret that is not in its namespace."""
|
|
71
|
+
if not ctx.snap.can_read("secrets"):
|
|
72
|
+
return
|
|
73
|
+
for pod in ctx.snap.all("pods"):
|
|
74
|
+
ns = meta(pod).get("namespace")
|
|
75
|
+
for ref in (pod.get("spec") or {}).get("imagePullSecrets") or []:
|
|
76
|
+
name = ref.get("name", "")
|
|
77
|
+
if ctx.snap.get("secrets", ns, name) is None:
|
|
78
|
+
yield ctx.finding(
|
|
79
|
+
"image-pull-secret-missing", pod,
|
|
80
|
+
f"imagePullSecret '{name}' does not exist in namespace {ns}; private images will fail to pull.",
|
|
81
|
+
remediation="Create the docker-registry Secret in this namespace (pull secrets are not shared across namespaces).",
|
|
82
|
+
commands=[f"kubectl create secret docker-registry {name} -n {ns} --docker-server=<registry> "
|
|
83
|
+
"--docker-username=<user> --docker-password=<token>"],
|
|
84
|
+
)
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"""Warning events that point at platform problems no other check sees."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from collections.abc import Iterator
|
|
7
|
+
from datetime import timedelta
|
|
8
|
+
|
|
9
|
+
from kubesift.checks.base import Context, check
|
|
10
|
+
from kubesift.cluster.snapshot import event_time
|
|
11
|
+
from kubesift.models import Category, Finding, ResourceRef, Severity
|
|
12
|
+
from kubesift.util import age, parse_time
|
|
13
|
+
|
|
14
|
+
PATTERNS: list[tuple[str, re.Pattern[str], str, Severity, str]] = [
|
|
15
|
+
("webhook", re.compile(r"failed calling webhook \"?([\w.-]+)", re.I),
|
|
16
|
+
"Admission webhook {0} is failing, so matching creates/updates are rejected (deploys and scaling can stall).",
|
|
17
|
+
Severity.CRITICAL,
|
|
18
|
+
"Check the webhook's service and pods. If the webhook is down and has failurePolicy: Fail, it blocks the API; "
|
|
19
|
+
"fix it or temporarily set failurePolicy: Ignore."),
|
|
20
|
+
("pod-security", re.compile(r"violates PodSecurity \"?([\w:.-]+)", re.I),
|
|
21
|
+
"Pod Security Admission ({0}) rejects pods: the controller cannot create them.",
|
|
22
|
+
Severity.HIGH,
|
|
23
|
+
"Set the securityContext fields named in the message (runAsNonRoot, drop ALL capabilities, seccompProfile) "
|
|
24
|
+
"or change the namespace's pod-security label."),
|
|
25
|
+
("quota", re.compile(r"exceeded quota: ([\w.-]+)", re.I),
|
|
26
|
+
"ResourceQuota {0} is exhausted, so new pods are rejected.",
|
|
27
|
+
Severity.HIGH, "Raise the quota or lower requests/replicas in that namespace."),
|
|
28
|
+
("ip-exhaustion", re.compile(r"failed to assign an IP address|no available IP|IP address.*exhaust|InsufficientFreeAddressesInSubnet|failed to allocate for range", re.I),
|
|
29
|
+
"The CNI cannot give pods an IP address: the subnet or IP pool is exhausted.",
|
|
30
|
+
Severity.CRITICAL,
|
|
31
|
+
"Add subnet capacity (secondary CIDR, prefix delegation on EKS) or reduce pods per node."),
|
|
32
|
+
("sandbox", re.compile(r"Failed to create pod sandbox|failed to setup network for sandbox", re.I),
|
|
33
|
+
"Pod sandboxes cannot be created: the CNI or container runtime on the node is failing.",
|
|
34
|
+
Severity.HIGH, "Check the CNI DaemonSet pods (aws-node, calico, cilium...) on the affected nodes."),
|
|
35
|
+
("cron-missed", re.compile(r"too many missed start times|Cannot determine if job needs to be started", re.I),
|
|
36
|
+
"A CronJob missed so many schedules that the controller stopped starting it.",
|
|
37
|
+
Severity.HIGH, "Set startingDeadlineSeconds (e.g. 200) on the CronJob so missed runs are skipped instead of blocking it."),
|
|
38
|
+
("certificate", re.compile(r"x509: certificate has expired|certificate has expired or is not yet valid", re.I),
|
|
39
|
+
"An expired certificate is breaking a connection.",
|
|
40
|
+
Severity.CRITICAL, "Find which certificate expired (webhook CA bundle, kubelet serving cert, ingress TLS) and rotate it."),
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@check("platform-warning", "Platform problem reported in events", Category.PLATFORM, Severity.HIGH)
|
|
45
|
+
def platform_warnings(ctx: Context) -> Iterator[Finding]:
|
|
46
|
+
"""Matches recent Warning events against known platform failure patterns."""
|
|
47
|
+
hits: dict[tuple[str, str], list[dict]] = {}
|
|
48
|
+
for e in ctx.snap.all("events"):
|
|
49
|
+
if e.get("type") != "Warning":
|
|
50
|
+
continue
|
|
51
|
+
a = age(event_time(e))
|
|
52
|
+
if a is not None and a > timedelta(hours=6):
|
|
53
|
+
continue
|
|
54
|
+
msg = e.get("message") or e.get("note") or ""
|
|
55
|
+
for key, pattern, *_ in PATTERNS:
|
|
56
|
+
m = pattern.search(msg)
|
|
57
|
+
if m:
|
|
58
|
+
hits.setdefault((key, m.group(1) if m.groups() and m.group(1) else ""), []).append(e)
|
|
59
|
+
break
|
|
60
|
+
for (key, arg), events in hits.items():
|
|
61
|
+
_, _, template, sev, fix = next(p for p in PATTERNS if p[0] == key)
|
|
62
|
+
events.sort(key=event_time, reverse=True)
|
|
63
|
+
inv = events[0].get("involvedObject") or events[0].get("regarding") or {}
|
|
64
|
+
ref = ResourceRef(kind=inv.get("kind", "Event"), name=inv.get("name", "?"), namespace=inv.get("namespace"))
|
|
65
|
+
count = sum(e.get("count", 1) or 1 for e in events)
|
|
66
|
+
yield ctx.finding(
|
|
67
|
+
"platform-warning", ref,
|
|
68
|
+
template.format(arg or "?") if "{0}" in template else template,
|
|
69
|
+
severity=sev,
|
|
70
|
+
title={"webhook": "Admission webhook is failing", "pod-security": "Pod Security Admission rejects pods",
|
|
71
|
+
"quota": "ResourceQuota exhausted", "ip-exhaustion": "Pod IP addresses exhausted",
|
|
72
|
+
"sandbox": "Pod sandbox creation failing", "certificate": "Expired certificate",
|
|
73
|
+
"cron-missed": "CronJob stopped scheduling"}[key],
|
|
74
|
+
evidence=[f"{count} event(s), latest: {events[0].get('message', '')[:300]}"],
|
|
75
|
+
remediation=fix,
|
|
76
|
+
since=parse_time(event_time(events[-1])),
|
|
77
|
+
)
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
"""Configuration risks found in pod templates. These run on live clusters and on manifest files."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Iterator
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from kubesift.checks.base import Context, check
|
|
9
|
+
from kubesift.models import Category, Finding, Severity
|
|
10
|
+
from kubesift.redact import SECRET_ENV_NAME
|
|
11
|
+
from kubesift.util import (
|
|
12
|
+
containers_of,
|
|
13
|
+
image_registry,
|
|
14
|
+
image_tag,
|
|
15
|
+
label_selector_matches,
|
|
16
|
+
meta,
|
|
17
|
+
pod_spec_of,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def templates(ctx: Context) -> Iterator[tuple[dict[str, Any], dict[str, Any]]]:
|
|
22
|
+
for w in ctx.snap.workloads():
|
|
23
|
+
if ctx.snap.live and ctx.is_system(meta(w).get("namespace")):
|
|
24
|
+
continue
|
|
25
|
+
spec = pod_spec_of(w)
|
|
26
|
+
if spec:
|
|
27
|
+
yield w, spec
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _long_running(obj: dict[str, Any]) -> bool:
|
|
31
|
+
return obj.get("kind") in ("Deployment", "StatefulSet", "DaemonSet", "Pod")
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@check("probe-missing-readiness", "No readiness probe", Category.RELIABILITY, Severity.MEDIUM, live_only=False, risk=True)
|
|
35
|
+
def missing_readiness(ctx: Context) -> Iterator[Finding]:
|
|
36
|
+
"""Without a readiness probe, traffic reaches pods before they can serve and during shutdown."""
|
|
37
|
+
for obj, spec in templates(ctx):
|
|
38
|
+
if obj.get("kind") not in ("Deployment", "StatefulSet"):
|
|
39
|
+
continue
|
|
40
|
+
missing = [c.get("name") for c in containers_of(spec) if not c.get("readinessProbe")]
|
|
41
|
+
if missing and any(c.get("ports") for c in containers_of(spec)):
|
|
42
|
+
yield ctx.finding("probe-missing-readiness", obj, f"Containers {', '.join(missing)} have no readinessProbe.",
|
|
43
|
+
remediation="Add a readinessProbe that checks the app can serve requests (e.g. GET /ready).")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@check("probe-liveness-equals-readiness", "Liveness probe is the same as readiness", Category.RELIABILITY,
|
|
47
|
+
Severity.MEDIUM, live_only=False, risk=True)
|
|
48
|
+
def liveness_equals_readiness(ctx: Context) -> Iterator[Finding]:
|
|
49
|
+
"""When a dependency slows down, identical probes restart every pod at once (cascading failure)."""
|
|
50
|
+
for obj, spec in templates(ctx):
|
|
51
|
+
for c in containers_of(spec):
|
|
52
|
+
live, ready = c.get("livenessProbe"), c.get("readinessProbe")
|
|
53
|
+
if not live or not ready:
|
|
54
|
+
continue
|
|
55
|
+
strip = lambda p: {k: v for k, v in p.items() if k in ("httpGet", "tcpSocket", "exec", "grpc")} # noqa: E731
|
|
56
|
+
if strip(live) == strip(ready):
|
|
57
|
+
yield ctx.finding(
|
|
58
|
+
"probe-liveness-equals-readiness", obj,
|
|
59
|
+
f"Container '{c.get('name')}' uses the same check for liveness and readiness.",
|
|
60
|
+
remediation="Liveness should only check that the process is alive (no dependency calls); "
|
|
61
|
+
"readiness can check dependencies. Otherwise a slow database restarts every replica.")
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@check("probe-no-startup", "Slow-start liveness without startupProbe", Category.RELIABILITY, Severity.LOW,
|
|
65
|
+
live_only=False, risk=True)
|
|
66
|
+
def aggressive_liveness(ctx: Context) -> Iterator[Finding]:
|
|
67
|
+
"""A liveness probe that can fail within 30s of start and no startupProbe kills slow-starting apps."""
|
|
68
|
+
for obj, spec in templates(ctx):
|
|
69
|
+
for c in containers_of(spec):
|
|
70
|
+
p = c.get("livenessProbe")
|
|
71
|
+
if not p or c.get("startupProbe"):
|
|
72
|
+
continue
|
|
73
|
+
budget = p.get("initialDelaySeconds", 0) + p.get("periodSeconds", 10) * p.get("failureThreshold", 3)
|
|
74
|
+
if budget < 30:
|
|
75
|
+
yield ctx.finding(
|
|
76
|
+
"probe-no-startup", obj,
|
|
77
|
+
f"Container '{c.get('name')}' is killed if not healthy within {budget}s of starting.",
|
|
78
|
+
remediation="Add a startupProbe (e.g. failureThreshold: 30, periodSeconds: 5) so slow starts are not killed.")
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@check("resources-missing", "No resource requests or memory limit", Category.RESOURCES, Severity.MEDIUM,
|
|
82
|
+
live_only=False, risk=True)
|
|
83
|
+
def resources_missing(ctx: Context) -> Iterator[Finding]:
|
|
84
|
+
"""Without requests the scheduler overpacks nodes; without a memory limit one pod can starve a node."""
|
|
85
|
+
for obj, spec in templates(ctx):
|
|
86
|
+
for c in containers_of(spec):
|
|
87
|
+
res = c.get("resources") or {}
|
|
88
|
+
req, lim = res.get("requests") or {}, res.get("limits") or {}
|
|
89
|
+
gaps = []
|
|
90
|
+
if not req.get("cpu") and not lim.get("cpu"):
|
|
91
|
+
gaps.append("cpu request")
|
|
92
|
+
if not req.get("memory") and not lim.get("memory"):
|
|
93
|
+
gaps.append("memory request")
|
|
94
|
+
if not lim.get("memory"):
|
|
95
|
+
gaps.append("memory limit")
|
|
96
|
+
if gaps:
|
|
97
|
+
yield ctx.finding(
|
|
98
|
+
"resources-missing", obj, f"Container '{c.get('name')}' has no {', '.join(gaps)}.",
|
|
99
|
+
remediation="Set requests to typical usage and a memory limit above peak usage "
|
|
100
|
+
"(`kubesift cost` suggests values from real usage).",
|
|
101
|
+
severity=Severity.MEDIUM if "memory request" in gaps else Severity.LOW)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@check("image-mutable-tag", "Image uses latest or no tag", Category.RELIABILITY, Severity.MEDIUM,
|
|
105
|
+
live_only=False, risk=True)
|
|
106
|
+
def mutable_tag(ctx: Context) -> Iterator[Finding]:
|
|
107
|
+
"""`latest` or untagged images change underneath you and make rollbacks impossible."""
|
|
108
|
+
for obj, spec in templates(ctx):
|
|
109
|
+
for c in containers_of(spec, include_init=True):
|
|
110
|
+
tag = image_tag(c.get("image", ""))
|
|
111
|
+
if tag in (None, "latest"):
|
|
112
|
+
yield ctx.finding("image-mutable-tag", obj,
|
|
113
|
+
f"Container '{c.get('name')}' uses image '{c.get('image')}' with "
|
|
114
|
+
f"{'no tag' if tag is None else 'the latest tag'}.",
|
|
115
|
+
remediation="Pin a version tag or digest so every node runs the same code and rollbacks work.")
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
@check("image-dockerhub-anonymous", "Images pulled from Docker Hub without credentials", Category.RELIABILITY,
|
|
119
|
+
Severity.LOW, live_only=False, risk=True)
|
|
120
|
+
def dockerhub_anonymous(ctx: Context) -> Iterator[Finding]:
|
|
121
|
+
"""Anonymous Docker Hub pulls are rate limited per IP; scale-ups and node replacements can fail."""
|
|
122
|
+
count = 0
|
|
123
|
+
first = None
|
|
124
|
+
for obj, spec in templates(ctx):
|
|
125
|
+
if spec.get("imagePullSecrets"):
|
|
126
|
+
continue
|
|
127
|
+
if any(image_registry(c.get("image", "")) == "docker.io" for c in containers_of(spec, include_init=True)):
|
|
128
|
+
count += 1
|
|
129
|
+
first = first or obj
|
|
130
|
+
if first is not None and count:
|
|
131
|
+
yield ctx.finding(
|
|
132
|
+
"image-dockerhub-anonymous", first,
|
|
133
|
+
f"{count} workload(s) pull from Docker Hub without an imagePullSecret and can hit pull rate limits.",
|
|
134
|
+
remediation="Mirror images to your registry or a pull-through cache (ECR, Artifact Registry, Harbor), "
|
|
135
|
+
"or authenticate pulls.")
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
@check("security-privileged", "Privileged or host-level access", Category.SECURITY, Severity.HIGH,
|
|
139
|
+
live_only=False, risk=True)
|
|
140
|
+
def privileged(ctx: Context) -> Iterator[Finding]:
|
|
141
|
+
"""privileged, hostNetwork/hostPID/hostIPC, hostPath or dangerous capabilities: a container escape means node takeover."""
|
|
142
|
+
for obj, spec in templates(ctx):
|
|
143
|
+
issues = []
|
|
144
|
+
for flag in ("hostNetwork", "hostPID", "hostIPC"):
|
|
145
|
+
if spec.get(flag):
|
|
146
|
+
issues.append(flag)
|
|
147
|
+
for v in spec.get("volumes") or []:
|
|
148
|
+
if v.get("hostPath"):
|
|
149
|
+
issues.append(f"hostPath {v['hostPath'].get('path')}")
|
|
150
|
+
for c in containers_of(spec, include_init=True):
|
|
151
|
+
sc = c.get("securityContext") or {}
|
|
152
|
+
if sc.get("privileged"):
|
|
153
|
+
issues.append(f"privileged container {c.get('name')}")
|
|
154
|
+
caps = (sc.get("capabilities") or {}).get("add") or []
|
|
155
|
+
bad = [x for x in caps if x in ("SYS_ADMIN", "NET_ADMIN", "ALL", "SYS_PTRACE", "SYS_MODULE")]
|
|
156
|
+
if bad:
|
|
157
|
+
issues.append(f"{c.get('name')} adds {','.join(bad)}")
|
|
158
|
+
if issues:
|
|
159
|
+
yield ctx.finding("security-privileged", obj, "Host-level access: " + "; ".join(issues[:5]) + ".",
|
|
160
|
+
severity=Severity.HIGH if any("privileged" in i or "SYS_ADMIN" in i for i in issues) else Severity.MEDIUM,
|
|
161
|
+
remediation="Remove what the app does not need. Node agents may need it; application workloads should not.")
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
@check("security-runs-as-root", "Container may run as root", Category.SECURITY, Severity.LOW, live_only=False, risk=True)
|
|
165
|
+
def runs_as_root(ctx: Context) -> Iterator[Finding]:
|
|
166
|
+
"""No runAsNonRoot and allowPrivilegeEscalation not disabled (fails the 'restricted' Pod Security level)."""
|
|
167
|
+
for obj, spec in templates(ctx):
|
|
168
|
+
psc = spec.get("securityContext") or {}
|
|
169
|
+
for c in containers_of(spec):
|
|
170
|
+
sc = c.get("securityContext") or {}
|
|
171
|
+
non_root = sc.get("runAsNonRoot", psc.get("runAsNonRoot")) or (sc.get("runAsUser", psc.get("runAsUser")) or 0) > 0
|
|
172
|
+
if not non_root or sc.get("allowPrivilegeEscalation") is not False:
|
|
173
|
+
yield ctx.finding(
|
|
174
|
+
"security-runs-as-root", obj,
|
|
175
|
+
f"Container '{c.get('name')}' does not set runAsNonRoot and allowPrivilegeEscalation: false.",
|
|
176
|
+
remediation="Set securityContext: {runAsNonRoot: true, allowPrivilegeEscalation: false, "
|
|
177
|
+
"capabilities: {drop: [ALL]}, seccompProfile: {type: RuntimeDefault}}.")
|
|
178
|
+
break
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
@check("security-plaintext-secret", "Secret value written directly in env", Category.SECURITY, Severity.HIGH,
|
|
182
|
+
live_only=False, risk=True)
|
|
183
|
+
def plaintext_secret(ctx: Context) -> Iterator[Finding]:
|
|
184
|
+
"""An env var named like a password/token has a literal value: it is visible to anyone who can read the workload."""
|
|
185
|
+
for obj, spec in templates(ctx):
|
|
186
|
+
for c in containers_of(spec, include_init=True):
|
|
187
|
+
for env in c.get("env") or []:
|
|
188
|
+
value = env.get("value")
|
|
189
|
+
if value and len(str(value)) >= 6 and SECRET_ENV_NAME.search(env.get("name", "")) \
|
|
190
|
+
and not str(value).startswith("$("):
|
|
191
|
+
yield ctx.finding(
|
|
192
|
+
"security-plaintext-secret", obj,
|
|
193
|
+
f"Env var {env.get('name')} in container '{c.get('name')}' holds a literal value that looks like a secret.",
|
|
194
|
+
remediation="Move it to a Secret (or an external secret store) and use valueFrom.secretKeyRef. Rotate the value.")
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
@check("security-gitrepo-volume", "gitRepo volume (disabled in 1.36)", Category.UPGRADE, Severity.HIGH,
|
|
198
|
+
live_only=False, risk=True)
|
|
199
|
+
def gitrepo_volume(ctx: Context) -> Iterator[Finding]:
|
|
200
|
+
"""gitRepo volumes are a known remote-code-execution vector and are disabled from Kubernetes 1.36."""
|
|
201
|
+
for obj, spec in templates(ctx):
|
|
202
|
+
if any(v.get("gitRepo") for v in spec.get("volumes") or []):
|
|
203
|
+
yield ctx.finding("security-gitrepo-volume", obj, "Uses a gitRepo volume, which Kubernetes 1.36 no longer supports.",
|
|
204
|
+
remediation="Clone in an init container into an emptyDir instead.")
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
@check("reliability-single-replica", "Single replica", Category.RELIABILITY, Severity.LOW, live_only=False, risk=True)
|
|
208
|
+
def single_replica(ctx: Context) -> Iterator[Finding]:
|
|
209
|
+
"""One replica means downtime on every deploy, node drain and crash."""
|
|
210
|
+
for obj, _ in templates(ctx):
|
|
211
|
+
if obj.get("kind") != "Deployment":
|
|
212
|
+
continue
|
|
213
|
+
if (obj.get("spec") or {}).get("replicas", 1) == 1 and not _has_hpa(ctx, obj):
|
|
214
|
+
yield ctx.finding("reliability-single-replica", obj, "Runs a single replica, so any restart is an outage.",
|
|
215
|
+
remediation="Run at least 2 replicas (with a PodDisruptionBudget) for anything user-facing.")
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def _has_hpa(ctx: Context, obj: dict[str, Any]) -> bool:
|
|
219
|
+
for h in ctx.snap.in_namespace("hpas", meta(obj).get("namespace")):
|
|
220
|
+
ref = (h.get("spec") or {}).get("scaleTargetRef") or {}
|
|
221
|
+
if ref.get("kind") == obj.get("kind") and ref.get("name") == meta(obj).get("name"):
|
|
222
|
+
return (h.get("spec") or {}).get("minReplicas", 1) > 1
|
|
223
|
+
return False
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
@check("reliability-no-pdb", "No PodDisruptionBudget", Category.RELIABILITY, Severity.LOW, live_only=False, risk=True)
|
|
227
|
+
def no_pdb(ctx: Context) -> Iterator[Finding]:
|
|
228
|
+
"""Without a PDB, node drains during upgrades can evict all replicas at once."""
|
|
229
|
+
for obj, _ in templates(ctx):
|
|
230
|
+
if obj.get("kind") not in ("Deployment", "StatefulSet") or (obj.get("spec") or {}).get("replicas", 1) < 2:
|
|
231
|
+
continue
|
|
232
|
+
labels = (((obj.get("spec") or {}).get("template") or {}).get("metadata") or {}).get("labels") or {}
|
|
233
|
+
if not any(label_selector_matches((p.get("spec") or {}).get("selector") or {}, labels)
|
|
234
|
+
for p in ctx.snap.in_namespace("pdbs", meta(obj).get("namespace"))):
|
|
235
|
+
yield ctx.finding("reliability-no-pdb", obj, "Has several replicas but no PodDisruptionBudget.",
|
|
236
|
+
remediation="Add a PDB with maxUnavailable: 1 so drains keep the service up.")
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
@check("pdb-blocks-drain", "PodDisruptionBudget allows no disruptions", Category.UPGRADE, Severity.HIGH, risk=True)
|
|
240
|
+
def pdb_blocks(ctx: Context) -> Iterator[Finding]:
|
|
241
|
+
"""disruptionsAllowed=0 makes node drains hang: node upgrades and autoscaler scale-down get stuck."""
|
|
242
|
+
for pdb in ctx.snap.all("pdbs"):
|
|
243
|
+
st = pdb.get("status") or {}
|
|
244
|
+
if st.get("expectedPods", 0) > 0 and st.get("disruptionsAllowed", 1) == 0:
|
|
245
|
+
healthy, want = st.get("currentHealthy", 0), st.get("desiredHealthy", 0)
|
|
246
|
+
yield ctx.finding(
|
|
247
|
+
"pdb-blocks-drain", pdb,
|
|
248
|
+
f"Allows 0 disruptions ({healthy} healthy, {want} required): draining nodes that run these pods will hang.",
|
|
249
|
+
evidence=[f"spec: {pdb.get('spec', {}).get('minAvailable') or pdb.get('spec', {}).get('maxUnavailable')}"],
|
|
250
|
+
remediation="Use maxUnavailable: 1 instead of minAvailable equal to the replica count, or add replicas; "
|
|
251
|
+
"if pods are unhealthy, fix them first.")
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
@check("rbac-cluster-admin-sa", "Service account has cluster-admin", Category.SECURITY, Severity.HIGH, risk=True)
|
|
255
|
+
def cluster_admin_sa(ctx: Context) -> Iterator[Finding]:
|
|
256
|
+
"""A pod token with cluster-admin means anyone who compromises the pod owns the cluster."""
|
|
257
|
+
for b in ctx.snap.all("clusterrolebindings"):
|
|
258
|
+
if (b.get("roleRef") or {}).get("name") != "cluster-admin":
|
|
259
|
+
continue
|
|
260
|
+
for s in b.get("subjects") or []:
|
|
261
|
+
if s.get("kind") == "ServiceAccount" and not ctx.is_system(s.get("namespace")):
|
|
262
|
+
yield ctx.finding("rbac-cluster-admin-sa", b,
|
|
263
|
+
f"ServiceAccount {s.get('namespace')}/{s.get('name')} is bound to cluster-admin.",
|
|
264
|
+
remediation="Grant only the verbs and resources it needs with a dedicated (Cluster)Role.")
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
"""Things that break later: removed APIs, expiring certificates, quotas, deprecated features."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Iterator
|
|
6
|
+
from datetime import timedelta
|
|
7
|
+
|
|
8
|
+
from kubesift.checks.base import Context, check
|
|
9
|
+
from kubesift.checks.nodes import minor
|
|
10
|
+
from kubesift.cluster.snapshot import APPLIED_API
|
|
11
|
+
from kubesift.models import Category, Finding, ResourceRef, Severity
|
|
12
|
+
from kubesift.util import meta, now, parse_quantity, parse_time
|
|
13
|
+
|
|
14
|
+
# (apiVersion, kind) -> (removed in minor version, replacement)
|
|
15
|
+
REMOVED_APIS: dict[tuple[str, str], tuple[int, str]] = {
|
|
16
|
+
("extensions/v1beta1", "Ingress"): (22, "networking.k8s.io/v1"),
|
|
17
|
+
("networking.k8s.io/v1beta1", "Ingress"): (22, "networking.k8s.io/v1"),
|
|
18
|
+
("networking.k8s.io/v1beta1", "IngressClass"): (22, "networking.k8s.io/v1"),
|
|
19
|
+
("extensions/v1beta1", "Deployment"): (16, "apps/v1"),
|
|
20
|
+
("apps/v1beta1", "Deployment"): (16, "apps/v1"),
|
|
21
|
+
("apps/v1beta2", "Deployment"): (16, "apps/v1"),
|
|
22
|
+
("extensions/v1beta1", "DaemonSet"): (16, "apps/v1"),
|
|
23
|
+
("apps/v1beta2", "StatefulSet"): (16, "apps/v1"),
|
|
24
|
+
("apiextensions.k8s.io/v1beta1", "CustomResourceDefinition"): (22, "apiextensions.k8s.io/v1"),
|
|
25
|
+
("admissionregistration.k8s.io/v1beta1", "ValidatingWebhookConfiguration"): (22, "admissionregistration.k8s.io/v1"),
|
|
26
|
+
("admissionregistration.k8s.io/v1beta1", "MutatingWebhookConfiguration"): (22, "admissionregistration.k8s.io/v1"),
|
|
27
|
+
("rbac.authorization.k8s.io/v1beta1", "ClusterRole"): (22, "rbac.authorization.k8s.io/v1"),
|
|
28
|
+
("rbac.authorization.k8s.io/v1beta1", "ClusterRoleBinding"): (22, "rbac.authorization.k8s.io/v1"),
|
|
29
|
+
("rbac.authorization.k8s.io/v1beta1", "Role"): (22, "rbac.authorization.k8s.io/v1"),
|
|
30
|
+
("rbac.authorization.k8s.io/v1beta1", "RoleBinding"): (22, "rbac.authorization.k8s.io/v1"),
|
|
31
|
+
("scheduling.k8s.io/v1beta1", "PriorityClass"): (22, "scheduling.k8s.io/v1"),
|
|
32
|
+
("storage.k8s.io/v1beta1", "StorageClass"): (22, "storage.k8s.io/v1"),
|
|
33
|
+
("storage.k8s.io/v1beta1", "CSIDriver"): (22, "storage.k8s.io/v1"),
|
|
34
|
+
("storage.k8s.io/v1beta1", "CSIStorageCapacity"): (27, "storage.k8s.io/v1"),
|
|
35
|
+
("batch/v1beta1", "CronJob"): (25, "batch/v1"),
|
|
36
|
+
("discovery.k8s.io/v1beta1", "EndpointSlice"): (25, "discovery.k8s.io/v1"),
|
|
37
|
+
("events.k8s.io/v1beta1", "Event"): (25, "events.k8s.io/v1"),
|
|
38
|
+
("autoscaling/v2beta1", "HorizontalPodAutoscaler"): (25, "autoscaling/v2"),
|
|
39
|
+
("autoscaling/v2beta2", "HorizontalPodAutoscaler"): (26, "autoscaling/v2"),
|
|
40
|
+
("policy/v1beta1", "PodDisruptionBudget"): (25, "policy/v1"),
|
|
41
|
+
("policy/v1beta1", "PodSecurityPolicy"): (25, "Pod Security Admission"),
|
|
42
|
+
("node.k8s.io/v1beta1", "RuntimeClass"): (25, "node.k8s.io/v1"),
|
|
43
|
+
("flowcontrol.apiserver.k8s.io/v1beta1", "FlowSchema"): (26, "flowcontrol.apiserver.k8s.io/v1"),
|
|
44
|
+
("flowcontrol.apiserver.k8s.io/v1beta2", "FlowSchema"): (29, "flowcontrol.apiserver.k8s.io/v1"),
|
|
45
|
+
("flowcontrol.apiserver.k8s.io/v1beta3", "FlowSchema"): (32, "flowcontrol.apiserver.k8s.io/v1"),
|
|
46
|
+
("flowcontrol.apiserver.k8s.io/v1beta1", "PriorityLevelConfiguration"): (26, "flowcontrol.apiserver.k8s.io/v1"),
|
|
47
|
+
("flowcontrol.apiserver.k8s.io/v1beta2", "PriorityLevelConfiguration"): (29, "flowcontrol.apiserver.k8s.io/v1"),
|
|
48
|
+
("flowcontrol.apiserver.k8s.io/v1beta3", "PriorityLevelConfiguration"): (32, "flowcontrol.apiserver.k8s.io/v1"),
|
|
49
|
+
("resource.k8s.io/v1alpha2", "ResourceClaim"): (31, "resource.k8s.io/v1"),
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _target_minor(ctx: Context) -> int | None:
|
|
54
|
+
return minor(ctx.target_version) or minor(ctx.snap.server_version)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@check("api-removed", "Manifest uses a removed or soon-removed API", Category.UPGRADE, Severity.HIGH,
|
|
58
|
+
live_only=False, risk=True)
|
|
59
|
+
def removed_apis(ctx: Context) -> Iterator[Finding]:
|
|
60
|
+
"""apiVersions that the target Kubernetes version no longer serves (manifests, Helm releases, last-applied)."""
|
|
61
|
+
target = _target_minor(ctx)
|
|
62
|
+
uses: list[tuple[str, str, ResourceRef | dict, str]] = []
|
|
63
|
+
for items in ctx.snap.objects.values():
|
|
64
|
+
for obj in items:
|
|
65
|
+
api = obj.get("apiVersion", "") if not ctx.snap.live else (meta(obj).get("annotations") or {}).get(APPLIED_API)
|
|
66
|
+
if api:
|
|
67
|
+
uses.append((api, obj.get("kind", ""), obj, "kubectl apply" if ctx.snap.live else "manifest"))
|
|
68
|
+
for rel in ctx.snap.helm_releases:
|
|
69
|
+
for r in rel.get("resources") or []:
|
|
70
|
+
uses.append((r["apiVersion"], r["kind"],
|
|
71
|
+
ResourceRef(kind=r["kind"], name=r["name"], namespace=rel.get("namespace")),
|
|
72
|
+
f"Helm release {rel.get('namespace')}/{rel.get('name')} ({rel.get('chart', '')})"))
|
|
73
|
+
for api, kind, ref, source in uses:
|
|
74
|
+
hit = REMOVED_APIS.get((api, kind))
|
|
75
|
+
if not hit:
|
|
76
|
+
continue
|
|
77
|
+
removed_in, replacement = hit
|
|
78
|
+
if target is not None and target < removed_in - 2:
|
|
79
|
+
continue
|
|
80
|
+
gone = target is None or target >= removed_in
|
|
81
|
+
msg = f"{kind} uses {api}, {'removed' if gone else 'to be removed'} in Kubernetes 1.{removed_in}. Use {replacement}."
|
|
82
|
+
if ctx.snap.live:
|
|
83
|
+
msg += (" The object itself was converted and keeps running, but re-applying the original manifest "
|
|
84
|
+
"or Helm chart will fail.") if gone else " Update the source manifest before upgrading."
|
|
85
|
+
yield ctx.finding(
|
|
86
|
+
"api-removed", ref, msg,
|
|
87
|
+
severity=Severity.HIGH,
|
|
88
|
+
evidence=[f"source: {source}"],
|
|
89
|
+
remediation=(f"Update the manifest/chart to {replacement}. For Helm releases whose stored manifest has the old "
|
|
90
|
+
"API, upgrade the chart; if upgrades fail, use the helm-mapkubeapis plugin."),
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
@check("certificate-expiring", "TLS certificate expires soon", Category.SECURITY, Severity.HIGH)
|
|
95
|
+
def cert_expiring(ctx: Context) -> Iterator[Finding]:
|
|
96
|
+
"""TLS Secrets whose certificate expires within 30 days (or already expired)."""
|
|
97
|
+
for s in ctx.snap.all("secrets"):
|
|
98
|
+
tls = s.get("tls") or {}
|
|
99
|
+
not_after = parse_time(tls.get("not_after"))
|
|
100
|
+
if not not_after:
|
|
101
|
+
continue
|
|
102
|
+
left = not_after - now()
|
|
103
|
+
if left > timedelta(days=30):
|
|
104
|
+
continue
|
|
105
|
+
managed = bool((meta(s).get("annotations") or {}).get("cert-manager.io/certificate-name"))
|
|
106
|
+
days = left.days
|
|
107
|
+
if left.total_seconds() <= 0:
|
|
108
|
+
msg, sev = f"Certificate expired on {not_after:%Y-%m-%d}: HTTPS clients will reject it.", Severity.CRITICAL
|
|
109
|
+
else:
|
|
110
|
+
msg = f"Certificate expires in {days} day(s) ({not_after:%Y-%m-%d})."
|
|
111
|
+
sev = Severity.CRITICAL if days < 7 else Severity.HIGH if days < 14 else Severity.MEDIUM
|
|
112
|
+
if managed:
|
|
113
|
+
msg += " It is managed by cert-manager, so renewal is failing."
|
|
114
|
+
yield ctx.finding(
|
|
115
|
+
"certificate-expiring", s, msg, severity=sev,
|
|
116
|
+
evidence=[f"subject: {tls.get('subject')}", f"dns names: {', '.join(tls.get('dns_names') or [])}"],
|
|
117
|
+
remediation="Check the cert-manager Certificate/Order/Challenge status." if managed
|
|
118
|
+
else "Renew the certificate and update the Secret, or automate it with cert-manager.",
|
|
119
|
+
commands=[f"kubectl describe certificate -n {meta(s).get('namespace')}"] if managed else [],
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
@check("certificate-not-ready", "cert-manager Certificate not ready", Category.SECURITY, Severity.HIGH)
|
|
124
|
+
def cert_manager_not_ready(ctx: Context) -> Iterator[Finding]:
|
|
125
|
+
"""cert-manager cannot issue or renew a Certificate (DNS/HTTP challenge, issuer or rate-limit problems)."""
|
|
126
|
+
for c in ctx.snap.all("certificates"):
|
|
127
|
+
ready = next((x for x in (c.get("status") or {}).get("conditions") or [] if x.get("type") == "Ready"), {})
|
|
128
|
+
if ready.get("status") == "False":
|
|
129
|
+
yield ctx.finding("certificate-not-ready", c, f"Not ready ({ready.get('reason')}): {ready.get('message', '')[:250]}",
|
|
130
|
+
commands=[f"kubectl describe certificate {meta(c).get('name')} -n {meta(c).get('namespace')}",
|
|
131
|
+
f"kubectl get certificaterequests,orders,challenges -n {meta(c).get('namespace')}"],
|
|
132
|
+
remediation="Follow the chain Certificate -> CertificateRequest -> Order -> Challenge to see which step fails.")
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
@check("quota-near-limit", "ResourceQuota almost used up", Category.RESOURCES, Severity.MEDIUM, risk=True)
|
|
136
|
+
def quota_near_limit(ctx: Context) -> Iterator[Finding]:
|
|
137
|
+
"""At 90%+ of a quota, the next deploy or scale-up will be rejected."""
|
|
138
|
+
for q in ctx.snap.all("resourcequotas"):
|
|
139
|
+
st = q.get("status") or {}
|
|
140
|
+
hard, used = st.get("hard") or {}, st.get("used") or {}
|
|
141
|
+
full = []
|
|
142
|
+
for res, limit in hard.items():
|
|
143
|
+
h, u = parse_quantity(limit), parse_quantity(used.get(res))
|
|
144
|
+
if h > 0 and u / h >= 0.9:
|
|
145
|
+
full.append(f"{res}: {used.get(res)}/{limit}")
|
|
146
|
+
if full:
|
|
147
|
+
yield ctx.finding("quota-near-limit", q, f"Quota nearly exhausted: {'; '.join(full[:4])}.",
|
|
148
|
+
severity=Severity.HIGH if any(parse_quantity(used.get(r)) >= parse_quantity(h)
|
|
149
|
+
for r, h in hard.items() if parse_quantity(h) > 0) else Severity.MEDIUM,
|
|
150
|
+
remediation="Raise the quota or lower requests; rollouts need headroom for surge pods.")
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
@check("kube-proxy-ipvs", "kube-proxy IPVS mode is deprecated", Category.UPGRADE, Severity.LOW, risk=True)
|
|
154
|
+
def kube_proxy_ipvs(ctx: Context) -> Iterator[Finding]:
|
|
155
|
+
"""IPVS mode was deprecated in Kubernetes 1.35; nftables is the successor."""
|
|
156
|
+
if (ctx.snap.metrics.get("cluster") or {}).get("kube_proxy", {}).get("mode_ipvs"):
|
|
157
|
+
yield ctx.finding("kube-proxy-ipvs", ResourceRef(kind="ConfigMap", name="kube-proxy", namespace="kube-system"),
|
|
158
|
+
"kube-proxy runs in IPVS mode, deprecated since Kubernetes 1.35.",
|
|
159
|
+
remediation="Plan a move to nftables mode (or an eBPF dataplane) and test it in staging.")
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
@check("service-external-ips", "Service uses externalIPs", Category.SECURITY, Severity.LOW, live_only=False, risk=True)
|
|
163
|
+
def external_ips(ctx: Context) -> Iterator[Finding]:
|
|
164
|
+
"""spec.externalIPs is deprecated (1.36) and was the vector for CVE-2020-8554."""
|
|
165
|
+
for svc in ctx.snap.all("services"):
|
|
166
|
+
if (svc.get("spec") or {}).get("externalIPs"):
|
|
167
|
+
yield ctx.finding("service-external-ips", svc, "Uses spec.externalIPs, deprecated in Kubernetes 1.36.",
|
|
168
|
+
remediation="Use a LoadBalancer Service or Gateway instead.")
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
@check("webhook-unavailable", "Admission webhook has no backend", Category.PLATFORM, Severity.CRITICAL)
|
|
172
|
+
def webhook_unavailable(ctx: Context) -> Iterator[Finding]:
|
|
173
|
+
"""A webhook with failurePolicy Fail points at a Service with no ready pods: matching API requests are rejected."""
|
|
174
|
+
if not ctx.snap.can_read("endpointslices"):
|
|
175
|
+
return
|
|
176
|
+
for key in ("validatingwebhooks", "mutatingwebhooks"):
|
|
177
|
+
for cfg in ctx.snap.all(key):
|
|
178
|
+
for wh in cfg.get("webhooks") or []:
|
|
179
|
+
svc = (wh.get("clientConfig") or {}).get("service")
|
|
180
|
+
if not svc or wh.get("failurePolicy", "Fail") != "Fail":
|
|
181
|
+
continue
|
|
182
|
+
ns, name = svc.get("namespace"), svc.get("name", "")
|
|
183
|
+
if ctx.snap.namespace and ns != ctx.snap.namespace and ctx.snap.get("services", ns, name) is None:
|
|
184
|
+
continue # service is outside the scanned namespace
|
|
185
|
+
exists = ctx.snap.get("services", ns, name) is not None
|
|
186
|
+
ready = sum(1 for es in ctx.snap.in_namespace("endpointslices", ns)
|
|
187
|
+
if (meta(es).get("labels") or {}).get("kubernetes.io/service-name") == name
|
|
188
|
+
for ep in es.get("endpoints") or [] if (ep.get("conditions") or {}).get("ready") is not False)
|
|
189
|
+
if exists and ready:
|
|
190
|
+
continue
|
|
191
|
+
yield ctx.finding(
|
|
192
|
+
"webhook-unavailable", cfg,
|
|
193
|
+
f"Webhook {wh.get('name')} calls Service {ns}/{name}, which "
|
|
194
|
+
f"{'has no ready endpoints' if exists else 'does not exist'}. With failurePolicy Fail, "
|
|
195
|
+
"every matching create/update in the cluster is rejected.",
|
|
196
|
+
evidence=[f"rules: {wh.get('rules')}"[:300], f"namespaceSelector: {wh.get('namespaceSelector')}"[:200]],
|
|
197
|
+
remediation="Bring the webhook's pods back, or delete the stale webhook configuration if its "
|
|
198
|
+
"operator was uninstalled. Temporarily setting failurePolicy: Ignore unblocks the API.",
|
|
199
|
+
commands=[f"kubectl get {cfg.get('kind', '').lower()} {meta(cfg).get('name')} -o yaml",
|
|
200
|
+
f"kubectl get pods -n {ns}"],
|
|
201
|
+
)
|
|
202
|
+
break
|