From ae68790bbd40f3ead2807827f0af555a5f002cbf Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 13 Nov 2020 14:48:19 +0300 Subject: [PATCH] [#168] acl: Implement binary/JSON encoders/decoders on Record Signed-off-by: Leonard Lyubich --- pkg/acl/eacl/record.go | 44 +++++++++++++++++++++++++++++++++++++ pkg/acl/eacl/record_test.go | 28 +++++++++++++++++++++++ v2/acl/json.go | 20 +++++++++++++++++ v2/acl/marshal.go | 11 ++++++++++ v2/acl/marshal_test.go | 8 +++---- 5 files changed, 106 insertions(+), 5 deletions(-) diff --git a/pkg/acl/eacl/record.go b/pkg/acl/eacl/record.go index 126652e6..db077aa4 100644 --- a/pkg/acl/eacl/record.go +++ b/pkg/acl/eacl/record.go @@ -162,3 +162,47 @@ func NewRecordFromV2(record *v2acl.Record) *Record { return r } + +// Marshal marshals Record into a protobuf binary form. +// +// Buffer is allocated when the argument is empty. +// Otherwise, the first buffer is used. +func (r *Record) Marshal(b ...[]byte) ([]byte, error) { + var buf []byte + if len(b) > 0 { + buf = b[0] + } + + return r.ToV2(). + StableMarshal(buf) +} + +// Unmarshal unmarshals protobuf binary representation of Record. +func (r *Record) Unmarshal(data []byte) error { + fV2 := new(v2acl.Record) + if err := fV2.Unmarshal(data); err != nil { + return err + } + + *r = *NewRecordFromV2(fV2) + + return nil +} + +// MarshalJSON encodes Record to protobuf JSON format. +func (r *Record) MarshalJSON() ([]byte, error) { + return r.ToV2(). + MarshalJSON() +} + +// UnmarshalJSON decodes Record from protobuf JSON format. +func (r *Record) UnmarshalJSON(data []byte) error { + tV2 := new(v2acl.Record) + if err := tV2.UnmarshalJSON(data); err != nil { + return err + } + + *r = *NewRecordFromV2(tV2) + + return nil +} diff --git a/pkg/acl/eacl/record_test.go b/pkg/acl/eacl/record_test.go index 7cddd2e0..fef691dc 100644 --- a/pkg/acl/eacl/record_test.go +++ b/pkg/acl/eacl/record_test.go @@ -71,3 +71,31 @@ func TestRecord_AddFilter(t *testing.T) { require.Equal(t, filters, r.Filters()) } + +func TestRecordEncoding(t *testing.T) { + r := NewRecord() + r.SetOperation(OperationHead) + r.SetAction(ActionDeny) + r.AddObjectAttributeFilter(MatchStringEqual, "key", "value") + r.AddTarget(RoleSystem, test.DecodeKey(-1).PublicKey) + + t.Run("binary", func(t *testing.T) { + data, err := r.Marshal() + require.NoError(t, err) + + r2 := NewRecord() + require.NoError(t, r2.Unmarshal(data)) + + require.Equal(t, r, r2) + }) + + t.Run("json", func(t *testing.T) { + data, err := r.MarshalJSON() + require.NoError(t, err) + + r2 := NewRecord() + require.NoError(t, r2.UnmarshalJSON(data)) + + require.Equal(t, r, r2) + }) +} diff --git a/v2/acl/json.go b/v2/acl/json.go index 72cf022b..a45f666e 100644 --- a/v2/acl/json.go +++ b/v2/acl/json.go @@ -122,3 +122,23 @@ func (t *Target) UnmarshalJSON(data []byte) error { return nil } + +func (r *Record) MarshalJSON() ([]byte, error) { + return protojson.MarshalOptions{ + EmitUnpopulated: true, + }.Marshal( + RecordToGRPCMessage(r), + ) +} + +func (r *Record) UnmarshalJSON(data []byte) error { + msg := new(acl.EACLRecord) + + if err := protojson.Unmarshal(data, msg); err != nil { + return err + } + + *r = *RecordFromGRPCMessage(msg) + + return nil +} diff --git a/v2/acl/marshal.go b/v2/acl/marshal.go index 8b614725..fcf70b75 100644 --- a/v2/acl/marshal.go +++ b/v2/acl/marshal.go @@ -165,6 +165,17 @@ func (r *Record) StableSize() (size int) { return size } +func (r *Record) Unmarshal(data []byte) error { + m := new(acl.EACLRecord) + if err := proto.Unmarshal(data, m); err != nil { + return err + } + + *r = *RecordFromGRPCMessage(m) + + return nil +} + // StableMarshal marshals unified header filter structure in a protobuf // compatible way without field order shuffle. func (f *HeaderFilter) StableMarshal(buf []byte) ([]byte, error) { diff --git a/v2/acl/marshal_test.go b/v2/acl/marshal_test.go index f2df14aa..8d79bc10 100644 --- a/v2/acl/marshal_test.go +++ b/v2/acl/marshal_test.go @@ -156,17 +156,15 @@ func TestTargetInfo_StableMarshal(t *testing.T) { func TestRecord_StableMarshal(t *testing.T) { recordFrom := generateRecord(false) - transport := new(grpc.EACLRecord) t.Run("non empty", func(t *testing.T) { wire, err := recordFrom.StableMarshal(nil) require.NoError(t, err) - err = goproto.Unmarshal(wire, transport) - require.NoError(t, err) + to := new(acl.Record) + require.NoError(t, to.Unmarshal(wire)) - recordTo := acl.RecordFromGRPCMessage(transport) - require.Equal(t, recordFrom, recordTo) + require.Equal(t, recordFrom, to) }) }