From fb21733e08d5f0f81045ea3cd4adb94c875fd9f0 Mon Sep 17 00:00:00 2001 From: Cyril Rohr Date: Mon, 9 Nov 2026 14:16:24 +0100 Subject: [PATCH] Report registry image references from CRI container status --- README.md | 16 ++++++++++++++-- charts/recense/Chart.yaml | 4 ++-- charts/recense/values.yaml | 2 +- internal/agent/agent.go | 1 + internal/agent/agent_test.go | 16 +++++++++++++--- internal/agent/runtime.go | 36 +++++++++++++++++++++++++----------- 6 files changed, 56 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 3367e38..78d2e51 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ curl -fsSLO https://github.com/opf/recense/releases/latest/download/install.sh sudo sh install.sh ``` -The installer verifies the release archive checksum, installs binaries under `/opt/recense/bin`, and starts `recense.service`. Rerun with a release tag to upgrade or downgrade: `sudo sh install.sh v0.2.2`. +The installer verifies the release archive checksum, installs binaries under `/opt/recense/bin`, and starts `recense.service`. Rerun with a release tag to upgrade or downgrade: `sudo sh install.sh v0.2.3`. ```sh sudo systemctl status recense @@ -40,7 +40,7 @@ For EC2-backed EKS or other Linux Kubernetes nodes with containerd and CRI v1: ```sh helm upgrade --install recense \ - https://github.com/opf/recense/releases/download/v0.2.2/recense-0.2.2.tgz \ + https://github.com/opf/recense/releases/download/v0.2.3/recense-0.2.3.tgz \ --namespace recense --create-namespace \ --set clusterName=production-eks \ --set environment=production @@ -145,3 +145,15 @@ Filter logs by `service.name = recense`, then by event type: Keep all detected packages in scope. Use dashboard filters to prioritize High/Critical findings and available fixes. Counts represent package/advisory matches, not unique CVEs. Choose the latest successful scan per target before aggregating; summing several days repeats findings. Show failed scans and scan age separately. Schema 3 uses standard OpenTelemetry names for service, host, cluster, and deployment identity. All custom fields use `recense.`. Scanned-container metadata uses `recense.target.*`; collector resource attributes identify the Recense pod. Old field aliases are not emitted. + +### Container image identity + +For CRI/containerd, `recense.target.image.name` comes from container status. +`recense.target.image.reference` holds the repository-qualified registry digest +(for example, `registry/app@sha256:…`). `recense.target.image.id` remains the +runtime image ID; it may identify the image configuration rather than the +manifest shown in ECR. Registry references are empty when unavailable. Docker +discovery currently reports its image name and runtime ID only. + +These fields describe the image used to create the container. Scans inspect its +running filesystem, which can differ from the original image. diff --git a/charts/recense/Chart.yaml b/charts/recense/Chart.yaml index 7ba08ed..12a48dd 100644 --- a/charts/recense/Chart.yaml +++ b/charts/recense/Chart.yaml @@ -2,5 +2,5 @@ apiVersion: v2 name: recense description: Host and container software inventory and vulnerability logs type: application -version: 0.2.2 -appVersion: "0.2.2" +version: 0.2.3 +appVersion: "0.2.3" diff --git a/charts/recense/values.yaml b/charts/recense/values.yaml index a6754f8..b47670c 100644 --- a/charts/recense/values.yaml +++ b/charts/recense/values.yaml @@ -1,6 +1,6 @@ image: repository: ghcr.io/opf/recense - tag: "0.2.2" + tag: "0.2.3" pullPolicy: IfNotPresent imagePullSecrets: [] clusterName: "" diff --git a/internal/agent/agent.go b/internal/agent/agent.go index bc45827..c4659b0 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -212,6 +212,7 @@ func containerFields(c container, runtimeName string) Event { "recense.target.container.runtime": runtimeName, "recense.target.image.id": c.ImageID, "recense.target.image.name": c.ImageName, + "recense.target.image.reference": c.ImageReference, "recense.target.k8s.namespace.name": c.PodNamespace, "recense.target.k8s.pod.name": c.PodName, "recense.target.k8s.container.name": c.PodContainer, diff --git a/internal/agent/agent_test.go b/internal/agent/agent_test.go index 6f68f39..945acd2 100644 --- a/internal/agent/agent_test.go +++ b/internal/agent/agent_test.go @@ -184,13 +184,13 @@ func (f *criFixture) ListContainers(_ context.Context, r *cri.ListContainersRequ if r.Filter.State.State != cri.ContainerState_CONTAINER_RUNNING { panic("wrong filter") } - return &cri.ListContainersResponse{Containers: []*cri.Container{{Id: "c1", ImageRef: "sha256:config", Image: &cri.ImageSpec{Image: "app:latest"}, Metadata: &cri.ContainerMetadata{Name: "api"}, Labels: map[string]string{"io.kubernetes.pod.name": "api-123", "io.kubernetes.pod.namespace": "prod"}}}}, nil + return &cri.ListContainersResponse{Containers: []*cri.Container{{Id: "c1", ImageRef: "sha256:config", Image: &cri.ImageSpec{Image: "sha256:config"}, Metadata: &cri.ContainerMetadata{Name: "api"}, Labels: map[string]string{"io.kubernetes.pod.name": "api-123", "io.kubernetes.pod.namespace": "prod"}}}}, nil } func (f *criFixture) ContainerStatus(_ context.Context, r *cri.ContainerStatusRequest) (*cri.ContainerStatusResponse, error) { if !r.Verbose || r.ContainerId != "c1" { panic("wrong status request") } - return &cri.ContainerStatusResponse{Status: &cri.ContainerStatus{State: cri.ContainerState_CONTAINER_RUNNING}, Info: map[string]string{"info": `{"pid":456,"secret":"never-log"}`}}, nil + return &cri.ContainerStatusResponse{Status: &cri.ContainerStatus{State: cri.ContainerState_CONTAINER_RUNNING, Image: &cri.ImageSpec{Image: "registry.example/app:release"}, ImageRef: "registry.example/app@sha256:manifest"}, Info: map[string]string{"info": `{"pid":456,"secret":"never-log"}`}}, nil } func TestCRIDiscoveryReadsPID(t *testing.T) { socket := testSocket(t) @@ -208,7 +208,7 @@ func TestCRIDiscoveryReadsPID(t *testing.T) { if err != nil { t.Fatal(err) } - if len(rows) != 1 || rows[0].ImageID != "sha256:config" || rows[0].PID != 456 || rows[0].PodNamespace != "prod" { + if len(rows) != 1 || rows[0].ImageID != "sha256:config" || rows[0].PID != 456 || rows[0].PodNamespace != "prod" || rows[0].ImageName != "registry.example/app:release" || rows[0].ImageReference != "registry.example/app@sha256:manifest" { t.Fatalf("bad discovery: %#v", rows) } } @@ -343,3 +343,13 @@ func testSocket(t *testing.T) string { t.Cleanup(func() { os.RemoveAll(dir) }) return filepath.Join(dir, "s") } + +func TestRegistryReferenceDoesNotMislabelConfigID(t *testing.T) { + if registryReference("sha256:config") != "" { + t.Fatal("configuration ID is not a registry reference") + } + fields := containerFields(container{ImageID: "sha256:config", ImageName: "registry/app:tag", ImageReference: "registry/app@sha256:manifest"}, "containerd") + if fields["recense.target.image.reference"] != "registry/app@sha256:manifest" || fields["recense.target.image.id"] != "sha256:config" { + t.Fatalf("bad image fields: %#v", fields) + } +} diff --git a/internal/agent/runtime.go b/internal/agent/runtime.go index a3480d0..fd5bef4 100644 --- a/internal/agent/runtime.go +++ b/internal/agent/runtime.go @@ -22,8 +22,8 @@ import ( var errContainerStopped = errors.New("container is no longer running") type container struct { - ID, Name, ImageID, ImageName, PodName, PodNamespace, PodContainer string - PID int + ID, Name, ImageID, ImageName, ImageReference, PodName, PodNamespace, PodContainer string + PID int } func discover(ctx context.Context, c Config) ([]container, string, string, error) { @@ -132,7 +132,7 @@ func criContainers(ctx context.Context, socket string) ([]container, error) { if r.Metadata != nil { name = r.Metadata.Name } - pid, err := criPID(ctx, cri.NewRuntimeServiceClient(conn), r.Id) + pid, imageStatus, err := criStatus(ctx, cri.NewRuntimeServiceClient(conn), r.Id) if errors.Is(err, errContainerStopped) { continue } @@ -140,7 +140,7 @@ func criContainers(ctx context.Context, socket string) ([]container, error) { return nil, err } - result = append(result, container{ID: r.Id, Name: name, ImageID: r.ImageRef, ImageName: r.GetImage().GetImage(), PID: pid, PodName: r.Labels["io.kubernetes.pod.name"], PodNamespace: r.Labels["io.kubernetes.pod.namespace"], PodContainer: name}) + result = append(result, container{ID: r.Id, Name: name, ImageID: r.ImageRef, ImageName: imageStatus.GetImage().GetImage(), ImageReference: registryReference(imageStatus.ImageRef), PID: pid, PodName: r.Labels["io.kubernetes.pod.name"], PodNamespace: r.Labels["io.kubernetes.pod.namespace"], PodContainer: name}) } return result, nil } @@ -173,28 +173,42 @@ func dockerPID(ctx context.Context, client *http.Client, id string) (int, error) return status.State.Pid, nil } -func criPID(ctx context.Context, client cri.RuntimeServiceClient, id string) (int, error) { +func criStatus(ctx context.Context, client cri.RuntimeServiceClient, id string) (int, *cri.ContainerStatus, error) { response, err := client.ContainerStatus(ctx, &cri.ContainerStatusRequest{ContainerId: id, Verbose: true}) if status.Code(err) == codes.NotFound { - return 0, errContainerStopped + return 0, nil, errContainerStopped } if err != nil { - return 0, fmt.Errorf("CRI container status: %w", err) + return 0, nil, fmt.Errorf("CRI container status: %w", err) } if response.Status == nil || response.Status.State != cri.ContainerState_CONTAINER_RUNNING { - return 0, errContainerStopped + return 0, nil, errContainerStopped } // Runtime info may contain environment secrets; decode only the PID. var info struct { PID int `json:"pid"` } if err := json.Unmarshal([]byte(response.Info["info"]), &info); err != nil { - return 0, fmt.Errorf("invalid CRI process metadata") + return 0, nil, fmt.Errorf("invalid CRI process metadata") } if info.PID <= 1 { - return 0, fmt.Errorf("runtime did not provide a running container PID") + return 0, nil, fmt.Errorf("runtime did not provide a running container PID") } - return info.PID, nil + return info.PID, response.Status, nil +} + +// Only repository-qualified references identify an image in a registry. +// Some runtimes return a bare configuration ID in ImageRef instead. +func registryReference(ref string) string { + if strings.Contains(ref, "@") { + return ref + } + return "" +} + +func criPID(ctx context.Context, client cri.RuntimeServiceClient, id string) (int, error) { + pid, _, err := criStatus(ctx, client, id) + return pid, err } // Runtime identity must still match at scan time, even if the discovered PID