Skip to content

Commit

Permalink
store workload name in the summary object
Browse files Browse the repository at this point in the history
Signed-off-by: rcohencyberarmor <rcohen@armosec.io>
  • Loading branch information
rcohencyberarmor committed Aug 16, 2023
1 parent 178fe93 commit 8e28acd
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
14 changes: 13 additions & 1 deletion repositories/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,14 @@ func enrichSummaryManifestObjectLabels(ctx context.Context, labels map[string]st
return enrichedLabels, nil
}

func getCVESummaryK8sResourceName(ctx context.Context) (string, error) {
workload, ok := ctx.Value(domain.WorkloadKey{}).(domain.ScanCommand)
if !ok {
return "", domain.ErrMissingWorkload
}
return workload.ImageSlug, nil
}

func (a *APIServerStore) storeCVESummary(ctx context.Context, cve domain.CVEManifest, withRelevancy bool) error {
_, span := otel.Tracer("").Start(ctx, "APIServerStore.storeCVESummary")
defer span.End()
Expand All @@ -327,10 +335,14 @@ func (a *APIServerStore) storeCVESummary(ctx context.Context, cve domain.CVEMani
if err != nil {
return err
}
summaryK8sResourceName, err := getCVESummaryK8sResourceName(ctx)
if err != nil {
return err
}

manifest := v1beta1.VulnerabilityManifestSummary{
ObjectMeta: metav1.ObjectMeta{
Name: cve.Name,
Name: summaryK8sResourceName,
Annotations: annotations,
Labels: labels,
},
Expand Down
45 changes: 45 additions & 0 deletions repositories/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,48 @@ func TestAPIServerStore_enrichSummaryManifestObjectAnnotations(t *testing.T) {
}

}

func TestAPIServerStore_getCVESummaryK8sResourceName(t *testing.T) {
tests := []struct {
workload domain.ScanCommand
}{
{
workload: domain.ScanCommand{
ImageSlug: "default-replicaset-nginx-77b4fdf86c-6e03-a89e",
},
},
{
workload: domain.ScanCommand{
ImageSlug: "kubescape-replicaset-gateway-66967b649-495e-df93",
},
},
{
workload: domain.ScanCommand{
ImageSlug: "kubescape-replicaset-kubescape-5d4bf4589c-7b8d-5074",
},
},
{
workload: domain.ScanCommand{
ImageSlug: "kubescape-replicaset-kubevuln-988fd7dbd-468f-8953",
},
},
{
workload: domain.ScanCommand{
ImageSlug: "kubescape-replicaset-operator-647f75985c-ff5d-6454",
},
},
{
workload: domain.ScanCommand{
ImageSlug: "kubescape-replicaset-storage-5ff864df8f-fc62-4b1c",
},
},
}

for i := range tests {
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)
}

}

0 comments on commit 8e28acd

Please sign in to comment.