From ce6dad9a582c353aa316a2fb70a60ca922bb1bb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9=20=D0=A7=D0=B5?= =?UTF-8?q?=D1=84=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Fri, 25 Sep 2026 00:50:37 +0300 Subject: [PATCH] v0.9.0: vulnerabilities on the map; event counts scaled to the window Vulnerabilities: VulnerabilityReports from operator-trivy are cached (trimmed to the fields shown) and mapped to cards through the scanned object in their labels. A card shows a red shield with its CRITICAL CVE count; the details list CRITICAL and HIGH CVEs with packages, installed and fixed versions and the advisory link, one row per CVE and image. Counted per CVE, not per package: openssh's six subpackages had made one CVE count six times. Kept apart from problems on purpose: on the live cluster 22 cards have some, which would bury the "is it working" badges. Fixes, both found on the live cluster, where they made all five current "problems": - a Warning event's count is cumulative since its first occurrence; four dex-authenticators on xeon had 38-51 probe failures over two days (one an hour, pods never left Ready) and were badged as a burst. The count is now scaled to the part of the event's lifetime the window covers. - upmeter's smoke-mini services and pods are its synthetic test, moved between nodes on purpose (NoEndpoints, FailedMount while moving). Co-Authored-By: Claude Opus 5.5 --- Chart.yaml | 2 +- changelog.yaml | 6 + docs/README.md | 10 +- docs/README_RU.md | 10 +- images/cluster-map/src/internal/kube/cache.go | 69 +++++- .../src/internal/server/inspect.go | 19 ++ .../cluster-map/src/internal/server/server.go | 1 + .../src/internal/topology/graph.go | 5 + .../src/internal/topology/problems.go | 39 +++- .../src/internal/topology/problems_test.go | 30 +++ .../src/internal/topology/vulns.go | 203 ++++++++++++++++++ .../src/internal/topology/vulns_test.go | 73 +++++++ images/cluster-map/src/web/static/app.css | 8 + images/cluster-map/src/web/static/app.js | 56 ++++- images/cluster-map/src/web/static/i18n.js | 14 ++ templates/rbac-for-us.yaml | 8 +- 16 files changed, 531 insertions(+), 22 deletions(-) create mode 100644 images/cluster-map/src/internal/topology/vulns.go create mode 100644 images/cluster-map/src/internal/topology/vulns_test.go diff --git a/Chart.yaml b/Chart.yaml index b985eec..a8e1a45 100644 --- a/Chart.yaml +++ b/Chart.yaml @@ -1,5 +1,5 @@ name: cluster-map -version: 0.8.3 +version: 0.9.0 dependencies: - name: deckhouse_lib_helm version: 1.72.24 diff --git a/changelog.yaml b/changelog.yaml index fb70bc5..1aef669 100644 --- a/changelog.yaml +++ b/changelog.yaml @@ -1,3 +1,9 @@ +v0.9.0: + features: + - "Vulnerabilities from operator-trivy: a red shield with the number of CRITICAL CVEs on each card, and in the details every CRITICAL and HIGH CVE with its packages, installed and fixed versions and a link. A CVE counts once per card." + fixes: + - "Event counts are scaled to the 30-minute window: a cumulative count spread over days (40 probe failures in two days) no longer badges a card as a burst." + - upmeter's smoke-mini services and pods, which it moves between nodes on purpose, are not reported as problems. v0.8.3: fixes: - UnexpectedJob warnings (the CronJob controller's note on every manual run) are listed for information and no longer badge the CronJob. diff --git a/docs/README.md b/docs/README.md index 0f1601a..a159b6d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -26,11 +26,17 @@ Workloads managed by non-native controllers are included: Advanced DaemonSets of A card with a problem carries a badge: yellow `!` for a warning, red `!` for an error. Namespace boxes and cards, node boxes and the tree take the colour of the worst problem inside. Sources: - **Object state**: containers in `CrashLoopBackOff`, `ImagePullBackOff` and similar, recent restarts and OOM kills, pods pending or unschedulable, workloads with no or not all replicas ready for more than 2 minutes, a stuck rollout, a CronJob whose last run failed, a Service with no ready pod, an Ingress routing to a Service that does not exist, a node that is not ready or is under memory, disk or PID pressure. -- **Warning events** of the last 30 minutes, folded per object and reason. Scheduling, volume and back-off failures badge the card at once; any other reason needs five repeats and is listed without a badge before that, so a single failed probe during a restart does not colour the map. +- **Warning events** of the last 30 minutes, folded per object and reason. Scheduling, volume and back-off failures badge the card at once; any other reason needs five repeats inside the window and is listed without a badge before that, so a single failed probe during a restart does not colour the map. An event's count is cumulative since its first occurrence, so a long-lived event is scaled to the window: 40 probe failures over two days is one an hour, not a burst. - **Prometheus alerts** that Deckhouse mirrors into `ClusterAlert` objects. An alert is attached by its labels (`deployment`, `statefulset`, `daemonset`, `pod`, `service`, `ingress`, `node`, `namespace`); one without an object or namespace goes to the cluster list. `severity_level` 1–3 is an error, 4–6 a warning, 7–9 information. Information-level entries (a cordoned node, a single ordinary warning) are listed and never badge a card. Problems are recomputed on every map request and the open page refreshes them every minute. +## Vulnerabilities + +With operator-trivy installed, a card whose images have CRITICAL CVEs shows a red shield with their number in the bottom-right corner. The details panel lists every CRITICAL and HIGH CVE of the workload's images: the packages it hits, the installed version and the version that fixes it, with a link to the advisory. A CVE counts once per card, however many packages and containers it is found in. Vulnerabilities are shown apart from problems: nearly every image has some, and a badge on each card would hide what is actually broken. + +The data comes from the `VulnerabilityReport` objects operator-trivy writes, so only namespaces it scans appear (labelled `security-scanning.deckhouse.io/enabled`), and only the severities its `severities` setting keeps. + ## Actions With `actions.enabled` (the default), the details panel offers: @@ -75,7 +81,7 @@ History is kept in memory: after a pod restart the windows fill again. The statu ## Security -The web interface reads the cluster through the module's own service account (`d8:cluster-map` ClusterRole): `get`, `list`, `watch` on namespaces, pods, services, nodes, events, deployments, statefulsets, daemonsets, cronjobs, ingresses and Deckhouse `clusteralerts`, `get` and `list` on `metrics.k8s.io` pods and nodes, and `get` on `pods/log`. It never reads Secrets or ConfigMaps and has no write verbs. Changes go through [actions](#actions), which use the viewer's own identity. +The web interface reads the cluster through the module's own service account (`d8:cluster-map` ClusterRole): `get`, `list`, `watch` on namespaces, pods, services, nodes, events, deployments, statefulsets, daemonsets, cronjobs, ingresses, Deckhouse `clusteralerts` and operator-trivy `vulnerabilityreports`, `get` and `list` on `metrics.k8s.io` pods and nodes, and `get` on `pods/log`. It never reads Secrets or ConfigMaps and has no write verbs. Changes go through [actions](#actions), which use the viewer's own identity. This means **anyone admitted to the web interface sees every namespace and can read container logs**, whatever their own RBAC permissions are; what they can change is limited by their own RBAC. Restrict access with `auth.allowedUserGroups` (and optionally `auth.allowedUserEmails` and `auth.whitelistSourceRanges`). diff --git a/docs/README_RU.md b/docs/README_RU.md index efec052..c20a32e 100644 --- a/docs/README_RU.md +++ b/docs/README_RU.md @@ -26,11 +26,17 @@ Workload'ы под нестандартными контроллерами то Карточка с проблемой несёт значок: жёлтый `!` — предупреждение, красный `!` — ошибка. Рамки и карточки namespace, рамки нод и дерево окрашиваются по самой серьёзной проблеме внутри. Источники: - **Состояние объектов**: контейнеры в `CrashLoopBackOff`, `ImagePullBackOff` и подобном, недавние перезапуски и убийства за нехватку памяти, поды в ожидании или без места для запуска, workload'ы, у которых больше 2 минут не готовы все или часть реплик, застрявшая выкатка, CronJob с неудачным последним запуском, Service без готовых подов, Ingress на несуществующий Service, нода не готова или ей не хватает памяти, диска или PID. -- **Warning-события** за последние 30 минут, сгруппированные по объекту и причине. Сбои планирования, томов и back-off дают значок сразу; любой другой причине нужно пять повторов, до этого она в списке без значка — одна проваленная проба во время рестарта не красит карту. +- **Warning-события** за последние 30 минут, сгруппированные по объекту и причине. Сбои планирования, томов и back-off дают значок сразу; любой другой причине нужно пять повторов в пределах окна, до этого она в списке без значка — одна проваленная проба во время рестарта не красит карту. Счётчик события накапливается с первого появления, поэтому давнее событие пересчитывается на окно: 40 проваленных проб за двое суток — это раз в час, а не всплеск. - **Алерты Prometheus**, которые Deckhouse зеркалирует в объекты `ClusterAlert`. Алерт привязывается по меткам (`deployment`, `statefulset`, `daemonset`, `pod`, `service`, `ingress`, `node`, `namespace`); алерт без объекта и namespace попадает в список кластера. `severity_level` 1–3 — ошибка, 4–6 — предупреждение, 7–9 — информация. Записи уровня «для сведения» (нода закрыта для новых подов, одиночное обычное предупреждение) есть в списке, но значка не дают. Проблемы пересчитываются при каждом запросе карты, открытая страница обновляет их раз в минуту. +## Уязвимости + +Если установлен operator-trivy, у карточки, в образах которой есть CVE уровня CRITICAL, в правом нижнем углу красный щит с их числом. В панели деталей — все CVE уровней CRITICAL и HIGH образов workload'а: какие пакеты затронуты, стоящая версия и версия с исправлением, ссылка на описание. Одна CVE считается на карточке один раз, в скольких бы пакетах и контейнерах она ни нашлась. Уязвимости показываются отдельно от проблем: они есть почти в каждом образе, и значок на каждой карточке скрыл бы то, что действительно сломано. + +Данные берутся из объектов `VulnerabilityReport`, которые пишет operator-trivy, поэтому видны только namespace, которые он сканирует (с меткой `security-scanning.deckhouse.io/enabled`), и только уровни, оставленные его настройкой `severities`. + ## Действия При `actions.enabled` (по умолчанию включено) в панели деталей есть: @@ -75,7 +81,7 @@ Workload'ы под нестандартными контроллерами то ## Безопасность -Веб-интерфейс читает кластер через собственную учётку модуля (ClusterRole `d8:cluster-map`): `get`, `list`, `watch` на namespaces, pods, services, nodes, events, deployments, statefulsets, daemonsets, cronjobs, ingresses и `clusteralerts` Deckhouse, `get` и `list` на pods и nodes из `metrics.k8s.io` и `get` на `pods/log`. Secret и ConfigMap он не читает, прав на запись нет. Изменения идут через [действия](#действия), которые выполняются от имени пользователя. +Веб-интерфейс читает кластер через собственную учётку модуля (ClusterRole `d8:cluster-map`): `get`, `list`, `watch` на namespaces, pods, services, nodes, events, deployments, statefulsets, daemonsets, cronjobs, ingresses, `clusteralerts` Deckhouse и `vulnerabilityreports` operator-trivy, `get` и `list` на pods и nodes из `metrics.k8s.io` и `get` на `pods/log`. Secret и ConfigMap он не читает, прав на запись нет. Изменения идут через [действия](#действия), которые выполняются от имени пользователя. Отсюда следует: **любой, кого пустили в веб-интерфейс, видит все namespace и может читать логи контейнеров** — независимо от собственных прав RBAC; изменить он может только то, что разрешает его RBAC. Ограничивайте доступ через `auth.allowedUserGroups` (а также `auth.allowedUserEmails` и `auth.whitelistSourceRanges`). diff --git a/images/cluster-map/src/internal/kube/cache.go b/images/cluster-map/src/internal/kube/cache.go index 20aeedd..96e3b3c 100644 --- a/images/cluster-map/src/internal/kube/cache.go +++ b/images/cluster-map/src/internal/kube/cache.go @@ -46,6 +46,10 @@ import ( // firing Alertmanager alerts into. var ClusterAlerts = schema.GroupVersionResource{Group: "deckhouse.io", Version: "v1alpha1", Resource: "clusteralerts"} +// VulnerabilityReports are what operator-trivy writes per scanned +// container (namespaced, owned by the workload's ReplicaSet/StatefulSet/…). +var VulnerabilityReports = schema.GroupVersionResource{Group: "aquasecurity.github.io", Version: "v1alpha1", Resource: "vulnerabilityreports"} + // Cache holds synced listers plus a client for reads that must be live // (YAML view, logs). type Cache struct { @@ -55,7 +59,9 @@ type Cache struct { Events corelisters.EventLister // Alerts is nil when the ClusterAlert resource is not served (the // prometheus module is off). - Alerts cache.GenericLister + Alerts cache.GenericLister + // VulnReports is nil when operator-trivy is not installed. + VulnReports cache.GenericLister Namespaces corelisters.NamespaceLister Pods corelisters.PodLister Services corelisters.ServiceLister @@ -100,12 +106,23 @@ func Start(ctx context.Context, client kubernetes.Interface, dyn dynamic.Interfa Events: ef.Core().V1().Events().Lister(), } var df dynamicinformer.DynamicSharedInformerFactory - if dyn != nil && served(client, ClusterAlerts) { + if dyn != nil { df = dynamicinformer.NewDynamicSharedInformerFactory(dyn, 0) + } + if df != nil && served(client, ClusterAlerts) { c.Alerts = df.ForResource(ClusterAlerts).Lister() } else { slog.Info("ClusterAlert resource is not served; alerts are not shown") } + if df != nil && served(client, VulnerabilityReports) { + inf := df.ForResource(VulnerabilityReports) + if err := inf.Informer().SetTransform(trimVulnReport); err != nil { + return nil, err + } + c.VulnReports = inf.Lister() + } else { + slog.Info("VulnerabilityReport resource is not served; vulnerabilities are not shown") + } f.Start(ctx.Done()) ef.Start(ctx.Done()) if df != nil { @@ -149,6 +166,54 @@ func served(client kubernetes.Interface, gvr schema.GroupVersionResource) bool { return false } +// vulnFields are the parts of a vulnerability entry the map shows; the +// rest (links, PURLs, dates) is dropped before caching. +var vulnFields = []string{"vulnerabilityID", "severity", "resource", "installedVersion", "fixedVersion", "title", "primaryLink", "score"} + +func trimVulnReport(obj any) (any, error) { + u, ok := obj.(*unstructured.Unstructured) + if !ok { + return obj, nil + } + u.SetManagedFields(nil) + vulns, found, _ := unstructured.NestedSlice(u.Object, "report", "vulnerabilities") + if !found { + return u, nil + } + for i, v := range vulns { + m, ok := v.(map[string]any) + if !ok { + continue + } + kept := make(map[string]any, len(vulnFields)) + for _, f := range vulnFields { + if x, ok := m[f]; ok { + kept[f] = x + } + } + vulns[i] = kept + } + return u, unstructured.SetNestedSlice(u.Object, vulns, "report", "vulnerabilities") +} + +// VulnReportList returns the cached VulnerabilityReports, nil when off. +func (c *Cache) VulnReportList() []*unstructured.Unstructured { + if c.VulnReports == nil { + return nil + } + objs, err := c.VulnReports.List(labels.Everything()) + if err != nil { + return nil + } + out := make([]*unstructured.Unstructured, 0, len(objs)) + for _, o := range objs { + if u, ok := o.(*unstructured.Unstructured); ok { + out = append(out, u) + } + } + return out +} + // AlertList returns the cached ClusterAlerts, or nil when they are off. func (c *Cache) AlertList() []*unstructured.Unstructured { if c.Alerts == nil { diff --git a/images/cluster-map/src/internal/server/inspect.go b/images/cluster-map/src/internal/server/inspect.go index 4eaa266..d71725f 100644 --- a/images/cluster-map/src/internal/server/inspect.go +++ b/images/cluster-map/src/internal/server/inspect.go @@ -312,3 +312,22 @@ func isReady(p corev1.Pod) bool { } return false } + +// maxVulns bounds the details list; an old base image can carry hundreds. +const maxVulns = 300 + +// handleVulns lists the CRITICAL/HIGH findings of a card's images from the +// cached VulnerabilityReports. +func (s *Server) handleVulns(w http.ResponseWriter, r *http.Request) { + id := r.URL.Query().Get("id") + if !strings.HasPrefix(id, "wl/") { + http.Error(w, "id must be a workload (wl/...)", http.StatusBadRequest) + return + } + ns := strings.SplitN(id, "/", 3)[1] + pods, _ := kube.List[corev1.Pod](s.cache.Pods.Pods(ns)) + writeJSON(w, map[string]any{ + "available": s.cache.VulnReports != nil, + "vulns": topology.VulnsFor(s.cache.VulnReportList(), pods, id, maxVulns), + }) +} diff --git a/images/cluster-map/src/internal/server/server.go b/images/cluster-map/src/internal/server/server.go index 2113fce..e22fb33 100644 --- a/images/cluster-map/src/internal/server/server.go +++ b/images/cluster-map/src/internal/server/server.go @@ -124,6 +124,7 @@ func (s *Server) Handler() http.Handler { mux.Handle("GET /api/logs", s.api("logs", s.handleLogs)) mux.Handle("GET /api/events", s.api("events", s.handleEvents)) mux.Handle("GET /api/usage", s.api("usage", s.handleUsage)) + mux.Handle("GET /api/vulns", s.api("vulns", s.handleVulns)) mux.Handle("GET /api/me", s.api("me", s.handleMe)) mux.Handle("GET /api/permissions", s.api("permissions", s.handlePermissions)) mux.Handle("POST /api/action", s.api("action", s.handleAction)) diff --git a/images/cluster-map/src/internal/topology/graph.go b/images/cluster-map/src/internal/topology/graph.go index 9d284c6..b6b9dce 100644 --- a/images/cluster-map/src/internal/topology/graph.go +++ b/images/cluster-map/src/internal/topology/graph.go @@ -42,6 +42,7 @@ type Node struct { Parent string `json:"parent,omitempty"` Facts map[string]string `json:"facts,omitempty"` // short summary shown without a follow-up fetch Problems []Problem `json:"problems,omitempty"` + Vulns *VulnSummary `json:"vulns,omitempty"` } // Edge is a configuration or placement link between two nodes. @@ -62,6 +63,8 @@ type Graph struct { // AlertsAvailable is false when ClusterAlerts are not served, so the UI // can say that alerts are not part of the picture. AlertsAvailable bool `json:"alertsAvailable"` + // VulnsAvailable is false when operator-trivy is not installed. + VulnsAvailable bool `json:"vulnsAvailable"` } // WorkloadID is the graph ID of a workload. Traffic endpoints use the same @@ -179,6 +182,8 @@ func Build(c *kube.Cache, isSystem func(string) bool) (*Graph, error) { services: services, ingresses: ingresses, nodes: hostNodes, events: events, alerts: c.AlertList(), }) g.AlertsAvailable = c.Alerts != nil + attachVulns(g, c.VulnReportList(), pods) + g.VulnsAvailable = c.VulnReports != nil sort.Slice(g.Nodes, func(i, j int) bool { return g.Nodes[i].ID < g.Nodes[j].ID }) sort.Slice(g.Edges, func(i, j int) bool { return g.Edges[i].ID < g.Edges[j].ID }) diff --git a/images/cluster-map/src/internal/topology/problems.go b/images/cluster-map/src/internal/topology/problems.go index 1eb3ebb..1c5e732 100644 --- a/images/cluster-map/src/internal/topology/problems.go +++ b/images/cluster-map/src/internal/topology/problems.go @@ -148,7 +148,9 @@ func attachProblems(g *Graph, in problemInputs) { cronJobProblems(s, cj) } for _, svc := range in.services { - serviceProblems(s, svc, podsByNs[svc.Namespace]) + if !syntheticProbe(svc.Namespace, svc.Name) { + serviceProblems(s, svc, podsByNs[svc.Namespace]) + } } services := map[string]bool{} for _, svc := range in.services { @@ -363,6 +365,28 @@ func EventTargetID(ref corev1.ObjectReference, podOwner map[string]string) strin return "" } +// eventsInWindow estimates how many occurrences of an event fell in the +// window. The event's count is cumulative since its first occurrence: 40 +// probe failures over two days is one an hour, not 40 in half an hour, so +// a long-lived event is scaled by the part of its lifetime the window +// covers. +func eventsInWindow(e corev1.Event, last time.Time) int { + count := int(e.Count) + if e.Series != nil { + count = int(e.Series.Count) + } + count = max(count, 1) + first := e.FirstTimestamp.Time + if first.IsZero() { + first = e.EventTime.Time + } + span := last.Sub(first) + if first.IsZero() || span <= eventWindow { + return count + } + return max(1, int(float64(count)*float64(eventWindow)/float64(span)+0.5)) +} + // eventProblems folds recent Warning events into one problem per card and // reason, keeping the latest message. func eventProblems(s *problemSet, events []corev1.Event, podOwner map[string]string, live map[string]bool) { @@ -376,11 +400,7 @@ func eventProblems(s *problemSet, events []corev1.Event, podOwner map[string]str } id := EventTargetID(e.InvolvedObject, podOwner) k := key{id, e.Reason} - count := int(e.Count) - if e.Series != nil { - count = int(e.Series.Count) - } - count = max(count, 1) + count := eventsInWindow(e, at) if p := folded[k]; p != nil { p.Count += count if stamp(at) > p.Since { @@ -455,8 +475,13 @@ func podsLive(pods []corev1.Pod) map[string]bool { // lived StatefulSets and pods named upmeter-probe-* that are unschedulable // on purpose (it measures how fast the control plane reacts). Their // FailedScheduling and NotReady are the probe working, not a problem. +// +// smoke-mini-a…e are upmeter's synthetic service: upmeter moves their pods +// between nodes on purpose, so the Service goes without endpoints and the +// pods restart as part of the test. upmeter reports that availability +// itself. func syntheticProbe(ns, name string) bool { - return ns == "d8-upmeter" && strings.HasPrefix(name, "upmeter-probe-") + return ns == "d8-upmeter" && (strings.HasPrefix(name, "upmeter-probe-") || strings.HasPrefix(name, "smoke-mini-")) } // notReadyFrom is when a running or pending pod stopped being ready (or diff --git a/images/cluster-map/src/internal/topology/problems_test.go b/images/cluster-map/src/internal/topology/problems_test.go index bf9fd4a..1215a8d 100644 --- a/images/cluster-map/src/internal/topology/problems_test.go +++ b/images/cluster-map/src/internal/topology/problems_test.go @@ -199,6 +199,36 @@ func TestRedirectIngressAndGonePods(t *testing.T) { } } +func TestEventRateAndSmokeMini(t *testing.T) { + withNow(t) + g := &Graph{Nodes: []Node{{ID: "ns/shop"}, {ID: "ns/d8-upmeter"}, {ID: "wl/shop/deployment/auth"}, {ID: "wl/shop/deployment/burst"}, {ID: "service/d8-upmeter/smoke-mini-a"}}} + attachProblems(g, problemInputs{ + pods: []corev1.Pod{ + {ObjectMeta: metav1.ObjectMeta{Name: "auth-5f-a", Namespace: "shop", OwnerReferences: owned("ReplicaSet", "auth-5f")}}, + {ObjectMeta: metav1.ObjectMeta{Name: "burst-5f-a", Namespace: "shop", OwnerReferences: owned("ReplicaSet", "burst-5f")}}, + }, + services: []corev1.Service{{ObjectMeta: metav1.ObjectMeta{Name: "smoke-mini-a", Namespace: "d8-upmeter"}, Spec: corev1.ServiceSpec{Selector: map[string]string{"smoke-mini": "a"}}}}, + events: []corev1.Event{ + // 40 over two days: about one an hour + {Type: corev1.EventTypeWarning, Reason: "Unhealthy", Count: 40, FirstTimestamp: metav1.NewTime(t0.Add(-48 * time.Hour)), LastTimestamp: metav1.NewTime(t0.Add(-time.Minute)), + InvolvedObject: corev1.ObjectReference{Kind: "Pod", Namespace: "shop", Name: "auth-5f-a"}}, + // 12 within ten minutes + {Type: corev1.EventTypeWarning, Reason: "Unhealthy", Count: 12, FirstTimestamp: metav1.NewTime(t0.Add(-10 * time.Minute)), LastTimestamp: metav1.NewTime(t0.Add(-time.Minute)), + InvolvedObject: corev1.ObjectReference{Kind: "Pod", Namespace: "shop", Name: "burst-5f-a"}}, + {Type: corev1.EventTypeWarning, Reason: "FailedMount", LastTimestamp: metav1.NewTime(t0), InvolvedObject: corev1.ObjectReference{Kind: "Pod", Namespace: "d8-upmeter", Name: "smoke-mini-c-0"}}, + }, + }) + if p := problemsOf(g, "wl/shop/deployment/auth")["Unhealthy"]; p.Severity != Info || p.Count > 2 { + t.Errorf("a slow trickle over two days must be information: %+v", p) + } + if p := problemsOf(g, "wl/shop/deployment/burst")["Unhealthy"]; p.Severity != Warning || p.Count != 12 { + t.Errorf("a burst inside the window must badge: %+v", p) + } + if len(problemsOf(g, "service/d8-upmeter/smoke-mini-a"))+len(problemsOf(g, "ns/d8-upmeter")) != 0 { + t.Error("upmeter's smoke-mini reported as a problem") + } +} + func alert(name, level string, labels map[string]string) *unstructured.Unstructured { l := map[string]any{} for k, v := range labels { diff --git a/images/cluster-map/src/internal/topology/vulns.go b/images/cluster-map/src/internal/topology/vulns.go new file mode 100644 index 0000000..e22a332 --- /dev/null +++ b/images/cluster-map/src/internal/topology/vulns.go @@ -0,0 +1,203 @@ +/* +Copyright 2026 The cluster-map Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package topology + +import ( + "sort" + + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" +) + +// VulnSummary is what a card shows about its images: counts of distinct +// CRITICAL and HIGH CVEs (operator-trivy keeps only those here) and how +// many of them a newer package version fixes. One CVE in six subpackages +// of openssh is one CVE. +type VulnSummary struct { + Critical int `json:"critical"` + High int `json:"high"` + FixableCritical int `json:"fixableCritical"` + Fixable int `json:"fixable"` +} + +// Vuln is one finding in the details list. +type Vuln struct { + ID string `json:"id"` + Severity string `json:"severity"` + Package string `json:"package"` // comma-separated when the CVE hits several packages of one image + Installed string `json:"installed"` + Fixed string `json:"fixed,omitempty"` + Title string `json:"title,omitempty"` + Link string `json:"link,omitempty"` + Score float64 `json:"score,omitempty"` + Image string `json:"image"` + Container string `json:"container"` +} + +// vulnTargetID maps a report to its card the way events are mapped: the +// report is labelled with the scanned object (a ReplicaSet, StatefulSet, +// CronJob, Job or bare Pod). +func vulnTargetID(r *unstructured.Unstructured, podOwner map[string]string) string { + l := r.GetLabels() + ref := corev1.ObjectReference{ + Kind: l["trivy-operator.resource.kind"], + Name: l["trivy-operator.resource.name"], + Namespace: l["trivy-operator.resource.namespace"], + } + if ref.Namespace == "" { + ref.Namespace = r.GetNamespace() + } + return EventTargetID(ref, podOwner) +} + +func reportImage(r *unstructured.Unstructured) string { + server, _, _ := unstructured.NestedString(r.Object, "report", "registry", "server") + repo, _, _ := unstructured.NestedString(r.Object, "report", "artifact", "repository") + tag, _, _ := unstructured.NestedString(r.Object, "report", "artifact", "tag") + img := repo + if server != "" { + img = server + "/" + repo + } + if tag != "" { + img += ":" + tag + } + return img +} + +func reportVulns(r *unstructured.Unstructured) []Vuln { + raw, _, _ := unstructured.NestedSlice(r.Object, "report", "vulnerabilities") + image, container := reportImage(r), r.GetLabels()["trivy-operator.container.name"] + out := make([]Vuln, 0, len(raw)) + for _, x := range raw { + m, ok := x.(map[string]any) + if !ok { + continue + } + str := func(k string) string { s, _ := m[k].(string); return s } + score, _ := m["score"].(float64) + out = append(out, Vuln{ + ID: str("vulnerabilityID"), Severity: str("severity"), Package: str("resource"), + Installed: str("installedVersion"), Fixed: str("fixedVersion"), Title: str("title"), + Link: str("primaryLink"), Score: score, Image: image, Container: container, + }) + } + return out +} + +func podOwners(pods []corev1.Pod) map[string]string { + out := make(map[string]string, len(pods)) + for _, p := range pods { + out[p.Namespace+"/"+p.Name] = OwnerWorkloadID(p) + } + return out +} + +// attachVulns fills Node.Vulns. A CVE counts once per card, whatever the +// number of packages and containers it is found in. +func attachVulns(g *Graph, reports []*unstructured.Unstructured, pods []corev1.Pod) { + if len(reports) == 0 { + return + } + owners := podOwners(pods) + seen := map[string]map[string]bool{} + sums := map[string]*VulnSummary{} + for _, r := range reports { + card := vulnTargetID(r, owners) + if card == "" { + continue + } + if seen[card] == nil { + seen[card], sums[card] = map[string]bool{}, &VulnSummary{} + } + for _, v := range reportVulns(r) { + if seen[card][v.ID] { + continue + } + seen[card][v.ID] = true + s := sums[card] + switch v.Severity { + case "CRITICAL": + s.Critical++ + if v.Fixed != "" { + s.FixableCritical++ + } + case "HIGH": + s.High++ + default: + continue + } + if v.Fixed != "" { + s.Fixable++ + } + } + } + for i := range g.Nodes { + if s, ok := sums[g.Nodes[i].ID]; ok && (s.Critical > 0 || s.High > 0) { + g.Nodes[i].Vulns = s + } + } +} + +var severityOrder = map[string]int{"CRITICAL": 0, "HIGH": 1, "MEDIUM": 2, "LOW": 3} + +// VulnsFor lists the findings of one card, most severe first, fixable +// before unfixable, one entry per CVE and image with its packages joined. +func VulnsFor(reports []*unstructured.Unstructured, pods []corev1.Pod, cardID string, limit int) []Vuln { + owners := podOwners(pods) + type key struct{ id, image string } + at := map[key]int{} + pkgs := map[key]map[string]bool{} + out := []Vuln{} + for _, r := range reports { + if vulnTargetID(r, owners) != cardID { + continue + } + for _, v := range reportVulns(r) { + k := key{v.ID, v.Image} + i, ok := at[k] + if !ok { + at[k], pkgs[k] = len(out), map[string]bool{v.Package: true} + out = append(out, v) + continue + } + if !pkgs[k][v.Package] { + pkgs[k][v.Package] = true + out[i].Package += ", " + v.Package + } + if out[i].Fixed == "" { + out[i].Fixed = v.Fixed + } + } + } + sort.SliceStable(out, func(i, j int) bool { + a, b := out[i], out[j] + if severityOrder[a.Severity] != severityOrder[b.Severity] { + return severityOrder[a.Severity] < severityOrder[b.Severity] + } + if (a.Fixed != "") != (b.Fixed != "") { + return a.Fixed != "" + } + if a.Score != b.Score { + return a.Score > b.Score + } + return a.ID < b.ID + }) + if limit > 0 && len(out) > limit { + out = out[:limit] + } + return out +} diff --git a/images/cluster-map/src/internal/topology/vulns_test.go b/images/cluster-map/src/internal/topology/vulns_test.go new file mode 100644 index 0000000..01728f1 --- /dev/null +++ b/images/cluster-map/src/internal/topology/vulns_test.go @@ -0,0 +1,73 @@ +/* +Copyright 2026 The cluster-map Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package topology + +import ( + "testing" + + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" +) + +func vulnReport(ns, kind, name, container, repo string, vulns ...map[string]any) *unstructured.Unstructured { + list := make([]any, len(vulns)) + for i, v := range vulns { + list[i] = v + } + u := &unstructured.Unstructured{Object: map[string]any{ + "report": map[string]any{ + "registry": map[string]any{"server": "ghcr.io"}, + "artifact": map[string]any{"repository": repo, "tag": "1.0"}, + "vulnerabilities": list, + }, + }} + u.SetNamespace(ns) + u.SetLabels(map[string]string{"trivy-operator.resource.kind": kind, "trivy-operator.resource.name": name, "trivy-operator.resource.namespace": ns, "trivy-operator.container.name": container}) + return u +} + +func cve(id, sev, pkg, fixed string, score float64) map[string]any { + return map[string]any{"vulnerabilityID": id, "severity": sev, "resource": pkg, "installedVersion": "1.0", "fixedVersion": fixed, "score": score} +} + +func TestVulns(t *testing.T) { + g := &Graph{Nodes: []Node{{ID: "wl/shop/deployment/web"}, {ID: "wl/shop/cronjob/backup"}, {ID: "wl/shop/deployment/clean"}}} + reports := []*unstructured.Unstructured{ + vulnReport("shop", "ReplicaSet", "web-5f", "app", "acme/web", + cve("CVE-1", "CRITICAL", "openssl", "3.0.2", 9.8), cve("CVE-1", "CRITICAL", "libssl3", "3.0.2", 9.8), + cve("CVE-2", "CRITICAL", "zlib", "", 9.1), cve("CVE-3", "HIGH", "curl", "8.1", 7.5)), + // the sidecar on the same base image repeats CVE-1: counted once + vulnReport("shop", "ReplicaSet", "web-5f", "sidecar", "acme/web-sidecar", cve("CVE-1", "CRITICAL", "openssl", "3.0.2", 9.8)), + vulnReport("shop", "Job", "backup-29100000", "dump", "library/alpine", cve("CVE-4", "HIGH", "busybox", "1.37", 7.0)), + } + attachVulns(g, reports, nil) + web := g.Nodes[0].Vulns + if web == nil || web.Critical != 2 || web.High != 1 || web.FixableCritical != 1 || web.Fixable != 2 { + t.Errorf("web summary: %+v", web) + } + if b := g.Nodes[1].Vulns; b == nil || b.High != 1 { + t.Errorf("a CronJob's job report attaches to the CronJob: %+v", b) + } + if g.Nodes[2].Vulns != nil { + t.Error("a card without reports carries no summary") + } + list := VulnsFor(reports, []corev1.Pod{{ObjectMeta: metav1.ObjectMeta{Name: "x"}}}, "wl/shop/deployment/web", 0) + if len(list) != 4 || list[0].ID != "CVE-1" || list[0].Package != "openssl, libssl3" || list[0].Image != "ghcr.io/acme/web:1.0" || list[2].ID != "CVE-2" || list[3].Severity != "HIGH" { + t.Errorf("order (critical fixable, critical fixable sidecar, critical unfixable, high): %+v", list) + } +} diff --git a/images/cluster-map/src/web/static/app.css b/images/cluster-map/src/web/static/app.css index 748ee31..1cf600b 100644 --- a/images/cluster-map/src/web/static/app.css +++ b/images/cluster-map/src/web/static/app.css @@ -282,6 +282,14 @@ table.pods th { text-align: left; color: var(--muted); font-weight: 500; padding table.pods td { padding: 0.3rem; border-bottom: 1px solid var(--line); vertical-align: top; overflow-wrap: anywhere; } table.pods .warn-text { color: var(--warn); font-weight: 600; } +/* vulnerabilities */ +.vuln-crit { color: var(--error); font-weight: 600; } +.sev-chip { display: inline-grid; place-items: center; width: 14px; height: 14px; border-radius: 3px; font-size: 0.6rem; font-weight: 800; color: #fff; } +.sev-chip.critical { background: var(--error); } +.sev-chip.high { background: var(--warn); } +table.vulns a { color: var(--accent); } +.shield-legend { display: inline-block; width: 10px; height: 12px; background: var(--error); clip-path: polygon(50% 0, 100% 15%, 100% 50%, 50% 100%, 0 50%, 0 15%); } + /* events */ .event-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.35rem; } .event-list li { border-left: 3px solid var(--line); padding: 0.1rem 0 0.1rem 0.5rem; } diff --git a/images/cluster-map/src/web/static/app.js b/images/cluster-map/src/web/static/app.js index 1adc1b8..a72f6e5 100644 --- a/images/cluster-map/src/web/static/app.js +++ b/images/cluster-map/src/web/static/app.js @@ -83,12 +83,12 @@ function fitText(text, font, max) { return text.slice(0, lo) + '…'; } const imgCache = new Map(); -function cardImage({ kind, title, sub, health, ghost, w, h, badge }) { - const key = [kind, title, sub, health, ghost, w, h, badge].join('|'); +function cardImage({ kind, title, sub, health, ghost, w, h, badge, shield }) { + const key = [kind, title, sub, health, ghost, w, h, badge, shield].join('|'); if (imgCache.has(key)) return imgCache.get(key); const c = (KIND[kind] || KIND.external).color; const name = fitText(title, `600 12.5px ${FONT}`, w - (badge ? 74 : 58)); - const line2 = sub ? fitText(sub, `11px ${FONT}`, w - 50) : ''; + const line2 = sub ? fitText(sub, `11px ${FONT}`, w - (shield ? 78 : 50)) : ''; const nameY = sub ? h / 2 - 3 : h / 2 + 4.5; const dot = health ? `` : ''; // "!" in a circle left of the health dot: yellow warning, red critical @@ -103,12 +103,22 @@ function cardImage({ kind, title, sub, health, ghost, w, h, badge }) { ${ICON[kind] || ''} ${esc(name)} ${sub ? `${esc(line2)}` : ''} -${dot}${mark}`; +${dot}${mark}${shieldMark(shield, w, h)}`; const url = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svg); imgCache.set(key, url); return url; } +// Shield in the bottom-right corner: critical CVEs in the card's images +// (operator-trivy). Kept apart from the problem badge: nearly every image +// has some, and they would drown the "is it working" signal. +function shieldMark(n, w, h) { + if (!n) return ''; + const x = w - 30, y = h - 17, label = n > 99 ? '99+' : String(n); + return ` +${label}`; +} + /* ---------- data ---------- */ const G = { raw: null, byId: new Map(), byNs: new Map(), nsNodes: [], hostNodes: [], @@ -530,7 +540,7 @@ function cardNode(n, parent, ghost) { group: 'nodes', classes: ghost ? 'ghostcard' : '', data: { id: n.id, ref: n.id, parent, kind: n.kind, w: size.w, h: size.h, - img: cardImage({ kind: n.kind, title: n.label, sub: ghost ? n.namespace + ' · ' + KIND[n.kind].title : subline(n), health: ghost ? null : n.facts?.health, ghost, w: size.w, h: size.h, badge: cardSev(n) }), + img: cardImage({ kind: n.kind, title: n.label, sub: ghost ? n.namespace + ' · ' + KIND[n.kind].title : subline(n), health: ghost ? null : n.facts?.health, ghost, w: size.w, h: size.h, badge: cardSev(n), shield: ghost ? 0 : n.vulns?.critical }), }, }; } @@ -1029,7 +1039,9 @@ function renderLegend() { ); } function badgeLegend() { - return ['warning', 'critical'].map((sev) => el('span', {}, el('span', { class: 'pbadge ' + sev, 'aria-hidden': 'true' }, '!'), t('legend.' + sev))); + const items = ['warning', 'critical'].map((sev) => el('span', {}, el('span', { class: 'pbadge ' + sev, 'aria-hidden': 'true' }, '!'), t('legend.' + sev))); + if (G.raw?.vulnsAvailable) items.push(el('span', {}, el('span', { class: 'shield-legend', 'aria-hidden': 'true' }), t('legend.vulns'))); + return items; } /* ---------- details panel ---------- */ @@ -1152,6 +1164,7 @@ function renderOverview(n) { } parts.push(dl); if (hasPods(n) || n.kind === 'node') parts.push(el('div', { class: 'section-title' }, t('usage.title')), el('div', { id: 'd-usage' }, el('p', { class: 'muted small' }, t('loading')))); + if (n.id.startsWith('wl/') && G.raw?.vulnsAvailable) parts.push(el('div', { class: 'section-title' }, t('vulns.title')), el('div', { id: 'd-vulns' }, vulnSummary(n))); const hosts = [...(G.placement.get(id) || [])].sort(); if (hosts.length) { parts.push(el('div', { class: 'section-title' }, t('details.runsOn')), @@ -1160,6 +1173,7 @@ function renderOverview(n) { } panel.replaceChildren(...parts); if (hasPods(n) || n.kind === 'node') loadUsage(n); + if (n.vulns && G.raw?.vulnsAvailable) loadVulns(n); } /* ---------- usage ---------- */ @@ -1211,6 +1225,36 @@ async function loadUsage(n) { if (detailsId === n.id) box.replaceChildren(...parts); } +/* ---------- vulnerabilities ---------- */ +function vulnSummary(n) { + const v = n.vulns; + if (!v) return el('p', { class: 'muted small' }, t('vulns.none')); + return el('p', { class: 'small' }, + el('span', { class: v.critical ? 'vuln-crit' : '' }, t('plural.critical_cve', { n: v.critical })), ', ', + t('plural.high_cve', { n: v.high }), ' · ', t('vulns.fixable', { n: v.fixable })); +} +async function loadVulns(n) { + const r = await fetch('/api/vulns?' + new URLSearchParams({ id: n.id })); + const box = $('#d-vulns'); + if (detailsId !== n.id || !box) return; + if (!r.ok) { box.append(el('p', { class: 'muted small' }, t('vulns.failed', { code: r.status }))); return; } + const { vulns } = await r.json(); + if (!vulns.length) return; + const shown = vulns.slice(0, 40); + const table = el('table', { class: 'pods vulns' }, + el('thead', {}, el('tr', {}, ...[t('vulns.cve'), t('vulns.package'), t('vulns.version')].map((x) => el('th', {}, x)))), + el('tbody', {}, shown.map((v) => el('tr', {}, + el('td', {}, el('span', { class: 'sev-chip ' + v.severity.toLowerCase() }, v.severity === 'CRITICAL' ? 'C' : 'H'), ' ', + v.link ? el('a', { href: v.link, target: '_blank', rel: 'noopener noreferrer', title: v.title || '' }, v.id) : v.id), + el('td', {}, v.package, el('div', { class: 'muted small' }, shortImage(v.image))), + el('td', {}, v.installed, ' → ', v.fixed ? el('strong', {}, v.fixed) : el('span', { class: 'muted' }, t('vulns.noFix'))))))); + const parts = [table]; + if (vulns.length > shown.length) parts.push(el('p', { class: 'muted small' }, t('vulns.more', { n: vulns.length - shown.length }))); + box.append(...parts); +} +// drop the registry host (a first segment with a dot) and any digest +const shortImage = (img) => img.replace(/^[^/]*\.[^/]*\//, '').replace(/@sha256:.*/, ''); + /* ---------- events ---------- */ async function loadEvents(id) { const panel = document.querySelector('[data-panel="events"]'); diff --git a/images/cluster-map/src/web/static/i18n.js b/images/cluster-map/src/web/static/i18n.js index ffa7679..afdfefe 100644 --- a/images/cluster-map/src/web/static/i18n.js +++ b/images/cluster-map/src/web/static/i18n.js @@ -110,6 +110,13 @@ const I18N = { 'confirm.cordon': 'No new pods are scheduled on {target}. Running pods stay.', 'confirm.uncordon': 'New pods can be scheduled on {target} again.', 'confirm.delete-pod': 'Pod {target} in {ns} is deleted; its controller starts a new one.', + 'vulns.title': 'Vulnerabilities', 'vulns.none': 'No CRITICAL or HIGH vulnerabilities found in the scanned images.', + 'vulns.fixable': 'fixed in a newer version: {n}', 'vulns.failed': 'could not read vulnerabilities: HTTP {code}', + 'vulns.cve': 'CVE', 'vulns.package': 'Package', 'vulns.version': 'Installed → fixed', 'vulns.noFix': 'no fix yet', + 'vulns.more': 'and {n} more', + 'plural.critical_cve': { one: '{n} critical', other: '{n} critical' }, + 'plural.high_cve': { one: '{n} high', other: '{n} high' }, + 'legend.vulns': 'critical vulnerabilities in the images (trivy)', }, ru: { 'kind.pod': 'Контроллер', 'kind.external': 'Внешний', 'folder.pod': 'Другие контроллеры', @@ -217,6 +224,13 @@ const I18N = { 'confirm.cordon': 'На {target} не будут назначаться новые поды. Работающие остаются.', 'confirm.uncordon': 'На {target} снова можно назначать новые поды.', 'confirm.delete-pod': 'Под {target} в {ns} удаляется; контроллер запустит новый.', + 'vulns.title': 'Уязвимости', 'vulns.none': 'В проверенных образах нет уязвимостей уровня CRITICAL и HIGH.', + 'vulns.fixable': 'исправлено в новой версии: {n}', 'vulns.failed': 'не удалось получить уязвимости: HTTP {code}', + 'vulns.cve': 'CVE', 'vulns.package': 'Пакет', 'vulns.version': 'Стоит → исправлено', 'vulns.noFix': 'исправления нет', + 'vulns.more': 'и ещё {n}', + 'plural.critical_cve': { one: '{n} критическая', few: '{n} критические', many: '{n} критических', other: '{n} критической' }, + 'plural.high_cve': { one: '{n} высокая', few: '{n} высокие', many: '{n} высоких', other: '{n} высокой' }, + 'legend.vulns': 'критические уязвимости в образах (trivy)', }, }; diff --git a/templates/rbac-for-us.yaml b/templates/rbac-for-us.yaml index 7f727f4..237a01c 100644 --- a/templates/rbac-for-us.yaml +++ b/templates/rbac-for-us.yaml @@ -9,8 +9,9 @@ automountServiceAccountToken: true --- # Read-only on exactly what the map shows. No Secrets, no ConfigMaps, no # write verbs. pods/log backs the Logs tab, events and clusteralerts the -# problem badges, metrics.k8s.io the Load section. Actions (restart, scale…) -# never use this account: they run with the viewer's own token. +# problem badges, vulnerabilityreports (operator-trivy) the shield on +# cards, metrics.k8s.io the Load section. Actions (restart, scale…) never +# use this account: they run with the viewer's own token. apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: @@ -29,6 +30,9 @@ rules: - apiGroups: ["deckhouse.io"] resources: ["clusteralerts"] verbs: ["get", "list", "watch"] +- apiGroups: ["aquasecurity.github.io"] + resources: ["vulnerabilityreports"] + verbs: ["get", "list", "watch"] - apiGroups: ["metrics.k8s.io"] resources: ["pods", "nodes"] verbs: ["get", "list"]