2020-09-16 14:46:31 +00:00
|
|
|
package transformer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
2020-09-16 14:46:31 +00:00
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/netmap"
|
|
|
|
"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"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/version"
|
2020-09-16 14:46:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type formatter struct {
|
2020-10-21 15:10:22 +00:00
|
|
|
prm *FormatterParams
|
2020-09-16 14:46:31 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
obj *object.Object
|
2020-09-16 14:46:31 +00:00
|
|
|
|
|
|
|
sz uint64
|
2020-10-21 15:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FormatterParams groups NewFormatTarget parameters.
|
|
|
|
type FormatterParams struct {
|
|
|
|
Key *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
NextTarget ObjectTarget
|
2020-09-29 12:41:29 +00:00
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
SessionToken *session.Object
|
2020-10-21 15:11:19 +00:00
|
|
|
|
|
|
|
NetworkState netmap.State
|
2020-09-16 14:46:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFormatTarget returns ObjectTarget instance that finalizes object structure
|
|
|
|
// and writes it to the next target.
|
|
|
|
//
|
|
|
|
// Chunks must be written before the WriteHeader call.
|
|
|
|
//
|
|
|
|
// Object changes:
|
|
|
|
// - sets version to current SDK version;
|
|
|
|
// - sets payload size to the total length of all written chunks;
|
2020-09-29 12:41:29 +00:00
|
|
|
// - sets session token;
|
2020-10-21 15:11:19 +00:00
|
|
|
// - sets number of creation epoch;
|
2020-09-16 14:46:31 +00:00
|
|
|
// - calculates and sets verification fields (ID, Signature).
|
2020-10-21 15:10:22 +00:00
|
|
|
func NewFormatTarget(p *FormatterParams) ObjectTarget {
|
2020-09-16 14:46:31 +00:00
|
|
|
return &formatter{
|
2020-10-21 15:10:22 +00:00
|
|
|
prm: p,
|
2020-09-16 14:46:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
func (f *formatter) WriteHeader(obj *object.Object) error {
|
2020-09-16 14:46:31 +00:00
|
|
|
f.obj = obj
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *formatter) Write(p []byte) (n int, err error) {
|
2020-10-21 15:10:22 +00:00
|
|
|
n, err = f.prm.NextTarget.Write(p)
|
2020-09-16 14:46:31 +00:00
|
|
|
|
|
|
|
f.sz += uint64(n)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *formatter) Close() (*AccessIdentifiers, error) {
|
2020-10-21 15:11:19 +00:00
|
|
|
curEpoch := f.prm.NetworkState.CurrentEpoch()
|
2022-05-11 14:58:52 +00:00
|
|
|
ver := version.Current()
|
2020-10-21 15:11:19 +00:00
|
|
|
|
2022-05-11 14:58:52 +00:00
|
|
|
f.obj.SetVersion(&ver)
|
2020-09-16 14:46:31 +00:00
|
|
|
f.obj.SetPayloadSize(f.sz)
|
2020-10-21 15:10:22 +00:00
|
|
|
f.obj.SetSessionToken(f.prm.SessionToken)
|
2020-10-21 15:11:19 +00:00
|
|
|
f.obj.SetCreationEpoch(curEpoch)
|
2020-09-16 14:46:31 +00:00
|
|
|
|
2020-12-18 10:39:44 +00:00
|
|
|
var (
|
2022-05-31 17:00:41 +00:00
|
|
|
parID *oid.ID
|
2022-03-03 14:19:05 +00:00
|
|
|
parHdr *object.Object
|
2020-12-18 10:39:44 +00:00
|
|
|
)
|
2020-09-16 14:46:31 +00:00
|
|
|
|
2020-12-18 10:39:44 +00:00
|
|
|
if par := f.obj.Parent(); par != nil && par.Signature() == nil {
|
2022-03-03 14:19:05 +00:00
|
|
|
rawPar := object.NewFromV2(par.ToV2())
|
2020-09-16 14:46:31 +00:00
|
|
|
|
2020-10-21 15:10:22 +00:00
|
|
|
rawPar.SetSessionToken(f.prm.SessionToken)
|
2020-10-21 15:11:19 +00:00
|
|
|
rawPar.SetCreationEpoch(curEpoch)
|
2020-09-29 12:41:29 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
if err := object.SetIDWithSignature(*f.prm.Key, rawPar); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not finalize parent object: %w", err)
|
2020-09-16 14:46:31 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
id, _ := rawPar.ID()
|
|
|
|
parID = &id
|
2022-03-03 14:19:05 +00:00
|
|
|
parHdr = rawPar
|
2020-09-16 14:46:31 +00:00
|
|
|
|
2020-12-18 10:39:44 +00:00
|
|
|
f.obj.SetParent(parHdr)
|
2020-09-16 14:46:31 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
if err := object.SetIDWithSignature(*f.prm.Key, f.obj); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not finalize object: %w", err)
|
2020-09-16 14:46:31 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 15:10:22 +00:00
|
|
|
if err := f.prm.NextTarget.WriteHeader(f.obj); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not write header to next target: %w", err)
|
2020-09-16 14:46:31 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 15:10:22 +00:00
|
|
|
if _, err := f.prm.NextTarget.Close(); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not close next target: %w", err)
|
2020-09-16 14:46:31 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
id, _ := f.obj.ID()
|
|
|
|
|
2020-09-16 14:46:31 +00:00
|
|
|
return new(AccessIdentifiers).
|
2022-05-31 17:00:41 +00:00
|
|
|
WithSelfID(id).
|
2020-12-18 10:39:44 +00:00
|
|
|
WithParentID(parID).
|
|
|
|
WithParent(parHdr), nil
|
2020-09-16 14:46:31 +00:00
|
|
|
}
|