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
|
|
|
|
2024-10-07 14:20:25 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/acl"
|
|
|
|
v2object "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/object"
|
|
|
|
v2refs "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
|
|
|
rpcapi "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/rpc"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/rpc/client"
|
|
|
|
v2session "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/session"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/signature"
|
2023-03-07 11:20:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
2023-09-04 09:42:55 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/checksum"
|
2023-03-07 11:20:03 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-09-04 09:42:55 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-03-07 11:20:03 +00:00
|
|
|
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 {
|
2023-09-04 09:42:55 +00:00
|
|
|
XHeaders []string
|
2022-02-15 05:36:47 +00:00
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
BearerToken *bearer.Token
|
2022-02-15 05:36:47 +00:00
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
Session *session.Object
|
2022-02-15 05:36:47 +00:00
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
Local bool
|
2022-11-22 16:22:00 +00:00
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
Ranges []object.Range
|
|
|
|
|
|
|
|
Salt []byte
|
|
|
|
|
|
|
|
ChecksumType checksum.Type
|
|
|
|
|
|
|
|
ContainerID *cid.ID
|
|
|
|
|
|
|
|
ObjectID *oid.ID
|
|
|
|
|
|
|
|
Key *ecdsa.PrivateKey
|
2022-11-22 16:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UseKey specifies private key to sign the requests.
|
|
|
|
// If key is not provided, then Client default key is used.
|
2023-09-04 09:42:55 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmObjectHash.Key instead.
|
|
|
|
func (prm *PrmObjectHash) UseKey(key ecdsa.PrivateKey) {
|
|
|
|
prm.Key = &key
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
// TillichZemorAlgo changes the hash function to Tillich-Zemor
|
|
|
|
// (https://link.springer.com/content/pdf/10.1007/3-540-48658-5_5.pdf).
|
2022-02-15 05:36:47 +00:00
|
|
|
//
|
2023-09-04 09:42:55 +00:00
|
|
|
// By default, SHA256 hash function is used/.
|
2022-02-15 05:36:47 +00:00
|
|
|
//
|
2023-09-04 09:42:55 +00:00
|
|
|
// Deprecated: Use PrmObjectHash.ChecksumType instead.
|
|
|
|
func (prm *PrmObjectHash) TillichZemorAlgo() {
|
|
|
|
prm.ChecksumType = checksum.TZ
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResObjectHash groups resulting values of ObjectHash operation.
|
|
|
|
type ResObjectHash struct {
|
|
|
|
statusRes
|
2022-04-07 16:09:15 +00:00
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
checksums [][]byte
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
// Checksums returns a list of calculated checksums in range order.
|
|
|
|
func (x ResObjectHash) Checksums() [][]byte {
|
|
|
|
return x.checksums
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
func (prm *PrmObjectHash) buildRequest(c *Client) (*v2object.GetRangeHashRequest, error) {
|
|
|
|
if prm.ContainerID == nil {
|
|
|
|
return nil, errorMissingContainer
|
|
|
|
}
|
2022-04-11 16:25:14 +00:00
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
if prm.ObjectID == nil {
|
|
|
|
return nil, errorMissingObject
|
|
|
|
}
|
2022-02-15 05:36:47 +00:00
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
if len(prm.XHeaders)%2 != 0 {
|
|
|
|
return nil, errorInvalidXHeaders
|
|
|
|
}
|
2022-04-11 16:25:14 +00:00
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
if len(prm.Ranges) == 0 {
|
|
|
|
return nil, errorMissingRanges
|
|
|
|
}
|
2022-02-15 05:36:47 +00:00
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
meta := new(v2session.RequestMetaHeader)
|
|
|
|
writeXHeadersToMeta(prm.XHeaders, meta)
|
|
|
|
|
|
|
|
if prm.BearerToken != nil {
|
|
|
|
v2BearerToken := new(acl.BearerToken)
|
|
|
|
prm.BearerToken.WriteToV2(v2BearerToken)
|
|
|
|
meta.SetBearerToken(v2BearerToken)
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
if prm.Session != nil {
|
|
|
|
v2SessionToken := new(v2session.Token)
|
|
|
|
prm.Session.WriteToV2(v2SessionToken)
|
|
|
|
meta.SetSessionToken(v2SessionToken)
|
|
|
|
}
|
2022-02-15 05:36:47 +00:00
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
if prm.Local {
|
|
|
|
meta.SetTTL(1)
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
addr := new(v2refs.Address)
|
2022-02-15 05:36:47 +00:00
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
cnrV2 := new(v2refs.ContainerID)
|
|
|
|
prm.ContainerID.WriteToV2(cnrV2)
|
|
|
|
addr.SetContainerID(cnrV2)
|
2022-02-15 05:36:47 +00:00
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
objV2 := new(v2refs.ObjectID)
|
|
|
|
prm.ObjectID.WriteToV2(objV2)
|
|
|
|
addr.SetObjectID(objV2)
|
2022-02-15 05:36:47 +00:00
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
rs := make([]v2object.Range, len(prm.Ranges))
|
|
|
|
for i := range prm.Ranges {
|
|
|
|
rs[i].SetOffset(prm.Ranges[i].GetOffset())
|
|
|
|
rs[i].SetLength(prm.Ranges[i].GetLength())
|
|
|
|
}
|
2022-03-03 11:04:53 +00:00
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
body := new(v2object.GetRangeHashRequestBody)
|
|
|
|
body.SetAddress(addr)
|
|
|
|
body.SetRanges(rs)
|
|
|
|
body.SetSalt(prm.Salt)
|
2022-02-15 05:36:47 +00:00
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
if prm.ChecksumType == checksum.Unknown {
|
|
|
|
body.SetType(v2refs.SHA256)
|
|
|
|
} else {
|
|
|
|
body.SetType(v2refs.ChecksumType(prm.ChecksumType))
|
|
|
|
}
|
2022-02-15 05:36:47 +00:00
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
req := new(v2object.GetRangeHashRequest)
|
|
|
|
req.SetBody(body)
|
|
|
|
c.prepareRequest(req, meta)
|
|
|
|
|
|
|
|
return req, nil
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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`,
|
2023-09-15 15:52:08 +00:00
|
|
|
// If PrmInit.DisableFrostFSFailuresResolution has been called, unsuccessful
|
2023-09-12 15:57:04 +00:00
|
|
|
// FrostFS status codes are included in the returned result structure,
|
|
|
|
// otherwise, are also returned as `error`.
|
2022-02-15 05:36:47 +00:00
|
|
|
//
|
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) {
|
2023-09-04 09:42:55 +00:00
|
|
|
req, err := prm.buildRequest(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-15 05:36:47 +00:00
|
|
|
}
|
|
|
|
|
2023-11-13 12:18:07 +00:00
|
|
|
key := c.prm.Key
|
2023-09-04 09:42:55 +00:00
|
|
|
if prm.Key != nil {
|
|
|
|
key = *prm.Key
|
2022-11-22 16:22:00 +00:00
|
|
|
}
|
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
err = signature.SignServiceMessage(&key, req)
|
2022-08-23 17:51:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("sign request: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-09-04 09:42:55 +00:00
|
|
|
resp, err := rpcapi.HashObjectRange(&c.c, req, client.WithContext(ctx))
|
2022-08-23 17:51:05 +00:00
|
|
|
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)
|
2024-09-24 13:31:56 +00:00
|
|
|
if err != nil || !apistatus.IsSuccessful(res.st) {
|
|
|
|
return &res, err
|
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
|
|
|
|
}
|