2018-07-16 21:44:28 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package streams
|
|
|
|
|
|
|
|
import (
|
2018-09-26 14:32:23 +01:00
|
|
|
"bytes"
|
2018-07-16 21:44:28 +01:00
|
|
|
"context"
|
2018-09-26 14:32:23 +01:00
|
|
|
"crypto/rand"
|
2018-08-24 04:56:38 +01:00
|
|
|
"fmt"
|
2018-07-16 21:44:28 +01:00
|
|
|
"io"
|
2018-09-26 14:32:23 +01:00
|
|
|
"io/ioutil"
|
2018-07-16 21:44:28 +01:00
|
|
|
"time"
|
|
|
|
|
2018-08-24 04:56:38 +01:00
|
|
|
proto "github.com/gogo/protobuf/proto"
|
|
|
|
"github.com/zeebo/errs"
|
2018-10-04 14:52:12 +01:00
|
|
|
"go.uber.org/zap"
|
2018-08-24 04:56:38 +01:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
|
|
|
|
2018-09-26 14:32:23 +01:00
|
|
|
"storj.io/storj/pkg/eestream"
|
2018-07-16 21:44:28 +01:00
|
|
|
"storj.io/storj/pkg/paths"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-08-24 04:56:38 +01:00
|
|
|
ranger "storj.io/storj/pkg/ranger"
|
2018-09-12 14:03:31 +01:00
|
|
|
"storj.io/storj/pkg/storage/meta"
|
2018-08-24 04:56:38 +01:00
|
|
|
"storj.io/storj/pkg/storage/segments"
|
2018-10-08 15:19:54 +01:00
|
|
|
"storj.io/storj/storage"
|
2018-07-16 21:44:28 +01:00
|
|
|
)
|
|
|
|
|
2018-08-24 04:56:38 +01:00
|
|
|
var mon = monkit.Package()
|
|
|
|
|
2018-07-30 19:57:50 +01:00
|
|
|
// Meta info about a segment
|
|
|
|
type Meta struct {
|
|
|
|
Modified time.Time
|
|
|
|
Expiration time.Time
|
|
|
|
Size int64
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
2018-08-24 04:56:38 +01:00
|
|
|
// convertMeta converts segment metadata to stream metadata
|
2018-10-03 14:05:40 +01:00
|
|
|
func convertMeta(lastSegmentMeta segments.Meta) (Meta, error) {
|
2018-09-18 05:39:06 +01:00
|
|
|
msi := pb.MetaStreamInfo{}
|
2018-10-03 14:05:40 +01:00
|
|
|
err := proto.Unmarshal(lastSegmentMeta.Data, &msi)
|
2018-08-24 04:56:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return Meta{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return Meta{
|
2018-10-03 14:05:40 +01:00
|
|
|
Modified: lastSegmentMeta.Modified,
|
|
|
|
Expiration: lastSegmentMeta.Expiration,
|
2018-08-24 04:56:38 +01:00
|
|
|
Size: ((msi.NumberOfSegments - 1) * msi.SegmentsSize) + msi.LastSegmentSize,
|
|
|
|
Data: msi.Metadata,
|
|
|
|
}, nil
|
2018-07-30 19:57:50 +01:00
|
|
|
}
|
|
|
|
|
2018-08-24 04:56:38 +01:00
|
|
|
// Store interface methods for streams to satisfy to be a store
|
2018-07-16 21:44:28 +01:00
|
|
|
type Store interface {
|
2018-07-30 19:57:50 +01:00
|
|
|
Meta(ctx context.Context, path paths.Path) (Meta, error)
|
2018-09-14 15:10:43 +01:00
|
|
|
Get(ctx context.Context, path paths.Path) (ranger.Ranger, Meta, error)
|
2018-10-03 14:05:40 +01:00
|
|
|
Put(ctx context.Context, path paths.Path, data io.Reader, metadata []byte, expiration time.Time) (Meta, error)
|
2018-07-16 21:44:28 +01:00
|
|
|
Delete(ctx context.Context, path paths.Path) error
|
2018-10-03 14:05:40 +01:00
|
|
|
List(ctx context.Context, prefix, startAfter, endBefore paths.Path, recursive bool, limit int, metaFlags uint32) (items []ListItem, more bool, err error)
|
2018-07-16 21:44:28 +01:00
|
|
|
}
|
2018-08-24 04:56:38 +01:00
|
|
|
|
|
|
|
// streamStore is a store for streams
|
|
|
|
type streamStore struct {
|
2018-09-26 14:32:23 +01:00
|
|
|
segments segments.Store
|
|
|
|
segmentSize int64
|
|
|
|
rootKey []byte
|
|
|
|
encBlockSize int
|
|
|
|
encType eestream.Cipher
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewStreamStore stuff
|
2018-09-26 14:32:23 +01:00
|
|
|
func NewStreamStore(segments segments.Store, segmentSize int64, rootKey string, encBlockSize int, encType int) (Store, error) {
|
2018-08-24 04:56:38 +01:00
|
|
|
if segmentSize <= 0 {
|
|
|
|
return nil, errs.New("segment size must be larger than 0")
|
|
|
|
}
|
2018-09-26 14:32:23 +01:00
|
|
|
if rootKey == "" {
|
|
|
|
return nil, errs.New("encryption key must not be empty")
|
|
|
|
}
|
|
|
|
if encBlockSize <= 0 {
|
|
|
|
return nil, errs.New("encryption block size must be larger than 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &streamStore{
|
|
|
|
segments: segments,
|
|
|
|
segmentSize: segmentSize,
|
|
|
|
rootKey: []byte(rootKey),
|
|
|
|
encBlockSize: encBlockSize,
|
|
|
|
encType: eestream.Cipher(encType),
|
|
|
|
}, nil
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put breaks up data as it comes in into s.segmentSize length pieces, then
|
|
|
|
// store the first piece at s0/<path>, second piece at s1/<path>, and the
|
|
|
|
// *last* piece at l/<path>. Store the given metadata, along with the number
|
|
|
|
// of segments, in a new protobuf, in the metadata of l/<path>.
|
2018-10-03 14:05:40 +01:00
|
|
|
func (s *streamStore) Put(ctx context.Context, path paths.Path, data io.Reader, metadata []byte, expiration time.Time) (m Meta, err error) {
|
2018-08-24 04:56:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-10-08 15:19:54 +01:00
|
|
|
// previously file uploaded?
|
|
|
|
err = s.Delete(ctx, path)
|
|
|
|
if err != nil && !storage.ErrKeyNotFound.Has(err) {
|
|
|
|
//something wrong happened checking for an existing
|
|
|
|
//file with the same name
|
|
|
|
return Meta{}, err
|
|
|
|
}
|
|
|
|
|
2018-10-03 14:05:40 +01:00
|
|
|
var currentSegment int64
|
|
|
|
var streamSize int64
|
|
|
|
var putMeta segments.Meta
|
2018-08-24 04:56:38 +01:00
|
|
|
|
2018-10-04 14:52:12 +01:00
|
|
|
defer func() {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
s.cancelHandler(context.Background(), currentSegment, path)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-09-26 14:32:23 +01:00
|
|
|
derivedKey, err := path.DeriveContentKey(s.rootKey)
|
|
|
|
if err != nil {
|
|
|
|
return Meta{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cipher := s.encType
|
|
|
|
|
|
|
|
eofReader := NewEOFReader(data)
|
|
|
|
|
|
|
|
for !eofReader.isEOF() && !eofReader.hasError() {
|
|
|
|
var encKey eestream.Key
|
|
|
|
_, err = rand.Read(encKey[:])
|
|
|
|
if err != nil {
|
|
|
|
return Meta{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var nonce eestream.Nonce
|
2018-10-03 14:05:40 +01:00
|
|
|
_, err := nonce.Increment(currentSegment)
|
2018-09-26 14:32:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return Meta{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
encrypter, err := cipher.NewEncrypter(&encKey, &nonce, s.encBlockSize)
|
|
|
|
if err != nil {
|
|
|
|
return Meta{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
encryptedEncKey, err := cipher.Encrypt(encKey[:], (*eestream.Key)(derivedKey), &nonce)
|
|
|
|
if err != nil {
|
|
|
|
return Meta{}, err
|
|
|
|
}
|
2018-08-24 04:56:38 +01:00
|
|
|
|
2018-09-26 14:32:23 +01:00
|
|
|
sizeReader := NewSizeReader(eofReader)
|
|
|
|
segmentReader := io.LimitReader(sizeReader, s.segmentSize)
|
|
|
|
peekReader := segments.NewPeekThresholdReader(segmentReader)
|
|
|
|
largeData, err := peekReader.IsLargerThan(encrypter.InBlockSize())
|
|
|
|
if err != nil {
|
|
|
|
return Meta{}, err
|
|
|
|
}
|
|
|
|
var transformedReader io.Reader
|
|
|
|
if largeData {
|
|
|
|
paddedReader := eestream.PadReader(ioutil.NopCloser(peekReader), encrypter.InBlockSize())
|
|
|
|
transformedReader = eestream.TransformReader(paddedReader, encrypter, 0)
|
|
|
|
} else {
|
|
|
|
data, err := ioutil.ReadAll(peekReader)
|
|
|
|
if err != nil {
|
|
|
|
return Meta{}, err
|
|
|
|
}
|
|
|
|
cipherData, err := cipher.Encrypt(data, &encKey, &nonce)
|
|
|
|
if err != nil {
|
|
|
|
return Meta{}, err
|
|
|
|
}
|
|
|
|
transformedReader = bytes.NewReader(cipherData)
|
|
|
|
}
|
2018-08-24 04:56:38 +01:00
|
|
|
|
2018-10-03 14:05:40 +01:00
|
|
|
putMeta, err = s.segments.Put(ctx, transformedReader, expiration, func() (paths.Path, []byte, error) {
|
|
|
|
if !eofReader.isEOF() {
|
|
|
|
segmentPath := getSegmentPath(path, currentSegment)
|
|
|
|
return segmentPath, encryptedEncKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lastSegmentPath := path.Prepend("l")
|
|
|
|
msi := pb.MetaStreamInfo{
|
|
|
|
NumberOfSegments: currentSegment + 1,
|
|
|
|
SegmentsSize: s.segmentSize,
|
|
|
|
LastSegmentSize: sizeReader.Size(),
|
|
|
|
Metadata: metadata,
|
|
|
|
EncryptionType: int32(s.encType),
|
|
|
|
EncryptionBlockSize: int32(s.encBlockSize),
|
|
|
|
LastSegmentEncryptionKey: encryptedEncKey,
|
|
|
|
}
|
|
|
|
lastSegmentMeta, err := proto.Marshal(&msi)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return lastSegmentPath, lastSegmentMeta, nil
|
|
|
|
})
|
2018-08-24 04:56:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return Meta{}, err
|
|
|
|
}
|
2018-09-26 14:32:23 +01:00
|
|
|
|
2018-10-03 14:05:40 +01:00
|
|
|
currentSegment++
|
|
|
|
streamSize += sizeReader.Size()
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
2018-09-26 14:32:23 +01:00
|
|
|
if eofReader.hasError() {
|
|
|
|
return Meta{}, eofReader.err
|
2018-09-08 16:41:40 +01:00
|
|
|
}
|
2018-08-24 04:56:38 +01:00
|
|
|
|
|
|
|
resultMeta := Meta{
|
|
|
|
Modified: putMeta.Modified,
|
|
|
|
Expiration: expiration,
|
2018-10-03 14:05:40 +01:00
|
|
|
Size: streamSize,
|
2018-08-24 04:56:38 +01:00
|
|
|
Data: metadata,
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultMeta, nil
|
|
|
|
}
|
|
|
|
|
2018-10-03 14:05:40 +01:00
|
|
|
// getSegmentPath returns the unique path for a particular segment
|
2018-09-26 14:32:23 +01:00
|
|
|
func getSegmentPath(p paths.Path, segNum int64) paths.Path {
|
|
|
|
return p.Prepend(fmt.Sprintf("s%d", segNum))
|
|
|
|
}
|
|
|
|
|
2018-08-24 04:56:38 +01:00
|
|
|
// Get returns a ranger that knows what the overall size is (from l/<path>)
|
|
|
|
// and then returns the appropriate data from segments s0/<path>, s1/<path>,
|
|
|
|
// ..., l/<path>.
|
2018-10-03 14:05:40 +01:00
|
|
|
func (s *streamStore) Get(ctx context.Context, path paths.Path) (rr ranger.Ranger, meta Meta, err error) {
|
2018-08-24 04:56:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-10-03 14:05:40 +01:00
|
|
|
lastSegmentRanger, lastSegmentMeta, err := s.segments.Get(ctx, path.Prepend("l"))
|
2018-08-24 04:56:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, err
|
|
|
|
}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
msi := pb.MetaStreamInfo{}
|
2018-08-24 04:56:38 +01:00
|
|
|
err = proto.Unmarshal(lastSegmentMeta.Data, &msi)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, err
|
|
|
|
}
|
|
|
|
|
2018-10-03 14:05:40 +01:00
|
|
|
streamMeta, err := convertMeta(lastSegmentMeta)
|
2018-08-24 04:56:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, err
|
|
|
|
}
|
|
|
|
|
2018-09-26 14:32:23 +01:00
|
|
|
derivedKey, err := path.DeriveContentKey(s.rootKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, err
|
|
|
|
}
|
2018-08-24 04:56:38 +01:00
|
|
|
|
2018-09-26 14:32:23 +01:00
|
|
|
var rangers []ranger.Ranger
|
2018-10-03 14:05:40 +01:00
|
|
|
for i := int64(0); i < msi.NumberOfSegments-1; i++ {
|
2018-09-26 14:32:23 +01:00
|
|
|
currentPath := getSegmentPath(path, i)
|
2018-09-12 14:06:44 +01:00
|
|
|
size := msi.SegmentsSize
|
2018-09-26 14:32:23 +01:00
|
|
|
var nonce eestream.Nonce
|
|
|
|
_, err := nonce.Increment(i)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, err
|
|
|
|
}
|
2018-09-12 14:06:44 +01:00
|
|
|
rr := &lazySegmentRanger{
|
2018-09-26 14:32:23 +01:00
|
|
|
segments: s.segments,
|
|
|
|
path: currentPath,
|
|
|
|
size: size,
|
|
|
|
derivedKey: (*eestream.Key)(derivedKey),
|
|
|
|
startingNonce: &nonce,
|
|
|
|
encBlockSize: int(msi.EncryptionBlockSize),
|
|
|
|
encType: eestream.Cipher(msi.EncryptionType),
|
2018-09-12 14:06:44 +01:00
|
|
|
}
|
|
|
|
rangers = append(rangers, rr)
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
|
|
|
|
2018-10-03 14:05:40 +01:00
|
|
|
var nonce eestream.Nonce
|
|
|
|
_, err = nonce.Increment(msi.NumberOfSegments - 1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, err
|
|
|
|
}
|
|
|
|
decryptedLastSegmentRanger, err := decryptRanger(
|
|
|
|
ctx,
|
|
|
|
lastSegmentRanger,
|
|
|
|
msi.LastSegmentSize,
|
|
|
|
eestream.Cipher(msi.EncryptionType),
|
|
|
|
msi.LastSegmentEncryptionKey,
|
|
|
|
(*eestream.Key)(derivedKey),
|
|
|
|
&nonce,
|
|
|
|
int(msi.EncryptionBlockSize),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, Meta{}, err
|
|
|
|
}
|
|
|
|
rangers = append(rangers, decryptedLastSegmentRanger)
|
|
|
|
|
2018-08-24 04:56:38 +01:00
|
|
|
catRangers := ranger.Concat(rangers...)
|
|
|
|
|
2018-10-03 14:05:40 +01:00
|
|
|
return catRangers, streamMeta, nil
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Meta implements Store.Meta
|
|
|
|
func (s *streamStore) Meta(ctx context.Context, path paths.Path) (Meta, error) {
|
2018-10-03 14:05:40 +01:00
|
|
|
lastSegmentMeta, err := s.segments.Meta(ctx, path.Prepend("l"))
|
2018-08-24 04:56:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return Meta{}, err
|
|
|
|
}
|
|
|
|
|
2018-10-03 14:05:40 +01:00
|
|
|
streamMeta, err := convertMeta(lastSegmentMeta)
|
2018-08-24 04:56:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return Meta{}, err
|
|
|
|
}
|
|
|
|
|
2018-10-03 14:05:40 +01:00
|
|
|
return streamMeta, nil
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all the segments, with the last one last
|
|
|
|
func (s *streamStore) Delete(ctx context.Context, path paths.Path) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
lastSegmentMeta, err := s.segments.Meta(ctx, path.Prepend("l"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-18 05:39:06 +01:00
|
|
|
msi := pb.MetaStreamInfo{}
|
2018-08-24 04:56:38 +01:00
|
|
|
err = proto.Unmarshal(lastSegmentMeta.Data, &msi)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-03 14:05:40 +01:00
|
|
|
for i := 0; i < int(msi.NumberOfSegments-1); i++ {
|
2018-09-26 14:32:23 +01:00
|
|
|
currentPath := getSegmentPath(path, int64(i))
|
|
|
|
err := s.segments.Delete(ctx, currentPath)
|
2018-08-24 04:56:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.segments.Delete(ctx, path.Prepend("l"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListItem is a single item in a listing
|
|
|
|
type ListItem struct {
|
2018-09-07 15:20:15 +01:00
|
|
|
Path paths.Path
|
|
|
|
Meta Meta
|
|
|
|
IsPrefix bool
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// List all the paths inside l/, stripping off the l/ prefix
|
2018-10-03 14:05:40 +01:00
|
|
|
func (s *streamStore) List(ctx context.Context, prefix, startAfter, endBefore paths.Path, recursive bool, limit int, metaFlags uint32) (items []ListItem, more bool, err error) {
|
2018-08-24 04:56:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2018-09-12 14:03:31 +01:00
|
|
|
if metaFlags&meta.Size != 0 {
|
|
|
|
// Calculating the stream's size require also the user-defined metadata,
|
|
|
|
// where stream store keeps info about the number of segments and their size.
|
|
|
|
metaFlags |= meta.UserDefined
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
segments, more, err := s.segments.List(ctx, prefix.Prepend("l"), startAfter, endBefore, recursive, limit, metaFlags)
|
2018-08-24 04:56:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
2018-09-07 15:20:15 +01:00
|
|
|
items = make([]ListItem, len(segments))
|
|
|
|
for i, item := range segments {
|
2018-08-24 04:56:38 +01:00
|
|
|
newMeta, err := convertMeta(item.Meta)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
2018-09-09 07:34:23 +01:00
|
|
|
items[i] = ListItem{Path: item.Path, Meta: newMeta, IsPrefix: item.IsPrefix}
|
2018-08-24 04:56:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return items, more, nil
|
|
|
|
}
|
2018-09-12 14:06:44 +01:00
|
|
|
|
|
|
|
type lazySegmentRanger struct {
|
2018-09-26 14:32:23 +01:00
|
|
|
ranger ranger.Ranger
|
|
|
|
segments segments.Store
|
|
|
|
path paths.Path
|
|
|
|
size int64
|
|
|
|
derivedKey *eestream.Key
|
|
|
|
startingNonce *eestream.Nonce
|
|
|
|
encBlockSize int
|
|
|
|
encType eestream.Cipher
|
2018-09-12 14:06:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Size implements Ranger.Size
|
|
|
|
func (lr *lazySegmentRanger) Size() int64 {
|
|
|
|
return lr.size
|
|
|
|
}
|
|
|
|
|
|
|
|
// Range implements Ranger.Range to be lazily connected
|
|
|
|
func (lr *lazySegmentRanger) Range(ctx context.Context, offset, length int64) (io.ReadCloser, error) {
|
|
|
|
if lr.ranger == nil {
|
2018-09-26 14:32:23 +01:00
|
|
|
rr, m, err := lr.segments.Get(ctx, lr.path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-03 14:05:40 +01:00
|
|
|
lr.ranger, err = decryptRanger(ctx, rr, lr.size, lr.encType, m.Data, lr.derivedKey, lr.startingNonce, lr.encBlockSize)
|
2018-09-26 14:32:23 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-03 14:05:40 +01:00
|
|
|
}
|
|
|
|
return lr.ranger.Range(ctx, offset, length)
|
|
|
|
}
|
|
|
|
|
|
|
|
// decryptRanger returns a decrypted ranger of the given rr ranger
|
|
|
|
func decryptRanger(ctx context.Context, rr ranger.Ranger, decryptedSize int64, cipher eestream.Cipher, encryptedEncKey []byte, derivedKey *eestream.Key, startingNonce *eestream.Nonce, encBlockSize int) (ranger.Ranger, error) {
|
|
|
|
e, err := cipher.Decrypt(encryptedEncKey, derivedKey, startingNonce)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var encKey eestream.Key
|
|
|
|
copy(encKey[:], e)
|
|
|
|
decrypter, err := cipher.NewDecrypter(&encKey, startingNonce, encBlockSize)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var rd ranger.Ranger
|
|
|
|
if rr.Size()%int64(decrypter.InBlockSize()) != 0 {
|
|
|
|
reader, err := rr.Range(ctx, 0, rr.Size())
|
2018-09-12 14:06:44 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-03 14:05:40 +01:00
|
|
|
cipherData, err := ioutil.ReadAll(reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-09-26 14:32:23 +01:00
|
|
|
}
|
2018-10-03 14:05:40 +01:00
|
|
|
data, err := cipher.Decrypt(cipherData, &encKey, startingNonce)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ranger.ByteRanger(data), nil
|
2018-09-12 14:06:44 +01:00
|
|
|
}
|
2018-10-03 14:05:40 +01:00
|
|
|
|
|
|
|
rd, err = eestream.Transform(rr, decrypter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return eestream.Unpad(rd, int(rd.Size()-decryptedSize))
|
2018-09-12 14:06:44 +01:00
|
|
|
}
|
2018-10-04 14:52:12 +01:00
|
|
|
|
|
|
|
// CancelHandler handles clean up of segments on receiving CTRL+C
|
|
|
|
func (s *streamStore) cancelHandler(ctx context.Context, totalSegments int64, path paths.Path) {
|
|
|
|
for i := int64(0); i < totalSegments; i++ {
|
|
|
|
currentPath := getSegmentPath(path, i)
|
|
|
|
err := s.segments.Delete(ctx, currentPath)
|
|
|
|
if err != nil {
|
|
|
|
zap.S().Warnf("Failed deleting a segment %v %v", currentPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|