2020-12-18 13:30:13 +00:00
|
|
|
package innerring
|
|
|
|
|
|
|
|
import (
|
2020-12-23 08:44:29 +00:00
|
|
|
"context"
|
2020-12-18 13:30:13 +00:00
|
|
|
"crypto/ecdsa"
|
2020-12-23 08:44:29 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
2020-12-18 13:30:13 +00:00
|
|
|
|
2021-09-28 04:46:10 +00:00
|
|
|
clientcore "github.com/nspcc-dev/neofs-node/pkg/core/client"
|
2020-12-23 08:44:29 +00:00
|
|
|
coreObject "github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2021-10-27 12:12:05 +00:00
|
|
|
neofsapiclient "github.com/nspcc-dev/neofs-node/pkg/innerring/internal/client"
|
|
|
|
auditproc "github.com/nspcc-dev/neofs-node/pkg/innerring/processors/audit"
|
2020-12-18 13:30:13 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network/cache"
|
2020-12-22 12:14:50 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/audit"
|
2020-12-23 08:44:29 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/client"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-01-26 12:11:13 +00:00
|
|
|
addressSDK "github.com/nspcc-dev/neofs-sdk-go/object/address"
|
|
|
|
oidSDK "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/storagegroup"
|
2020-12-23 08:44:29 +00:00
|
|
|
"go.uber.org/zap"
|
2020-12-18 13:30:13 +00:00
|
|
|
)
|
|
|
|
|
2020-12-23 08:44:29 +00:00
|
|
|
type (
|
|
|
|
ClientCache struct {
|
|
|
|
log *zap.Logger
|
2021-09-28 05:39:18 +00:00
|
|
|
cache interface {
|
2022-01-13 15:01:50 +00:00
|
|
|
Get(clientcore.NodeInfo) (clientcore.Client, error)
|
2021-09-28 05:39:18 +00:00
|
|
|
CloseAll()
|
|
|
|
}
|
|
|
|
key *ecdsa.PrivateKey
|
2020-12-23 08:44:29 +00:00
|
|
|
|
|
|
|
sgTimeout, headTimeout, rangeTimeout time.Duration
|
|
|
|
}
|
2020-12-18 13:30:13 +00:00
|
|
|
|
2020-12-23 08:44:29 +00:00
|
|
|
clientCacheParams struct {
|
|
|
|
Log *zap.Logger
|
|
|
|
Key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
SGTimeout, HeadTimeout, RangeTimeout time.Duration
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func newClientCache(p *clientCacheParams) *ClientCache {
|
2020-12-18 13:30:13 +00:00
|
|
|
return &ClientCache{
|
2020-12-23 08:44:29 +00:00
|
|
|
log: p.Log,
|
|
|
|
cache: cache.NewSDKClientCache(),
|
|
|
|
key: p.Key,
|
|
|
|
sgTimeout: p.SGTimeout,
|
|
|
|
headTimeout: p.HeadTimeout,
|
|
|
|
rangeTimeout: p.RangeTimeout,
|
2020-12-18 13:30:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 15:01:50 +00:00
|
|
|
func (c *ClientCache) Get(info clientcore.NodeInfo) (clientcore.Client, error) {
|
2021-03-13 15:22:21 +00:00
|
|
|
// Because cache is used by `ClientCache` exclusively,
|
|
|
|
// client will always have valid key.
|
2021-09-28 04:46:10 +00:00
|
|
|
return c.cache.Get(info)
|
2020-12-18 13:30:13 +00:00
|
|
|
}
|
2020-12-22 12:14:50 +00:00
|
|
|
|
|
|
|
// GetSG polls the container from audit task to get the object by id.
|
|
|
|
// Returns storage groups structure from received object.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (c *ClientCache) GetSG(task *audit.Task, id *oidSDK.ID) (*storagegroup.StorageGroup, error) {
|
|
|
|
sgAddress := new(addressSDK.Address)
|
2020-12-23 08:44:29 +00:00
|
|
|
sgAddress.SetContainerID(task.ContainerID())
|
|
|
|
sgAddress.SetObjectID(id)
|
|
|
|
|
2021-01-26 10:33:28 +00:00
|
|
|
return c.getSG(task.AuditContext(), sgAddress, task.NetworkMap(), task.ContainerNodes())
|
|
|
|
}
|
|
|
|
|
2022-01-26 12:11:13 +00:00
|
|
|
func (c *ClientCache) getSG(ctx context.Context, addr *addressSDK.Address, nm *netmap.Netmap, cn netmap.ContainerNodes) (*storagegroup.StorageGroup, error) {
|
2021-01-26 10:33:28 +00:00
|
|
|
nodes, err := placement.BuildObjectPlacement(nm, cn, addr.ObjectID())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't build object placement: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-12-23 08:44:29 +00:00
|
|
|
getParams := new(client.GetObjectParams)
|
2021-01-26 10:33:28 +00:00
|
|
|
getParams.WithAddress(addr)
|
2020-12-23 08:44:29 +00:00
|
|
|
|
2021-09-28 05:32:30 +00:00
|
|
|
var info clientcore.NodeInfo
|
2021-06-18 06:00:21 +00:00
|
|
|
|
2021-09-28 05:32:30 +00:00
|
|
|
for _, node := range placement.FlattenNodes(nodes) {
|
|
|
|
err := clientcore.NodeInfoFromRawNetmapElement(&info, node)
|
2020-12-23 08:44:29 +00:00
|
|
|
if err != nil {
|
2021-09-28 05:32:30 +00:00
|
|
|
return nil, fmt.Errorf("parse client node info: %w", err)
|
2020-12-23 08:44:29 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 12:12:05 +00:00
|
|
|
cli, err := c.getWrappedClient(info)
|
2020-12-23 08:44:29 +00:00
|
|
|
if err != nil {
|
|
|
|
c.log.Warn("can't setup remote connection",
|
|
|
|
zap.String("error", err.Error()))
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-01-26 10:33:28 +00:00
|
|
|
cctx, cancel := context.WithTimeout(ctx, c.sgTimeout)
|
2021-10-27 12:12:05 +00:00
|
|
|
|
|
|
|
// NOTE: we use the function which does not verify object integrity (checksums, signature),
|
|
|
|
// but it would be useful to do as part of a data audit.
|
|
|
|
payload, err := neofsapiclient.GetObjectPayload(cctx, cli, addr)
|
2020-12-23 09:54:34 +00:00
|
|
|
|
2020-12-23 08:44:29 +00:00
|
|
|
cancel()
|
|
|
|
|
|
|
|
if err != nil {
|
2021-10-27 12:12:05 +00:00
|
|
|
c.log.Warn("can't get payload of storage group object",
|
2020-12-23 08:44:29 +00:00
|
|
|
zap.String("error", err.Error()))
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
sg := storagegroup.New()
|
|
|
|
|
2021-10-27 12:12:05 +00:00
|
|
|
err = sg.Unmarshal(payload)
|
2020-12-23 08:44:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't parse storage group payload: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, coreObject.ErrNotFound
|
2020-12-22 12:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetHeader requests node from the container under audit to return object header by id.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (c *ClientCache) GetHeader(task *audit.Task, node *netmap.Node, id *oidSDK.ID, relay bool) (*object.Object, error) {
|
|
|
|
objAddress := new(addressSDK.Address)
|
2020-12-23 08:44:29 +00:00
|
|
|
objAddress.SetContainerID(task.ContainerID())
|
|
|
|
objAddress.SetObjectID(id)
|
|
|
|
|
2021-09-28 05:32:30 +00:00
|
|
|
var info clientcore.NodeInfo
|
2021-06-18 06:00:21 +00:00
|
|
|
|
2021-09-28 05:32:30 +00:00
|
|
|
err := clientcore.NodeInfoFromRawNetmapElement(&info, node)
|
2020-12-23 08:44:29 +00:00
|
|
|
if err != nil {
|
2021-09-28 05:32:30 +00:00
|
|
|
return nil, fmt.Errorf("parse client node info: %w", err)
|
2020-12-23 08:44:29 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 12:12:05 +00:00
|
|
|
cli, err := c.getWrappedClient(info)
|
2020-12-23 08:44:29 +00:00
|
|
|
if err != nil {
|
2021-09-28 05:32:30 +00:00
|
|
|
return nil, fmt.Errorf("can't setup remote connection with %s: %w", info.AddressGroup(), err)
|
2020-12-23 08:44:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cctx, cancel := context.WithTimeout(task.AuditContext(), c.headTimeout)
|
2021-10-27 12:12:05 +00:00
|
|
|
|
|
|
|
var obj *object.Object
|
|
|
|
|
|
|
|
if relay {
|
|
|
|
// todo: function sets hardcoded TTL value, but instead we can set TTL based on container length
|
|
|
|
obj, err = neofsapiclient.GetObjectHeaderFromContainer(cctx, cli, objAddress)
|
|
|
|
} else {
|
|
|
|
obj, err = neofsapiclient.GetRawObjectHeaderLocally(cctx, cli, objAddress)
|
|
|
|
}
|
2020-12-23 09:54:34 +00:00
|
|
|
|
2020-12-23 08:44:29 +00:00
|
|
|
cancel()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("object head error: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-10-27 12:12:05 +00:00
|
|
|
return obj, nil
|
2020-12-22 12:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRangeHash requests node from the container under audit to return Tillich-Zemor hash of the
|
|
|
|
// payload range of the object with specified identifier.
|
2022-01-26 12:11:13 +00:00
|
|
|
func (c *ClientCache) GetRangeHash(task *audit.Task, node *netmap.Node, id *oidSDK.ID, rng *object.Range) ([]byte, error) {
|
|
|
|
objAddress := new(addressSDK.Address)
|
2020-12-23 14:11:14 +00:00
|
|
|
objAddress.SetContainerID(task.ContainerID())
|
|
|
|
objAddress.SetObjectID(id)
|
|
|
|
|
2021-09-28 05:32:30 +00:00
|
|
|
var info clientcore.NodeInfo
|
2021-06-18 06:00:21 +00:00
|
|
|
|
2021-09-28 05:32:30 +00:00
|
|
|
err := clientcore.NodeInfoFromRawNetmapElement(&info, node)
|
2020-12-23 14:11:14 +00:00
|
|
|
if err != nil {
|
2021-09-28 05:32:30 +00:00
|
|
|
return nil, fmt.Errorf("parse client node info: %w", err)
|
2020-12-23 14:11:14 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 12:12:05 +00:00
|
|
|
cli, err := c.getWrappedClient(info)
|
2020-12-23 14:11:14 +00:00
|
|
|
if err != nil {
|
2021-09-28 05:32:30 +00:00
|
|
|
return nil, fmt.Errorf("can't setup remote connection with %s: %w", info.AddressGroup(), err)
|
2020-12-23 14:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cctx, cancel := context.WithTimeout(task.AuditContext(), c.rangeTimeout)
|
2021-10-27 12:12:05 +00:00
|
|
|
|
|
|
|
h, err := neofsapiclient.HashObjectRange(cctx, cli, objAddress, rng)
|
2020-12-23 14:11:14 +00:00
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("object rangehash error: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-10-27 12:12:05 +00:00
|
|
|
return h, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientCache) getWrappedClient(info clientcore.NodeInfo) (neofsapiclient.Client, error) {
|
|
|
|
// can be also cached
|
|
|
|
var cInternal neofsapiclient.Client
|
|
|
|
|
|
|
|
cli, err := c.Get(info)
|
|
|
|
if err != nil {
|
|
|
|
return cInternal, fmt.Errorf("could not get API client from cache")
|
|
|
|
}
|
|
|
|
|
|
|
|
cInternal.WrapBasicClient(cli)
|
|
|
|
cInternal.SetPrivateKey(c.key)
|
|
|
|
|
|
|
|
return cInternal, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c ClientCache) ListSG(dst *auditproc.SearchSGDst, prm auditproc.SearchSGPrm) error {
|
|
|
|
cli, err := c.getWrappedClient(prm.NodeInfo())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not get API client from cache")
|
|
|
|
}
|
|
|
|
|
|
|
|
var cliPrm neofsapiclient.SearchSGPrm
|
|
|
|
|
|
|
|
cliPrm.SetContext(prm.Context())
|
|
|
|
cliPrm.SetContainerID(prm.CID())
|
|
|
|
|
|
|
|
res, err := cli.SearchSG(cliPrm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dst.WriteIDList(res.IDList())
|
2020-12-23 14:11:14 +00:00
|
|
|
|
2021-10-27 12:12:05 +00:00
|
|
|
return nil
|
2020-12-22 12:14:50 +00:00
|
|
|
}
|