From 7a6c5eac1ee4242561bc637948af9ef9664f71b1 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Fri, 4 Sep 2020 10:50:36 +0300 Subject: [PATCH] [#134] v2/acl: Update unified structures Signed-off-by: Alex Vanin --- v2/acl/convert.go | 74 +++++++++++++++++++----------------- v2/acl/marshal.go | 21 +++++++--- v2/acl/marshal_test.go | 28 +++++++++----- v2/acl/types.go | 40 +++++++++++++------ v2/container/marshal_test.go | 2 +- v2/session/marshal_test.go | 2 +- 6 files changed, 104 insertions(+), 63 deletions(-) diff --git a/v2/acl/convert.go b/v2/acl/convert.go index 7bf1684..79ebe5f 100644 --- a/v2/acl/convert.go +++ b/v2/acl/convert.go @@ -5,31 +5,31 @@ import ( "github.com/nspcc-dev/neofs-api-go/v2/refs" ) -// TargetToGRPCField converts unified target enum into grpc enum. -func TargetToGRPCField(t Target) acl.Target { +// RoleToGRPCField converts unified role enum into grpc enum. +func RoleToGRPCField(t Role) acl.Role { switch t { - case TargetUser: - return acl.Target_USER - case TargetSystem: - return acl.Target_SYSTEM - case TargetOthers: - return acl.Target_OTHERS + case RoleUser: + return acl.Role_USER + case RoleSystem: + return acl.Role_SYSTEM + case RoleOthers: + return acl.Role_OTHERS default: - return acl.Target_TARGET_UNSPECIFIED + return acl.Role_ROLE_UNSPECIFIED } } -// TargetFromGRPCField converts grpc enum into unified target enum. -func TargetFromGRPCField(t acl.Target) Target { +// RoleFromGRPCField converts grpc enum into unified role enum. +func RoleFromGRPCField(t acl.Role) Role { switch t { - case acl.Target_USER: - return TargetUser - case acl.Target_SYSTEM: - return TargetSystem - case acl.Target_OTHERS: - return TargetOthers + case acl.Role_USER: + return RoleUser + case acl.Role_SYSTEM: + return RoleSystem + case acl.Role_OTHERS: + return RoleOthers default: - return TargetUnknown + return RoleUnknown } } @@ -150,12 +150,12 @@ func MatchTypeFromGRPCField(t acl.MatchType) MatchType { } // HeaderFilterToGRPCMessage converts unified header filter struct into grpc struct. -func HeaderFilterToGRPCMessage(f *HeaderFilter) *acl.EACLRecord_FilterInfo { +func HeaderFilterToGRPCMessage(f *HeaderFilter) *acl.EACLRecord_Filter { if f == nil { return nil } - m := new(acl.EACLRecord_FilterInfo) + m := new(acl.EACLRecord_Filter) m.SetHeader( HeaderTypeToGRPCField(f.GetHeaderType()), @@ -172,7 +172,7 @@ func HeaderFilterToGRPCMessage(f *HeaderFilter) *acl.EACLRecord_FilterInfo { } // HeaderFilterFromGRPCMessage converts grpc struct into unified header filter struct. -func HeaderFilterFromGRPCMessage(m *acl.EACLRecord_FilterInfo) *HeaderFilter { +func HeaderFilterFromGRPCMessage(m *acl.EACLRecord_Filter) *HeaderFilter { if m == nil { return nil } @@ -180,7 +180,7 @@ func HeaderFilterFromGRPCMessage(m *acl.EACLRecord_FilterInfo) *HeaderFilter { f := new(HeaderFilter) f.SetHeaderType( - HeaderTypeFromGRPCField(m.GetHeader()), + HeaderTypeFromGRPCField(m.GetHeaderType()), ) f.SetMatchType( @@ -193,16 +193,16 @@ func HeaderFilterFromGRPCMessage(m *acl.EACLRecord_FilterInfo) *HeaderFilter { return f } -// TargetInfoToGRPCMessage converts unified target info struct into grpc struct. -func TargetInfoToGRPCMessage(t *TargetInfo) *acl.EACLRecord_TargetInfo { +// TargetToGRPCMessage converts unified role info struct into grpc struct. +func TargetToGRPCMessage(t *TargetInfo) *acl.EACLRecord_Target { if t == nil { return nil } - m := new(acl.EACLRecord_TargetInfo) + m := new(acl.EACLRecord_Target) - m.SetTarget( - TargetToGRPCField(t.GetTarget()), + m.SetRole( + RoleToGRPCField(t.GetRole()), ) m.SetKeyList(t.GetKeyList()) @@ -210,16 +210,16 @@ func TargetInfoToGRPCMessage(t *TargetInfo) *acl.EACLRecord_TargetInfo { return m } -// TargetInfoFromGRPCMessage converts grpc struct into unified target info struct. -func TargetInfoFromGRPCMessage(m *acl.EACLRecord_TargetInfo) *TargetInfo { +// TargetInfoFromGRPCMessage converts grpc struct into unified role info struct. +func TargetInfoFromGRPCMessage(m *acl.EACLRecord_Target) *TargetInfo { if m == nil { return nil } t := new(TargetInfo) - t.SetTarget( - TargetFromGRPCField(m.GetTarget()), + t.SetRole( + RoleFromGRPCField(m.GetRole()), ) t.SetKeyList(m.GetKeyList()) @@ -244,7 +244,7 @@ func RecordToGRPCMessage(r *Record) *acl.EACLRecord { ) filters := r.GetFilters() - filterMsg := make([]*acl.EACLRecord_FilterInfo, 0, len(filters)) + filterMsg := make([]*acl.EACLRecord_Filter, 0, len(filters)) for i := range filters { filterMsg = append(filterMsg, HeaderFilterToGRPCMessage(filters[i])) @@ -253,10 +253,10 @@ func RecordToGRPCMessage(r *Record) *acl.EACLRecord { m.SetFilters(filterMsg) targets := r.GetTargets() - targetMsg := make([]*acl.EACLRecord_TargetInfo, 0, len(targets)) + targetMsg := make([]*acl.EACLRecord_Target, 0, len(targets)) for i := range targets { - targetMsg = append(targetMsg, TargetInfoToGRPCMessage(targets[i])) + targetMsg = append(targetMsg, TargetToGRPCMessage(targets[i])) } m.SetTargets(targetMsg) @@ -309,6 +309,9 @@ func TableToGRPCMessage(t *Table) *acl.EACLTable { m := new(acl.EACLTable) + m.SetVersion( + refs.VersionToGRPCMessage(t.GetVersion()), + ) m.SetContainerId( refs.ContainerIDToGRPCMessage(t.GetContainerID()), ) @@ -333,6 +336,9 @@ func TableFromGRPCMessage(m *acl.EACLTable) *Table { t := new(Table) + t.SetVersion( + refs.VersionFromGRPCMessage(m.GetVersion()), + ) t.SetContainerID( refs.ContainerIDFromGRPCMessage(m.GetContainerId()), ) diff --git a/v2/acl/marshal.go b/v2/acl/marshal.go index ccf5760..dbbf630 100644 --- a/v2/acl/marshal.go +++ b/v2/acl/marshal.go @@ -18,8 +18,9 @@ const ( recordFiltersField = 3 recordTargetsField = 4 - tableContainerIDField = 1 - tableRecordsField = 2 + tableVersionField = 1 + tableContainerIDField = 2 + tableRecordsField = 3 lifetimeExpirationField = 1 lifetimeNotValidBeforeField = 2 @@ -49,6 +50,13 @@ func (t *Table) StableMarshal(buf []byte) ([]byte, error) { err error ) + n, err = proto.NestedStructureMarshal(tableVersionField, buf[offset:], t.version) + if err != nil { + return nil, err + } + + offset += n + n, err = proto.NestedStructureMarshal(tableContainerIDField, buf[offset:], t.cid) if err != nil { return nil, err @@ -74,6 +82,7 @@ func (t *Table) StableSize() (size int) { return 0 } + size += proto.NestedStructureSize(tableVersionField, t.version) size += proto.NestedStructureSize(tableContainerIDField, t.cid) for i := range t.records { @@ -213,7 +222,7 @@ func (f *HeaderFilter) StableSize() (size int) { return size } -// StableMarshal marshals unified target info structure in a protobuf +// StableMarshal marshals unified role info structure in a protobuf // compatible way without field order shuffle. func (t *TargetInfo) StableMarshal(buf []byte) ([]byte, error) { if t == nil { @@ -229,7 +238,7 @@ func (t *TargetInfo) StableMarshal(buf []byte) ([]byte, error) { err error ) - n, err = proto.EnumMarshal(targetTypeField, buf[offset:], int32(t.target)) + n, err = proto.EnumMarshal(targetTypeField, buf[offset:], int32(t.role)) if err != nil { return nil, err } @@ -244,13 +253,13 @@ func (t *TargetInfo) StableMarshal(buf []byte) ([]byte, error) { return buf, nil } -// StableSize of target info structure marshalled by StableMarshal function. +// StableSize of role info structure marshalled by StableMarshal function. func (t *TargetInfo) StableSize() (size int) { if t == nil { return 0 } - size += proto.EnumSize(targetTypeField, int32(t.target)) + size += proto.EnumSize(targetTypeField, int32(t.role)) size += proto.RepeatedBytesSize(targetKeysField, t.keys) return size diff --git a/v2/acl/marshal_test.go b/v2/acl/marshal_test.go index cd471d7..79b187b 100644 --- a/v2/acl/marshal_test.go +++ b/v2/acl/marshal_test.go @@ -10,9 +10,9 @@ import ( "github.com/stretchr/testify/require" ) -func generateTarget(u acl.Target, k int) *acl.TargetInfo { +func generateTarget(u acl.Role, k int) *acl.TargetInfo { target := new(acl.TargetInfo) - target.SetTarget(u) + target.SetRole(u) keys := make([][]byte, k) @@ -39,7 +39,7 @@ func generateRecord(another bool) *acl.Record { switch another { case true: - t1 := generateTarget(acl.TargetUser, 2) + t1 := generateTarget(acl.RoleUser, 2) f1 := generateFilter(acl.HeaderTypeObject, "OID", "ObjectID Value") record.SetOperation(acl.OperationHead) @@ -47,8 +47,8 @@ func generateRecord(another bool) *acl.Record { record.SetTargets([]*acl.TargetInfo{t1}) record.SetFilters([]*acl.HeaderFilter{f1}) default: - t1 := generateTarget(acl.TargetUser, 2) - t2 := generateTarget(acl.TargetSystem, 0) + t1 := generateTarget(acl.RoleUser, 2) + t2 := generateTarget(acl.RoleSystem, 0) f1 := generateFilter(acl.HeaderTypeObject, "CID", "Container ID Value") f2 := generateFilter(acl.HeaderTypeRequest, "X-Header-Key", "X-Header-Value") @@ -65,7 +65,12 @@ func generateEACL() *acl.Table { cid := new(refs.ContainerID) cid.SetValue([]byte("Container ID")) + ver := new(refs.Version) + ver.SetMajor(2) + ver.SetMinor(3) + table := new(acl.Table) + table.SetVersion(ver) table.SetContainerID(cid) table.SetRecords([]*acl.Record{generateRecord(true)}) @@ -111,7 +116,7 @@ func generateBearerToken(id string) *acl.BearerToken { func TestHeaderFilter_StableMarshal(t *testing.T) { filterFrom := generateFilter(acl.HeaderTypeObject, "CID", "Container ID Value") - transport := new(grpc.EACLRecord_FilterInfo) + transport := new(grpc.EACLRecord_Filter) t.Run("non empty", func(t *testing.T) { filterFrom.SetHeaderType(acl.HeaderTypeObject) @@ -131,11 +136,11 @@ func TestHeaderFilter_StableMarshal(t *testing.T) { } func TestTargetInfo_StableMarshal(t *testing.T) { - targetFrom := generateTarget(acl.TargetUser, 2) - transport := new(grpc.EACLRecord_TargetInfo) + targetFrom := generateTarget(acl.RoleUser, 2) + transport := new(grpc.EACLRecord_Target) t.Run("non empty", func(t *testing.T) { - targetFrom.SetTarget(acl.TargetUser) + targetFrom.SetRole(acl.RoleUser) targetFrom.SetKeyList([][]byte{ []byte("Public Key 1"), []byte("Public Key 2"), @@ -176,9 +181,14 @@ func TestTable_StableMarshal(t *testing.T) { cid := new(refs.ContainerID) cid.SetValue([]byte("Container ID")) + ver := new(refs.Version) + ver.SetMajor(2) + ver.SetMinor(3) + r1 := generateRecord(false) r2 := generateRecord(true) + tableFrom.SetVersion(ver) tableFrom.SetContainerID(cid) tableFrom.SetRecords([]*acl.Record{r1, r2}) diff --git a/v2/acl/types.go b/v2/acl/types.go index 5a50b88..38a7044 100644 --- a/v2/acl/types.go +++ b/v2/acl/types.go @@ -15,7 +15,7 @@ type HeaderFilter struct { // TargetInfo is a unified structure of TargetInfo // message from proto definition. type TargetInfo struct { - target Target + role Role keys [][]byte } @@ -35,6 +35,8 @@ type Record struct { // Table is a unified structure of EACLTable // message from proto definition. type Table struct { + version *refs.Version + cid *refs.ContainerID records []*Record @@ -70,8 +72,8 @@ type Action uint32 // Operation is a unified enum of Operation enum from proto definition. type Operation uint32 -// Target is a unified enum of Target enum from proto definition. -type Target uint32 +// Role is a unified enum of Role enum from proto definition. +type Role uint32 const ( MatchTypeUnknown MatchType = iota @@ -103,10 +105,10 @@ const ( ) const ( - TargetUnknown Target = iota - TargetUser - TargetSystem - TargetOthers + RoleUnknown Role = iota + RoleUser + RoleSystem + RoleOthers ) func (f *HeaderFilter) GetHeaderType() HeaderType { @@ -165,17 +167,17 @@ func (f *HeaderFilter) SetValue(v string) { } } -func (t *TargetInfo) GetTarget() Target { +func (t *TargetInfo) GetRole() Role { if t != nil { - return t.target + return t.role } - return TargetUnknown + return RoleUnknown } -func (t *TargetInfo) SetTarget(v Target) { +func (t *TargetInfo) SetRole(v Role) { if t != nil { - t.target = v + t.role = v } } @@ -249,6 +251,20 @@ func (r *Record) SetTargets(v []*TargetInfo) { } } +func (t *Table) GetVersion() *refs.Version { + if t != nil { + return t.version + } + + return nil +} + +func (t *Table) SetVersion(v *refs.Version) { + if t != nil { + t.version = v + } +} + func (t *Table) GetContainerID() *refs.ContainerID { if t != nil { return t.cid diff --git a/v2/container/marshal_test.go b/v2/container/marshal_test.go index 19e5d6e..fba58f5 100644 --- a/v2/container/marshal_test.go +++ b/v2/container/marshal_test.go @@ -351,7 +351,7 @@ func generateListResponseBody(n int) *container.ListResponseBody { func generateEACL(n int, k, v string) *acl.Table { target := new(acl.TargetInfo) - target.SetTarget(acl.TargetUser) + target.SetRole(acl.RoleUser) keys := make([][]byte, n) diff --git a/v2/session/marshal_test.go b/v2/session/marshal_test.go index 0f038c0..c2f4415 100644 --- a/v2/session/marshal_test.go +++ b/v2/session/marshal_test.go @@ -282,7 +282,7 @@ func generateObjectCtx(id string) *session.ObjectSessionContext { func generateEACL(n int, k, v string) *acl.Table { target := new(acl.TargetInfo) - target.SetTarget(acl.TargetUser) + target.SetRole(acl.RoleUser) keys := make([][]byte, n)