forked from ratify-project/ratify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fill ErrorReason and Remediation during verifierReport generati…
…on (ratify-project#1682)
- Loading branch information
Showing
17 changed files
with
954 additions
and
341 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
Copyright The Ratify Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package verifier | ||
|
||
import "github.com/ratify-project/ratify/errors" | ||
|
||
// VerifierResult describes the result of verifying a reference manifest for a subject. | ||
// Note: This struct is used to represent the result of verification in v0. | ||
type VerifierResult struct { //nolint:revive // ignore linter to have unique type name | ||
Subject string `json:"subject,omitempty"` | ||
IsSuccess bool `json:"isSuccess"` | ||
// Name will be deprecated in v2, tracking issue: https://github.com/ratify-project/ratify/issues/1707 | ||
Name string `json:"name,omitempty"` | ||
VerifierName string `json:"verifierName,omitempty"` | ||
// Type will be deprecated in v2, tracking issue: https://github.com/ratify-project/ratify/issues/1707 | ||
Type string `json:"type,omitempty"` | ||
VerifierType string `json:"verifierType,omitempty"` | ||
ReferenceDigest string `json:"referenceDigest,omitempty"` | ||
ArtifactType string `json:"artifactType,omitempty"` | ||
Message string `json:"message,omitempty"` | ||
ErrorReason string `json:"errorReason,omitempty"` | ||
Remediation string `json:"remediation,omitempty"` | ||
Extensions interface{} `json:"extensions,omitempty"` | ||
NestedResults []VerifierResult `json:"nestedResults,omitempty"` | ||
} | ||
|
||
// NewVerifierResult creates a new VerifierResult object with the given parameters. | ||
func NewVerifierResult(subject, verifierName, verifierType, message string, isSuccess bool, err *errors.Error, extensions interface{}) VerifierResult { | ||
var errorReason, remediation string | ||
if err != nil { | ||
if err.GetDetail() != "" { | ||
message = err.GetDetail() | ||
} | ||
errorReason = err.GetErrorReason() | ||
remediation = err.GetRemediation() | ||
} | ||
return VerifierResult{ | ||
Subject: subject, | ||
IsSuccess: isSuccess, | ||
Name: verifierName, | ||
Type: verifierType, | ||
VerifierName: verifierName, | ||
VerifierType: verifierType, | ||
Message: message, | ||
ErrorReason: errorReason, | ||
Remediation: remediation, | ||
Extensions: extensions, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
Copyright The Ratify Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package verifier | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/ratify-project/ratify/errors" | ||
) | ||
|
||
const ( | ||
testMsg1 = "test message 1" | ||
testMsg2 = "test message 2" | ||
testErrReason = "test error reason" | ||
testRemediation = "test remediation" | ||
) | ||
|
||
func TestNewVerifierResult(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
message string | ||
err errors.Error | ||
expectedMsg string | ||
expectedErrReason string | ||
expectedRemediation string | ||
}{ | ||
{ | ||
name: "nil error", | ||
message: testMsg1, | ||
err: errors.Error{}, | ||
expectedMsg: testMsg1, | ||
}, | ||
{ | ||
name: "error without detail", | ||
message: testMsg1, | ||
err: errors.ErrorCodeUnknown.WithError(fmt.Errorf(testErrReason)).WithRemediation(testRemediation), | ||
expectedMsg: testMsg1, | ||
expectedErrReason: testErrReason, | ||
expectedRemediation: testRemediation, | ||
}, | ||
{ | ||
name: "error with detail", | ||
message: testMsg1, | ||
err: errors.ErrorCodeUnknown.WithError(fmt.Errorf(testErrReason)).WithRemediation(testRemediation).WithDetail(testMsg2), | ||
expectedMsg: testMsg2, | ||
expectedErrReason: testErrReason, | ||
expectedRemediation: testRemediation, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := &tt.err | ||
if tt.err == (errors.Error{}) { | ||
err = nil | ||
} | ||
|
||
result := NewVerifierResult("", "", "", tt.message, false, err, nil) | ||
if result.Message != tt.expectedMsg { | ||
t.Errorf("expected message %s, got %s", tt.expectedMsg, result.Message) | ||
} | ||
if result.ErrorReason != tt.expectedErrReason { | ||
t.Errorf("expected error reason %s, got %s", tt.expectedErrReason, result.ErrorReason) | ||
} | ||
if result.Remediation != tt.expectedRemediation { | ||
t.Errorf("expected remediation %s, got %s", tt.expectedRemediation, result.Remediation) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.