From 32a216e38090e40de70dbe1fee4f551a24dc6759 Mon Sep 17 00:00:00 2001 From: rcohencyberarmor Date: Wed, 16 Aug 2023 18:19:37 +0300 Subject: [PATCH] get name from instance id Signed-off-by: rcohencyberarmor --- repositories/apiserver.go | 6 +++++- repositories/apiserver_test.go | 21 ++++++++++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/repositories/apiserver.go b/repositories/apiserver.go index b5ef885..c24d59f 100644 --- a/repositories/apiserver.go +++ b/repositories/apiserver.go @@ -314,7 +314,11 @@ func getCVESummaryK8sResourceName(ctx context.Context) (string, error) { if !ok { return "", domain.ErrCastingWorkload } - return workload.ImageSlug, nil + instanceID, err := instanceidhandler.GenerateInstanceIDFromString(workload.InstanceID) + if err != nil { + return "", nil + } + return instanceID.GetSlug() } func (a *APIServerStore) storeCVESummary(ctx context.Context, cve domain.CVEManifest, withRelevancy bool) error { diff --git a/repositories/apiserver_test.go b/repositories/apiserver_test.go index c8f5d34..bf4bdd2 100644 --- a/repositories/apiserver_test.go +++ b/repositories/apiserver_test.go @@ -592,36 +592,43 @@ func TestAPIServerStore_enrichSummaryManifestObjectAnnotations(t *testing.T) { func TestAPIServerStore_getCVESummaryK8sResourceName(t *testing.T) { tests := []struct { workload domain.ScanCommand + expRes string }{ { workload: domain.ScanCommand{ - ImageSlug: "default-replicaset-nginx-77b4fdf86c-6e03-a89e", + InstanceID: "apiVersion-apps/v1/namespace-default/kind-ReplicaSet/name-nginx-77b4fdf86c/containerName-nginx", }, + expRes: "default-replicaset-nginx-77b4fdf86c-6e03-a89e", }, { workload: domain.ScanCommand{ - ImageSlug: "kubescape-replicaset-gateway-66967b649-495e-df93", + InstanceID: "apiVersion-apps/v1/namespace-kubescape/kind-ReplicaSet/name-otel-collector-76bb986488/containerName-otel-collector", }, + expRes: "kubescape-replicaset-otel-collector-76bb986488-6d83-cca3", }, { workload: domain.ScanCommand{ - ImageSlug: "kubescape-replicaset-kubescape-5d4bf4589c-7b8d-5074", + InstanceID: "apiVersion-apps/v1/namespace-kubescape/kind-ReplicaSet/name-gateway-66967b649/containerName-gateway", }, + expRes: "kubescape-replicaset-gateway-66967b649-495e-df93", }, { workload: domain.ScanCommand{ - ImageSlug: "kubescape-replicaset-kubevuln-988fd7dbd-468f-8953", + InstanceID: "apiVersion-apps/v1/namespace-kubescape/kind-StatefulSet/name-kollector/containerName-kollector", }, + expRes: "kubescape-statefulset-kollector-c1be-77d8", }, { workload: domain.ScanCommand{ - ImageSlug: "kubescape-replicaset-operator-647f75985c-ff5d-6454", + InstanceID: "apiVersion-apps/v1/namespace-kubescape/kind-ReplicaSet/name-kubescape-5d4bf4589c/containerName-kubescape", }, + expRes: "kubescape-replicaset-kubescape-5d4bf4589c-7b8d-5074", }, { workload: domain.ScanCommand{ - ImageSlug: "kubescape-replicaset-storage-5ff864df8f-fc62-4b1c", + InstanceID: "apiVersion-apps/v1/namespace-kubescape/kind-ReplicaSet/name-kubevuln-65bfbfdcdd/containerName-kubevuln", }, + expRes: "kubescape-replicaset-kubevuln-65bfbfdcdd-9730-b4bb", }, } @@ -638,7 +645,7 @@ func TestAPIServerStore_getCVESummaryK8sResourceName(t *testing.T) { ctx := context.WithValue(context.Background(), domain.WorkloadKey{}, tests[i].workload) name, err := getCVESummaryK8sResourceName(ctx) assert.Equal(t, err, nil) - assert.Equal(t, tests[i].workload.ImageSlug, name) + assert.Equal(t, tests[i].expRes, name) } for i := range testsErrorCases {