Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remaining well-known eACL filter setters #333

Merged
merged 2 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/acl/eacl/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package eacl

import (
"fmt"
"strconv"

v2acl "github.com/nspcc-dev/neofs-api-go/v2/acl"
)
Expand All @@ -19,6 +20,8 @@ type Filter struct {

type staticStringer string

type u64Stringer uint64

type filterKey struct {
typ filterKeyType

Expand All @@ -45,6 +48,10 @@ func (s staticStringer) String() string {
return string(s)
}

func (u u64Stringer) String() string {
return strconv.FormatUint(uint64(u), 10)
}

// Value returns filtered string value.
func (f Filter) Value() string {
return f.value.String()
Expand Down
31 changes: 30 additions & 1 deletion pkg/acl/eacl/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/nspcc-dev/neofs-api-go/pkg"
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"
v2acl "github.com/nspcc-dev/neofs-api-go/v2/acl"
)
Expand Down Expand Up @@ -115,6 +116,11 @@ func (r *Record) AddObjectVersionFilter(m Match, v *pkg.Version) {
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.
func (r *Record) AddObjectContainerIDFilter(m Match, id *cid.ID) {
r.addObjectReservedFilter(m, fKeyObjContainerID, id)
Expand All @@ -125,7 +131,30 @@ func (r *Record) AddObjectOwnerIDFilter(m Match, id *owner.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.
//
Expand Down
103 changes: 103 additions & 0 deletions pkg/acl/eacl/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ package eacl

import (
"crypto/ecdsa"
"fmt"
"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"
"github.com/nspcc-dev/neofs-crypto/test"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -145,3 +151,100 @@ func TestRecord_ToV2(t *testing.T) {
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)
}
}
3 changes: 0 additions & 3 deletions v2/acl/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,4 @@ const (

// FilterObjectHomomorphicHash is a filter key to "homomorphic_hash" field of the object header.
FilterObjectHomomorphicHash = ObjectFilterPrefix + "homomorphicHash"

// FilterObjectParent is a filter key to "split.parent" field of the object header.
FilterObjectParent = ObjectFilterPrefix + "split.parent"
)