2018-10-05 14:34:15 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package audit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"math/big"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/vivint/infectious"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/eestream"
|
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
"storj.io/storj/pkg/pointerdb/pdbclient"
|
|
|
|
"storj.io/storj/pkg/storage/meta"
|
2018-10-25 21:28:16 +01:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-10-05 14:34:15 +01:00
|
|
|
)
|
|
|
|
|
2018-10-10 19:25:46 +01:00
|
|
|
// Stripe keeps track of a stripe's index and its parent segment
|
|
|
|
type Stripe struct {
|
2018-11-07 01:16:43 +00:00
|
|
|
Index int
|
|
|
|
Segment *pb.Pointer
|
2019-01-06 18:51:01 +00:00
|
|
|
PBA *pb.PayerBandwidthAllocation
|
2018-11-07 01:16:43 +00:00
|
|
|
Authorization *pb.SignedMessage
|
2018-10-10 19:25:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cursor keeps track of audit location in pointer db
|
|
|
|
type Cursor struct {
|
2018-10-05 14:34:15 +01:00
|
|
|
pointers pdbclient.Client
|
2018-10-25 21:28:16 +01:00
|
|
|
lastPath storj.Path
|
2018-10-05 14:34:15 +01:00
|
|
|
mutex sync.Mutex
|
|
|
|
}
|
|
|
|
|
2018-10-10 19:25:46 +01:00
|
|
|
// NewCursor creates a Cursor which iterates over pointer db
|
|
|
|
func NewCursor(pointers pdbclient.Client) *Cursor {
|
|
|
|
return &Cursor{pointers: pointers}
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NextStripe returns a random stripe to be audited
|
2018-10-10 19:25:46 +01:00
|
|
|
func (cursor *Cursor) NextStripe(ctx context.Context) (stripe *Stripe, err error) {
|
|
|
|
cursor.mutex.Lock()
|
|
|
|
defer cursor.mutex.Unlock()
|
2018-10-05 14:34:15 +01:00
|
|
|
|
|
|
|
var pointerItems []pdbclient.ListItem
|
2018-10-25 21:28:16 +01:00
|
|
|
var path storj.Path
|
2018-10-10 19:25:46 +01:00
|
|
|
var more bool
|
2018-10-05 14:34:15 +01:00
|
|
|
|
2018-10-25 21:28:16 +01:00
|
|
|
if cursor.lastPath == "" {
|
|
|
|
pointerItems, more, err = cursor.pointers.List(ctx, "", "", "", true, 0, meta.None)
|
2018-10-05 14:34:15 +01:00
|
|
|
} else {
|
2018-10-25 21:28:16 +01:00
|
|
|
pointerItems, more, err = cursor.pointers.List(ctx, "", cursor.lastPath, "", true, 0, meta.None)
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
2018-10-10 19:25:46 +01:00
|
|
|
return nil, err
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
|
|
|
|
2018-11-07 01:16:43 +00:00
|
|
|
if len(pointerItems) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 14:34:15 +01:00
|
|
|
pointerItem, err := getRandomPointer(pointerItems)
|
|
|
|
if err != nil {
|
2018-10-10 19:25:46 +01:00
|
|
|
return nil, err
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
path = pointerItem.Path
|
|
|
|
|
|
|
|
// keep track of last path listed
|
|
|
|
if !more {
|
2018-10-25 21:28:16 +01:00
|
|
|
cursor.lastPath = ""
|
2018-10-05 14:34:15 +01:00
|
|
|
} else {
|
2018-10-25 21:28:16 +01:00
|
|
|
cursor.lastPath = pointerItems[len(pointerItems)-1].Path
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// get pointer info
|
2019-01-06 18:51:01 +00:00
|
|
|
pointer, _, pba, err := cursor.pointers.Get(ctx, path)
|
2018-10-05 14:34:15 +01:00
|
|
|
if err != nil {
|
2018-10-10 19:25:46 +01:00
|
|
|
return nil, err
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
|
|
|
|
2018-11-07 01:16:43 +00:00
|
|
|
if pointer.GetType() != pb.Pointer_REMOTE {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 14:34:15 +01:00
|
|
|
// create the erasure scheme so we can get the stripe size
|
|
|
|
es, err := makeErasureScheme(pointer.GetRemote().GetRedundancy())
|
|
|
|
if err != nil {
|
2018-10-10 19:25:46 +01:00
|
|
|
return nil, err
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
|
|
|
|
2018-11-20 17:09:35 +00:00
|
|
|
if pointer.GetSegmentSize() == 0 {
|
2018-11-07 16:51:36 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-10-05 14:34:15 +01:00
|
|
|
index, err := getRandomStripe(es, pointer)
|
|
|
|
if err != nil {
|
2018-10-10 19:25:46 +01:00
|
|
|
return nil, err
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
|
|
|
|
2018-11-07 01:16:43 +00:00
|
|
|
authorization := cursor.pointers.SignedMessage()
|
2019-01-06 18:51:01 +00:00
|
|
|
return &Stripe{Index: index, Segment: pointer, PBA: pba, Authorization: authorization}, nil
|
2018-10-05 14:34:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func makeErasureScheme(rs *pb.RedundancyScheme) (eestream.ErasureScheme, error) {
|
2018-11-07 01:16:43 +00:00
|
|
|
required := int(rs.GetMinReq())
|
|
|
|
total := int(rs.GetTotal())
|
|
|
|
|
|
|
|
fc, err := infectious.NewFEC(required, total)
|
2018-10-05 14:34:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
es := eestream.NewRSScheme(fc, int(rs.GetErasureShareSize()))
|
|
|
|
return es, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRandomStripe(es eestream.ErasureScheme, pointer *pb.Pointer) (index int, err error) {
|
|
|
|
stripeSize := es.StripeSize()
|
2018-11-07 21:23:05 +00:00
|
|
|
|
|
|
|
// the last segment could be smaller than stripe size
|
2018-11-20 17:09:35 +00:00
|
|
|
if pointer.GetSegmentSize() < int64(stripeSize) {
|
2018-11-07 21:23:05 +00:00
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2018-11-20 17:09:35 +00:00
|
|
|
randomStripeIndex, err := rand.Int(rand.Reader, big.NewInt(pointer.GetSegmentSize()/int64(stripeSize)))
|
2018-10-05 14:34:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return int(randomStripeIndex.Int64()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRandomPointer(pointerItems []pdbclient.ListItem) (pointer pdbclient.ListItem, err error) {
|
|
|
|
randomNum, err := rand.Int(rand.Reader, big.NewInt(int64(len(pointerItems))))
|
|
|
|
if err != nil {
|
|
|
|
return pdbclient.ListItem{}, err
|
|
|
|
}
|
|
|
|
randomNumInt64 := randomNum.Int64()
|
|
|
|
pointerItem := pointerItems[randomNumInt64]
|
|
|
|
return pointerItem, nil
|
|
|
|
}
|