diff --git a/adapters/v1/armo.go b/adapters/v1/armo.go index 20db9fe..65e6441 100644 --- a/adapters/v1/armo.go +++ b/adapters/v1/armo.go @@ -70,7 +70,7 @@ func (a *ArmoAdapter) GetCVEExceptions(ctx context.Context) (domain.CVEException // retrieve workload from context workload, ok := ctx.Value(domain.WorkloadKey{}).(domain.ScanCommand) if !ok { - return nil, domain.ErrMissingWorkload + return nil, domain.ErrCastingWorkload } designator := armotypes.PortalDesignator{ @@ -99,7 +99,7 @@ func (a *ArmoAdapter) SendStatus(ctx context.Context, step int) error { // retrieve workload from context workload, ok := ctx.Value(domain.WorkloadKey{}).(domain.ScanCommand) if !ok { - return domain.ErrMissingWorkload + return domain.ErrCastingWorkload } lastAction := workload.LastAction + 1 @@ -142,7 +142,7 @@ func (a *ArmoAdapter) SubmitCVE(ctx context.Context, cve domain.CVEManifest, cve // retrieve workload from context workload, ok := ctx.Value(domain.WorkloadKey{}).(domain.ScanCommand) if !ok { - return domain.ErrMissingWorkload + return domain.ErrCastingWorkload } // validate one more time the scanID before sending it to the platform diff --git a/adapters/v1/domain_to_armo.go b/adapters/v1/domain_to_armo.go index 75fd89c..a4c881f 100644 --- a/adapters/v1/domain_to_armo.go +++ b/adapters/v1/domain_to_armo.go @@ -31,7 +31,7 @@ func domainToArmo(ctx context.Context, grypeDocument v1beta1.GrypeDocument, vuln // retrieve workload from context workload, ok := ctx.Value(domain.WorkloadKey{}).(domain.ScanCommand) if !ok { - return vulnerabilityResults, domain.ErrMissingWorkload + return vulnerabilityResults, domain.ErrCastingWorkload } if grypeDocument.Source != nil { diff --git a/core/domain/scan.go b/core/domain/scan.go index 38e2a4d..c9d2b5a 100644 --- a/core/domain/scan.go +++ b/core/domain/scan.go @@ -20,7 +20,7 @@ var ( ErrMissingImageInfo = errors.New("missing image information") ErrMissingScanID = errors.New("missing scanID") ErrMissingTimestamp = errors.New("missing timestamp") - ErrMissingWorkload = errors.New("missing workload") + ErrCastingWorkload = errors.New("casting workload") ErrMockError = errors.New("mock error") ErrTooManyRequests = errors.New("too many requests") ) diff --git a/core/services/scan.go b/core/services/scan.go index df1cc1e..f59912d 100644 --- a/core/services/scan.go +++ b/core/services/scan.go @@ -74,7 +74,7 @@ func (s *ScanService) GenerateSBOM(ctx context.Context) error { // retrieve workload from context workload, ok := ctx.Value(domain.WorkloadKey{}).(domain.ScanCommand) if !ok { - return domain.ErrMissingWorkload + return domain.ErrCastingWorkload } // check if SBOM is already available @@ -124,7 +124,7 @@ func (s *ScanService) ScanCVE(ctx context.Context) error { // retrieve workload from context workload, ok := ctx.Value(domain.WorkloadKey{}).(domain.ScanCommand) if !ok { - return domain.ErrMissingWorkload + return domain.ErrCastingWorkload } logger.L().Info("scan started", helpers.String("imageSlug", workload.ImageSlug), @@ -260,7 +260,7 @@ func (s *ScanService) ScanRegistry(ctx context.Context) error { // retrieve workload from context workload, ok := ctx.Value(domain.WorkloadKey{}).(domain.ScanCommand) if !ok { - return domain.ErrMissingWorkload + return domain.ErrCastingWorkload } logger.L().Info("registry scan started", helpers.String("imageSlug", workload.ImageSlug), diff --git a/repositories/apiserver.go b/repositories/apiserver.go index 0703001..b5ef885 100644 --- a/repositories/apiserver.go +++ b/repositories/apiserver.go @@ -269,7 +269,7 @@ func enrichSummaryManifestObjectAnnotations(ctx context.Context, annotations map workload, ok := ctx.Value(domain.WorkloadKey{}).(domain.ScanCommand) if !ok { - return nil, domain.ErrMissingWorkload + return nil, domain.ErrCastingWorkload } enrichedAnnotations[v1.WlidMetadataKey] = workload.Wlid enrichedAnnotations[v1.ContainerNameMetadataKey] = workload.ContainerName @@ -290,7 +290,7 @@ func enrichSummaryManifestObjectLabels(ctx context.Context, labels map[string]st workload, ok := ctx.Value(domain.WorkloadKey{}).(domain.ScanCommand) if !ok { - return nil, domain.ErrMissingWorkload + return nil, domain.ErrCastingWorkload } workloadKind := wlid.GetKindFromWlid(workload.Wlid) @@ -312,7 +312,7 @@ func enrichSummaryManifestObjectLabels(ctx context.Context, labels map[string]st func getCVESummaryK8sResourceName(ctx context.Context) (string, error) { workload, ok := ctx.Value(domain.WorkloadKey{}).(domain.ScanCommand) if !ok { - return "", domain.ErrMissingWorkload + return "", domain.ErrCastingWorkload } return workload.ImageSlug, nil } diff --git a/repositories/apiserver_test.go b/repositories/apiserver_test.go index 994bcfc..c8f5d34 100644 --- a/repositories/apiserver_test.go +++ b/repositories/apiserver_test.go @@ -625,6 +625,15 @@ func TestAPIServerStore_getCVESummaryK8sResourceName(t *testing.T) { }, } + testsErrorCases := []struct { + notWorkload any + err error + }{ + { + err: domain.ErrCastingWorkload, + }, + } + for i := range tests { ctx := context.WithValue(context.Background(), domain.WorkloadKey{}, tests[i].workload) name, err := getCVESummaryK8sResourceName(ctx) @@ -632,4 +641,11 @@ func TestAPIServerStore_getCVESummaryK8sResourceName(t *testing.T) { assert.Equal(t, tests[i].workload.ImageSlug, name) } + for i := range testsErrorCases { + ctx := context.WithValue(context.Background(), domain.WorkloadKey{}, testsErrorCases[i].notWorkload) + name, err := getCVESummaryK8sResourceName(ctx) + assert.NotEqual(t, err, nil) + assert.Equal(t, err, testsErrorCases[i].err) + assert.Equal(t, name, "") + } }