forked from TrueCloudLab/frostfs-node
[#291] object: Split validating target in two
Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
parent
ee58b390bb
commit
35ea207df6
2 changed files with 70 additions and 58 deletions
|
@ -77,7 +77,7 @@ func (p *Streamer) initUntrustedTarget(prm *PutInitPrm) error {
|
|||
p.relay = prm.relay
|
||||
|
||||
// prepare untrusted-Put object target
|
||||
p.target = &validatingTarget{
|
||||
p.target = &validatingPreparedTarget{
|
||||
nextTarget: p.newCommonTarget(prm),
|
||||
fmt: p.fmtValidator,
|
||||
|
||||
|
@ -126,7 +126,6 @@ func (p *Streamer) initTrustedTarget(prm *PutInitPrm) error {
|
|||
p.sessionKey = sessionKey
|
||||
p.target = &validatingTarget{
|
||||
fmt: p.fmtValidator,
|
||||
unpreparedObject: true,
|
||||
nextTarget: transformer.NewPayloadSizeLimiter(
|
||||
p.maxPayloadSz,
|
||||
containerSDK.IsHomomorphicHashingDisabled(prm.cnr),
|
||||
|
|
|
@ -15,13 +15,18 @@ import (
|
|||
"git.frostfs.info/TrueCloudLab/tzhash/tz"
|
||||
)
|
||||
|
||||
// validatingTarget validates object format and content.
|
||||
// validatingTarget validates unprepared object format and content (streaming PUT case).
|
||||
type validatingTarget struct {
|
||||
nextTarget transformer.ObjectTarget
|
||||
|
||||
fmt *object.FormatValidator
|
||||
}
|
||||
|
||||
unpreparedObject bool
|
||||
// validatingPreparedTarget validates prepared object format and content.
|
||||
type validatingPreparedTarget struct {
|
||||
nextTarget transformer.ObjectTarget
|
||||
|
||||
fmt *object.FormatValidator
|
||||
|
||||
hash hash.Hash
|
||||
|
||||
|
@ -42,10 +47,25 @@ var (
|
|||
)
|
||||
|
||||
func (t *validatingTarget) WriteHeader(ctx context.Context, obj *objectSDK.Object) error {
|
||||
if err := t.fmt.Validate(ctx, obj, true); err != nil {
|
||||
return fmt.Errorf("(%T) coult not validate object format: %w", t, err)
|
||||
}
|
||||
|
||||
return t.nextTarget.WriteHeader(ctx, obj)
|
||||
}
|
||||
|
||||
func (t *validatingTarget) Write(ctx context.Context, p []byte) (n int, err error) {
|
||||
return t.nextTarget.Write(ctx, p)
|
||||
}
|
||||
|
||||
func (t *validatingTarget) Close(ctx context.Context) (*transformer.AccessIdentifiers, error) {
|
||||
return t.nextTarget.Close(ctx)
|
||||
}
|
||||
|
||||
func (t *validatingPreparedTarget) WriteHeader(ctx context.Context, obj *objectSDK.Object) error {
|
||||
t.payloadSz = obj.PayloadSize()
|
||||
chunkLn := uint64(len(obj.Payload()))
|
||||
|
||||
if !t.unpreparedObject {
|
||||
// check chunk size
|
||||
if chunkLn > t.payloadSz {
|
||||
return ErrWrongPayloadSize
|
||||
|
@ -71,9 +91,8 @@ func (t *validatingTarget) WriteHeader(ctx context.Context, obj *objectSDK.Objec
|
|||
}
|
||||
|
||||
t.checksum = cs.Value()
|
||||
}
|
||||
|
||||
if err := t.fmt.Validate(ctx, obj, t.unpreparedObject); err != nil {
|
||||
if err := t.fmt.Validate(ctx, obj, false); err != nil {
|
||||
return fmt.Errorf("(%T) coult not validate object format: %w", t, err)
|
||||
}
|
||||
|
||||
|
@ -82,21 +101,18 @@ func (t *validatingTarget) WriteHeader(ctx context.Context, obj *objectSDK.Objec
|
|||
return err
|
||||
}
|
||||
|
||||
if !t.unpreparedObject {
|
||||
// update written bytes
|
||||
//
|
||||
// Note: we MUST NOT add obj.PayloadSize() since obj
|
||||
// can carry only the chunk of the full payload
|
||||
t.writtenPayload += chunkLn
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *validatingTarget) Write(ctx context.Context, p []byte) (n int, err error) {
|
||||
func (t *validatingPreparedTarget) Write(ctx context.Context, p []byte) (n int, err error) {
|
||||
chunkLn := uint64(len(p))
|
||||
|
||||
if !t.unpreparedObject {
|
||||
// check if new chunk will overflow payload size
|
||||
if t.writtenPayload+chunkLn > t.payloadSz {
|
||||
return 0, ErrWrongPayloadSize
|
||||
|
@ -106,7 +122,6 @@ func (t *validatingTarget) Write(ctx context.Context, p []byte) (n int, err erro
|
|||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
n, err = t.nextTarget.Write(ctx, p)
|
||||
if err == nil {
|
||||
|
@ -116,8 +131,7 @@ func (t *validatingTarget) Write(ctx context.Context, p []byte) (n int, err erro
|
|||
return
|
||||
}
|
||||
|
||||
func (t *validatingTarget) Close(ctx context.Context) (*transformer.AccessIdentifiers, error) {
|
||||
if !t.unpreparedObject {
|
||||
func (t *validatingPreparedTarget) Close(ctx context.Context) (*transformer.AccessIdentifiers, error) {
|
||||
// check payload size correctness
|
||||
if t.payloadSz != t.writtenPayload {
|
||||
return nil, ErrWrongPayloadSize
|
||||
|
@ -126,7 +140,6 @@ func (t *validatingTarget) Close(ctx context.Context) (*transformer.AccessIdenti
|
|||
if !bytes.Equal(t.hash.Sum(nil), t.checksum) {
|
||||
return nil, fmt.Errorf("(%T) incorrect payload checksum", t)
|
||||
}
|
||||
}
|
||||
|
||||
return t.nextTarget.Close(ctx)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue