Skip to content

Commit

Permalink
feat: fill ErrorReason and Remediation during verifierReport generation
Browse files Browse the repository at this point in the history
  • Loading branch information
binbin-li committed Aug 2, 2024
1 parent 65eb936 commit ca94540
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 164 deletions.
12 changes: 10 additions & 2 deletions pkg/executor/core/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,17 @@ func (executor Executor) verifyReferenceForJSONPolicy(ctx context.Context, subje
verifierStartTime := time.Now()
verifyResult, err := verifier.Verify(ctx, subjectRef, referenceDesc, referrerStore)
if err != nil {
verifierErr := errors.ErrorCodeVerifyReferenceFailure.NewError(errors.Verifier, verifier.Name(), errors.EmptyLink, err, nil, errors.HideStackTrace)
verifyResult = vr.VerifierResult{
IsSuccess: false,
Name: verifier.Name(), // Deprecating Name in v2, switch to VerifierName instead.
Type: verifier.Type(), // Deprecating Type in v2, switch to VerifierType instead.
VerifierName: verifier.Name(),
VerifierType: verifier.Type(),
Message: errors.ErrorCodeVerifyReferenceFailure.NewError(errors.Verifier, verifier.Name(), errors.EmptyLink, err, nil, errors.HideStackTrace).Error()}
Message: verifierErr.GetFullDetails(),
ErrorReason: verifierErr.GetRootCause(),
Remediation: verifierErr.GetRootRemediation(),
}
}

