Skip to content

Commit

Permalink
change error
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 8e28acd commit 307955d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
6 changes: 3 additions & 3 deletions adapters/v1/armo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion adapters/v1/domain_to_armo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion core/domain/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand Down
6 changes: 3 additions & 3 deletions core/services/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions repositories/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
}
Expand Down
16 changes: 16 additions & 0 deletions repositories/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -625,11 +625,27 @@ 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)
assert.Equal(t, err, nil)
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, "")
}
}

0 comments on commit 307955d

Please sign in to comment.