2022-05-05 11:00:30 +00:00
|
|
|
package tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/sha256"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
aclV2 "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/acl"
|
|
|
|
containercore "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/container"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/util/logger"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/acl"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
|
|
|
eaclSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/eacl"
|
|
|
|
netmapSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2022-05-05 11:00:30 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
)
|
|
|
|
|
|
|
|
type dummyNetmapSource struct {
|
|
|
|
netmap.Source
|
|
|
|
}
|
|
|
|
|
|
|
|
type dummyContainerSource map[string]*containercore.Container
|
|
|
|
|
2022-10-18 21:33:45 +00:00
|
|
|
func (s dummyContainerSource) List() ([]cid.ID, error) {
|
|
|
|
res := make([]cid.ID, 0, len(s))
|
|
|
|
var cnr cid.ID
|
|
|
|
|
|
|
|
for cidStr := range s {
|
|
|
|
err := cnr.DecodeString(cidStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res = append(res, cnr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2022-05-05 11:00:30 +00:00
|
|
|
func (s dummyContainerSource) Get(id cid.ID) (*containercore.Container, error) {
|
|
|
|
cnt, ok := s[id.String()]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("container not found")
|
|
|
|
}
|
|
|
|
return cnt, nil
|
|
|
|
}
|
|
|
|
|
2022-10-25 12:24:06 +00:00
|
|
|
type dummyEACLSource map[string]*containercore.EACL
|
|
|
|
|
|
|
|
func (s dummyEACLSource) GetEACL(id cid.ID) (*containercore.EACL, error) {
|
|
|
|
cntEACL, ok := s[id.String()]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("container not found")
|
|
|
|
}
|
|
|
|
return cntEACL, nil
|
|
|
|
}
|
|
|
|
|
2022-05-05 11:00:30 +00:00
|
|
|
func testContainer(owner user.ID) container.Container {
|
|
|
|
var r netmapSDK.ReplicaDescriptor
|
|
|
|
r.SetNumberOfObjects(1)
|
|
|
|
|
|
|
|
var pp netmapSDK.PlacementPolicy
|
|
|
|
pp.AddReplicas(r)
|
|
|
|
|
|
|
|
var cnt container.Container
|
|
|
|
cnt.SetOwner(owner)
|
|
|
|
cnt.SetPlacementPolicy(pp)
|
|
|
|
|
|
|
|
return cnt
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMessageSign(t *testing.T) {
|
|
|
|
privs := make([]*keys.PrivateKey, 4)
|
|
|
|
for i := range privs {
|
|
|
|
p, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
privs[i] = p
|
|
|
|
}
|
|
|
|
|
|
|
|
cid1 := cidtest.ID()
|
|
|
|
cid2 := cidtest.ID()
|
|
|
|
|
|
|
|
var ownerID user.ID
|
|
|
|
user.IDFromKey(&ownerID, (ecdsa.PublicKey)(*privs[0].PublicKey()))
|
|
|
|
|
2022-09-08 12:44:27 +00:00
|
|
|
cnr := &containercore.Container{
|
|
|
|
Value: testContainer(ownerID),
|
|
|
|
}
|
|
|
|
|
2022-05-05 11:00:30 +00:00
|
|
|
s := &Service{
|
|
|
|
cfg: cfg{
|
2022-09-28 07:41:01 +00:00
|
|
|
log: &logger.Logger{Logger: zaptest.NewLogger(t)},
|
2022-05-05 11:00:30 +00:00
|
|
|
key: &privs[0].PrivateKey,
|
|
|
|
nmSource: dummyNetmapSource{},
|
|
|
|
cnrSource: dummyContainerSource{
|
2022-09-08 12:44:27 +00:00
|
|
|
cid1.String(): cnr,
|
2022-05-05 11:00:30 +00:00
|
|
|
},
|
2022-10-25 12:24:06 +00:00
|
|
|
eaclSource: dummyEACLSource{
|
|
|
|
cid1.String(): &containercore.EACL{
|
|
|
|
Value: testTable(cid1, privs[0].PublicKey(), privs[1].PublicKey()),
|
|
|
|
},
|
|
|
|
},
|
2022-05-05 11:00:30 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
rawCID1 := make([]byte, sha256.Size)
|
|
|
|
cid1.Encode(rawCID1)
|
|
|
|
|
|
|
|
req := &MoveRequest{
|
|
|
|
Body: &MoveRequest_Body{
|
|
|
|
ContainerId: rawCID1,
|
|
|
|
ParentId: 1,
|
|
|
|
NodeId: 2,
|
|
|
|
Meta: []*KeyValue{
|
|
|
|
{Key: "kkk", Value: []byte("vvv")},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-09-08 12:44:27 +00:00
|
|
|
op := acl.OpObjectPut
|
|
|
|
cnr.Value.SetBasicACL(acl.PublicRW)
|
|
|
|
|
2022-05-05 11:00:30 +00:00
|
|
|
t.Run("missing signature, no panic", func(t *testing.T) {
|
2022-09-08 12:44:27 +00:00
|
|
|
require.Error(t, s.verifyClient(req, cid2, nil, op))
|
2022-05-05 11:00:30 +00:00
|
|
|
})
|
|
|
|
|
2022-10-10 15:32:04 +00:00
|
|
|
require.NoError(t, SignMessage(req, &privs[0].PrivateKey))
|
2022-09-08 12:44:27 +00:00
|
|
|
require.NoError(t, s.verifyClient(req, cid1, nil, op))
|
2022-05-05 11:00:30 +00:00
|
|
|
|
|
|
|
t.Run("invalid CID", func(t *testing.T) {
|
2022-09-08 12:44:27 +00:00
|
|
|
require.Error(t, s.verifyClient(req, cid2, nil, op))
|
2022-05-05 11:00:30 +00:00
|
|
|
})
|
2022-09-08 12:44:27 +00:00
|
|
|
|
|
|
|
cnr.Value.SetBasicACL(acl.Private)
|
|
|
|
|
|
|
|
t.Run("extension disabled", func(t *testing.T) {
|
2022-10-10 15:32:04 +00:00
|
|
|
require.NoError(t, SignMessage(req, &privs[0].PrivateKey))
|
2022-09-08 12:44:27 +00:00
|
|
|
require.Error(t, s.verifyClient(req, cid2, nil, op))
|
|
|
|
})
|
|
|
|
|
2022-05-05 11:00:30 +00:00
|
|
|
t.Run("invalid key", func(t *testing.T) {
|
2022-10-10 15:32:04 +00:00
|
|
|
require.NoError(t, SignMessage(req, &privs[1].PrivateKey))
|
2022-09-08 12:44:27 +00:00
|
|
|
require.Error(t, s.verifyClient(req, cid1, nil, op))
|
2022-05-05 11:00:30 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("bearer", func(t *testing.T) {
|
2022-09-08 12:44:27 +00:00
|
|
|
bACL := acl.PrivateExtended
|
|
|
|
bACL.AllowBearerRules(op)
|
|
|
|
cnr.Value.SetBasicACL(bACL)
|
|
|
|
|
|
|
|
bACL.DisableExtension()
|
|
|
|
|
2022-05-05 11:00:30 +00:00
|
|
|
t.Run("invalid bearer", func(t *testing.T) {
|
|
|
|
req.Body.BearerToken = []byte{0xFF}
|
2022-10-10 15:32:04 +00:00
|
|
|
require.NoError(t, SignMessage(req, &privs[0].PrivateKey))
|
2022-09-08 12:44:27 +00:00
|
|
|
require.Error(t, s.verifyClient(req, cid1, req.GetBody().GetBearerToken(), acl.OpObjectPut))
|
2022-05-05 11:00:30 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalid bearer CID", func(t *testing.T) {
|
|
|
|
bt := testBearerToken(cid2, privs[1].PublicKey(), privs[2].PublicKey())
|
|
|
|
require.NoError(t, bt.Sign(privs[0].PrivateKey))
|
|
|
|
req.Body.BearerToken = bt.Marshal()
|
|
|
|
|
2022-10-10 15:32:04 +00:00
|
|
|
require.NoError(t, SignMessage(req, &privs[1].PrivateKey))
|
2022-09-08 12:44:27 +00:00
|
|
|
require.Error(t, s.verifyClient(req, cid1, req.GetBody().GetBearerToken(), acl.OpObjectPut))
|
2022-05-05 11:00:30 +00:00
|
|
|
})
|
|
|
|
t.Run("invalid bearer owner", func(t *testing.T) {
|
|
|
|
bt := testBearerToken(cid1, privs[1].PublicKey(), privs[2].PublicKey())
|
|
|
|
require.NoError(t, bt.Sign(privs[1].PrivateKey))
|
|
|
|
req.Body.BearerToken = bt.Marshal()
|
|
|
|
|
2022-10-10 15:32:04 +00:00
|
|
|
require.NoError(t, SignMessage(req, &privs[1].PrivateKey))
|
2022-09-08 12:44:27 +00:00
|
|
|
require.Error(t, s.verifyClient(req, cid1, req.GetBody().GetBearerToken(), acl.OpObjectPut))
|
2022-05-05 11:00:30 +00:00
|
|
|
})
|
|
|
|
t.Run("invalid bearer signature", func(t *testing.T) {
|
|
|
|
bt := testBearerToken(cid1, privs[1].PublicKey(), privs[2].PublicKey())
|
|
|
|
require.NoError(t, bt.Sign(privs[0].PrivateKey))
|
|
|
|
|
2022-09-08 12:44:27 +00:00
|
|
|
var bv2 aclV2.BearerToken
|
2022-05-05 11:00:30 +00:00
|
|
|
bt.WriteToV2(&bv2)
|
|
|
|
bv2.GetSignature().SetSign([]byte{1, 2, 3})
|
|
|
|
req.Body.BearerToken = bv2.StableMarshal(nil)
|
|
|
|
|
2022-10-10 15:32:04 +00:00
|
|
|
require.NoError(t, SignMessage(req, &privs[1].PrivateKey))
|
2022-09-08 12:44:27 +00:00
|
|
|
require.Error(t, s.verifyClient(req, cid1, req.GetBody().GetBearerToken(), acl.OpObjectPut))
|
2022-05-05 11:00:30 +00:00
|
|
|
})
|
|
|
|
|
2022-10-25 12:24:06 +00:00
|
|
|
t.Run("impersonate", func(t *testing.T) {
|
|
|
|
cnr.Value.SetBasicACL(acl.PublicRWExtended)
|
|
|
|
var bt bearer.Token
|
|
|
|
bt.SetImpersonate(true)
|
|
|
|
|
|
|
|
require.NoError(t, bt.Sign(privs[1].PrivateKey))
|
|
|
|
req.Body.BearerToken = bt.Marshal()
|
|
|
|
|
|
|
|
require.NoError(t, SignMessage(req, &privs[0].PrivateKey))
|
|
|
|
require.Error(t, s.verifyClient(req, cid1, req.GetBody().GetBearerToken(), acl.OpObjectPut))
|
|
|
|
require.NoError(t, s.verifyClient(req, cid1, req.GetBody().GetBearerToken(), acl.OpObjectGet))
|
|
|
|
})
|
|
|
|
|
2022-05-05 11:00:30 +00:00
|
|
|
bt := testBearerToken(cid1, privs[1].PublicKey(), privs[2].PublicKey())
|
|
|
|
require.NoError(t, bt.Sign(privs[0].PrivateKey))
|
|
|
|
req.Body.BearerToken = bt.Marshal()
|
2022-09-08 12:44:27 +00:00
|
|
|
cnr.Value.SetBasicACL(acl.PublicRWExtended)
|
2022-05-05 11:00:30 +00:00
|
|
|
|
|
|
|
t.Run("put and get", func(t *testing.T) {
|
2022-10-10 15:32:04 +00:00
|
|
|
require.NoError(t, SignMessage(req, &privs[1].PrivateKey))
|
2022-09-08 12:44:27 +00:00
|
|
|
require.NoError(t, s.verifyClient(req, cid1, req.GetBody().GetBearerToken(), acl.OpObjectPut))
|
|
|
|
require.NoError(t, s.verifyClient(req, cid1, req.GetBody().GetBearerToken(), acl.OpObjectGet))
|
2022-05-05 11:00:30 +00:00
|
|
|
})
|
|
|
|
t.Run("only get", func(t *testing.T) {
|
2022-10-10 15:32:04 +00:00
|
|
|
require.NoError(t, SignMessage(req, &privs[2].PrivateKey))
|
2022-09-08 12:44:27 +00:00
|
|
|
require.Error(t, s.verifyClient(req, cid1, req.GetBody().GetBearerToken(), acl.OpObjectPut))
|
|
|
|
require.NoError(t, s.verifyClient(req, cid1, req.GetBody().GetBearerToken(), acl.OpObjectGet))
|
2022-05-05 11:00:30 +00:00
|
|
|
})
|
|
|
|
t.Run("none", func(t *testing.T) {
|
2022-10-10 15:32:04 +00:00
|
|
|
require.NoError(t, SignMessage(req, &privs[3].PrivateKey))
|
2022-09-08 12:44:27 +00:00
|
|
|
require.Error(t, s.verifyClient(req, cid1, req.GetBody().GetBearerToken(), acl.OpObjectPut))
|
|
|
|
require.Error(t, s.verifyClient(req, cid1, req.GetBody().GetBearerToken(), acl.OpObjectGet))
|
2022-05-05 11:00:30 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-08 12:44:27 +00:00
|
|
|
func testBearerToken(cid cid.ID, forPutGet, forGet *keys.PublicKey) bearer.Token {
|
2022-10-25 12:24:06 +00:00
|
|
|
var b bearer.Token
|
|
|
|
b.SetEACLTable(*testTable(cid, forPutGet, forGet))
|
|
|
|
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func testTable(cid cid.ID, forPutGet, forGet *keys.PublicKey) *eaclSDK.Table {
|
2022-05-05 11:00:30 +00:00
|
|
|
tgtGet := eaclSDK.NewTarget()
|
|
|
|
tgtGet.SetRole(eaclSDK.RoleUnknown)
|
2022-09-08 12:44:27 +00:00
|
|
|
tgtGet.SetBinaryKeys([][]byte{forPutGet.Bytes(), forGet.Bytes()})
|
2022-05-05 11:00:30 +00:00
|
|
|
|
|
|
|
rGet := eaclSDK.NewRecord()
|
|
|
|
rGet.SetAction(eaclSDK.ActionAllow)
|
|
|
|
rGet.SetOperation(eaclSDK.OperationGet)
|
|
|
|
rGet.SetTargets(*tgtGet)
|
|
|
|
|
|
|
|
tgtPut := eaclSDK.NewTarget()
|
|
|
|
tgtPut.SetRole(eaclSDK.RoleUnknown)
|
2022-09-08 12:44:27 +00:00
|
|
|
tgtPut.SetBinaryKeys([][]byte{forPutGet.Bytes()})
|
2022-05-05 11:00:30 +00:00
|
|
|
|
|
|
|
rPut := eaclSDK.NewRecord()
|
|
|
|
rPut.SetAction(eaclSDK.ActionAllow)
|
|
|
|
rPut.SetOperation(eaclSDK.OperationPut)
|
|
|
|
rPut.SetTargets(*tgtPut)
|
|
|
|
|
|
|
|
tb := eaclSDK.NewTable()
|
|
|
|
tb.AddRecord(rGet)
|
|
|
|
tb.AddRecord(rPut)
|
|
|
|
|
|
|
|
tgt := eaclSDK.NewTarget()
|
|
|
|
tgt.SetRole(eaclSDK.RoleOthers)
|
|
|
|
|
|
|
|
for _, op := range []eaclSDK.Operation{eaclSDK.OperationGet, eaclSDK.OperationPut} {
|
|
|
|
r := eaclSDK.NewRecord()
|
|
|
|
r.SetAction(eaclSDK.ActionDeny)
|
|
|
|
r.SetTargets(*tgt)
|
|
|
|
r.SetOperation(op)
|
|
|
|
tb.AddRecord(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
tb.SetCID(cid)
|
|
|
|
|
2022-10-25 12:24:06 +00:00
|
|
|
return tb
|
2022-05-05 11:00:30 +00:00
|
|
|
}
|