if len(verifier.GetNestedReferences()) > 0 {
Expand Down Expand Up @@ -228,13 +232,17 @@ func (executor Executor) verifyReferenceForRegoPolicy(ctx context.Context, subje
verifierStartTime := time.Now()
verifierResult, err := verifier.Verify(errCtx, subjectRef, referenceDesc, referrerStore)
if err != nil {
verifierErr := errors.ErrorCodeVerifyReferenceFailure.NewError(errors.Verifier, verifier.Name(), errors.EmptyLink, err, nil, errors.HideStackTrace)
verifierReport = vt.VerifierResult{
IsSuccess: false,
Name: verifier.Name(), // Deprecating Name in v2, switch to VerifierName instead.
Type: verifier.Type(), // Deprecating Type in v2, switch to VerifierType instead.
VerifierName: verifier.Name(),
VerifierType: verifier.Type(),
Message: errors.ErrorCodeVerifyReferenceFailure.NewError(errors.Verifier, verifier.Name(), errors.EmptyLink, err, nil, errors.HideStackTrace).Error()}
Message: verifierErr.GetFullDetails(),
ErrorReason: verifierErr.GetRootCause(),
Remediation: verifierErr.GetRootRemediation(),
}
} else {
verifierReport = vt.NewVerifierResult(verifierResult)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/executor/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type VerifyResult struct {

// NestedVerifierReport describes the results of verifying an artifact and its
// nested artifacts by available verifiers.
// Note: NestedVerifierReport is used for verification results in v1.
type NestedVerifierReport struct {
Subject string `json:"subject"`
ReferenceDigest string `json:"referenceDigest"`
Expand Down
9 changes: 6 additions & 3 deletions pkg/policyprovider/configpolicy/configpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,13 @@ func (enforcer PolicyEnforcer) ContinueVerifyOnFailure(_ context.Context, _ comm

// ErrorToVerifyResult converts an error to a properly formatted verify result
func (enforcer PolicyEnforcer) ErrorToVerifyResult(_ context.Context, subjectRefString string, verifyError error) types.VerifyResult {
verifierErr := re.ErrorCodeVerifyReferenceFailure.WithDetail(fmt.Sprintf("failed to verify artifact: %s", subjectRefString)).WithError(verifyError)
errorReport := verifier.VerifierResult{
Subject: subjectRefString,
IsSuccess: false,
Message: fmt.Sprintf("verification failed: %v", verifyError),
Subject: subjectRefString,
IsSuccess: false,
Message: verifierErr.GetFullDetails(),
ErrorReason: verifierErr.GetRootCause(),
Remediation: verifierErr.GetRootRemediation(),
}
var reports []interface{}
reports = append(reports, errorReport)
Expand Down
6 changes: 4 additions & 2 deletions pkg/verifier/cosign/cosign.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,14 +485,16 @@ func staticLayerOpts(desc imgspec.Descriptor) ([]static.Option, error) {

// ErrorToVerifyResult returns a verifier result with the error message and isSuccess set to false
func errorToVerifyResult(name string, verifierType string, err error) verifier.VerifierResult {
verifierErr := re.ErrorCodeVerifyReferenceFailure.WithDetail("cosign verification failed").WithError(err)
return verifier.VerifierResult{
IsSuccess: false,
Name: name, // Deprecating Name in v2, switch to VerifierName instead.
Type: verifierType, // Deprecating Type in v2, switch to VerifierType instead.
VerifierName: name,
VerifierType: verifierType,
Message: "cosign verification failed",
ErrorReason: err.Error(),
Message: verifierErr.GetFullDetails(),
ErrorReason: verifierErr.GetRootCause(),
Remediation: verifierErr.GetRootRemediation(),
}
}

Expand Down
80 changes: 53 additions & 27 deletions plugins/verifier/sbom/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/ratify-project/ratify/plugins/verifier/sbom/utils"

// This import is required to utilize the oras built-in referrer store
re "github.com/ratify-project/ratify/errors"
_ "github.com/ratify-project/ratify/pkg/referrerstore/oras"
"github.com/ratify-project/ratify/pkg/verifier"
"github.com/ratify-project/ratify/pkg/verifier/plugin/skel"
Expand Down Expand Up @@ -82,19 +83,28 @@ func VerifyReference(args *skel.CmdArgs, subjectReference common.Reference, refe
ctx := context.Background()
referenceManifest, err := referrerStore.GetReferenceManifest(ctx, subjectReference, referenceDescriptor)
if err != nil {
storeErr := re.ErrorCodeGetReferenceManifestFailure.WithDetail(fmt.Sprintf("Error fetching reference manifest for subject: %s reference descriptor: %v", subjectReference, referenceDescriptor.Descriptor)).WithError(err)

Check warning on line 86 in plugins/verifier/sbom/sbom.go

View check run for this annotation

Codecov / codecov/patch

plugins/verifier/sbom/sbom.go#L86

Added line #L86 was not covered by tests
return &verifier.VerifierResult{
Name: input.Name,
Type: verifierType,
IsSuccess: false,
Message: fmt.Sprintf("Error fetching reference manifest for subject: %s reference descriptor: %v, err: %v", subjectReference, referenceDescriptor.Descriptor, err),
Name: input.Name,
Type: verifierType,
VerifierName: input.Name,
VerifierType: verifierType,
IsSuccess: false,
Message: storeErr.GetFullDetails(),
ErrorReason: storeErr.GetFullDetails(),
Remediation: storeErr.GetRootRemediation(),

Check warning on line 95 in plugins/verifier/sbom/sbom.go

View check run for this annotation

Codecov / codecov/patch

plugins/verifier/sbom/sbom.go#L88-L95

Added lines #L88 - L95 were not covered by tests
}, nil
}

if len(referenceManifest.Blobs) == 0 {
return &verifier.VerifierResult{
Name: input.Name,
IsSuccess: false,
Message: fmt.Sprintf("SBOM validation failed: no layers found in manifest for referrer %s@%s", subjectReference.Path, referenceDescriptor.Digest.String()),
Name: input.Name,
Type: verifierType,
VerifierName: input.Name,
VerifierType: verifierType,
IsSuccess: false,
Message: "SBOM validation failed",
ErrorReason: fmt.Sprintf("No layers found in manifest for referrer %s@%s", subjectReference.Path, referenceDescriptor.Digest.String()),

Check warning on line 107 in plugins/verifier/sbom/sbom.go

View check run for this annotation

Codecov / codecov/patch

plugins/verifier/sbom/sbom.go#L101-L107

Added lines #L101 - L107 were not covered by tests
}, nil
}

Expand All @@ -103,11 +113,16 @@ func VerifyReference(args *skel.CmdArgs, subjectReference common.Reference, refe
refBlob, err := referrerStore.GetBlobContent(ctx, subjectReference, blobDesc.Digest)

if err != nil {
storeErr := re.ErrorCodeGetBlobContentFailure.WithDetail(fmt.Sprintf("Error fetching blob for subject: %s digest: %s", subjectReference, blobDesc.Digest)).WithError(err)

Check warning on line 116 in plugins/verifier/sbom/sbom.go

View check run for this annotation

Codecov / codecov/patch

plugins/verifier/sbom/sbom.go#L116

Added line #L116 was not covered by tests
return &verifier.VerifierResult{
Name: input.Name,
Type: verifierType,
IsSuccess: false,
Message: fmt.Sprintf("Error fetching blob for subject: %s digest: %s, err: %v", subjectReference, blobDesc.Digest, err),
Name: input.Name,
Type: verifierType,
VerifierName: input.Name,
VerifierType: verifierType,
IsSuccess: false,
Message: storeErr.GetFullDetails(),
ErrorReason: storeErr.GetRootCause(),
Remediation: storeErr.GetRootRemediation(),

Check warning on line 125 in plugins/verifier/sbom/sbom.go

View check run for this annotation

Codecov / codecov/patch

plugins/verifier/sbom/sbom.go#L118-L125

Added lines #L118 - L125 were not covered by tests
}, nil
}

Expand All @@ -116,19 +131,24 @@ func VerifyReference(args *skel.CmdArgs, subjectReference common.Reference, refe
return processSpdxJSONMediaType(input.Name, verifierType, refBlob, input.DisallowedLicenses, input.DisallowedPackages), nil
default:
return &verifier.VerifierResult{
Name: input.Name,
Type: verifierType,
IsSuccess: false,
Message: fmt.Sprintf("Unsupported artifactType: %s", artifactType),
Name: input.Name,
Type: verifierType,
VerifierName: input.Name,
VerifierType: verifierType,
IsSuccess: false,
Message: "Failed to process SBOM blobs.",
ErrorReason: fmt.Sprintf("Unsupported artifactType: %s", artifactType),

Check warning on line 140 in plugins/verifier/sbom/sbom.go

View check run for this annotation

Codecov / codecov/patch

plugins/verifier/sbom/sbom.go#L134-L140

Added lines #L134 - L140 were not covered by tests
}, nil
}
}

return &verifier.VerifierResult{
Name: input.Name,
Type: verifierType,
IsSuccess: true,
Message: "SBOM verification success. No license or package violation found.",
Name: input.Name,
Type: verifierType,
VerifierName: input.Name,
VerifierType: verifierType,
IsSuccess: true,
Message: "SBOM verification success. No license or package violation found.",

Check warning on line 151 in plugins/verifier/sbom/sbom.go

View check run for this annotation

Codecov / codecov/patch

plugins/verifier/sbom/sbom.go#L146-L151

Added lines #L146 - L151 were not covered by tests
}, nil
}

Expand Down Expand Up @@ -178,10 +198,12 @@ func processSpdxJSONMediaType(name string, verifierType string, refBlob []byte,

if len(licenseViolation) != 0 || len(packageViolation) != 0 {
return &verifier.VerifierResult{
Name: name,
IsSuccess: false,
Extensions: extensionData,
Message: "SBOM validation failed. Please review extensions data for license and package violation found.",
Name: name,
IsSuccess: false,
Extensions: extensionData,
Message: "SBOM validation failed.",
ErrorReason: "License or package violation found.",
Remediation: "Please review extensions data for license and package violation found.",
}
}
}
Expand All @@ -196,11 +218,15 @@ func processSpdxJSONMediaType(name string, verifierType string, refBlob []byte,
Message: "SBOM verification success. No license or package violation found.",
}
}
verifierErr := re.ErrorCodeVerifyPluginFailure.WithDetail(fmt.Sprintf("failed to verify artifact: %s", name)).WithError(err)
return &verifier.VerifierResult{
Name: name,
Type: verifierType,
IsSuccess: false,
Message: fmt.Sprintf("SBOM failed to parse: %v", err),
Name: name,
Type: verifierType,
VerifierName: name,
VerifierType: verifierType,
IsSuccess: false,
Message: verifierErr.GetFullDetails(),
ErrorReason: verifierErr.GetRootCause(),
}
}

Expand Down
7 changes: 5 additions & 2 deletions plugins/verifier/sbom/sbom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ func TestProcessInvalidSPDXJsonMediaType(t *testing.T) {
}
report := processSpdxJSONMediaType("test", "", b, nil, nil)

if !strings.Contains(report.Message, "SBOM failed to parse") {
t.Fatalf("expected to have an error processing spdx json file: %s", filepath.Join("testdata", "bom.json"))
if !strings.Contains(report.Message, "failed to verify artifact") {
t.Fatalf("report message: %s does not contain expected error message", report.Message)
}
if report.ErrorReason != "JSON document does not contain spdxVersion field" {
t.Fatalf("expected error reason: %s, got: %s", "JSON document does not contain spdxVersion field", report.ErrorReason)
}
}

Expand Down
34 changes: 22 additions & 12 deletions plugins/verifier/schemavalidator/schema_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"fmt"

re "github.com/ratify-project/ratify/errors"
"github.com/ratify-project/ratify/pkg/common"
"github.com/ratify-project/ratify/pkg/ocispecs"
"github.com/ratify-project/ratify/pkg/referrerstore"
Expand Down Expand Up @@ -72,10 +73,12 @@ func VerifyReference(args *skel.CmdArgs, subjectReference common.Reference, refe

if len(referenceManifest.Blobs) == 0 {
return &verifier.VerifierResult{
Name: input.Name,
Type: verifierType,
IsSuccess: false,
Message: fmt.Sprintf("schema validation failed: no blobs found for referrer %s@%s", subjectReference.Path, referenceDescriptor.Digest.String()),
Name: input.Name,
Type: verifierType,
VerifierName: input.Name,
VerifierType: verifierType,
IsSuccess: false,
Message: fmt.Sprintf("No blobs found for referrer %s@%s.", subjectReference.Path, referenceDescriptor.Digest.String()),

Check warning on line 81 in plugins/verifier/schemavalidator/schema_validator.go

View check run for this annotation

Codecov / codecov/patch

plugins/verifier/schemavalidator/schema_validator.go#L76-L81

Added lines #L76 - L81 were not covered by tests
}, nil
}

Expand All @@ -87,20 +90,27 @@ func VerifyReference(args *skel.CmdArgs, subjectReference common.Reference, refe

err = processMediaType(schemaMap, blobDesc.MediaType, refBlob)
if err != nil {
verifierErr := re.ErrorCodeVerifyPluginFailure.WithDetail(fmt.Sprintf("schema validation failed for digest:[%s], media type:[%s].", blobDesc.Digest, blobDesc.MediaType)).WithError(err)

Check warning on line 93 in plugins/verifier/schemavalidator/schema_validator.go

View check run for this annotation

Codecov / codecov/patch

plugins/verifier/schemavalidator/schema_validator.go#L93

Added line #L93 was not covered by tests
return &verifier.VerifierResult{
Name: input.Name,
Type: verifierType,
IsSuccess: false,
Message: fmt.Sprintf("schema validation failed for digest:[%s],media type:[%s],parse errors:[%v]", blobDesc.Digest, blobDesc.MediaType, err.Error()),
Name: input.Name,
Type: verifierType,
VerifierName: input.Name,
VerifierType: verifierType,
IsSuccess: false,
Message: verifierErr.GetFullDetails(),
ErrorReason: verifierErr.GetRootCause(),
Remediation: verifierErr.GetRootRemediation(),

Check warning on line 102 in plugins/verifier/schemavalidator/schema_validator.go

View check run for this annotation

Codecov / codecov/patch

plugins/verifier/schemavalidator/schema_validator.go#L95-L102

Added lines #L95 - L102 were not covered by tests
}, nil
}
}

return &verifier.VerifierResult{
Name: input.Name,
Type: verifierType,
IsSuccess: true,
Message: "schema validation passed for configured media types",
Name: input.Name,
Type: verifierType,
VerifierName: input.Name,
VerifierType: verifierType,
IsSuccess: true,
Message: "schema validation passed for configured media types",
}, nil
}

Expand Down
Loading

0 comments on commit ca94540

Please sign in to comment.