Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve error message for plugin #406

Merged
merged 7 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,20 +173,21 @@ func run(ctx context.Context, pluginName string, pluginPath string, req plugin.R
if len(stderr) == 0 {
// if stderr is empty, it is possible that the plugin is not
// running properly.
logger.Errorf("failed to execute the %s command for plugin %s: %s", req.Command(), pluginName, err)
return &PluginExecutableFileError{
Msg: fmt.Sprintf("failed to execute the %s command for plugin %s", req.Command(), pluginName),
InnerError: err,
}
} else {
var re proto.RequestError
jsonErr := json.Unmarshal(stderr, &re)
if jsonErr != nil {
logger.Errorf("failed to execute the %s command for plugin %s: %s", req.Command(), pluginName, strings.TrimSuffix(string(stderr), "\n"))
Two-Hearts marked this conversation as resolved.
Show resolved Hide resolved
return &PluginMalformedError{
Msg: fmt.Sprintf("failed to execute the %s command for plugin %s: %s", req.Command(), pluginName, strings.TrimSuffix(string(stderr), "\n")),
InnerError: jsonErr,
}
}
return fmt.Errorf("failed to execute the %s command for plugin %s: %w", req.Command(), pluginName, re)
logger.Errorf("failed to execute the %s command for plugin %s: %s: %w", req.Command(), pluginName, re.Code, re)
return re
}
}

Expand Down
4 changes: 2 additions & 2 deletions plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestGetMetadata(t *testing.T) {
t.Run("plugin error is in invalid json format", func(t *testing.T) {
exitErr := errors.New("unknown error")
stderr := []byte("sad")
expectedErrMsg := "failed to execute the get-plugin-metadata command for plugin test-plugin: sad"
expectedErrMsg := "invalid character 's' looking for beginning of value"
plugin := CLIPlugin{name: "test-plugin"}
executor = testCommander{stdout: nil, stderr: stderr, err: exitErr}
_, err := plugin.GetMetadata(context.Background(), &proto.GetMetadataRequest{})
Expand All @@ -55,7 +55,7 @@ func TestGetMetadata(t *testing.T) {
t.Run("plugin cause system error", func(t *testing.T) {
exitErr := errors.New("system error")
stderr := []byte("")
expectedErrMsg := "failed to execute the get-plugin-metadata command for plugin test-plugin"
expectedErrMsg := "system error"
plugin := CLIPlugin{name: "test-plugin"}
executor = testCommander{stdout: nil, stderr: stderr, err: exitErr}
_, err := plugin.GetMetadata(context.Background(), &proto.GetMetadataRequest{})
Expand Down
2 changes: 1 addition & 1 deletion plugin/proto/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type RequestError struct {
}

func (e RequestError) Error() string {
return fmt.Sprintf("%s: %v", e.Code, e.Err)
return fmt.Sprintf("%v", e.Err)
}

func (e RequestError) Unwrap() error {
Expand Down
2 changes: 1 addition & 1 deletion plugin/proto/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

func TestRequestError_Error(t *testing.T) {
err := RequestError{Code: ErrorCodeAccessDenied, Err: errors.New("an error")}
want := string(ErrorCodeAccessDenied) + ": an error"
want := "an error"
if got := err.Error(); got != want {
t.Errorf("RequestError.Error() = %v, want %v", got, want)
}
Expand Down
16 changes: 13 additions & 3 deletions signer/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,22 @@ func (s *PluginSigner) Sign(ctx context.Context, desc ocispec.Descriptor, opts n
if metadata.HasCapability(plugin.CapabilitySignatureGenerator) {
ks, err := s.getKeySpec(ctx, mergedConfig)
if err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("failed to sign with the plugin %s: %w", metadata.Name, err)
}
return s.generateSignature(ctx, desc, opts, ks, metadata, mergedConfig)

sig, signerInfo, err := s.generateSignature(ctx, desc, opts, ks, metadata, mergedConfig)
if err != nil {
return nil, nil, fmt.Errorf("failed to sign with the plugin %s: %w", metadata.Name, err)
}
return sig, signerInfo, nil
} else if metadata.HasCapability(plugin.CapabilityEnvelopeGenerator) {
return s.generateSignatureEnvelope(ctx, desc, opts)
sig, signerInfo, err := s.generateSignatureEnvelope(ctx, desc, opts)
if err != nil {
return nil, nil, fmt.Errorf("failed to sign with the plugin %s: %w", metadata.Name, err)
}
return sig, signerInfo, nil
}

return nil, nil, fmt.Errorf("plugin does not have signing capabilities")
}

Expand Down
2 changes: 1 addition & 1 deletion verifier/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ func (v *verifier) processSignature(ctx context.Context, sigBlob []byte, envelop
logger.Debugf("Executing verification plugin %q with capabilities %v", verificationPluginName, capabilitiesToVerify)
response, err := executePlugin(ctx, installedPlugin, capabilitiesToVerify, outcome.EnvelopeContent, trustedIdentities, pluginConfig)
if err != nil {
return err
return fmt.Errorf("failed to verify with plugin %s: %w", verificationPluginName, err)
}

return processPluginResponse(capabilitiesToVerify, response, outcome)
Expand Down
2 changes: 1 addition & 1 deletion verifier/verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ func assertPluginVerification(scheme signature.SigningScheme, t *testing.T) {
VerificationLevel: verificationLevel,
}
outcome, err = v.Verify(context.Background(), mock.ImageDescriptor, pluginSigEnv, opts)
if err == nil || outcome.Error == nil || outcome.Error.Error() != "invalid plugin response" {
if err == nil || outcome.Error == nil || outcome.Error.Error() != "failed to verify with plugin plugin-name: invalid plugin response" {
t.Fatalf("verification should fail when the verification plugin returns unexpected response. error : %v", outcome.Error)
}

Expand Down
Loading