2022-02-12 20:30:16 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2023-03-07 11:20:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/acl"
|
|
|
|
v2object "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/object"
|
|
|
|
v2refs "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs"
|
|
|
|
rpcapi "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client"
|
|
|
|
v2session "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/signature"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session"
|
2022-02-12 20:30:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PrmObjectSearch groups parameters of ObjectSearch operation.
|
|
|
|
type PrmObjectSearch struct {
|
2023-10-23 13:50:19 +00:00
|
|
|
XHeaders []string
|
2022-03-03 11:04:53 +00:00
|
|
|
|
2023-10-23 13:50:19 +00:00
|
|
|
Local bool
|
2022-02-12 20:30:16 +00:00
|
|
|
|
2023-10-23 13:50:19 +00:00
|
|
|
BearerToken *bearer.Token
|
2022-02-12 20:30:16 +00:00
|
|
|
|
2023-10-23 13:50:19 +00:00
|
|
|
Session *session.Object
|
|
|
|
|
|
|
|
ContainerID *cid.ID
|
|
|
|
|
|
|
|
Key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
Filters object.SearchFilters
|
2022-02-12 20:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarkLocal tells the server to execute the operation locally.
|
2023-10-23 13:50:19 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmObjectSearch.Local instead.
|
2022-02-12 20:30:16 +00:00
|
|
|
func (x *PrmObjectSearch) MarkLocal() {
|
2023-10-23 13:50:19 +00:00
|
|
|
x.Local = true
|
2022-02-12 20:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithinSession specifies session within which the search query must be executed.
|
|
|
|
//
|
|
|
|
// Creator of the session acquires the authorship of the request.
|
|
|
|
// This may affect the execution of an operation (e.g. access control).
|
|
|
|
//
|
|
|
|
// Must be signed.
|
2023-10-23 13:50:19 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmObjectSearch.Session instead.
|
2022-04-07 16:09:15 +00:00
|
|
|
func (x *PrmObjectSearch) WithinSession(t session.Object) {
|
2023-10-23 13:50:19 +00:00
|
|
|
x.Session = &t
|
2022-02-12 20:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithBearerToken attaches bearer token to be used for the operation.
|
|
|
|
//
|
|
|
|
// If set, underlying eACL rules will be used in access control.
|
|
|
|
//
|
|
|
|
// Must be signed.
|
2023-10-23 13:50:19 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmObjectSearch.BearerToken instead.
|
2022-03-24 08:05:41 +00:00
|
|
|
func (x *PrmObjectSearch) WithBearerToken(t bearer.Token) {
|
2023-10-23 13:50:19 +00:00
|
|
|
x.BearerToken = &t
|
2022-08-23 18:56:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WithXHeaders specifies list of extended headers (string key-value pairs)
|
|
|
|
// to be attached to the request. Must have an even length.
|
|
|
|
//
|
|
|
|
// Slice must not be mutated until the operation completes.
|
2023-10-23 13:50:19 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmObjectSearch.XHeaders instead.
|
2022-08-23 18:56:55 +00:00
|
|
|
func (x *PrmObjectSearch) WithXHeaders(hs ...string) {
|
2023-10-23 13:50:19 +00:00
|
|
|
x.XHeaders = hs
|
2022-08-23 18:56:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UseKey specifies private key to sign the requests.
|
|
|
|
// If key is not provided, then Client default key is used.
|
2023-10-23 13:50:19 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmObjectSearch.Key instead.
|
2022-08-23 18:56:55 +00:00
|
|
|
func (x *PrmObjectSearch) UseKey(key ecdsa.PrivateKey) {
|
2023-10-23 13:50:19 +00:00
|
|
|
x.Key = &key
|
2022-02-12 20:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InContainer specifies the container in which to look for objects.
|
|
|
|
// Required parameter.
|
2023-10-23 13:50:19 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmObjectSearch.ContainerID instead.
|
2022-02-12 20:30:16 +00:00
|
|
|
func (x *PrmObjectSearch) InContainer(id cid.ID) {
|
2023-10-23 13:50:19 +00:00
|
|
|
x.ContainerID = &id
|
2022-02-12 20:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetFilters sets filters by which to select objects. All container objects
|
|
|
|
// match unset/empty filters.
|
2023-10-23 13:50:19 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmObjectSearch.Filters instead.
|
2022-02-12 20:30:16 +00:00
|
|
|
func (x *PrmObjectSearch) SetFilters(filters object.SearchFilters) {
|
2023-10-23 13:50:19 +00:00
|
|
|
x.Filters = filters
|
2022-02-12 20:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ResObjectSearch groups the final result values of ObjectSearch operation.
|
|
|
|
type ResObjectSearch struct {
|
|
|
|
statusRes
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// ObjectListReader is designed to read list of object identifiers from FrostFS system.
|
2022-02-12 20:30:16 +00:00
|
|
|
//
|
|
|
|
// Must be initialized using Client.ObjectSearch, any other usage is unsafe.
|
|
|
|
type ObjectListReader struct {
|
2022-08-23 18:56:55 +00:00
|
|
|
client *Client
|
2022-02-12 20:30:16 +00:00
|
|
|
cancelCtxStream context.CancelFunc
|
2022-08-23 18:56:55 +00:00
|
|
|
err error
|
|
|
|
res ResObjectSearch
|
|
|
|
stream interface {
|
|
|
|
Read(resp *v2object.SearchResponse) error
|
|
|
|
}
|
2022-03-11 09:16:08 +00:00
|
|
|
tail []v2refs.ObjectID
|
2022-02-12 20:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read reads another list of the object identifiers. Works similar to
|
|
|
|
// io.Reader.Read but copies oid.ID and returns success flag instead of error.
|
|
|
|
//
|
|
|
|
// Failure reason can be received via Close.
|
|
|
|
//
|
|
|
|
// Panics if buf has zero length.
|
|
|
|
func (x *ObjectListReader) Read(buf []oid.ID) (int, bool) {
|
|
|
|
if len(buf) == 0 {
|
|
|
|
panic("empty buffer in ObjectListReader.ReadList")
|
|
|
|
}
|
|
|
|
|
2022-08-23 18:56:55 +00:00
|
|
|
read := copyIDBuffers(buf, x.tail)
|
2022-02-12 20:30:16 +00:00
|
|
|
x.tail = x.tail[read:]
|
|
|
|
|
|
|
|
if len(buf) == read {
|
|
|
|
return read, true
|
|
|
|
}
|
|
|
|
|
2022-02-24 11:09:26 +00:00
|
|
|
for {
|
2022-08-23 18:56:55 +00:00
|
|
|
var resp v2object.SearchResponse
|
|
|
|
x.err = x.stream.Read(&resp)
|
|
|
|
if x.err != nil {
|
2022-02-24 11:09:26 +00:00
|
|
|
return read, false
|
|
|
|
}
|
2022-02-12 20:30:16 +00:00
|
|
|
|
2022-08-23 18:56:55 +00:00
|
|
|
x.res.st, x.err = x.client.processResponse(&resp)
|
|
|
|
if x.err != nil || !apistatus.IsSuccessful(x.res.st) {
|
|
|
|
return read, false
|
|
|
|
}
|
2022-02-12 20:30:16 +00:00
|
|
|
|
2022-08-23 18:56:55 +00:00
|
|
|
// read new chunk of objects
|
|
|
|
ids := resp.GetBody().GetIDList()
|
|
|
|
if len(ids) == 0 {
|
2022-02-24 11:09:26 +00:00
|
|
|
// just skip empty lists since they are not prohibited by protocol
|
|
|
|
continue
|
|
|
|
}
|
2022-02-12 20:30:16 +00:00
|
|
|
|
2022-08-23 18:56:55 +00:00
|
|
|
ln := copyIDBuffers(buf[read:], ids)
|
2022-02-24 11:09:26 +00:00
|
|
|
read += ln
|
2022-02-12 20:30:16 +00:00
|
|
|
|
2022-02-24 11:33:43 +00:00
|
|
|
if read == len(buf) {
|
|
|
|
// save the tail
|
|
|
|
x.tail = append(x.tail, ids[ln:]...)
|
2022-02-24 11:09:26 +00:00
|
|
|
|
2022-02-24 11:33:43 +00:00
|
|
|
return read, true
|
|
|
|
}
|
2022-02-24 11:09:26 +00:00
|
|
|
}
|
2022-02-12 20:30:16 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 18:56:55 +00:00
|
|
|
func copyIDBuffers(dst []oid.ID, src []v2refs.ObjectID) int {
|
|
|
|
var i int
|
|
|
|
for ; i < len(dst) && i < len(src); i++ {
|
|
|
|
_ = dst[i].ReadFromV2(src[i])
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2022-02-25 10:20:15 +00:00
|
|
|
// Iterate iterates over the list of found object identifiers.
|
|
|
|
// f can return true to stop iteration earlier.
|
|
|
|
//
|
|
|
|
// Returns an error if object can't be read.
|
|
|
|
func (x *ObjectListReader) Iterate(f func(oid.ID) bool) error {
|
|
|
|
buf := make([]oid.ID, 1)
|
|
|
|
|
|
|
|
for {
|
|
|
|
// Do not check first return value because `len(buf) == 1`,
|
|
|
|
// so false means nothing was read.
|
|
|
|
_, ok := x.Read(buf)
|
|
|
|
if !ok {
|
|
|
|
res, err := x.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return apistatus.ErrFromStatus(res.Status())
|
|
|
|
}
|
|
|
|
if f(buf[0]) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-12 20:30:16 +00:00
|
|
|
// Close ends reading list of the matched objects and returns the result of the operation
|
|
|
|
// along with the final results. Must be called after using the ObjectListReader.
|
|
|
|
//
|
|
|
|
// Exactly one return value is non-nil. By default, server status is returned in res structure.
|
|
|
|
// Any client's internal or transport errors are returned as Go built-in error.
|
2022-12-29 10:46:18 +00:00
|
|
|
// If Client is tuned to resolve FrostFS API statuses, then FrostFS failures
|
2022-02-12 20:30:16 +00:00
|
|
|
// codes are returned as error.
|
|
|
|
//
|
|
|
|
// Return statuses:
|
2022-02-28 10:56:46 +00:00
|
|
|
// - global (see Client docs);
|
|
|
|
// - *apistatus.ContainerNotFound;
|
|
|
|
// - *apistatus.ObjectAccessDenied;
|
|
|
|
// - *apistatus.SessionTokenExpired.
|
2022-02-12 20:30:16 +00:00
|
|
|
func (x *ObjectListReader) Close() (*ResObjectSearch, error) {
|
|
|
|
defer x.cancelCtxStream()
|
|
|
|
|
2022-08-23 18:56:55 +00:00
|
|
|
if x.err != nil && !errors.Is(x.err, io.EOF) {
|
|
|
|
return nil, x.err
|
2022-02-12 20:30:16 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 18:56:55 +00:00
|
|
|
return &x.res, nil
|
2022-02-12 20:30:16 +00:00
|
|
|
}
|
|
|
|
|
2023-10-23 13:50:19 +00:00
|
|
|
func (x *PrmObjectSearch) buildRequest(c *Client) (*v2object.SearchRequest, error) {
|
|
|
|
if x.ContainerID == nil {
|
|
|
|
return nil, errorMissingContainer
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(x.XHeaders)%2 != 0 {
|
|
|
|
return nil, errorInvalidXHeaders
|
|
|
|
}
|
|
|
|
|
|
|
|
meta := new(v2session.RequestMetaHeader)
|
|
|
|
writeXHeadersToMeta(x.XHeaders, meta)
|
|
|
|
|
|
|
|
if x.BearerToken != nil {
|
|
|
|
v2BearerToken := new(acl.BearerToken)
|
|
|
|
x.BearerToken.WriteToV2(v2BearerToken)
|
|
|
|
meta.SetBearerToken(v2BearerToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
if x.Session != nil {
|
|
|
|
v2SessionToken := new(v2session.Token)
|
|
|
|
x.Session.WriteToV2(v2SessionToken)
|
|
|
|
meta.SetSessionToken(v2SessionToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
if x.Local {
|
|
|
|
meta.SetTTL(1)
|
|
|
|
}
|
|
|
|
cnrV2 := new(v2refs.ContainerID)
|
|
|
|
x.ContainerID.WriteToV2(cnrV2)
|
|
|
|
|
|
|
|
body := new(v2object.SearchRequestBody)
|
|
|
|
body.SetVersion(1)
|
|
|
|
body.SetContainerID(cnrV2)
|
|
|
|
body.SetFilters(x.Filters.ToV2())
|
|
|
|
|
|
|
|
req := new(v2object.SearchRequest)
|
|
|
|
req.SetBody(body)
|
|
|
|
c.prepareRequest(req, meta)
|
|
|
|
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// ObjectSearchInit initiates object selection through a remote server using FrostFS API protocol.
|
2022-02-12 20:30:16 +00:00
|
|
|
//
|
|
|
|
// The call only opens the transmission channel, explicit fetching of matched objects
|
|
|
|
// is done using the ObjectListReader. Exactly one return value is non-nil.
|
|
|
|
// Resulting reader must be finally closed.
|
|
|
|
//
|
2023-02-27 07:53:27 +00:00
|
|
|
// Returns an error if parameters are set incorrectly (see PrmObjectSearch docs).
|
2022-02-12 20:30:16 +00:00
|
|
|
// Context is required and must not be nil. It is used for network communication.
|
|
|
|
func (c *Client) ObjectSearchInit(ctx context.Context, prm PrmObjectSearch) (*ObjectListReader, error) {
|
2023-10-23 13:50:19 +00:00
|
|
|
req, err := prm.buildRequest(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-12 20:30:16 +00:00
|
|
|
}
|
|
|
|
|
2023-10-23 13:50:19 +00:00
|
|
|
key := prm.Key
|
2022-08-23 18:56:55 +00:00
|
|
|
if key == nil {
|
|
|
|
key = &c.prm.key
|
2022-02-12 20:30:16 +00:00
|
|
|
}
|
|
|
|
|
2023-10-23 13:50:19 +00:00
|
|
|
err = signature.SignServiceMessage(key, req)
|
2022-08-23 18:56:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("sign request: %w", err)
|
2022-02-12 20:30:16 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 18:56:55 +00:00
|
|
|
var r ObjectListReader
|
2022-02-12 20:30:16 +00:00
|
|
|
ctx, r.cancelCtxStream = context.WithCancel(ctx)
|
|
|
|
|
2023-10-23 13:50:19 +00:00
|
|
|
r.stream, err = rpcapi.SearchObjects(&c.c, req, client.WithContext(ctx))
|
2022-08-23 18:56:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("open stream: %w", err)
|
2022-02-12 20:30:16 +00:00
|
|
|
}
|
2022-08-23 18:56:55 +00:00
|
|
|
r.client = c
|
2022-02-12 20:30:16 +00:00
|
|
|
|
|
|
|
return &r, nil
|
|
|
|
}
|