2020-09-22 15:04:08 +00:00
|
|
|
package headsvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-09-22 15:04:08 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/client"
|
2020-10-21 09:18:36 +00:00
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2020-09-22 15:04:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/network"
|
2020-09-29 16:44:59 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
2020-09-22 15:04:08 +00:00
|
|
|
)
|
|
|
|
|
2021-03-23 18:40:36 +00:00
|
|
|
type ClientConstructor interface {
|
2021-05-20 15:17:16 +00:00
|
|
|
Get(*network.Address) (client.Client, error)
|
2021-03-23 18:40:36 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 09:18:36 +00:00
|
|
|
// RemoteHeader represents utility for getting
|
|
|
|
// the object header from a remote host.
|
|
|
|
type RemoteHeader struct {
|
2020-09-29 16:44:59 +00:00
|
|
|
keyStorage *util.KeyStorage
|
2020-11-18 13:03:00 +00:00
|
|
|
|
2021-03-23 18:40:36 +00:00
|
|
|
clientCache ClientConstructor
|
2020-10-21 09:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteHeadPrm groups remote header operation parameters.
|
|
|
|
type RemoteHeadPrm struct {
|
|
|
|
commonHeadPrm *Prm
|
2020-09-22 15:04:08 +00:00
|
|
|
|
|
|
|
node *network.Address
|
|
|
|
}
|
|
|
|
|
2020-12-29 14:39:33 +00:00
|
|
|
var ErrNotFound = errors.New("object header not found")
|
|
|
|
|
2020-10-21 09:18:36 +00:00
|
|
|
// NewRemoteHeader creates, initializes and returns new RemoteHeader instance.
|
2021-03-23 18:40:36 +00:00
|
|
|
func NewRemoteHeader(keyStorage *util.KeyStorage, cache ClientConstructor) *RemoteHeader {
|
2020-10-21 09:18:36 +00:00
|
|
|
return &RemoteHeader{
|
2020-11-18 13:03:00 +00:00
|
|
|
keyStorage: keyStorage,
|
|
|
|
clientCache: cache,
|
2020-10-21 09:18:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithNodeAddress sets network address of the remote node.
|
|
|
|
func (p *RemoteHeadPrm) WithNodeAddress(v *network.Address) *RemoteHeadPrm {
|
|
|
|
if p != nil {
|
|
|
|
p.node = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithObjectAddress sets object address.
|
|
|
|
func (p *RemoteHeadPrm) WithObjectAddress(v *objectSDK.Address) *RemoteHeadPrm {
|
|
|
|
if p != nil {
|
|
|
|
p.commonHeadPrm = new(Prm).WithAddress(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Head requests object header from the remote node.
|
|
|
|
func (h *RemoteHeader) Head(ctx context.Context, prm *RemoteHeadPrm) (*object.Object, error) {
|
|
|
|
key, err := h.keyStorage.GetKey(prm.commonHeadPrm.common.SessionToken())
|
2020-09-29 16:44:59 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("(%T) could not receive private key: %w", h, err)
|
2020-09-29 16:44:59 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 15:17:16 +00:00
|
|
|
c, err := h.clientCache.Get(prm.node)
|
2020-09-24 07:37:42 +00:00
|
|
|
if err != nil {
|
2021-05-20 15:17:16 +00:00
|
|
|
return nil, fmt.Errorf("(%T) could not create SDK client %s: %w", h, prm.node, err)
|
2020-09-22 15:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p := new(client.ObjectHeaderParams).
|
2020-12-08 12:59:01 +00:00
|
|
|
WithAddress(prm.commonHeadPrm.addr).
|
|
|
|
WithRawFlag(prm.commonHeadPrm.raw)
|
2020-09-22 15:04:08 +00:00
|
|
|
|
2020-10-21 09:18:36 +00:00
|
|
|
if prm.commonHeadPrm.short {
|
2020-09-22 15:04:08 +00:00
|
|
|
p = p.WithMainFields()
|
|
|
|
}
|
|
|
|
|
|
|
|
hdr, err := c.GetObjectHeader(ctx, p,
|
|
|
|
client.WithTTL(1), // FIXME: use constant
|
2020-10-21 09:18:36 +00:00
|
|
|
client.WithSession(prm.commonHeadPrm.common.SessionToken()),
|
|
|
|
client.WithBearer(prm.commonHeadPrm.common.BearerToken()),
|
2021-03-13 15:22:21 +00:00
|
|
|
client.WithKey(key),
|
2020-09-22 15:04:08 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2021-05-20 15:17:16 +00:00
|
|
|
return nil, fmt.Errorf("(%T) could not head object in %s: %w", h, prm.node, err)
|
2020-09-22 15:04:08 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 09:18:36 +00:00
|
|
|
return object.NewFromSDK(hdr), nil
|
2020-09-22 15:04:08 +00:00
|
|
|
}
|