[#168] acl: Implement binary/JSON encoders/decoders on Target

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-13 14:40:57 +03:00 committed by Alex Vanin
parent 7f42156201
commit 9ddc4c1f48
6 changed files with 127 additions and 4 deletions

View file

@ -14,10 +14,18 @@ type Target struct {
keys []ecdsa.PublicKey
}
func (t *Target) SetKeys(keys ...ecdsa.PublicKey) {
t.keys = keys
}
func (t Target) Keys() []ecdsa.PublicKey {
return t.keys
}
func (t *Target) SetRole(r Role) {
t.role = r
}
func (t Target) Role() Role {
return t.role
}
@ -37,6 +45,10 @@ func (t *Target) ToV2() *v2acl.Target {
return target
}
func NewTarget() *Target {
return NewTargetFromV2(new(v2acl.Target))
}
func NewTargetFromV2(target *v2acl.Target) *Target {
t := new(Target)
@ -54,3 +66,47 @@ func NewTargetFromV2(target *v2acl.Target) *Target {
return t
}
// Marshal marshals Target into a protobuf binary form.
//
// Buffer is allocated when the argument is empty.
// Otherwise, the first buffer is used.
func (t *Target) Marshal(b ...[]byte) ([]byte, error) {
var buf []byte
if len(b) > 0 {
buf = b[0]
}
return t.ToV2().
StableMarshal(buf)
}
// Unmarshal unmarshals protobuf binary representation of Target.
func (t *Target) Unmarshal(data []byte) error {
fV2 := new(v2acl.Target)
if err := fV2.Unmarshal(data); err != nil {
return err
}
*t = *NewTargetFromV2(fV2)
return nil
}
// MarshalJSON encodes Target to protobuf JSON format.
func (t *Target) MarshalJSON() ([]byte, error) {
return t.ToV2().
MarshalJSON()
}
// UnmarshalJSON decodes Target from protobuf JSON format.
func (t *Target) UnmarshalJSON(data []byte) error {
tV2 := new(v2acl.Target)
if err := tV2.UnmarshalJSON(data); err != nil {
return err
}
*t = *NewTargetFromV2(tV2)
return nil
}

View file

@ -36,3 +36,29 @@ func TestTarget(t *testing.T) {
require.Equal(t, new(Target), NewTargetFromV2(nil))
})
}
func TestTargetEncoding(t *testing.T) {
tar := NewTarget()
tar.SetRole(RoleSystem)
tar.SetKeys(test.DecodeKey(-1).PublicKey)
t.Run("binary", func(t *testing.T) {
data, err := tar.Marshal()
require.NoError(t, err)
tar2 := NewTarget()
require.NoError(t, tar2.Unmarshal(data))
require.Equal(t, tar, tar2)
})
t.Run("json", func(t *testing.T) {
data, err := tar.MarshalJSON()
require.NoError(t, err)
tar2 := NewTarget()
require.NoError(t, tar2.UnmarshalJSON(data))
require.Equal(t, tar, tar2)
})
}

View file

@ -102,3 +102,23 @@ func (f *HeaderFilter) UnmarshalJSON(data []byte) error {
return nil
}
func (t *Target) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
TargetToGRPCMessage(t),
)
}
func (t *Target) UnmarshalJSON(data []byte) error {
msg := new(acl.EACLRecord_Target)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*t = *TargetInfoFromGRPCMessage(msg)
return nil
}

View file

@ -76,3 +76,15 @@ func TestFilterJSON(t *testing.T) {
require.Equal(t, f, f2)
}
func TestTargetJSON(t *testing.T) {
tar := generateTarget(acl.RoleSystem, 3)
data, err := tar.MarshalJSON()
require.NoError(t, err)
tar2 := new(acl.Target)
require.NoError(t, tar2.UnmarshalJSON(data))
require.Equal(t, tar, tar2)
}

View file

@ -278,6 +278,17 @@ func (t *Target) StableSize() (size int) {
return size
}
func (t *Target) Unmarshal(data []byte) error {
m := new(acl.EACLRecord_Target)
if err := proto.Unmarshal(data, m); err != nil {
return err
}
*t = *TargetInfoFromGRPCMessage(m)
return nil
}
func (l *TokenLifetime) StableMarshal(buf []byte) ([]byte, error) {
if l == nil {
return []byte{}, nil

View file

@ -136,7 +136,6 @@ func TestHeaderFilter_StableMarshal(t *testing.T) {
func TestTargetInfo_StableMarshal(t *testing.T) {
targetFrom := generateTarget(acl.RoleUser, 2)
transport := new(grpc.EACLRecord_Target)
t.Run("non empty", func(t *testing.T) {
targetFrom.SetRole(acl.RoleUser)
@ -148,10 +147,9 @@ func TestTargetInfo_StableMarshal(t *testing.T) {
wire, err := targetFrom.StableMarshal(nil)
require.NoError(t, err)
err = goproto.Unmarshal(wire, transport)
require.NoError(t, err)
targetTo := new(acl.Target)
require.NoError(t, targetTo.Unmarshal(wire))
targetTo := acl.TargetInfoFromGRPCMessage(transport)
require.Equal(t, targetFrom, targetTo)
})
}