[#333] pkg/acl: Add remaining well-known eACL filter setters

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2021-08-25 14:11:35 +03:00 committed by Alex Vanin
parent 86df0eac93
commit 1ddc98dc75
3 changed files with 140 additions and 1 deletions

View file

@ -2,6 +2,7 @@ package eacl
import ( import (
"fmt" "fmt"
"strconv"
v2acl "github.com/nspcc-dev/neofs-api-go/v2/acl" v2acl "github.com/nspcc-dev/neofs-api-go/v2/acl"
) )
@ -19,6 +20,8 @@ type Filter struct {
type staticStringer string type staticStringer string
type u64Stringer uint64
type filterKey struct { type filterKey struct {
typ filterKeyType typ filterKeyType
@ -45,6 +48,10 @@ func (s staticStringer) String() string {
return string(s) return string(s)
} }
func (u u64Stringer) String() string {
return strconv.FormatUint(uint64(u), 10)
}
// Value returns filtered string value. // Value returns filtered string value.
func (f Filter) Value() string { func (f Filter) Value() string {
return f.value.String() return f.value.String()

View file

@ -6,6 +6,7 @@ import (
"github.com/nspcc-dev/neofs-api-go/pkg" "github.com/nspcc-dev/neofs-api-go/pkg"
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id" cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
"github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-api-go/pkg/owner" "github.com/nspcc-dev/neofs-api-go/pkg/owner"
v2acl "github.com/nspcc-dev/neofs-api-go/v2/acl" v2acl "github.com/nspcc-dev/neofs-api-go/v2/acl"
) )
@ -115,6 +116,11 @@ func (r *Record) AddObjectVersionFilter(m Match, v *pkg.Version) {
r.addObjectReservedFilter(m, fKeyObjVersion, v) r.addObjectReservedFilter(m, fKeyObjVersion, v)
} }
// AddObjectIDFilter adds filter by object ID.
func (r *Record) AddObjectIDFilter(m Match, id *object.ID) {
r.addObjectReservedFilter(m, fKeyObjID, id)
}
// AddObjectContainerIDFilter adds filter by object container ID. // AddObjectContainerIDFilter adds filter by object container ID.
func (r *Record) AddObjectContainerIDFilter(m Match, id *cid.ID) { func (r *Record) AddObjectContainerIDFilter(m Match, id *cid.ID) {
r.addObjectReservedFilter(m, fKeyObjContainerID, id) r.addObjectReservedFilter(m, fKeyObjContainerID, id)
@ -125,7 +131,30 @@ func (r *Record) AddObjectOwnerIDFilter(m Match, id *owner.ID) {
r.addObjectReservedFilter(m, fKeyObjOwnerID, id) r.addObjectReservedFilter(m, fKeyObjOwnerID, id)
} }
// TODO: add remaining filters after neofs-api#72 // AddObjectCreationEpoch adds filter by object creation epoch.
func (r *Record) AddObjectCreationEpoch(m Match, epoch uint64) {
r.addObjectReservedFilter(m, fKeyObjCreationEpoch, u64Stringer(epoch))
}
// AddObjectPayloadLengthFilter adds filter by object payload length.
func (r *Record) AddObjectPayloadLengthFilter(m Match, size uint64) {
r.addObjectReservedFilter(m, fKeyObjPayloadLength, u64Stringer(size))
}
// AddObjectPayloadHashFilter adds filter by object payload hash value.
func (r *Record) AddObjectPayloadHashFilter(m Match, h *pkg.Checksum) {
r.addObjectReservedFilter(m, fKeyObjPayloadHash, h)
}
// AddObjectTypeFilter adds filter by object type.
func (r *Record) AddObjectTypeFilter(m Match, t object.Type) {
r.addObjectReservedFilter(m, fKeyObjType, t)
}
// AddObjectHomomorphicHashFilter adds filter by object payload homomorphic hash value.
func (r *Record) AddObjectHomomorphicHashFilter(m Match, h *pkg.Checksum) {
r.addObjectReservedFilter(m, fKeyObjHomomorphicHash, h)
}
// ToV2 converts Record to v2 acl.EACLRecord message. // ToV2 converts Record to v2 acl.EACLRecord message.
// //

View file

@ -2,8 +2,14 @@ package eacl
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"fmt"
"testing" "testing"
cidtest "github.com/nspcc-dev/neofs-api-go/pkg/container/id/test"
"github.com/nspcc-dev/neofs-api-go/pkg/object"
objecttest "github.com/nspcc-dev/neofs-api-go/pkg/object/test"
ownertest "github.com/nspcc-dev/neofs-api-go/pkg/owner/test"
refstest "github.com/nspcc-dev/neofs-api-go/pkg/test"
v2acl "github.com/nspcc-dev/neofs-api-go/v2/acl" v2acl "github.com/nspcc-dev/neofs-api-go/v2/acl"
"github.com/nspcc-dev/neofs-crypto/test" "github.com/nspcc-dev/neofs-crypto/test"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -145,3 +151,100 @@ func TestRecord_ToV2(t *testing.T) {
require.Nil(t, recordV2.GetFilters()) require.Nil(t, recordV2.GetFilters())
}) })
} }
func TestReservedRecords(t *testing.T) {
var (
v = refstest.Version()
oid = objecttest.ID()
cid = cidtest.Generate()
ownerid = ownertest.Generate()
h = refstest.Checksum()
typ = new(object.Type)
)
testSuit := []struct {
f func(r *Record)
key string
value string
}{
{
f: func(r *Record) { r.AddObjectAttributeFilter(MatchStringEqual, "foo", "bar") },
key: "foo",
value: "bar",
},
{
f: func(r *Record) { r.AddObjectVersionFilter(MatchStringEqual, v) },
key: v2acl.FilterObjectVersion,
value: v.String(),
},
{
f: func(r *Record) { r.AddObjectIDFilter(MatchStringEqual, oid) },
key: v2acl.FilterObjectID,
value: oid.String(),
},
{
f: func(r *Record) { r.AddObjectContainerIDFilter(MatchStringEqual, cid) },
key: v2acl.FilterObjectContainerID,
value: cid.String(),
},
{
f: func(r *Record) { r.AddObjectOwnerIDFilter(MatchStringEqual, ownerid) },
key: v2acl.FilterObjectOwnerID,
value: ownerid.String(),
},
{
f: func(r *Record) { r.AddObjectCreationEpoch(MatchStringEqual, 100) },
key: v2acl.FilterObjectCreationEpoch,
value: "100",
},
{
f: func(r *Record) { r.AddObjectPayloadLengthFilter(MatchStringEqual, 5000) },
key: v2acl.FilterObjectPayloadLength,
value: "5000",
},
{
f: func(r *Record) { r.AddObjectPayloadHashFilter(MatchStringEqual, h) },
key: v2acl.FilterObjectPayloadHash,
value: h.String(),
},
{
f: func(r *Record) { r.AddObjectHomomorphicHashFilter(MatchStringEqual, h) },
key: v2acl.FilterObjectHomomorphicHash,
value: h.String(),
},
{
f: func(r *Record) {
require.True(t, typ.FromString("REGULAR"))
r.AddObjectTypeFilter(MatchStringEqual, *typ)
},
key: v2acl.FilterObjectType,
value: "REGULAR",
},
{
f: func(r *Record) {
require.True(t, typ.FromString("TOMBSTONE"))
r.AddObjectTypeFilter(MatchStringEqual, *typ)
},
key: v2acl.FilterObjectType,
value: "TOMBSTONE",
},
{
f: func(r *Record) {
require.True(t, typ.FromString("STORAGE_GROUP"))
r.AddObjectTypeFilter(MatchStringEqual, *typ)
},
key: v2acl.FilterObjectType,
value: "STORAGE_GROUP",
},
}
for n, testCase := range testSuit {
desc := fmt.Sprintf("case #%d", n)
record := NewRecord()
testCase.f(record)
require.Len(t, record.Filters(), 1, desc)
f := record.Filters()[0]
require.Equal(t, f.Key(), testCase.key, desc)
require.Equal(t, f.Value(), testCase.value, desc)
}
}