2023-03-31 13:30:46 +00:00
|
|
|
package getsvc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-04-04 10:20:57 +00:00
|
|
|
"crypto/ecdsa"
|
2023-03-31 13:30:46 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/network"
|
2023-05-31 09:24:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2024-11-07 14:32:10 +00:00
|
|
|
objectV2 "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/object"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/rpc"
|
|
|
|
rpcclient "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/rpc/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/session"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/signature"
|
2023-03-31 13:30:46 +00:00
|
|
|
frostfscrypto "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto"
|
2023-07-06 12:36:41 +00:00
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-03-31 13:30:46 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2023-03-13 11:37:35 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2023-03-31 13:30:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type headRequestForwarder struct {
|
|
|
|
Request *objectV2.HeadRequest
|
2023-05-30 07:14:37 +00:00
|
|
|
OnceResign sync.Once
|
2023-03-31 13:30:46 +00:00
|
|
|
ObjectAddr oid.Address
|
2023-04-04 10:20:57 +00:00
|
|
|
Key *ecdsa.PrivateKey
|
2023-03-31 13:30:46 +00:00
|
|
|
}
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
func (f *headRequestForwarder) forwardRequestToNode(ctx context.Context, addr network.Address, c client.MultiAddressClient, pubkey []byte) (*objectSDK.Object, error) {
|
2023-03-13 11:37:35 +00:00
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "headRequestForwarder.forwardRequestToNode",
|
|
|
|
trace.WithAttributes(attribute.String("address", addr.String())),
|
|
|
|
)
|
|
|
|
defer span.End()
|
|
|
|
|
2023-03-31 13:30:46 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
// once compose and resign forwarding request
|
|
|
|
f.OnceResign.Do(func() {
|
|
|
|
// compose meta header of the local server
|
|
|
|
metaHdr := new(session.RequestMetaHeader)
|
|
|
|
metaHdr.SetTTL(f.Request.GetMetaHeader().GetTTL() - 1)
|
|
|
|
// TODO: #1165 think how to set the other fields
|
|
|
|
metaHdr.SetOrigin(f.Request.GetMetaHeader())
|
|
|
|
writeCurrentVersion(metaHdr)
|
|
|
|
|
|
|
|
f.Request.SetMetaHeader(metaHdr)
|
|
|
|
|
2023-04-04 10:20:57 +00:00
|
|
|
err = signature.SignServiceMessage(f.Key, f.Request)
|
2023-03-31 13:30:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
headResp, err := f.sendHeadRequest(ctx, addr, c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-15 08:56:17 +00:00
|
|
|
if err := verifyResponse(headResp, pubkey); err != nil {
|
2023-03-31 13:30:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
hdr *objectV2.Header
|
|
|
|
idSig *refs.Signature
|
|
|
|
)
|
|
|
|
|
|
|
|
switch v := headResp.GetBody().GetHeaderPart().(type) {
|
|
|
|
case nil:
|
2023-04-24 10:11:44 +00:00
|
|
|
return nil, errUnexpectedHeaderPart(v)
|
2023-03-31 13:30:46 +00:00
|
|
|
case *objectV2.ShortHeader:
|
|
|
|
if hdr, err = f.getHeaderFromShortHeader(v); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case *objectV2.HeaderWithSignature:
|
|
|
|
if hdr, idSig, err = f.getHeaderAndSignature(v); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case *objectV2.SplitInfo:
|
2023-07-06 12:36:41 +00:00
|
|
|
si := objectSDK.NewSplitInfoFromV2(v)
|
|
|
|
return nil, objectSDK.NewSplitInfoError(si)
|
2024-04-22 06:43:42 +00:00
|
|
|
case *objectV2.ECInfo:
|
|
|
|
ei := objectSDK.NewECInfoFromV2(v)
|
|
|
|
return nil, objectSDK.NewECInfoError(ei)
|
2023-03-31 13:30:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
objv2 := new(objectV2.Object)
|
|
|
|
objv2.SetHeader(hdr)
|
|
|
|
objv2.SetSignature(idSig)
|
|
|
|
|
2023-07-06 12:36:41 +00:00
|
|
|
obj := objectSDK.NewFromV2(objv2)
|
2023-03-31 13:30:46 +00:00
|
|
|
obj.SetID(f.ObjectAddr.Object())
|
|
|
|
|
|
|
|
return obj, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *headRequestForwarder) getHeaderFromShortHeader(sh *objectV2.ShortHeader) (*objectV2.Header, error) {
|
|
|
|
if !f.Request.GetBody().GetMainOnly() {
|
2023-04-24 10:11:44 +00:00
|
|
|
return nil, errWrongHeaderPartTypeExpShortRecvWithSignature
|
2023-03-31 13:30:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hdr := new(objectV2.Header)
|
|
|
|
hdr.SetPayloadLength(sh.GetPayloadLength())
|
|
|
|
hdr.SetVersion(sh.GetVersion())
|
|
|
|
hdr.SetOwnerID(sh.GetOwnerID())
|
|
|
|
hdr.SetObjectType(sh.GetObjectType())
|
|
|
|
hdr.SetCreationEpoch(sh.GetCreationEpoch())
|
|
|
|
hdr.SetPayloadHash(sh.GetPayloadHash())
|
|
|
|
hdr.SetHomomorphicHash(sh.GetHomomorphicHash())
|
|
|
|
return hdr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *headRequestForwarder) getHeaderAndSignature(hdrWithSig *objectV2.HeaderWithSignature) (*objectV2.Header, *refs.Signature, error) {
|
|
|
|
if f.Request.GetBody().GetMainOnly() {
|
2023-04-24 10:11:44 +00:00
|
|
|
return nil, nil, errWrongHeaderPartTypeExpWithSignRecvShort
|
2023-03-31 13:30:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if hdrWithSig == nil {
|
2023-04-24 10:11:44 +00:00
|
|
|
return nil, nil, errNilObjectPart
|
2023-03-31 13:30:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hdr := hdrWithSig.GetHeader()
|
|
|
|
idSig := hdrWithSig.GetSignature()
|
|
|
|
|
|
|
|
if idSig == nil {
|
2023-04-24 10:11:44 +00:00
|
|
|
return nil, nil, errMissingSignature
|
2023-03-31 13:30:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
binID, err := f.ObjectAddr.Object().Marshal()
|
|
|
|
if err != nil {
|
2023-04-24 10:11:44 +00:00
|
|
|
return nil, nil, errMarshalID(err)
|
2023-03-31 13:30:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var sig frostfscrypto.Signature
|
|
|
|
if err := sig.ReadFromV2(*idSig); err != nil {
|
2023-04-24 10:11:44 +00:00
|
|
|
return nil, nil, errCantReadSignature(err)
|
2023-03-31 13:30:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !sig.Verify(binID) {
|
2023-04-24 10:11:44 +00:00
|
|
|
return nil, nil, errInvalidObjectIDSign
|
2023-03-31 13:30:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hdr, idSig, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *headRequestForwarder) sendHeadRequest(ctx context.Context, addr network.Address, c client.MultiAddressClient) (*objectV2.HeadResponse, error) {
|
|
|
|
var headResp *objectV2.HeadResponse
|
2023-04-18 09:04:59 +00:00
|
|
|
err := c.RawForAddress(ctx, addr, func(cli *rpcclient.Client) error {
|
2023-03-31 13:30:46 +00:00
|
|
|
var e error
|
|
|
|
headResp, e = rpc.HeadObject(cli, f.Request, rpcclient.WithContext(ctx))
|
|
|
|
return e
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-04-24 10:11:44 +00:00
|
|
|
return nil, errSendingRequestFailed(err)
|
2023-03-31 13:30:46 +00:00
|
|
|
}
|
|
|
|
return headResp, nil
|
|
|
|
}
|