2022-02-15 05:36:47 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-11-22 16:22:00 +00:00
|
|
|
"crypto/ecdsa"
|
2022-08-23 17:51:05 +00:00
|
|
|
"fmt"
|
2022-02-15 05:36:47 +00:00
|
|
|
|
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"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session"
|
2022-02-15 05:36:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PrmObjectHash groups parameters of ObjectHash operation.
|
|
|
|
type PrmObjectHash struct {
|
|
|
|
meta v2session.RequestMetaHeader
|
|
|
|
|
|
|
|
body v2object.GetRangeHashRequestBody
|
|
|
|
|
2022-08-23 17:59:21 +00:00
|
|
|
csAlgo v2refs.ChecksumType
|
2022-02-15 05:36:47 +00:00
|
|
|
|
|
|
|
addr v2refs.Address
|
2022-11-22 16:22:00 +00:00
|
|
|
|
|
|
|
keySet bool
|
|
|
|
key ecdsa.PrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// UseKey specifies private key to sign the requests.
|
|
|
|
// If key is not provided, then Client default key is used.
|
|
|
|
func (x *PrmObjectHash) UseKey(key ecdsa.PrivateKey) {
|
|
|
|
x.keySet = true
|
|
|
|
x.key = key
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarkLocal tells the server to execute the operation locally.
|
|
|
|
func (x *PrmObjectHash) MarkLocal() {
|
|
|
|
x.meta.SetTTL(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithinSession specifies session within which object should be read.
|
|
|
|
//
|
|
|
|
// 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.
|
2022-04-07 16:09:15 +00:00
|
|
|
func (x *PrmObjectHash) WithinSession(t session.Object) {
|
|
|
|
var tv2 v2session.Token
|
|
|
|
t.WriteToV2(&tv2)
|
|
|
|
|
|
|
|
x.meta.SetSessionToken(&tv2)
|
2022-02-15 05:36:47 +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.
|
2022-03-24 08:05:41 +00:00
|
|
|
func (x *PrmObjectHash) WithBearerToken(t bearer.Token) {
|
|
|
|
var v2token acl.BearerToken
|
|
|
|
t.WriteToV2(&v2token)
|
|
|
|
x.meta.SetBearerToken(&v2token)
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// FromContainer specifies FrostFS container of the object.
|
2022-02-15 05:36:47 +00:00
|
|
|
// Required parameter.
|
|
|
|
func (x *PrmObjectHash) FromContainer(id cid.ID) {
|
2022-04-11 16:25:14 +00:00
|
|
|
var cidV2 v2refs.ContainerID
|
|
|
|
id.WriteToV2(&cidV2)
|
|
|
|
|
|
|
|
x.addr.SetContainerID(&cidV2)
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ByID specifies identifier of the requested object.
|
|
|
|
// Required parameter.
|
|
|
|
func (x *PrmObjectHash) ByID(id oid.ID) {
|
2022-04-11 16:25:14 +00:00
|
|
|
var idV2 v2refs.ObjectID
|
|
|
|
id.WriteToV2(&idV2)
|
|
|
|
|
|
|
|
x.addr.SetObjectID(&idV2)
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetRangeList sets list of ranges in (offset, length) pair format.
|
|
|
|
// Required parameter.
|
|
|
|
//
|
|
|
|
// If passed as slice, then it must not be mutated before the operation completes.
|
|
|
|
func (x *PrmObjectHash) SetRangeList(r ...uint64) {
|
|
|
|
ln := len(r)
|
|
|
|
if ln%2 != 0 {
|
|
|
|
panic("odd number of range parameters")
|
|
|
|
}
|
|
|
|
|
2022-03-11 09:16:08 +00:00
|
|
|
rs := make([]v2object.Range, ln/2)
|
2022-02-15 05:36:47 +00:00
|
|
|
|
|
|
|
for i := 0; i < ln/2; i++ {
|
|
|
|
rs[i].SetOffset(r[2*i])
|
2022-03-02 12:49:11 +00:00
|
|
|
rs[i].SetLength(r[2*i+1])
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
x.body.SetRanges(rs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TillichZemorAlgo changes the hash function to Tillich-Zemor
|
|
|
|
// (https://link.springer.com/content/pdf/10.1007/3-540-48658-5_5.pdf).
|
|
|
|
//
|
|
|
|
// By default, SHA256 hash function is used.
|
|
|
|
func (x *PrmObjectHash) TillichZemorAlgo() {
|
2022-08-23 17:59:21 +00:00
|
|
|
x.csAlgo = v2refs.TillichZemor
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UseSalt sets the salt to XOR the data range before hashing.
|
|
|
|
//
|
|
|
|
// Must not be mutated before the operation completes.
|
|
|
|
func (x *PrmObjectHash) UseSalt(salt []byte) {
|
|
|
|
x.body.SetSalt(salt)
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:04:53 +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.
|
|
|
|
func (x *PrmObjectHash) WithXHeaders(hs ...string) {
|
2022-08-23 17:51:05 +00:00
|
|
|
writeXHeadersToMeta(hs, &x.meta)
|
2022-03-03 11:04:53 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 05:36:47 +00:00
|
|
|
// ResObjectHash groups resulting values of ObjectHash operation.
|
|
|
|
type ResObjectHash struct {
|
|
|
|
statusRes
|
|
|
|
|
|
|
|
checksums [][]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checksums returns a list of calculated checksums in range order.
|
|
|
|
func (x ResObjectHash) Checksums() [][]byte {
|
|
|
|
return x.checksums
|
|
|
|
}
|
|
|
|
|
|
|
|
// ObjectHash requests checksum of the range list of the object payload using
|
2022-12-29 10:46:18 +00:00
|
|
|
// FrostFS API protocol.
|
2022-02-15 05:36:47 +00:00
|
|
|
//
|
|
|
|
// Returns a list of checksums in raw form: the format of hashes and their number
|
|
|
|
// is left for the caller to check. Client preserves the order of the server's response.
|
|
|
|
//
|
|
|
|
// 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 `error`,
|
2022-12-29 10:46:18 +00:00
|
|
|
// If PrmInit.ResolveFrostFSFailures has been called, unsuccessful
|
|
|
|
// FrostFS status codes are returned as `error`, otherwise, are included
|
2022-02-15 05:36:47 +00:00
|
|
|
// in the returned result structure.
|
|
|
|
//
|
2023-02-27 07:53:27 +00:00
|
|
|
// Returns an error if parameters are set incorrectly (see PrmObjectHash docs).
|
2022-02-15 05:36:47 +00:00
|
|
|
// Context is required and must not be nil. It is used for network communication.
|
|
|
|
//
|
|
|
|
// Return statuses:
|
2022-02-28 10:56:46 +00:00
|
|
|
// - global (see Client docs);
|
|
|
|
// - *apistatus.ContainerNotFound;
|
|
|
|
// - *apistatus.ObjectNotFound;
|
|
|
|
// - *apistatus.ObjectAccessDenied;
|
2022-06-29 17:24:56 +00:00
|
|
|
// - *apistatus.ObjectOutOfRange;
|
2022-02-28 10:56:46 +00:00
|
|
|
// - *apistatus.SessionTokenExpired.
|
2022-02-15 05:36:47 +00:00
|
|
|
func (c *Client) ObjectHash(ctx context.Context, prm PrmObjectHash) (*ResObjectHash, error) {
|
|
|
|
switch {
|
|
|
|
case prm.addr.GetContainerID() == nil:
|
2023-02-27 07:53:27 +00:00
|
|
|
return nil, errorMissingContainer
|
2022-02-15 05:36:47 +00:00
|
|
|
case prm.addr.GetObjectID() == nil:
|
2023-02-27 07:53:27 +00:00
|
|
|
return nil, errorMissingObject
|
2022-02-15 05:36:47 +00:00
|
|
|
case len(prm.body.GetRanges()) == 0:
|
2023-02-27 07:53:27 +00:00
|
|
|
return nil, errorMissingRanges
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prm.body.SetAddress(&prm.addr)
|
2022-08-23 17:59:21 +00:00
|
|
|
if prm.csAlgo == v2refs.UnknownChecksum {
|
2022-02-15 05:36:47 +00:00
|
|
|
prm.body.SetType(v2refs.SHA256)
|
2022-08-23 17:59:21 +00:00
|
|
|
} else {
|
|
|
|
prm.body.SetType(prm.csAlgo)
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var req v2object.GetRangeHashRequest
|
2022-08-23 17:51:05 +00:00
|
|
|
c.prepareRequest(&req, &prm.meta)
|
2022-02-15 05:36:47 +00:00
|
|
|
req.SetBody(&prm.body)
|
2022-08-23 17:51:05 +00:00
|
|
|
|
2022-11-22 16:22:00 +00:00
|
|
|
key := c.prm.key
|
|
|
|
if prm.keySet {
|
|
|
|
key = prm.key
|
|
|
|
}
|
|
|
|
|
|
|
|
err := signature.SignServiceMessage(&key, &req)
|
2022-08-23 17:51:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("sign request: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := rpcapi.HashObjectRange(&c.c, &req, client.WithContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("write request: %w", err)
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
2022-08-23 17:51:05 +00:00
|
|
|
|
|
|
|
var res ResObjectHash
|
|
|
|
res.st, err = c.processResponse(resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !apistatus.IsSuccessful(res.st) {
|
|
|
|
return &res, nil
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 17:51:05 +00:00
|
|
|
res.checksums = resp.GetBody().GetHashList()
|
|
|
|
if len(res.checksums) == 0 {
|
|
|
|
return nil, newErrMissingResponseField("hash list")
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &res, nil
|
|
|
|
}
|