2020-09-04 10:05:57 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/sha256"
|
2021-05-17 12:56:17 +00:00
|
|
|
"errors"
|
2020-09-04 10:05:57 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2021-05-31 12:09:04 +00:00
|
|
|
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
|
2020-09-04 10:05:57 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/object"
|
2021-03-12 13:07:52 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/rpc/client"
|
2020-09-15 10:51:36 +00:00
|
|
|
signer "github.com/nspcc-dev/neofs-api-go/util/signature"
|
2020-09-04 10:05:57 +00:00
|
|
|
v2object "github.com/nspcc-dev/neofs-api-go/v2/object"
|
|
|
|
v2refs "github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2021-03-12 13:07:52 +00:00
|
|
|
rpcapi "github.com/nspcc-dev/neofs-api-go/v2/rpc"
|
2020-09-15 10:51:36 +00:00
|
|
|
v2session "github.com/nspcc-dev/neofs-api-go/v2/session"
|
2020-09-04 10:05:57 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/signature"
|
|
|
|
)
|
|
|
|
|
2021-03-12 15:02:26 +00:00
|
|
|
// Object contains methods for working with objects.
|
|
|
|
type Object interface {
|
|
|
|
// PutObject puts new object to NeoFS.
|
|
|
|
PutObject(context.Context, *PutObjectParams, ...CallOption) (*object.ID, error)
|
2021-03-17 10:59:45 +00:00
|
|
|
|
2021-03-12 15:02:26 +00:00
|
|
|
// DeleteObject deletes object to NeoFS.
|
|
|
|
DeleteObject(context.Context, *DeleteObjectParams, ...CallOption) error
|
2021-03-17 10:59:45 +00:00
|
|
|
|
2021-03-12 15:02:26 +00:00
|
|
|
// GetObject returns object stored in NeoFS.
|
|
|
|
GetObject(context.Context, *GetObjectParams, ...CallOption) (*object.Object, error)
|
2021-03-17 10:59:45 +00:00
|
|
|
|
2021-03-12 15:02:26 +00:00
|
|
|
// GetObjectHeader returns object header.
|
|
|
|
GetObjectHeader(context.Context, *ObjectHeaderParams, ...CallOption) (*object.Object, error)
|
2021-03-17 10:59:45 +00:00
|
|
|
|
2021-03-12 15:02:26 +00:00
|
|
|
// ObjectPayloadRangeData returns range of object payload.
|
|
|
|
ObjectPayloadRangeData(context.Context, *RangeDataParams, ...CallOption) ([]byte, error)
|
2021-03-17 10:59:45 +00:00
|
|
|
|
2021-03-12 15:02:26 +00:00
|
|
|
// ObjectPayloadRangeSHA256 returns sha-256 hashes of object sub-ranges from NeoFS.
|
|
|
|
ObjectPayloadRangeSHA256(context.Context, *RangeChecksumParams, ...CallOption) ([][sha256.Size]byte, error)
|
2021-03-17 10:59:45 +00:00
|
|
|
|
2021-03-12 15:02:26 +00:00
|
|
|
// ObjectPayloadRangeTZ returns homomorphic hashes of object sub-ranges from NeoFS.
|
|
|
|
ObjectPayloadRangeTZ(context.Context, *RangeChecksumParams, ...CallOption) ([][TZSize]byte, error)
|
2021-03-17 10:59:45 +00:00
|
|
|
|
2021-03-12 15:02:26 +00:00
|
|
|
// SearchObject searches for objects in NeoFS using provided parameters.
|
|
|
|
SearchObject(context.Context, *SearchObjectParams, ...CallOption) ([]*object.ID, error)
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
type PutObjectParams struct {
|
|
|
|
obj *object.Object
|
|
|
|
|
|
|
|
r io.Reader
|
|
|
|
}
|
|
|
|
|
2020-12-10 09:25:13 +00:00
|
|
|
// ObjectAddressWriter is an interface of the
|
|
|
|
// component that writes the object address.
|
|
|
|
type ObjectAddressWriter interface {
|
|
|
|
SetAddress(*object.Address)
|
|
|
|
}
|
|
|
|
|
|
|
|
type objectAddressWriter struct {
|
|
|
|
addr *object.Address
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
type DeleteObjectParams struct {
|
|
|
|
addr *object.Address
|
2020-12-10 09:25:13 +00:00
|
|
|
|
|
|
|
tombTgt ObjectAddressWriter
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type GetObjectParams struct {
|
|
|
|
addr *object.Address
|
|
|
|
|
2020-12-02 11:30:50 +00:00
|
|
|
raw bool
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
w io.Writer
|
2021-06-22 09:43:28 +00:00
|
|
|
|
|
|
|
readerHandler ReaderHandler
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ObjectHeaderParams struct {
|
|
|
|
addr *object.Address
|
|
|
|
|
2020-12-02 11:32:18 +00:00
|
|
|
raw bool
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
short bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type RangeDataParams struct {
|
|
|
|
addr *object.Address
|
|
|
|
|
2020-12-07 16:06:11 +00:00
|
|
|
raw bool
|
|
|
|
|
2020-09-25 12:51:34 +00:00
|
|
|
r *object.Range
|
2020-09-04 10:05:57 +00:00
|
|
|
|
|
|
|
w io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
type RangeChecksumParams struct {
|
|
|
|
typ checksumType
|
|
|
|
|
|
|
|
addr *object.Address
|
|
|
|
|
2020-09-25 12:51:34 +00:00
|
|
|
rs []*object.Range
|
2020-09-04 10:05:57 +00:00
|
|
|
|
|
|
|
salt []byte
|
|
|
|
}
|
|
|
|
|
2020-09-17 10:12:48 +00:00
|
|
|
type SearchObjectParams struct {
|
2021-05-31 12:09:04 +00:00
|
|
|
cid *cid.ID
|
2020-09-17 10:12:48 +00:00
|
|
|
|
|
|
|
filters object.SearchFilters
|
|
|
|
}
|
|
|
|
|
2021-01-25 07:59:08 +00:00
|
|
|
type putObjectV2Reader struct {
|
|
|
|
r io.Reader
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
type putObjectV2Writer struct {
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
chunkPart *v2object.PutObjectPartChunk
|
|
|
|
|
|
|
|
req *v2object.PutRequest
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
stream *rpcapi.PutRequestWriter
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type checksumType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
_ checksumType = iota
|
|
|
|
checksumSHA256
|
|
|
|
checksumTZ
|
|
|
|
)
|
|
|
|
|
|
|
|
const chunkSize = 3 * (1 << 20)
|
|
|
|
|
|
|
|
const TZSize = 64
|
|
|
|
|
2020-09-17 10:12:48 +00:00
|
|
|
const searchQueryVersion uint32 = 1
|
|
|
|
|
2020-11-16 15:10:51 +00:00
|
|
|
var errNilObjectPart = errors.New("received nil object part")
|
|
|
|
|
2020-12-11 08:19:45 +00:00
|
|
|
func (w *objectAddressWriter) SetAddress(addr *object.Address) {
|
2020-12-10 09:25:13 +00:00
|
|
|
w.addr = addr
|
|
|
|
}
|
|
|
|
|
2020-09-25 12:51:34 +00:00
|
|
|
func rangesToV2(rs []*object.Range) []*v2object.Range {
|
2020-09-04 10:05:57 +00:00
|
|
|
r2 := make([]*v2object.Range, 0, len(rs))
|
|
|
|
|
|
|
|
for i := range rs {
|
2020-09-25 12:51:34 +00:00
|
|
|
r2 = append(r2, rs[i].ToV2())
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return r2
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t checksumType) toV2() v2refs.ChecksumType {
|
|
|
|
switch t {
|
|
|
|
case checksumSHA256:
|
|
|
|
return v2refs.SHA256
|
|
|
|
case checksumTZ:
|
|
|
|
return v2refs.TillichZemor
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("invalid checksum type %d", t))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 07:59:08 +00:00
|
|
|
func (w *putObjectV2Reader) Read(p []byte) (int, error) {
|
|
|
|
return w.r.Read(p)
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
func (w *putObjectV2Writer) Write(p []byte) (int, error) {
|
|
|
|
w.chunkPart.SetChunk(p)
|
|
|
|
|
|
|
|
w.req.SetVerificationHeader(nil)
|
|
|
|
|
|
|
|
if err := signature.SignServiceMessage(w.key, w.req); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return 0, fmt.Errorf("could not sign chunk request message: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
if err := w.stream.Write(w.req); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return 0, fmt.Errorf("could not send chunk request message: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PutObjectParams) WithObject(v *object.Object) *PutObjectParams {
|
|
|
|
if p != nil {
|
|
|
|
p.obj = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:36:57 +00:00
|
|
|
func (p *PutObjectParams) Object() *object.Object {
|
|
|
|
if p != nil {
|
|
|
|
return p.obj
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
func (p *PutObjectParams) WithPayloadReader(v io.Reader) *PutObjectParams {
|
|
|
|
if p != nil {
|
|
|
|
p.r = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:36:57 +00:00
|
|
|
func (p *PutObjectParams) PayloadReader() io.Reader {
|
|
|
|
if p != nil {
|
|
|
|
return p.r
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-12 15:02:26 +00:00
|
|
|
func (c *clientImpl) PutObject(ctx context.Context, p *PutObjectParams, opts ...CallOption) (*object.ID, error) {
|
2020-10-27 12:54:02 +00:00
|
|
|
callOpts := c.defaultCallOptions()
|
2020-09-04 10:05:57 +00:00
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
if opts[i] != nil {
|
2021-03-12 13:07:52 +00:00
|
|
|
opts[i](callOpts)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create request
|
|
|
|
req := new(v2object.PutRequest)
|
|
|
|
|
|
|
|
// initialize request body
|
|
|
|
body := new(v2object.PutRequestBody)
|
|
|
|
req.SetBody(body)
|
|
|
|
|
2020-09-15 10:51:36 +00:00
|
|
|
v2Addr := new(v2refs.Address)
|
2020-11-16 08:48:38 +00:00
|
|
|
v2Addr.SetObjectID(p.obj.ID().ToV2())
|
|
|
|
v2Addr.SetContainerID(p.obj.ContainerID().ToV2())
|
2020-09-15 10:51:36 +00:00
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
// set meta header
|
2020-09-15 10:51:36 +00:00
|
|
|
meta := v2MetaHeaderFromOpts(callOpts)
|
2021-03-12 13:07:52 +00:00
|
|
|
|
|
|
|
if err := c.attachV2SessionToken(callOpts, meta, v2SessionReqInfo{
|
2020-09-15 10:51:36 +00:00
|
|
|
addr: v2Addr,
|
|
|
|
verb: v2session.ObjectVerbPut,
|
|
|
|
}); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("could not attach session token: %w", err)
|
2020-09-15 10:51:36 +00:00
|
|
|
}
|
2020-11-16 15:10:51 +00:00
|
|
|
|
2020-09-15 10:51:36 +00:00
|
|
|
req.SetMetaHeader(meta)
|
2020-09-04 10:05:57 +00:00
|
|
|
|
|
|
|
// initialize init part
|
|
|
|
initPart := new(v2object.PutObjectPartInit)
|
|
|
|
body.SetObjectPart(initPart)
|
|
|
|
|
2020-09-11 15:32:09 +00:00
|
|
|
obj := p.obj.ToV2()
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
// set init part fields
|
|
|
|
initPart.SetObjectID(obj.GetObjectID())
|
|
|
|
initPart.SetSignature(obj.GetSignature())
|
|
|
|
initPart.SetHeader(obj.GetHeader())
|
|
|
|
|
|
|
|
// sign the request
|
2021-03-10 12:15:54 +00:00
|
|
|
if err := signature.SignServiceMessage(callOpts.key, req); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("signing the request failed: %w", err)
|
2021-03-12 13:07:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// open stream
|
|
|
|
resp := new(v2object.PutResponse)
|
|
|
|
|
|
|
|
stream, err := rpcapi.PutObject(c.Raw(), resp, client.WithContext(ctx))
|
|
|
|
if err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("stream opening failed: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// send init part
|
2021-03-12 13:07:52 +00:00
|
|
|
err = stream.Write(req)
|
|
|
|
if err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("sending the initial message to stream failed: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create payload bytes reader
|
|
|
|
var rPayload io.Reader = bytes.NewReader(obj.GetPayload())
|
|
|
|
if p.r != nil {
|
|
|
|
rPayload = io.MultiReader(rPayload, p.r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create v2 payload stream writer
|
|
|
|
chunkPart := new(v2object.PutObjectPartChunk)
|
|
|
|
body.SetObjectPart(chunkPart)
|
|
|
|
|
|
|
|
w := &putObjectV2Writer{
|
2021-03-10 12:15:54 +00:00
|
|
|
key: callOpts.key,
|
2020-09-04 10:05:57 +00:00
|
|
|
chunkPart: chunkPart,
|
|
|
|
req: req,
|
|
|
|
stream: stream,
|
|
|
|
}
|
|
|
|
|
2021-01-25 07:59:08 +00:00
|
|
|
r := &putObjectV2Reader{r: rPayload}
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
// copy payload from reader to stream writer
|
2021-01-25 07:59:08 +00:00
|
|
|
_, err = io.CopyBuffer(w, r, make([]byte, chunkSize))
|
2021-05-17 12:56:17 +00:00
|
|
|
if err != nil && !errors.Is(err, io.EOF) {
|
|
|
|
return nil, fmt.Errorf("payload streaming failed: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// close object stream and receive response from remote node
|
2021-03-12 13:07:52 +00:00
|
|
|
err = stream.Close()
|
2020-09-04 10:05:57 +00:00
|
|
|
if err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("closing the stream failed: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 11:42:16 +00:00
|
|
|
// handle response meta info
|
|
|
|
if err := c.handleResponseInfoV2(callOpts, resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
// verify response structure
|
|
|
|
if err := signature.VerifyServiceMessage(resp); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("response verification failed: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// convert object identifier
|
2020-09-10 09:57:29 +00:00
|
|
|
id := object.NewIDFromV2(resp.GetBody().GetObjectID())
|
2020-09-04 10:05:57 +00:00
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *DeleteObjectParams) WithAddress(v *object.Address) *DeleteObjectParams {
|
|
|
|
if p != nil {
|
|
|
|
p.addr = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:36:57 +00:00
|
|
|
func (p *DeleteObjectParams) Address() *object.Address {
|
|
|
|
if p != nil {
|
|
|
|
return p.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-10 09:25:13 +00:00
|
|
|
// WithTombstoneAddressTarget sets target component to write tombstone address.
|
|
|
|
func (p *DeleteObjectParams) WithTombstoneAddressTarget(v ObjectAddressWriter) *DeleteObjectParams {
|
|
|
|
if p != nil {
|
|
|
|
p.tombTgt = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// TombstoneAddressTarget returns target component to write tombstone address.
|
|
|
|
func (p *DeleteObjectParams) TombstoneAddressTarget() ObjectAddressWriter {
|
|
|
|
if p != nil {
|
|
|
|
return p.tombTgt
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteObject is a wrapper over Client.DeleteObject method
|
|
|
|
// that provides the ability to receive tombstone address
|
|
|
|
// without setting a target in the parameters.
|
2021-03-12 15:02:26 +00:00
|
|
|
func DeleteObject(ctx context.Context, c Client, p *DeleteObjectParams, opts ...CallOption) (*object.Address, error) {
|
2020-12-10 09:25:13 +00:00
|
|
|
w := new(objectAddressWriter)
|
|
|
|
|
|
|
|
err := c.DeleteObject(ctx, p.WithTombstoneAddressTarget(w), opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.addr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteObject removes object by address.
|
|
|
|
//
|
|
|
|
// If target of tombstone address is not set, the address is ignored.
|
2021-03-12 15:02:26 +00:00
|
|
|
func (c *clientImpl) DeleteObject(ctx context.Context, p *DeleteObjectParams, opts ...CallOption) error {
|
2020-10-27 12:54:02 +00:00
|
|
|
callOpts := c.defaultCallOptions()
|
2020-09-04 10:05:57 +00:00
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
if opts[i] != nil {
|
2021-03-12 13:07:52 +00:00
|
|
|
opts[i](callOpts)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create request
|
|
|
|
req := new(v2object.DeleteRequest)
|
|
|
|
|
|
|
|
// initialize request body
|
|
|
|
body := new(v2object.DeleteRequestBody)
|
|
|
|
req.SetBody(body)
|
|
|
|
|
|
|
|
// set meta header
|
2020-09-15 10:51:36 +00:00
|
|
|
meta := v2MetaHeaderFromOpts(callOpts)
|
2021-03-12 13:07:52 +00:00
|
|
|
|
|
|
|
if err := c.attachV2SessionToken(callOpts, meta, v2SessionReqInfo{
|
2020-09-15 10:51:36 +00:00
|
|
|
addr: p.addr.ToV2(),
|
|
|
|
verb: v2session.ObjectVerbDelete,
|
|
|
|
}); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return fmt.Errorf("could not attach session token: %w", err)
|
2020-09-15 10:51:36 +00:00
|
|
|
}
|
2020-11-16 15:10:51 +00:00
|
|
|
|
2020-09-15 10:51:36 +00:00
|
|
|
req.SetMetaHeader(meta)
|
2020-09-04 10:05:57 +00:00
|
|
|
|
|
|
|
// fill body fields
|
|
|
|
body.SetAddress(p.addr.ToV2())
|
|
|
|
|
|
|
|
// sign the request
|
2021-03-10 12:15:54 +00:00
|
|
|
if err := signature.SignServiceMessage(callOpts.key, req); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return fmt.Errorf("signing the request failed: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// send request
|
2021-03-12 13:07:52 +00:00
|
|
|
resp, err := rpcapi.DeleteObject(c.Raw(), req, client.WithContext(ctx))
|
2020-09-04 10:05:57 +00:00
|
|
|
if err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return fmt.Errorf("sending the request failed: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 11:42:16 +00:00
|
|
|
// handle response meta info
|
|
|
|
if err := c.handleResponseInfoV2(callOpts, resp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
// verify response structure
|
|
|
|
if err := signature.VerifyServiceMessage(resp); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return fmt.Errorf("response verification failed: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
if p.tombTgt != nil {
|
|
|
|
p.tombTgt.SetAddress(object.NewAddressFromV2(resp.GetBody().GetTombstone()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GetObjectParams) WithAddress(v *object.Address) *GetObjectParams {
|
|
|
|
if p != nil {
|
|
|
|
p.addr = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:36:57 +00:00
|
|
|
func (p *GetObjectParams) Address() *object.Address {
|
|
|
|
if p != nil {
|
|
|
|
return p.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
func (p *GetObjectParams) WithPayloadWriter(w io.Writer) *GetObjectParams {
|
|
|
|
if p != nil {
|
|
|
|
p.w = w
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:36:57 +00:00
|
|
|
func (p *GetObjectParams) PayloadWriter() io.Writer {
|
|
|
|
if p != nil {
|
|
|
|
return p.w
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-02 11:30:50 +00:00
|
|
|
func (p *GetObjectParams) WithRawFlag(v bool) *GetObjectParams {
|
|
|
|
if p != nil {
|
|
|
|
p.raw = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:36:57 +00:00
|
|
|
func (p *GetObjectParams) RawFlag() bool {
|
|
|
|
if p != nil {
|
|
|
|
return p.raw
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-06-22 09:43:28 +00:00
|
|
|
// ReaderHandler is a function over io.Reader.
|
|
|
|
type ReaderHandler func(io.Reader)
|
|
|
|
|
|
|
|
// WithPayloadReaderHandler sets handler of the payload reader.
|
|
|
|
//
|
|
|
|
// If provided, payload reader is composed after receiving the header.
|
|
|
|
// In this case payload writer set via WithPayloadWriter is ignored.
|
|
|
|
//
|
|
|
|
// Handler should not be nil.
|
|
|
|
func (p *GetObjectParams) WithPayloadReaderHandler(f ReaderHandler) *GetObjectParams {
|
|
|
|
if p != nil {
|
|
|
|
p.readerHandler = f
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrapper over the Object Get stream that provides io.Reader.
|
|
|
|
type objectPayloadReader struct {
|
2021-06-23 11:15:58 +00:00
|
|
|
stream interface {
|
|
|
|
Read(*v2object.GetResponse) error
|
|
|
|
}
|
2021-06-22 09:43:28 +00:00
|
|
|
|
|
|
|
resp v2object.GetResponse
|
|
|
|
|
|
|
|
tail []byte
|
|
|
|
}
|
|
|
|
|
2021-06-23 11:15:58 +00:00
|
|
|
func (x *objectPayloadReader) Read(p []byte) (read int, err error) {
|
2021-06-22 09:43:28 +00:00
|
|
|
// read remaining tail
|
2021-06-23 11:15:58 +00:00
|
|
|
read = copy(p, x.tail)
|
2021-06-22 09:43:28 +00:00
|
|
|
|
|
|
|
x.tail = x.tail[read:]
|
|
|
|
|
|
|
|
if len(p)-read == 0 {
|
2021-06-23 11:15:58 +00:00
|
|
|
return
|
2021-06-22 09:43:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// receive message from server stream
|
2021-06-23 11:15:58 +00:00
|
|
|
err = x.stream.Read(&x.resp)
|
2021-06-22 09:43:28 +00:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, io.EOF) {
|
2021-06-23 11:15:58 +00:00
|
|
|
err = io.EOF
|
|
|
|
return
|
2021-06-22 09:43:28 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 11:15:58 +00:00
|
|
|
err = fmt.Errorf("reading the response failed: %w", err)
|
|
|
|
return
|
2021-06-22 09:43:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get chunk part message
|
|
|
|
part := x.resp.GetBody().GetObjectPart()
|
|
|
|
|
|
|
|
chunkPart, ok := part.(*v2object.GetObjectPartChunk)
|
|
|
|
if !ok {
|
2021-06-23 11:15:58 +00:00
|
|
|
err = errWrongMessageSeq
|
|
|
|
return
|
2021-06-22 09:43:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// verify response structure
|
2021-06-23 11:15:58 +00:00
|
|
|
if err = signature.VerifyServiceMessage(&x.resp); err != nil {
|
|
|
|
err = fmt.Errorf("response verification failed: %w", err)
|
|
|
|
return
|
2021-06-22 09:43:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// read new chunk
|
|
|
|
chunk := chunkPart.GetChunk()
|
|
|
|
|
|
|
|
tailOffset := copy(p[read:], chunk)
|
|
|
|
|
|
|
|
read += tailOffset
|
|
|
|
|
|
|
|
// save the tail
|
|
|
|
x.tail = append(x.tail, chunk[tailOffset:]...)
|
|
|
|
|
2021-06-23 11:15:58 +00:00
|
|
|
return
|
2021-06-22 09:43:28 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
var errWrongMessageSeq = errors.New("incorrect message sequence")
|
2020-09-04 10:05:57 +00:00
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
func (c *clientImpl) GetObject(ctx context.Context, p *GetObjectParams, opts ...CallOption) (*object.Object, error) {
|
2020-10-27 12:54:02 +00:00
|
|
|
callOpts := c.defaultCallOptions()
|
2020-09-04 10:05:57 +00:00
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
if opts[i] != nil {
|
2021-03-12 13:07:52 +00:00
|
|
|
opts[i](callOpts)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create request
|
|
|
|
req := new(v2object.GetRequest)
|
|
|
|
|
|
|
|
// initialize request body
|
|
|
|
body := new(v2object.GetRequestBody)
|
|
|
|
req.SetBody(body)
|
|
|
|
|
|
|
|
// set meta header
|
2020-09-15 10:51:36 +00:00
|
|
|
meta := v2MetaHeaderFromOpts(callOpts)
|
2021-03-12 13:07:52 +00:00
|
|
|
|
|
|
|
if err := c.attachV2SessionToken(callOpts, meta, v2SessionReqInfo{
|
2020-09-15 10:51:36 +00:00
|
|
|
addr: p.addr.ToV2(),
|
|
|
|
verb: v2session.ObjectVerbGet,
|
|
|
|
}); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("could not attach session token: %w", err)
|
2020-09-15 10:51:36 +00:00
|
|
|
}
|
2020-11-16 15:10:51 +00:00
|
|
|
|
2020-09-15 10:51:36 +00:00
|
|
|
req.SetMetaHeader(meta)
|
2020-09-04 10:05:57 +00:00
|
|
|
|
|
|
|
// fill body fields
|
|
|
|
body.SetAddress(p.addr.ToV2())
|
2020-12-02 11:30:50 +00:00
|
|
|
body.SetRaw(p.raw)
|
2020-09-04 10:05:57 +00:00
|
|
|
|
|
|
|
// sign the request
|
2021-03-10 12:15:54 +00:00
|
|
|
if err := signature.SignServiceMessage(callOpts.key, req); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("signing the request failed: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
// open stream
|
|
|
|
stream, err := rpcapi.GetObject(c.Raw(), req, client.WithContext(ctx))
|
2020-09-04 10:05:57 +00:00
|
|
|
if err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("stream opening failed: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2021-03-12 13:07:52 +00:00
|
|
|
headWas bool
|
2020-09-04 10:05:57 +00:00
|
|
|
payload []byte
|
|
|
|
obj = new(v2object.Object)
|
2021-03-12 13:07:52 +00:00
|
|
|
resp = new(v2object.GetResponse)
|
2020-09-04 10:05:57 +00:00
|
|
|
)
|
|
|
|
|
2021-06-22 09:43:28 +00:00
|
|
|
loop:
|
2020-09-04 10:05:57 +00:00
|
|
|
for {
|
|
|
|
// receive message from server stream
|
2021-03-12 13:07:52 +00:00
|
|
|
err := stream.Read(resp)
|
2020-09-04 10:05:57 +00:00
|
|
|
if err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
2021-03-12 13:07:52 +00:00
|
|
|
if !headWas {
|
|
|
|
return nil, io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("reading the response failed: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 11:42:16 +00:00
|
|
|
// handle response meta info
|
|
|
|
if err := c.handleResponseInfoV2(callOpts, resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
// verify response structure
|
|
|
|
if err := signature.VerifyServiceMessage(resp); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("response verification failed: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch v := resp.GetBody().GetObjectPart().(type) {
|
2021-03-12 13:07:52 +00:00
|
|
|
default:
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("unexpected object part %T", v)
|
2020-09-04 10:05:57 +00:00
|
|
|
case *v2object.GetObjectPartInit:
|
2021-03-12 13:07:52 +00:00
|
|
|
if headWas {
|
|
|
|
return nil, errWrongMessageSeq
|
|
|
|
}
|
|
|
|
|
|
|
|
headWas = true
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
obj.SetObjectID(v.GetObjectID())
|
|
|
|
obj.SetSignature(v.GetSignature())
|
|
|
|
|
|
|
|
hdr := v.GetHeader()
|
|
|
|
obj.SetHeader(hdr)
|
|
|
|
|
2021-06-22 09:43:28 +00:00
|
|
|
if p.readerHandler != nil {
|
|
|
|
p.readerHandler(&objectPayloadReader{
|
|
|
|
stream: stream,
|
|
|
|
})
|
|
|
|
|
|
|
|
break loop
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
if p.w == nil {
|
|
|
|
payload = make([]byte, 0, hdr.GetPayloadLength())
|
|
|
|
}
|
|
|
|
case *v2object.GetObjectPartChunk:
|
2021-03-12 13:07:52 +00:00
|
|
|
if !headWas {
|
|
|
|
return nil, errWrongMessageSeq
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
if p.w != nil {
|
|
|
|
if _, err := p.w.Write(v.GetChunk()); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("could not write payload chunk: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
payload = append(payload, v.GetChunk()...)
|
|
|
|
}
|
2020-12-02 11:25:25 +00:00
|
|
|
case *v2object.SplitInfo:
|
|
|
|
si := object.NewSplitInfoFromV2(v)
|
|
|
|
return nil, object.NewSplitInfoError(si)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.SetPayload(payload)
|
|
|
|
|
|
|
|
// convert the object
|
2020-09-11 15:32:09 +00:00
|
|
|
return object.NewFromV2(obj), nil
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ObjectHeaderParams) WithAddress(v *object.Address) *ObjectHeaderParams {
|
|
|
|
if p != nil {
|
|
|
|
p.addr = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:36:57 +00:00
|
|
|
func (p *ObjectHeaderParams) Address() *object.Address {
|
|
|
|
if p != nil {
|
|
|
|
return p.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
func (p *ObjectHeaderParams) WithAllFields() *ObjectHeaderParams {
|
|
|
|
if p != nil {
|
|
|
|
p.short = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:36:57 +00:00
|
|
|
// AllFields return true if parameter set to return all header fields, returns
|
|
|
|
// false if parameter set to return only main fields of header.
|
|
|
|
func (p *ObjectHeaderParams) AllFields() bool {
|
|
|
|
if p != nil {
|
|
|
|
return !p.short
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
func (p *ObjectHeaderParams) WithMainFields() *ObjectHeaderParams {
|
|
|
|
if p != nil {
|
|
|
|
p.short = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-02 11:32:18 +00:00
|
|
|
func (p *ObjectHeaderParams) WithRawFlag(v bool) *ObjectHeaderParams {
|
|
|
|
if p != nil {
|
|
|
|
p.raw = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:36:57 +00:00
|
|
|
func (p *ObjectHeaderParams) RawFlag() bool {
|
|
|
|
if p != nil {
|
|
|
|
return p.raw
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-03-12 15:02:26 +00:00
|
|
|
func (c *clientImpl) GetObjectHeader(ctx context.Context, p *ObjectHeaderParams, opts ...CallOption) (*object.Object, error) {
|
2020-10-27 12:54:02 +00:00
|
|
|
callOpts := c.defaultCallOptions()
|
2020-09-04 10:05:57 +00:00
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
if opts[i] != nil {
|
2021-03-12 13:07:52 +00:00
|
|
|
opts[i](callOpts)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create request
|
|
|
|
req := new(v2object.HeadRequest)
|
|
|
|
|
|
|
|
// initialize request body
|
|
|
|
body := new(v2object.HeadRequestBody)
|
|
|
|
req.SetBody(body)
|
|
|
|
|
|
|
|
// set meta header
|
2020-09-15 10:51:36 +00:00
|
|
|
meta := v2MetaHeaderFromOpts(callOpts)
|
2021-03-12 13:07:52 +00:00
|
|
|
|
|
|
|
if err := c.attachV2SessionToken(callOpts, meta, v2SessionReqInfo{
|
2020-09-15 10:51:36 +00:00
|
|
|
addr: p.addr.ToV2(),
|
|
|
|
verb: v2session.ObjectVerbHead,
|
|
|
|
}); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("could not attach session token: %w", err)
|
2020-09-15 10:51:36 +00:00
|
|
|
}
|
2020-11-16 15:10:51 +00:00
|
|
|
|
2020-09-15 10:51:36 +00:00
|
|
|
req.SetMetaHeader(meta)
|
2020-09-04 10:05:57 +00:00
|
|
|
|
|
|
|
// fill body fields
|
|
|
|
body.SetAddress(p.addr.ToV2())
|
|
|
|
body.SetMainOnly(p.short)
|
2020-12-02 11:32:18 +00:00
|
|
|
body.SetRaw(p.raw)
|
2020-09-04 10:05:57 +00:00
|
|
|
|
|
|
|
// sign the request
|
2021-03-10 12:15:54 +00:00
|
|
|
if err := signature.SignServiceMessage(callOpts.key, req); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("signing the request failed: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// send Head request
|
2021-03-12 13:07:52 +00:00
|
|
|
resp, err := rpcapi.HeadObject(c.Raw(), req, client.WithContext(ctx))
|
2020-09-04 10:05:57 +00:00
|
|
|
if err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("sending the request failed: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 11:42:16 +00:00
|
|
|
// handle response meta info
|
|
|
|
if err := c.handleResponseInfoV2(callOpts, resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
// verify response structure
|
|
|
|
if err := signature.VerifyServiceMessage(resp); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("response verification failed: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 11:17:15 +00:00
|
|
|
var (
|
|
|
|
hdr *v2object.Header
|
|
|
|
idSig *v2refs.Signature
|
|
|
|
)
|
2020-09-04 10:05:57 +00:00
|
|
|
|
|
|
|
switch v := resp.GetBody().GetHeaderPart().(type) {
|
|
|
|
case nil:
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("unexpected header type %T", v)
|
2020-12-07 14:41:05 +00:00
|
|
|
case *v2object.ShortHeader:
|
2020-09-04 10:05:57 +00:00
|
|
|
if !p.short {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("wrong header part type: expected %T, received %T",
|
2020-12-07 14:41:05 +00:00
|
|
|
(*v2object.ShortHeader)(nil), (*v2object.HeaderWithSignature)(nil),
|
2020-09-04 10:05:57 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-12-07 14:41:05 +00:00
|
|
|
h := v
|
2020-09-04 10:05:57 +00:00
|
|
|
|
|
|
|
hdr = new(v2object.Header)
|
|
|
|
hdr.SetPayloadLength(h.GetPayloadLength())
|
|
|
|
hdr.SetVersion(h.GetVersion())
|
|
|
|
hdr.SetOwnerID(h.GetOwnerID())
|
|
|
|
hdr.SetObjectType(h.GetObjectType())
|
|
|
|
hdr.SetCreationEpoch(h.GetCreationEpoch())
|
2020-12-17 10:20:29 +00:00
|
|
|
hdr.SetPayloadHash(h.GetPayloadHash())
|
|
|
|
hdr.SetHomomorphicHash(h.GetHomomorphicHash())
|
2020-12-07 14:41:05 +00:00
|
|
|
case *v2object.HeaderWithSignature:
|
2020-09-04 10:05:57 +00:00
|
|
|
if p.short {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("wrong header part type: expected %T, received %T",
|
2020-12-07 14:41:05 +00:00
|
|
|
(*v2object.HeaderWithSignature)(nil), (*v2object.ShortHeader)(nil),
|
2020-09-04 10:05:57 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-12-07 14:41:05 +00:00
|
|
|
hdrWithSig := v
|
2020-09-08 10:42:25 +00:00
|
|
|
if hdrWithSig == nil {
|
2020-11-16 15:10:51 +00:00
|
|
|
return nil, errNilObjectPart
|
2020-09-08 10:42:25 +00:00
|
|
|
}
|
2020-11-16 15:10:51 +00:00
|
|
|
|
2020-09-08 10:42:25 +00:00
|
|
|
hdr = hdrWithSig.GetHeader()
|
2020-11-17 11:17:15 +00:00
|
|
|
idSig = hdrWithSig.GetSignature()
|
2020-11-16 14:33:04 +00:00
|
|
|
|
|
|
|
if err := signer.VerifyDataWithSource(
|
|
|
|
signature.StableMarshalerWrapper{
|
2020-11-17 11:17:15 +00:00
|
|
|
SM: p.addr.ObjectID().ToV2(),
|
2020-11-16 14:33:04 +00:00
|
|
|
},
|
|
|
|
func() (key, sig []byte) {
|
2020-11-17 11:17:15 +00:00
|
|
|
return idSig.GetKey(), idSig.GetSign()
|
2020-11-16 14:33:04 +00:00
|
|
|
},
|
|
|
|
); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("incorrect object header signature: %w", err)
|
2020-11-16 14:33:04 +00:00
|
|
|
}
|
2020-12-07 16:06:11 +00:00
|
|
|
case *v2object.SplitInfo:
|
|
|
|
si := object.NewSplitInfoFromV2(v)
|
|
|
|
|
|
|
|
return nil, object.NewSplitInfoError(si)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
obj := new(v2object.Object)
|
|
|
|
obj.SetHeader(hdr)
|
2020-11-17 11:17:15 +00:00
|
|
|
obj.SetSignature(idSig)
|
2020-09-04 10:05:57 +00:00
|
|
|
|
2020-09-25 14:35:36 +00:00
|
|
|
raw := object.NewRawFromV2(obj)
|
2020-11-16 08:42:19 +00:00
|
|
|
raw.SetID(p.addr.ObjectID())
|
2020-09-25 14:35:36 +00:00
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
// convert the object
|
2020-09-25 14:35:36 +00:00
|
|
|
return raw.Object(), nil
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *RangeDataParams) WithAddress(v *object.Address) *RangeDataParams {
|
|
|
|
if p != nil {
|
|
|
|
p.addr = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:36:57 +00:00
|
|
|
func (p *RangeDataParams) Address() *object.Address {
|
|
|
|
if p != nil {
|
|
|
|
return p.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-07 16:06:11 +00:00
|
|
|
func (p *RangeDataParams) WithRaw(v bool) *RangeDataParams {
|
|
|
|
if p != nil {
|
|
|
|
p.raw = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *RangeDataParams) Raw() bool {
|
|
|
|
if p != nil {
|
|
|
|
return p.raw
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-09-25 12:51:34 +00:00
|
|
|
func (p *RangeDataParams) WithRange(v *object.Range) *RangeDataParams {
|
2020-09-04 10:05:57 +00:00
|
|
|
if p != nil {
|
|
|
|
p.r = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:36:57 +00:00
|
|
|
func (p *RangeDataParams) Range() *object.Range {
|
|
|
|
if p != nil {
|
|
|
|
return p.r
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
func (p *RangeDataParams) WithDataWriter(v io.Writer) *RangeDataParams {
|
|
|
|
if p != nil {
|
|
|
|
p.w = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:36:57 +00:00
|
|
|
func (p *RangeDataParams) DataWriter() io.Writer {
|
|
|
|
if p != nil {
|
|
|
|
return p.w
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-12 15:02:26 +00:00
|
|
|
func (c *clientImpl) ObjectPayloadRangeData(ctx context.Context, p *RangeDataParams, opts ...CallOption) ([]byte, error) {
|
2020-10-27 12:54:02 +00:00
|
|
|
callOpts := c.defaultCallOptions()
|
2020-09-04 10:05:57 +00:00
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
if opts[i] != nil {
|
2021-03-12 13:07:52 +00:00
|
|
|
opts[i](callOpts)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create request
|
|
|
|
req := new(v2object.GetRangeRequest)
|
|
|
|
|
|
|
|
// initialize request body
|
|
|
|
body := new(v2object.GetRangeRequestBody)
|
|
|
|
req.SetBody(body)
|
|
|
|
|
|
|
|
// set meta header
|
2020-09-15 10:51:36 +00:00
|
|
|
meta := v2MetaHeaderFromOpts(callOpts)
|
2021-03-12 13:07:52 +00:00
|
|
|
|
|
|
|
if err := c.attachV2SessionToken(callOpts, meta, v2SessionReqInfo{
|
2020-09-15 10:51:36 +00:00
|
|
|
addr: p.addr.ToV2(),
|
|
|
|
verb: v2session.ObjectVerbRange,
|
|
|
|
}); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("could not attach session token: %w", err)
|
2020-09-15 10:51:36 +00:00
|
|
|
}
|
2020-11-16 15:10:51 +00:00
|
|
|
|
2020-09-15 10:51:36 +00:00
|
|
|
req.SetMetaHeader(meta)
|
2020-09-04 10:05:57 +00:00
|
|
|
|
|
|
|
// fill body fields
|
|
|
|
body.SetAddress(p.addr.ToV2())
|
2020-09-25 12:51:34 +00:00
|
|
|
body.SetRange(p.r.ToV2())
|
2020-12-07 16:06:11 +00:00
|
|
|
body.SetRaw(p.raw)
|
2020-09-04 10:05:57 +00:00
|
|
|
|
|
|
|
// sign the request
|
2021-03-10 12:15:54 +00:00
|
|
|
if err := signature.SignServiceMessage(callOpts.key, req); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("signing the request failed: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
// open stream
|
|
|
|
stream, err := rpcapi.GetObjectRange(c.Raw(), req, client.WithContext(ctx))
|
2020-09-04 10:05:57 +00:00
|
|
|
if err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("could not create Get payload range stream: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var payload []byte
|
|
|
|
if p.w != nil {
|
2021-04-30 07:52:45 +00:00
|
|
|
payload = make([]byte, 0, p.r.GetLength())
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
resp := new(v2object.GetRangeResponse)
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
for {
|
|
|
|
// receive message from server stream
|
2021-03-12 13:07:52 +00:00
|
|
|
err := stream.Read(resp)
|
2020-09-04 10:05:57 +00:00
|
|
|
if err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
2020-09-04 10:05:57 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("reading the response failed: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 11:42:16 +00:00
|
|
|
// handle response meta info
|
|
|
|
if err := c.handleResponseInfoV2(callOpts, resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
// verify response structure
|
|
|
|
if err := signature.VerifyServiceMessage(resp); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("could not verify %T: %w", resp, err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 16:06:11 +00:00
|
|
|
switch v := resp.GetBody().GetRangePart().(type) {
|
|
|
|
case nil:
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("unexpected range type %T", v)
|
2020-12-07 16:06:11 +00:00
|
|
|
case *v2object.GetRangePartChunk:
|
|
|
|
if p.w != nil {
|
|
|
|
if _, err = p.w.Write(v.GetChunk()); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("could not write payload chunk: %w", err)
|
2020-12-07 16:06:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
payload = append(payload, v.GetChunk()...)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
2020-12-07 16:06:11 +00:00
|
|
|
case *v2object.SplitInfo:
|
|
|
|
si := object.NewSplitInfoFromV2(v)
|
|
|
|
|
|
|
|
return nil, object.NewSplitInfoError(si)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return payload, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *RangeChecksumParams) WithAddress(v *object.Address) *RangeChecksumParams {
|
|
|
|
if p != nil {
|
|
|
|
p.addr = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:36:57 +00:00
|
|
|
func (p *RangeChecksumParams) Address() *object.Address {
|
|
|
|
if p != nil {
|
|
|
|
return p.addr
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-25 12:51:34 +00:00
|
|
|
func (p *RangeChecksumParams) WithRangeList(rs ...*object.Range) *RangeChecksumParams {
|
2020-09-04 10:05:57 +00:00
|
|
|
if p != nil {
|
|
|
|
p.rs = rs
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:36:57 +00:00
|
|
|
func (p *RangeChecksumParams) RangeList() []*object.Range {
|
|
|
|
if p != nil {
|
|
|
|
return p.rs
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
func (p *RangeChecksumParams) WithSalt(v []byte) *RangeChecksumParams {
|
|
|
|
if p != nil {
|
|
|
|
p.salt = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:36:57 +00:00
|
|
|
func (p *RangeChecksumParams) Salt() []byte {
|
|
|
|
if p != nil {
|
|
|
|
return p.salt
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
func (p *RangeChecksumParams) withChecksumType(t checksumType) *RangeChecksumParams {
|
|
|
|
if p != nil {
|
|
|
|
p.typ = t
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-03-12 15:02:26 +00:00
|
|
|
func (c *clientImpl) ObjectPayloadRangeSHA256(ctx context.Context, p *RangeChecksumParams, opts ...CallOption) ([][sha256.Size]byte, error) {
|
2020-09-04 10:05:57 +00:00
|
|
|
res, err := c.objectPayloadRangeHash(ctx, p.withChecksumType(checksumSHA256), opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.([][sha256.Size]byte), nil
|
|
|
|
}
|
|
|
|
|
2021-03-12 15:02:26 +00:00
|
|
|
func (c *clientImpl) ObjectPayloadRangeTZ(ctx context.Context, p *RangeChecksumParams, opts ...CallOption) ([][TZSize]byte, error) {
|
2020-09-04 10:05:57 +00:00
|
|
|
res, err := c.objectPayloadRangeHash(ctx, p.withChecksumType(checksumTZ), opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.([][TZSize]byte), nil
|
|
|
|
}
|
|
|
|
|
2021-03-12 15:02:26 +00:00
|
|
|
func (c *clientImpl) objectPayloadRangeHash(ctx context.Context, p *RangeChecksumParams, opts ...CallOption) (interface{}, error) {
|
2020-10-27 12:54:02 +00:00
|
|
|
callOpts := c.defaultCallOptions()
|
2020-09-04 10:05:57 +00:00
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
if opts[i] != nil {
|
2021-03-12 13:07:52 +00:00
|
|
|
opts[i](callOpts)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create request
|
|
|
|
req := new(v2object.GetRangeHashRequest)
|
|
|
|
|
|
|
|
// initialize request body
|
|
|
|
body := new(v2object.GetRangeHashRequestBody)
|
|
|
|
req.SetBody(body)
|
|
|
|
|
|
|
|
// set meta header
|
2020-09-15 10:51:36 +00:00
|
|
|
meta := v2MetaHeaderFromOpts(callOpts)
|
2021-03-12 13:07:52 +00:00
|
|
|
|
|
|
|
if err := c.attachV2SessionToken(callOpts, meta, v2SessionReqInfo{
|
2020-09-15 10:51:36 +00:00
|
|
|
addr: p.addr.ToV2(),
|
|
|
|
verb: v2session.ObjectVerbRangeHash,
|
|
|
|
}); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("could not attach session token: %w", err)
|
2020-09-15 10:51:36 +00:00
|
|
|
}
|
2020-11-16 15:10:51 +00:00
|
|
|
|
2020-09-15 10:51:36 +00:00
|
|
|
req.SetMetaHeader(meta)
|
2020-09-04 10:05:57 +00:00
|
|
|
|
|
|
|
// fill body fields
|
|
|
|
body.SetAddress(p.addr.ToV2())
|
|
|
|
body.SetSalt(p.salt)
|
|
|
|
|
|
|
|
typV2 := p.typ.toV2()
|
|
|
|
body.SetType(typV2)
|
|
|
|
|
|
|
|
rsV2 := rangesToV2(p.rs)
|
|
|
|
body.SetRanges(rsV2)
|
|
|
|
|
|
|
|
// sign the request
|
2021-03-10 12:15:54 +00:00
|
|
|
if err := signature.SignServiceMessage(callOpts.key, req); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("signing the request failed: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// send request
|
2021-03-12 13:07:52 +00:00
|
|
|
resp, err := rpcapi.HashObjectRange(c.Raw(), req, client.WithContext(ctx))
|
2020-09-04 10:05:57 +00:00
|
|
|
if err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("sending the request failed: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 11:42:16 +00:00
|
|
|
// handle response meta info
|
|
|
|
if err := c.handleResponseInfoV2(callOpts, resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-04 10:05:57 +00:00
|
|
|
// verify response structure
|
|
|
|
if err := signature.VerifyServiceMessage(resp); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("response verification failed: %w", err)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
respBody := resp.GetBody()
|
|
|
|
respType := respBody.GetType()
|
|
|
|
respHashes := respBody.GetHashList()
|
|
|
|
|
|
|
|
if t := p.typ.toV2(); respType != t {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("invalid checksum type: expected %v, received %v", t, respType)
|
2020-09-04 10:05:57 +00:00
|
|
|
} else if reqLn, respLn := len(rsV2), len(respHashes); reqLn != respLn {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("wrong checksum number: expected %d, received %d", reqLn, respLn)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var res interface{}
|
|
|
|
|
|
|
|
switch p.typ {
|
|
|
|
case checksumSHA256:
|
|
|
|
r := make([][sha256.Size]byte, 0, len(respHashes))
|
|
|
|
|
|
|
|
for i := range respHashes {
|
|
|
|
if ln := len(respHashes[i]); ln != sha256.Size {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("invalid checksum length: expected %d, received %d", sha256.Size, ln)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cs := [sha256.Size]byte{}
|
|
|
|
copy(cs[:], respHashes[i])
|
|
|
|
|
|
|
|
r = append(r, cs)
|
|
|
|
}
|
|
|
|
|
|
|
|
res = r
|
|
|
|
case checksumTZ:
|
|
|
|
r := make([][TZSize]byte, 0, len(respHashes))
|
|
|
|
|
|
|
|
for i := range respHashes {
|
|
|
|
if ln := len(respHashes[i]); ln != TZSize {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("invalid checksum length: expected %d, received %d", TZSize, ln)
|
2020-09-04 10:05:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cs := [TZSize]byte{}
|
|
|
|
copy(cs[:], respHashes[i])
|
|
|
|
|
|
|
|
r = append(r, cs)
|
|
|
|
}
|
|
|
|
|
|
|
|
res = r
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2021-05-31 12:09:04 +00:00
|
|
|
func (p *SearchObjectParams) WithContainerID(v *cid.ID) *SearchObjectParams {
|
2020-09-17 10:12:48 +00:00
|
|
|
if p != nil {
|
|
|
|
p.cid = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-05-31 12:09:04 +00:00
|
|
|
func (p *SearchObjectParams) ContainerID() *cid.ID {
|
2020-12-03 13:36:57 +00:00
|
|
|
if p != nil {
|
|
|
|
return p.cid
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-17 10:12:48 +00:00
|
|
|
func (p *SearchObjectParams) WithSearchFilters(v object.SearchFilters) *SearchObjectParams {
|
|
|
|
if p != nil {
|
|
|
|
p.filters = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2020-12-03 13:36:57 +00:00
|
|
|
func (p *SearchObjectParams) SearchFilters() object.SearchFilters {
|
|
|
|
if p != nil {
|
|
|
|
return p.filters
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-12 15:02:26 +00:00
|
|
|
func (c *clientImpl) SearchObject(ctx context.Context, p *SearchObjectParams, opts ...CallOption) ([]*object.ID, error) {
|
2020-10-27 12:54:02 +00:00
|
|
|
callOpts := c.defaultCallOptions()
|
2020-09-17 10:12:48 +00:00
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
if opts[i] != nil {
|
2021-03-12 13:07:52 +00:00
|
|
|
opts[i](callOpts)
|
2020-09-17 10:12:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create request
|
|
|
|
req := new(v2object.SearchRequest)
|
|
|
|
|
|
|
|
// initialize request body
|
|
|
|
body := new(v2object.SearchRequestBody)
|
|
|
|
req.SetBody(body)
|
|
|
|
|
|
|
|
v2Addr := new(v2refs.Address)
|
|
|
|
v2Addr.SetContainerID(p.cid.ToV2())
|
|
|
|
|
|
|
|
// set meta header
|
|
|
|
meta := v2MetaHeaderFromOpts(callOpts)
|
2021-03-12 13:07:52 +00:00
|
|
|
|
|
|
|
if err := c.attachV2SessionToken(callOpts, meta, v2SessionReqInfo{
|
2020-09-17 10:12:48 +00:00
|
|
|
addr: v2Addr,
|
|
|
|
verb: v2session.ObjectVerbSearch,
|
|
|
|
}); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("could not attach session token: %w", err)
|
2020-09-17 10:12:48 +00:00
|
|
|
}
|
2020-11-16 15:10:51 +00:00
|
|
|
|
2020-09-17 10:12:48 +00:00
|
|
|
req.SetMetaHeader(meta)
|
|
|
|
|
|
|
|
// fill body fields
|
|
|
|
body.SetContainerID(v2Addr.GetContainerID())
|
|
|
|
body.SetVersion(searchQueryVersion)
|
|
|
|
body.SetFilters(p.filters.ToV2())
|
|
|
|
|
|
|
|
// sign the request
|
2021-03-10 12:15:54 +00:00
|
|
|
if err := signature.SignServiceMessage(callOpts.key, req); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("signing the request failed: %w", err)
|
2020-09-17 10:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create search stream
|
2021-03-12 13:07:52 +00:00
|
|
|
stream, err := rpcapi.SearchObjects(c.Raw(), req, client.WithContext(ctx))
|
2020-09-17 10:12:48 +00:00
|
|
|
if err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("stream opening failed: %w", err)
|
2020-09-17 10:12:48 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
var (
|
|
|
|
searchResult []*object.ID
|
|
|
|
resp = new(v2object.SearchResponse)
|
|
|
|
)
|
2020-09-17 10:12:48 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
// receive message from server stream
|
2021-03-12 13:07:52 +00:00
|
|
|
err := stream.Read(resp)
|
2020-09-17 10:12:48 +00:00
|
|
|
if err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
if errors.Is(err, io.EOF) {
|
2020-09-17 10:12:48 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("reading the response failed: %w", err)
|
2020-09-17 10:12:48 +00:00
|
|
|
}
|
|
|
|
|
2021-09-27 11:42:16 +00:00
|
|
|
// handle response meta info
|
|
|
|
if err := c.handleResponseInfoV2(callOpts, resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-17 10:12:48 +00:00
|
|
|
// verify response structure
|
|
|
|
if err := signature.VerifyServiceMessage(resp); err != nil {
|
2021-05-17 12:56:17 +00:00
|
|
|
return nil, fmt.Errorf("could not verify %T: %w", resp, err)
|
2020-09-17 10:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
chunk := resp.GetBody().GetIDList()
|
|
|
|
for i := range chunk {
|
|
|
|
searchResult = append(searchResult, object.NewIDFromV2(chunk[i]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return searchResult, nil
|
|
|
|
}
|
|
|
|
|
2021-03-12 13:07:52 +00:00
|
|
|
func (c *clientImpl) attachV2SessionToken(opts *callOptions, hdr *v2session.RequestMetaHeader, info v2SessionReqInfo) error {
|
2020-09-15 10:51:36 +00:00
|
|
|
if opts.session == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-14 17:06:05 +00:00
|
|
|
// Do not resign already prepared session token
|
|
|
|
if opts.session.Signature() != nil {
|
|
|
|
hdr.SetSessionToken(opts.session.ToV2())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-15 10:51:36 +00:00
|
|
|
opCtx := new(v2session.ObjectSessionContext)
|
|
|
|
opCtx.SetAddress(info.addr)
|
|
|
|
opCtx.SetVerb(info.verb)
|
|
|
|
|
|
|
|
lt := new(v2session.TokenLifetime)
|
|
|
|
lt.SetIat(info.iat)
|
|
|
|
lt.SetNbf(info.nbf)
|
|
|
|
lt.SetExp(info.exp)
|
|
|
|
|
2021-07-06 08:18:25 +00:00
|
|
|
body := new(v2session.SessionTokenBody)
|
|
|
|
body.SetID(opts.session.ID())
|
|
|
|
body.SetOwnerID(opts.session.OwnerID().ToV2())
|
2020-09-15 10:51:36 +00:00
|
|
|
body.SetSessionKey(opts.session.SessionKey())
|
|
|
|
body.SetContext(opCtx)
|
|
|
|
body.SetLifetime(lt)
|
|
|
|
|
2021-07-07 10:42:09 +00:00
|
|
|
token := new(v2session.SessionToken)
|
|
|
|
token.SetBody(body)
|
|
|
|
|
2020-09-15 10:51:36 +00:00
|
|
|
signWrapper := signature.StableMarshalerWrapper{SM: token.GetBody()}
|
2020-11-16 15:10:51 +00:00
|
|
|
|
2021-03-10 12:15:54 +00:00
|
|
|
err := signer.SignDataWithHandler(opts.key, signWrapper, func(key []byte, sig []byte) {
|
2020-09-15 10:51:36 +00:00
|
|
|
sessionTokenSignature := new(v2refs.Signature)
|
|
|
|
sessionTokenSignature.SetKey(key)
|
|
|
|
sessionTokenSignature.SetSign(sig)
|
|
|
|
token.SetSignature(sessionTokenSignature)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
hdr.SetSessionToken(token)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|