[#134] v2/acl: Update unified structures

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-09-04 10:50:36 +03:00 committed by Stanislav Bogatyrev
parent 09cc2a5037
commit 7a6c5eac1e
6 changed files with 104 additions and 63 deletions

View file

@ -5,31 +5,31 @@ import (
"github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-api-go/v2/refs"
) )
// TargetToGRPCField converts unified target enum into grpc enum. // RoleToGRPCField converts unified role enum into grpc enum.
func TargetToGRPCField(t Target) acl.Target { func RoleToGRPCField(t Role) acl.Role {
switch t { switch t {
case TargetUser: case RoleUser:
return acl.Target_USER return acl.Role_USER
case TargetSystem: case RoleSystem:
return acl.Target_SYSTEM return acl.Role_SYSTEM
case TargetOthers: case RoleOthers:
return acl.Target_OTHERS return acl.Role_OTHERS
default: default:
return acl.Target_TARGET_UNSPECIFIED return acl.Role_ROLE_UNSPECIFIED
} }
} }
// TargetFromGRPCField converts grpc enum into unified target enum. // RoleFromGRPCField converts grpc enum into unified role enum.
func TargetFromGRPCField(t acl.Target) Target { func RoleFromGRPCField(t acl.Role) Role {
switch t { switch t {
case acl.Target_USER: case acl.Role_USER:
return TargetUser return RoleUser
case acl.Target_SYSTEM: case acl.Role_SYSTEM:
return TargetSystem return RoleSystem
case acl.Target_OTHERS: case acl.Role_OTHERS:
return TargetOthers return RoleOthers
default: default:
return TargetUnknown return RoleUnknown
} }
} }
@ -150,12 +150,12 @@ func MatchTypeFromGRPCField(t acl.MatchType) MatchType {
} }
// HeaderFilterToGRPCMessage converts unified header filter struct into grpc struct. // 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 { if f == nil {
return nil return nil
} }
m := new(acl.EACLRecord_FilterInfo) m := new(acl.EACLRecord_Filter)
m.SetHeader( m.SetHeader(
HeaderTypeToGRPCField(f.GetHeaderType()), HeaderTypeToGRPCField(f.GetHeaderType()),
@ -172,7 +172,7 @@ func HeaderFilterToGRPCMessage(f *HeaderFilter) *acl.EACLRecord_FilterInfo {
} }
// HeaderFilterFromGRPCMessage converts grpc struct into unified header filter struct. // 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 { if m == nil {
return nil return nil
} }
@ -180,7 +180,7 @@ func HeaderFilterFromGRPCMessage(m *acl.EACLRecord_FilterInfo) *HeaderFilter {
f := new(HeaderFilter) f := new(HeaderFilter)
f.SetHeaderType( f.SetHeaderType(
HeaderTypeFromGRPCField(m.GetHeader()), HeaderTypeFromGRPCField(m.GetHeaderType()),
) )
f.SetMatchType( f.SetMatchType(
@ -193,16 +193,16 @@ func HeaderFilterFromGRPCMessage(m *acl.EACLRecord_FilterInfo) *HeaderFilter {
return f return f
} }
// TargetInfoToGRPCMessage converts unified target info struct into grpc struct. // TargetToGRPCMessage converts unified role info struct into grpc struct.
func TargetInfoToGRPCMessage(t *TargetInfo) *acl.EACLRecord_TargetInfo { func TargetToGRPCMessage(t *TargetInfo) *acl.EACLRecord_Target {
if t == nil { if t == nil {
return nil return nil
} }
m := new(acl.EACLRecord_TargetInfo) m := new(acl.EACLRecord_Target)
m.SetTarget( m.SetRole(
TargetToGRPCField(t.GetTarget()), RoleToGRPCField(t.GetRole()),
) )
m.SetKeyList(t.GetKeyList()) m.SetKeyList(t.GetKeyList())
@ -210,16 +210,16 @@ func TargetInfoToGRPCMessage(t *TargetInfo) *acl.EACLRecord_TargetInfo {
return m return m
} }
// TargetInfoFromGRPCMessage converts grpc struct into unified target info struct. // TargetInfoFromGRPCMessage converts grpc struct into unified role info struct.
func TargetInfoFromGRPCMessage(m *acl.EACLRecord_TargetInfo) *TargetInfo { func TargetInfoFromGRPCMessage(m *acl.EACLRecord_Target) *TargetInfo {
if m == nil { if m == nil {
return nil return nil
} }
t := new(TargetInfo) t := new(TargetInfo)
t.SetTarget( t.SetRole(
TargetFromGRPCField(m.GetTarget()), RoleFromGRPCField(m.GetRole()),
) )
t.SetKeyList(m.GetKeyList()) t.SetKeyList(m.GetKeyList())
@ -244,7 +244,7 @@ func RecordToGRPCMessage(r *Record) *acl.EACLRecord {
) )
filters := r.GetFilters() filters := r.GetFilters()
filterMsg := make([]*acl.EACLRecord_FilterInfo, 0, len(filters)) filterMsg := make([]*acl.EACLRecord_Filter, 0, len(filters))
for i := range filters { for i := range filters {
filterMsg = append(filterMsg, HeaderFilterToGRPCMessage(filters[i])) filterMsg = append(filterMsg, HeaderFilterToGRPCMessage(filters[i]))
@ -253,10 +253,10 @@ func RecordToGRPCMessage(r *Record) *acl.EACLRecord {
m.SetFilters(filterMsg) m.SetFilters(filterMsg)
targets := r.GetTargets() targets := r.GetTargets()
targetMsg := make([]*acl.EACLRecord_TargetInfo, 0, len(targets)) targetMsg := make([]*acl.EACLRecord_Target, 0, len(targets))
for i := range targets { for i := range targets {
targetMsg = append(targetMsg, TargetInfoToGRPCMessage(targets[i])) targetMsg = append(targetMsg, TargetToGRPCMessage(targets[i]))
} }
m.SetTargets(targetMsg) m.SetTargets(targetMsg)
@ -309,6 +309,9 @@ func TableToGRPCMessage(t *Table) *acl.EACLTable {
m := new(acl.EACLTable) m := new(acl.EACLTable)
m.SetVersion(
refs.VersionToGRPCMessage(t.GetVersion()),
)
m.SetContainerId( m.SetContainerId(
refs.ContainerIDToGRPCMessage(t.GetContainerID()), refs.ContainerIDToGRPCMessage(t.GetContainerID()),
) )
@ -333,6 +336,9 @@ func TableFromGRPCMessage(m *acl.EACLTable) *Table {
t := new(Table) t := new(Table)
t.SetVersion(
refs.VersionFromGRPCMessage(m.GetVersion()),
)
t.SetContainerID( t.SetContainerID(
refs.ContainerIDFromGRPCMessage(m.GetContainerId()), refs.ContainerIDFromGRPCMessage(m.GetContainerId()),
) )

View file

@ -18,8 +18,9 @@ const (
recordFiltersField = 3 recordFiltersField = 3
recordTargetsField = 4 recordTargetsField = 4
tableContainerIDField = 1 tableVersionField = 1
tableRecordsField = 2 tableContainerIDField = 2
tableRecordsField = 3
lifetimeExpirationField = 1 lifetimeExpirationField = 1
lifetimeNotValidBeforeField = 2 lifetimeNotValidBeforeField = 2
@ -49,6 +50,13 @@ func (t *Table) StableMarshal(buf []byte) ([]byte, error) {
err 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) n, err = proto.NestedStructureMarshal(tableContainerIDField, buf[offset:], t.cid)
if err != nil { if err != nil {
return nil, err return nil, err
@ -74,6 +82,7 @@ func (t *Table) StableSize() (size int) {
return 0 return 0
} }
size += proto.NestedStructureSize(tableVersionField, t.version)
size += proto.NestedStructureSize(tableContainerIDField, t.cid) size += proto.NestedStructureSize(tableContainerIDField, t.cid)
for i := range t.records { for i := range t.records {
@ -213,7 +222,7 @@ func (f *HeaderFilter) StableSize() (size int) {
return size 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. // compatible way without field order shuffle.
func (t *TargetInfo) StableMarshal(buf []byte) ([]byte, error) { func (t *TargetInfo) StableMarshal(buf []byte) ([]byte, error) {
if t == nil { if t == nil {
@ -229,7 +238,7 @@ func (t *TargetInfo) StableMarshal(buf []byte) ([]byte, error) {
err 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 { if err != nil {
return nil, err return nil, err
} }
@ -244,13 +253,13 @@ func (t *TargetInfo) StableMarshal(buf []byte) ([]byte, error) {
return buf, nil 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) { func (t *TargetInfo) StableSize() (size int) {
if t == nil { if t == nil {
return 0 return 0
} }
size += proto.EnumSize(targetTypeField, int32(t.target)) size += proto.EnumSize(targetTypeField, int32(t.role))
size += proto.RepeatedBytesSize(targetKeysField, t.keys) size += proto.RepeatedBytesSize(targetKeysField, t.keys)
return size return size

View file

@ -10,9 +10,9 @@ import (
"github.com/stretchr/testify/require" "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 := new(acl.TargetInfo)
target.SetTarget(u) target.SetRole(u)
keys := make([][]byte, k) keys := make([][]byte, k)
@ -39,7 +39,7 @@ func generateRecord(another bool) *acl.Record {
switch another { switch another {
case true: case true:
t1 := generateTarget(acl.TargetUser, 2) t1 := generateTarget(acl.RoleUser, 2)
f1 := generateFilter(acl.HeaderTypeObject, "OID", "ObjectID Value") f1 := generateFilter(acl.HeaderTypeObject, "OID", "ObjectID Value")
record.SetOperation(acl.OperationHead) record.SetOperation(acl.OperationHead)
@ -47,8 +47,8 @@ func generateRecord(another bool) *acl.Record {
record.SetTargets([]*acl.TargetInfo{t1}) record.SetTargets([]*acl.TargetInfo{t1})
record.SetFilters([]*acl.HeaderFilter{f1}) record.SetFilters([]*acl.HeaderFilter{f1})
default: default:
t1 := generateTarget(acl.TargetUser, 2) t1 := generateTarget(acl.RoleUser, 2)
t2 := generateTarget(acl.TargetSystem, 0) t2 := generateTarget(acl.RoleSystem, 0)
f1 := generateFilter(acl.HeaderTypeObject, "CID", "Container ID Value") f1 := generateFilter(acl.HeaderTypeObject, "CID", "Container ID Value")
f2 := generateFilter(acl.HeaderTypeRequest, "X-Header-Key", "X-Header-Value") f2 := generateFilter(acl.HeaderTypeRequest, "X-Header-Key", "X-Header-Value")
@ -65,7 +65,12 @@ func generateEACL() *acl.Table {
cid := new(refs.ContainerID) cid := new(refs.ContainerID)
cid.SetValue([]byte("Container ID")) cid.SetValue([]byte("Container ID"))
ver := new(refs.Version)
ver.SetMajor(2)
ver.SetMinor(3)
table := new(acl.Table) table := new(acl.Table)
table.SetVersion(ver)
table.SetContainerID(cid) table.SetContainerID(cid)
table.SetRecords([]*acl.Record{generateRecord(true)}) table.SetRecords([]*acl.Record{generateRecord(true)})
@ -111,7 +116,7 @@ func generateBearerToken(id string) *acl.BearerToken {
func TestHeaderFilter_StableMarshal(t *testing.T) { func TestHeaderFilter_StableMarshal(t *testing.T) {
filterFrom := generateFilter(acl.HeaderTypeObject, "CID", "Container ID Value") 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) { t.Run("non empty", func(t *testing.T) {
filterFrom.SetHeaderType(acl.HeaderTypeObject) filterFrom.SetHeaderType(acl.HeaderTypeObject)
@ -131,11 +136,11 @@ func TestHeaderFilter_StableMarshal(t *testing.T) {
} }
func TestTargetInfo_StableMarshal(t *testing.T) { func TestTargetInfo_StableMarshal(t *testing.T) {
targetFrom := generateTarget(acl.TargetUser, 2) targetFrom := generateTarget(acl.RoleUser, 2)
transport := new(grpc.EACLRecord_TargetInfo) transport := new(grpc.EACLRecord_Target)
t.Run("non empty", func(t *testing.T) { t.Run("non empty", func(t *testing.T) {
targetFrom.SetTarget(acl.TargetUser) targetFrom.SetRole(acl.RoleUser)
targetFrom.SetKeyList([][]byte{ targetFrom.SetKeyList([][]byte{
[]byte("Public Key 1"), []byte("Public Key 1"),
[]byte("Public Key 2"), []byte("Public Key 2"),
@ -176,9 +181,14 @@ func TestTable_StableMarshal(t *testing.T) {
cid := new(refs.ContainerID) cid := new(refs.ContainerID)
cid.SetValue([]byte("Container ID")) cid.SetValue([]byte("Container ID"))
ver := new(refs.Version)
ver.SetMajor(2)
ver.SetMinor(3)
r1 := generateRecord(false) r1 := generateRecord(false)
r2 := generateRecord(true) r2 := generateRecord(true)
tableFrom.SetVersion(ver)
tableFrom.SetContainerID(cid) tableFrom.SetContainerID(cid)
tableFrom.SetRecords([]*acl.Record{r1, r2}) tableFrom.SetRecords([]*acl.Record{r1, r2})

View file

@ -15,7 +15,7 @@ type HeaderFilter struct {
// TargetInfo is a unified structure of TargetInfo // TargetInfo is a unified structure of TargetInfo
// message from proto definition. // message from proto definition.
type TargetInfo struct { type TargetInfo struct {
target Target role Role
keys [][]byte keys [][]byte
} }
@ -35,6 +35,8 @@ type Record struct {
// Table is a unified structure of EACLTable // Table is a unified structure of EACLTable
// message from proto definition. // message from proto definition.
type Table struct { type Table struct {
version *refs.Version
cid *refs.ContainerID cid *refs.ContainerID
records []*Record records []*Record
@ -70,8 +72,8 @@ type Action uint32
// Operation is a unified enum of Operation enum from proto definition. // Operation is a unified enum of Operation enum from proto definition.
type Operation uint32 type Operation uint32
// Target is a unified enum of Target enum from proto definition. // Role is a unified enum of Role enum from proto definition.
type Target uint32 type Role uint32
const ( const (
MatchTypeUnknown MatchType = iota MatchTypeUnknown MatchType = iota
@ -103,10 +105,10 @@ const (
) )
const ( const (
TargetUnknown Target = iota RoleUnknown Role = iota
TargetUser RoleUser
TargetSystem RoleSystem
TargetOthers RoleOthers
) )
func (f *HeaderFilter) GetHeaderType() HeaderType { 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 { 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 { 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 { func (t *Table) GetContainerID() *refs.ContainerID {
if t != nil { if t != nil {
return t.cid return t.cid

View file

@ -351,7 +351,7 @@ func generateListResponseBody(n int) *container.ListResponseBody {
func generateEACL(n int, k, v string) *acl.Table { func generateEACL(n int, k, v string) *acl.Table {
target := new(acl.TargetInfo) target := new(acl.TargetInfo)
target.SetTarget(acl.TargetUser) target.SetRole(acl.RoleUser)
keys := make([][]byte, n) keys := make([][]byte, n)

View file

@ -282,7 +282,7 @@ func generateObjectCtx(id string) *session.ObjectSessionContext {
func generateEACL(n int, k, v string) *acl.Table { func generateEACL(n int, k, v string) *acl.Table {
target := new(acl.TargetInfo) target := new(acl.TargetInfo)
target.SetTarget(acl.TargetUser) target.SetRole(acl.RoleUser)
keys := make([][]byte, n) keys := make([][]byte, n)