From 27fe9c19a7884a92c5d87f11f44427474dda1b3d Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Fri, 17 Jun 2022 00:04:23 +0300 Subject: [PATCH] [#273] status: Support signature status Signed-off-by: Pavel Karpy --- client/status/common.go | 51 ++++++++++++++++++++++++++++++++++++ client/status/common_test.go | 39 +++++++++++++++++++++++++++ client/status/v2.go | 5 +++- 3 files changed, 94 insertions(+), 1 deletion(-) diff --git a/client/status/common.go b/client/status/common.go index 05c5ae28..8fec31eb 100644 --- a/client/status/common.go +++ b/client/status/common.go @@ -123,3 +123,54 @@ func (x WrongMagicNumber) CorrectMagic() (magic uint64, ok int8) { return } + +// SignatureVerification describes failure status related to signature verification. +// Instances provide Status and StatusV2 interfaces. +type SignatureVerification struct { + v2 status.Status +} + +func (x SignatureVerification) Error() string { + return errMessageStatusV2( + globalizeCodeV2(status.SignatureVerificationFail, status.GlobalizeCommonFail), + x.v2.Message(), + ) +} + +// implements local interface defined in FromStatusV2 func. +func (x *SignatureVerification) fromStatusV2(st *status.Status) { + x.v2 = *st +} + +// ToStatusV2 implements StatusV2 interface method. +// If the value was returned by FromStatusV2, returns the source message. +// Otherwise, returns message with +// * code: SIGNATURE_VERIFICATION_FAIL; +// * string message: written message via SetMessage or +// "signature verification failed" as a default message; +// * details: empty. +func (x SignatureVerification) ToStatusV2() *status.Status { + x.v2.SetCode(globalizeCodeV2(status.SignatureVerificationFail, status.GlobalizeCommonFail)) + + if x.v2.Message() == "" { + x.v2.SetMessage("signature verification failed") + } + + return &x.v2 +} + +// SetMessage writes signature verification failure message. +// Message should be used for debug purposes only. +// +// See also Message. +func (x *SignatureVerification) SetMessage(v string) { + x.v2.SetMessage(v) +} + +// Message returns status message. Zero status returns empty message. +// Message should be used for debug purposes only. +// +// See also SetMessage. +func (x SignatureVerification) Message() string { + return x.v2.Message() +} diff --git a/client/status/common_test.go b/client/status/common_test.go index 7328cddb..204bd1aa 100644 --- a/client/status/common_test.go +++ b/client/status/common_test.go @@ -50,3 +50,42 @@ func TestWrongMagicNumber_CorrectMagic(t *testing.T) { _, ok = st.CorrectMagic() require.EqualValues(t, -1, ok) } + +func TestSignatureVerification(t *testing.T) { + t.Run("default", func(t *testing.T) { + var st apistatus.SignatureVerification + + require.Empty(t, st.Message()) + }) + + t.Run("custom message", func(t *testing.T) { + var st apistatus.SignatureVerification + msg := "some message" + + st.SetMessage(msg) + + stV2 := st.ToStatusV2() + + require.Equal(t, msg, st.Message()) + require.Equal(t, msg, stV2.Message()) + }) + + t.Run("empty to V2", func(t *testing.T) { + var st apistatus.SignatureVerification + + stV2 := st.ToStatusV2() + + require.Equal(t, "signature verification failed", stV2.Message()) + }) + + t.Run("non-empty to V2", func(t *testing.T) { + var st apistatus.SignatureVerification + msg := "some other msg" + + st.SetMessage(msg) + + stV2 := st.ToStatusV2() + + require.Equal(t, msg, stV2.Message()) + }) +} diff --git a/client/status/v2.go b/client/status/v2.go index 5c2e2be9..627014cb 100644 --- a/client/status/v2.go +++ b/client/status/v2.go @@ -31,7 +31,8 @@ type StatusV2 interface { // * status.OK: *SuccessDefaultV2 (this also includes nil argument). // // Common failures: -// * status.Internal: *ServerInternal. +// * status.Internal: *ServerInternal; +// * status.SignatureVerificationFail: *SignatureVerification. // // Object failures: // * object.StatusLocked: *ObjectLocked; @@ -55,6 +56,8 @@ func FromStatusV2(st *status.Status) Status { decoder = new(ServerInternal) case status.WrongMagicNumber: decoder = new(WrongMagicNumber) + case status.SignatureVerificationFail: + decoder = new(SignatureVerification) } case object.LocalizeFailStatus(&code): switch code {