forked from TrueCloudLab/frostfs-sdk-go
client: Replace panics with errors in sign logic
Signed-off-by: Evgenii Baidakov <evgenii@nspcc.io>
This commit is contained in:
parent
ef887b3ab1
commit
5dec2b49b0
4 changed files with 52 additions and 7 deletions
|
@ -23,6 +23,8 @@ var (
|
||||||
|
|
||||||
ErrUnexpectedReadCall = errors.New("unexpected call to `Read`")
|
ErrUnexpectedReadCall = errors.New("unexpected call to `Read`")
|
||||||
|
|
||||||
|
ErrSign SignError
|
||||||
|
|
||||||
errMissingResponseField missingResponseFieldErr
|
errMissingResponseField missingResponseFieldErr
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -53,3 +55,28 @@ func newErrMissingResponseField(name string) error {
|
||||||
func newErrInvalidResponseField(name string, err error) error {
|
func newErrInvalidResponseField(name string, err error) error {
|
||||||
return fmt.Errorf("invalid %s field in the response: %w", name, err)
|
return fmt.Errorf("invalid %s field in the response: %w", name, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SignError struct {
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSignError(err error) SignError {
|
||||||
|
return SignError{err: err}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e SignError) Error() string {
|
||||||
|
return fmt.Sprintf("sign: %v", e.err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e SignError) Unwrap() error {
|
||||||
|
return e.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e SignError) Is(target error) bool {
|
||||||
|
switch target.(type) {
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
case SignError, *SignError:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
17
client/errors_test.go
Normal file
17
client/errors_test.go
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
package client_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/nspcc-dev/neofs-sdk-go/client"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_SignError(t *testing.T) {
|
||||||
|
someErr := errors.New("some error")
|
||||||
|
signErr := client.NewSignError(someErr)
|
||||||
|
|
||||||
|
require.ErrorIs(t, signErr, someErr)
|
||||||
|
require.ErrorIs(t, signErr, client.ErrSign)
|
||||||
|
}
|
|
@ -2,7 +2,6 @@ package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -152,7 +151,7 @@ func (s *singleStreamResponder) Read(resp *v2object.SearchResponse) error {
|
||||||
|
|
||||||
err := signServiceMessage(s.signer, resp)
|
err := signServiceMessage(s.signer, resp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Errorf("error: %w", err))
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
s.n++
|
s.n++
|
||||||
|
|
|
@ -132,6 +132,8 @@ func (s stableMarshalerWrapper) SignedDataSize() int {
|
||||||
}
|
}
|
||||||
|
|
||||||
// signServiceMessage signing request or response messages which can be sent or received from neofs endpoint.
|
// signServiceMessage signing request or response messages which can be sent or received from neofs endpoint.
|
||||||
|
// Return errors:
|
||||||
|
// - [ErrSign]
|
||||||
func signServiceMessage(signer neofscrypto.Signer, msg interface{}) error {
|
func signServiceMessage(signer neofscrypto.Signer, msg interface{}) error {
|
||||||
var (
|
var (
|
||||||
body, meta, verifyOrigin stableMarshaler
|
body, meta, verifyOrigin stableMarshaler
|
||||||
|
@ -165,24 +167,24 @@ func signServiceMessage(signer neofscrypto.Signer, msg interface{}) error {
|
||||||
verifyOrigin = h
|
verifyOrigin = h
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("unsupported session message %T", v))
|
return NewSignError(fmt.Errorf("unsupported session message %T", v))
|
||||||
}
|
}
|
||||||
|
|
||||||
if verifyOrigin == nil {
|
if verifyOrigin == nil {
|
||||||
// sign session message body
|
// sign session message body
|
||||||
if err := signServiceMessagePart(signer, body, verifyHdr.SetBodySignature); err != nil {
|
if err := signServiceMessagePart(signer, body, verifyHdr.SetBodySignature); err != nil {
|
||||||
return fmt.Errorf("could not sign body: %w", err)
|
return NewSignError(fmt.Errorf("body: %w", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// sign meta header
|
// sign meta header
|
||||||
if err := signServiceMessagePart(signer, meta, verifyHdr.SetMetaSignature); err != nil {
|
if err := signServiceMessagePart(signer, meta, verifyHdr.SetMetaSignature); err != nil {
|
||||||
return fmt.Errorf("could not sign meta header: %w", err)
|
return NewSignError(fmt.Errorf("meta header: %w", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
// sign verification header origin
|
// sign verification header origin
|
||||||
if err := signServiceMessagePart(signer, verifyOrigin, verifyHdr.SetOriginSignature); err != nil {
|
if err := signServiceMessagePart(signer, verifyOrigin, verifyHdr.SetOriginSignature); err != nil {
|
||||||
return fmt.Errorf("could not sign origin of verification header: %w", err)
|
return NewSignError(fmt.Errorf("origin of verification header: %w", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
// wrap origin verification header
|
// wrap origin verification header
|
||||||
|
@ -240,7 +242,7 @@ func verifyServiceMessage(msg interface{}) error {
|
||||||
ResponseVerificationHeader: v.GetVerificationHeader(),
|
ResponseVerificationHeader: v.GetVerificationHeader(),
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("unsupported session message %T", v))
|
return fmt.Errorf("unsupported session message %T", v)
|
||||||
}
|
}
|
||||||
|
|
||||||
body := serviceMessageBody(msg)
|
body := serviceMessageBody(msg)
|
||||||
|
|
Loading…
Reference in a new issue