2019-03-18 10:55:06 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package metainfo
import (
"context"
2019-06-24 18:15:45 +01:00
"crypto/sha256"
2019-04-02 15:55:58 +01:00
"errors"
2019-03-18 10:55:06 +00:00
"strconv"
2019-04-02 19:21:18 +01:00
"time"
2019-03-18 10:55:06 +00:00
"github.com/skyrings/skyring-common/tools/uuid"
"github.com/zeebo/errs"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
monkit "gopkg.in/spacemonkeygo/monkit.v2"
2019-04-02 19:21:18 +01:00
"storj.io/storj/pkg/accounting"
2019-03-18 10:55:06 +00:00
"storj.io/storj/pkg/eestream"
"storj.io/storj/pkg/identity"
2019-05-24 17:51:27 +01:00
"storj.io/storj/pkg/macaroon"
2019-03-18 10:55:06 +00:00
"storj.io/storj/pkg/overlay"
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/storj"
2019-06-21 20:14:34 +01:00
"storj.io/storj/satellite/attribution"
2019-03-18 10:55:06 +00:00
"storj.io/storj/satellite/console"
2019-03-27 10:24:35 +00:00
"storj.io/storj/satellite/orders"
2019-03-18 10:55:06 +00:00
"storj.io/storj/storage"
)
var (
mon = monkit . Package ( )
// Error general metainfo error
Error = errs . Class ( "metainfo error" )
)
// APIKeys is api keys store methods used by endpoint
type APIKeys interface {
2019-05-24 17:51:27 +01:00
GetByHead ( ctx context . Context , head [ ] byte ) ( * console . APIKeyInfo , error )
}
// Revocations is the revocations store methods used by the endpoint
type Revocations interface {
GetByProjectID ( ctx context . Context , projectID uuid . UUID ) ( [ ] [ ] byte , error )
2019-03-18 10:55:06 +00:00
}
2019-05-24 20:56:08 +01:00
// Containment is a copy/paste of containment interface to avoid import cycle error
type Containment interface {
Delete ( ctx context . Context , nodeID pb . NodeID ) ( bool , error )
}
2019-03-18 10:55:06 +00:00
// Endpoint metainfo endpoint
type Endpoint struct {
2019-06-05 17:41:02 +01:00
log * zap . Logger
metainfo * Service
orders * orders . Service
cache * overlay . Cache
2019-06-21 20:14:34 +01:00
partnerinfo attribution . DB
2019-06-05 17:41:02 +01:00
projectUsage * accounting . ProjectUsage
containment Containment
apiKeys APIKeys
createRequests * createRequests
2019-06-21 19:15:58 +01:00
rsConfig RSConfig
2019-03-18 10:55:06 +00:00
}
// NewEndpoint creates new metainfo endpoint instance
2019-06-21 20:14:34 +01:00
func NewEndpoint ( log * zap . Logger , metainfo * Service , orders * orders . Service , cache * overlay . Cache , partnerinfo attribution . DB ,
containment Containment , apiKeys APIKeys , projectUsage * accounting . ProjectUsage , rsConfig RSConfig ) * Endpoint {
2019-03-18 10:55:06 +00:00
// TODO do something with too many params
return & Endpoint {
2019-06-05 17:41:02 +01:00
log : log ,
metainfo : metainfo ,
orders : orders ,
cache : cache ,
2019-06-21 20:14:34 +01:00
partnerinfo : partnerinfo ,
2019-06-05 17:41:02 +01:00
containment : containment ,
apiKeys : apiKeys ,
projectUsage : projectUsage ,
createRequests : newCreateRequests ( ) ,
2019-06-21 19:15:58 +01:00
rsConfig : rsConfig ,
2019-03-18 10:55:06 +00:00
}
}
// Close closes resources
func ( endpoint * Endpoint ) Close ( ) error { return nil }
// SegmentInfo returns segment metadata info
func ( endpoint * Endpoint ) SegmentInfo ( ctx context . Context , req * pb . SegmentInfoRequest ) ( resp * pb . SegmentInfoResponse , err error ) {
defer mon . Task ( ) ( & ctx ) ( & err )
2019-05-24 17:51:27 +01:00
keyInfo , err := endpoint . validateAuth ( ctx , macaroon . Action {
Op : macaroon . ActionRead ,
Bucket : req . Bucket ,
EncryptedPath : req . Path ,
Time : time . Now ( ) ,
} )
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . Unauthenticated , err . Error ( ) )
}
2019-06-04 12:55:38 +01:00
err = endpoint . validateBucket ( ctx , req . Bucket )
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . InvalidArgument , err . Error ( ) )
}
2019-06-04 12:55:38 +01:00
path , err := CreatePath ( ctx , keyInfo . ProjectID , req . Segment , req . Bucket , req . Path )
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . InvalidArgument , err . Error ( ) )
}
// TODO refactor to use []byte directly
2019-06-05 15:23:10 +01:00
pointer , err := endpoint . metainfo . Get ( ctx , path )
2019-03-18 10:55:06 +00:00
if err != nil {
if storage . ErrKeyNotFound . Has ( err ) {
return nil , status . Errorf ( codes . NotFound , err . Error ( ) )
}
return nil , status . Errorf ( codes . Internal , err . Error ( ) )
}
return & pb . SegmentInfoResponse { Pointer : pointer } , nil
}
// CreateSegment will generate requested number of OrderLimit with coresponding node addresses for them
func ( endpoint * Endpoint ) CreateSegment ( ctx context . Context , req * pb . SegmentWriteRequest ) ( resp * pb . SegmentWriteResponse , err error ) {
defer mon . Task ( ) ( & ctx ) ( & err )
2019-05-24 17:51:27 +01:00
keyInfo , err := endpoint . validateAuth ( ctx , macaroon . Action {
Op : macaroon . ActionWrite ,
Bucket : req . Bucket ,
EncryptedPath : req . Path ,
Time : time . Now ( ) ,
} )
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . Unauthenticated , err . Error ( ) )
}
2019-06-04 12:55:38 +01:00
err = endpoint . validateBucket ( ctx , req . Bucket )
2019-04-01 21:14:58 +01:00
if err != nil {
return nil , status . Errorf ( codes . InvalidArgument , err . Error ( ) )
}
2019-06-04 12:55:38 +01:00
err = endpoint . validateRedundancy ( ctx , req . Redundancy )
2019-04-09 14:31:19 +01:00
if err != nil {
return nil , status . Errorf ( codes . InvalidArgument , err . Error ( ) )
}
2019-05-28 16:36:52 +01:00
exceeded , limit , err := endpoint . projectUsage . ExceedsStorageUsage ( ctx , keyInfo . ProjectID )
2019-04-02 19:21:18 +01:00
if err != nil {
2019-05-10 02:39:21 +01:00
endpoint . log . Error ( "retrieving project storage totals" , zap . Error ( err ) )
2019-04-02 19:21:18 +01:00
}
if exceeded {
2019-05-28 16:36:52 +01:00
endpoint . log . Sugar ( ) . Errorf ( "monthly project limits are %s of storage and bandwidth usage. This limit has been exceeded for storage for projectID %s" ,
limit , keyInfo . ProjectID ,
2019-04-02 19:21:18 +01:00
)
2019-05-28 16:36:52 +01:00
return nil , status . Errorf ( codes . ResourceExhausted , "Exceeded Usage Limit" )
2019-04-02 19:21:18 +01:00
}
2019-03-18 10:55:06 +00:00
redundancy , err := eestream . NewRedundancyStrategyFromProto ( req . GetRedundancy ( ) )
if err != nil {
return nil , err
}
maxPieceSize := eestream . CalcPieceSize ( req . GetMaxEncryptedSegmentSize ( ) , redundancy )
2019-03-23 08:06:11 +00:00
request := overlay . FindStorageNodesRequest {
RequestedCount : int ( req . Redundancy . Total ) ,
FreeBandwidth : maxPieceSize ,
FreeDisk : maxPieceSize ,
2019-03-18 10:55:06 +00:00
}
2019-03-23 08:06:11 +00:00
nodes , err := endpoint . cache . FindStorageNodes ( ctx , request )
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . Internal , err . Error ( ) )
}
uplinkIdentity , err := identity . PeerIdentityFromContext ( ctx )
if err != nil {
return nil , status . Errorf ( codes . Internal , err . Error ( ) )
}
2019-05-10 02:39:21 +01:00
bucketID := createBucketID ( keyInfo . ProjectID , req . Bucket )
2019-03-28 20:09:23 +00:00
rootPieceID , addressedLimits , err := endpoint . orders . CreatePutOrderLimits ( ctx , uplinkIdentity , bucketID , nodes , req . Expiration , maxPieceSize )
2019-03-27 10:24:35 +00:00
if err != nil {
2019-03-28 20:09:23 +00:00
return nil , Error . Wrap ( err )
2019-03-27 10:24:35 +00:00
}
2019-06-05 17:41:02 +01:00
if len ( addressedLimits ) > 0 {
endpoint . createRequests . Put ( addressedLimits [ 0 ] . Limit . SerialNumber , & createRequest {
Expiration : req . Expiration ,
Redundancy : req . Redundancy ,
} )
}
2019-03-27 10:24:35 +00:00
return & pb . SegmentWriteResponse { AddressedLimits : addressedLimits , RootPieceId : rootPieceID } , nil
2019-03-18 10:55:06 +00:00
}
2019-05-10 02:39:21 +01:00
func calculateSpaceUsed ( ptr * pb . Pointer ) ( inlineSpace , remoteSpace int64 ) {
inline := ptr . GetInlineSegment ( )
if inline != nil {
return int64 ( len ( inline ) ) , 0
}
segmentSize := ptr . GetSegmentSize ( )
remote := ptr . GetRemote ( )
if remote == nil {
return 0 , 0
}
minReq := remote . GetRedundancy ( ) . GetMinReq ( )
pieceSize := segmentSize / int64 ( minReq )
pieces := remote . GetRemotePieces ( )
return 0 , pieceSize * int64 ( len ( pieces ) )
}
2019-03-18 10:55:06 +00:00
// CommitSegment commits segment metadata
func ( endpoint * Endpoint ) CommitSegment ( ctx context . Context , req * pb . SegmentCommitRequest ) ( resp * pb . SegmentCommitResponse , err error ) {
defer mon . Task ( ) ( & ctx ) ( & err )
2019-05-24 17:51:27 +01:00
keyInfo , err := endpoint . validateAuth ( ctx , macaroon . Action {
Op : macaroon . ActionWrite ,
Bucket : req . Bucket ,
EncryptedPath : req . Path ,
Time : time . Now ( ) ,
} )
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . Unauthenticated , err . Error ( ) )
}
2019-06-04 12:55:38 +01:00
err = endpoint . validateBucket ( ctx , req . Bucket )
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . InvalidArgument , err . Error ( ) )
}
2019-06-05 17:41:02 +01:00
err = endpoint . validateCommitSegment ( ctx , req )
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . Internal , err . Error ( ) )
}
2019-06-04 12:55:38 +01:00
err = endpoint . filterValidPieces ( ctx , req . Pointer )
2019-03-30 11:21:49 +00:00
if err != nil {
return nil , status . Errorf ( codes . Internal , err . Error ( ) )
}
2019-03-18 10:55:06 +00:00
2019-06-04 12:55:38 +01:00
path , err := CreatePath ( ctx , keyInfo . ProjectID , req . Segment , req . Bucket , req . Path )
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . InvalidArgument , err . Error ( ) )
}
2019-05-10 02:39:21 +01:00
inlineUsed , remoteUsed := calculateSpaceUsed ( req . Pointer )
2019-05-28 16:36:52 +01:00
if err := endpoint . projectUsage . AddProjectStorageUsage ( ctx , keyInfo . ProjectID , inlineUsed , remoteUsed ) ; err != nil {
2019-05-10 02:39:21 +01:00
endpoint . log . Sugar ( ) . Errorf ( "Could not track new storage usage by project %v: %v" , keyInfo . ProjectID , err )
// but continue. it's most likely our own fault that we couldn't track it, and the only thing
// that will be affected is our per-project bandwidth and storage limits.
}
2019-06-05 15:23:10 +01:00
err = endpoint . metainfo . Put ( ctx , path , req . Pointer )
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . Internal , err . Error ( ) )
}
2019-04-05 08:42:56 +01:00
if req . Pointer . Type == pb . Pointer_INLINE {
// TODO or maybe use pointer.SegmentSize ??
2019-06-25 16:58:42 +01:00
err = endpoint . orders . UpdatePutInlineOrder ( ctx , keyInfo . ProjectID , req . Bucket , int64 ( len ( req . Pointer . InlineSegment ) ) )
2019-04-05 08:42:56 +01:00
if err != nil {
return nil , status . Errorf ( codes . Internal , err . Error ( ) )
}
}
2019-06-05 15:23:10 +01:00
pointer , err := endpoint . metainfo . Get ( ctx , path )
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . Internal , err . Error ( ) )
}
2019-06-05 17:41:02 +01:00
if len ( req . OriginalLimits ) > 0 {
endpoint . createRequests . Remove ( req . OriginalLimits [ 0 ] . SerialNumber )
}
2019-03-18 10:55:06 +00:00
return & pb . SegmentCommitResponse { Pointer : pointer } , nil
}
// DownloadSegment gets Pointer incase of INLINE data or list of OrderLimit necessary to download remote data
func ( endpoint * Endpoint ) DownloadSegment ( ctx context . Context , req * pb . SegmentDownloadRequest ) ( resp * pb . SegmentDownloadResponse , err error ) {
defer mon . Task ( ) ( & ctx ) ( & err )
2019-05-24 17:51:27 +01:00
keyInfo , err := endpoint . validateAuth ( ctx , macaroon . Action {
Op : macaroon . ActionRead ,
Bucket : req . Bucket ,
EncryptedPath : req . Path ,
Time : time . Now ( ) ,
} )
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . Unauthenticated , err . Error ( ) )
}
2019-06-04 12:55:38 +01:00
err = endpoint . validateBucket ( ctx , req . Bucket )
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . InvalidArgument , err . Error ( ) )
}
2019-04-02 19:21:18 +01:00
bucketID := createBucketID ( keyInfo . ProjectID , req . Bucket )
2019-05-28 16:36:52 +01:00
exceeded , limit , err := endpoint . projectUsage . ExceedsBandwidthUsage ( ctx , keyInfo . ProjectID , bucketID )
2019-04-02 19:21:18 +01:00
if err != nil {
2019-05-28 16:36:52 +01:00
endpoint . log . Error ( "retrieving project bandwidth total" , zap . Error ( err ) )
2019-04-02 19:21:18 +01:00
}
if exceeded {
2019-05-28 16:36:52 +01:00
endpoint . log . Sugar ( ) . Errorf ( "monthly project limits are %s of storage and bandwidth usage. This limit has been exceeded for bandwidth for projectID %s." ,
limit , keyInfo . ProjectID ,
2019-04-02 19:21:18 +01:00
)
2019-05-28 16:36:52 +01:00
return nil , status . Errorf ( codes . ResourceExhausted , "Exceeded Usage Limit" )
2019-04-02 19:21:18 +01:00
}
2019-06-04 12:55:38 +01:00
path , err := CreatePath ( ctx , keyInfo . ProjectID , req . Segment , req . Bucket , req . Path )
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . InvalidArgument , err . Error ( ) )
}
// TODO refactor to use []byte directly
2019-06-05 15:23:10 +01:00
pointer , err := endpoint . metainfo . Get ( ctx , path )
2019-03-18 10:55:06 +00:00
if err != nil {
if storage . ErrKeyNotFound . Has ( err ) {
return nil , status . Errorf ( codes . NotFound , err . Error ( ) )
}
return nil , status . Errorf ( codes . Internal , err . Error ( ) )
}
if pointer . Type == pb . Pointer_INLINE {
2019-04-05 08:42:56 +01:00
// TODO or maybe use pointer.SegmentSize ??
2019-06-25 16:58:42 +01:00
err := endpoint . orders . UpdateGetInlineOrder ( ctx , keyInfo . ProjectID , req . Bucket , int64 ( len ( pointer . InlineSegment ) ) )
2019-04-05 08:42:56 +01:00
if err != nil {
return nil , status . Errorf ( codes . Internal , err . Error ( ) )
}
2019-03-18 10:55:06 +00:00
return & pb . SegmentDownloadResponse { Pointer : pointer } , nil
} else if pointer . Type == pb . Pointer_REMOTE && pointer . Remote != nil {
2019-03-28 20:09:23 +00:00
uplinkIdentity , err := identity . PeerIdentityFromContext ( ctx )
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . Internal , err . Error ( ) )
}
2019-03-28 20:09:23 +00:00
limits , err := endpoint . orders . CreateGetOrderLimits ( ctx , uplinkIdentity , bucketID , pointer )
if err != nil {
2019-03-27 10:24:35 +00:00
return nil , status . Errorf ( codes . Internal , err . Error ( ) )
}
2019-03-18 10:55:06 +00:00
return & pb . SegmentDownloadResponse { Pointer : pointer , AddressedLimits : limits } , nil
}
return & pb . SegmentDownloadResponse { } , nil
}
// DeleteSegment deletes segment metadata from satellite and returns OrderLimit array to remove them from storage node
func ( endpoint * Endpoint ) DeleteSegment ( ctx context . Context , req * pb . SegmentDeleteRequest ) ( resp * pb . SegmentDeleteResponse , err error ) {
defer mon . Task ( ) ( & ctx ) ( & err )
2019-05-24 17:51:27 +01:00
keyInfo , err := endpoint . validateAuth ( ctx , macaroon . Action {
Op : macaroon . ActionDelete ,
Bucket : req . Bucket ,
EncryptedPath : req . Path ,
Time : time . Now ( ) ,
} )
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . Unauthenticated , err . Error ( ) )
}
2019-06-04 12:55:38 +01:00
err = endpoint . validateBucket ( ctx , req . Bucket )
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . InvalidArgument , err . Error ( ) )
}
2019-06-04 12:55:38 +01:00
path , err := CreatePath ( ctx , keyInfo . ProjectID , req . Segment , req . Bucket , req . Path )
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . InvalidArgument , err . Error ( ) )
}
// TODO refactor to use []byte directly
2019-06-05 15:23:10 +01:00
pointer , err := endpoint . metainfo . Get ( ctx , path )
2019-03-18 10:55:06 +00:00
if err != nil {
if storage . ErrKeyNotFound . Has ( err ) {
return nil , status . Errorf ( codes . NotFound , err . Error ( ) )
}
return nil , status . Errorf ( codes . Internal , err . Error ( ) )
}
2019-06-05 15:23:10 +01:00
err = endpoint . metainfo . Delete ( ctx , path )
2019-05-24 20:56:08 +01:00
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . Internal , err . Error ( ) )
}
if pointer . Type == pb . Pointer_REMOTE && pointer . Remote != nil {
2019-03-28 20:09:23 +00:00
uplinkIdentity , err := identity . PeerIdentityFromContext ( ctx )
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . Internal , err . Error ( ) )
}
2019-05-24 20:56:08 +01:00
for _ , piece := range pointer . GetRemote ( ) . GetRemotePieces ( ) {
_ , err := endpoint . containment . Delete ( ctx , piece . NodeId )
if err != nil {
return nil , status . Errorf ( codes . Internal , err . Error ( ) )
}
}
2019-03-28 20:09:23 +00:00
bucketID := createBucketID ( keyInfo . ProjectID , req . Bucket )
limits , err := endpoint . orders . CreateDeleteOrderLimits ( ctx , uplinkIdentity , bucketID , pointer )
2019-03-18 10:55:06 +00:00
if err != nil {
2019-03-28 20:09:23 +00:00
return nil , status . Errorf ( codes . Internal , err . Error ( ) )
2019-03-18 10:55:06 +00:00
}
2019-03-28 20:09:23 +00:00
return & pb . SegmentDeleteResponse { AddressedLimits : limits } , nil
2019-03-18 10:55:06 +00:00
}
2019-03-28 20:09:23 +00:00
return & pb . SegmentDeleteResponse { } , nil
2019-03-18 10:55:06 +00:00
}
// ListSegments returns all Path keys in the Pointers bucket
func ( endpoint * Endpoint ) ListSegments ( ctx context . Context , req * pb . ListSegmentsRequest ) ( resp * pb . ListSegmentsResponse , err error ) {
defer mon . Task ( ) ( & ctx ) ( & err )
2019-05-24 17:51:27 +01:00
keyInfo , err := endpoint . validateAuth ( ctx , macaroon . Action {
Op : macaroon . ActionList ,
Bucket : req . Bucket ,
EncryptedPath : req . Prefix ,
Time : time . Now ( ) ,
} )
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . Unauthenticated , err . Error ( ) )
}
2019-06-04 12:55:38 +01:00
prefix , err := CreatePath ( ctx , keyInfo . ProjectID , - 1 , req . Bucket , req . Prefix )
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . InvalidArgument , err . Error ( ) )
}
2019-06-05 15:23:10 +01:00
items , more , err := endpoint . metainfo . List ( ctx , prefix , string ( req . StartAfter ) , string ( req . EndBefore ) , req . Recursive , req . Limit , req . MetaFlags )
2019-03-18 10:55:06 +00:00
if err != nil {
return nil , status . Errorf ( codes . Internal , "ListV2: %v" , err )
}
segmentItems := make ( [ ] * pb . ListSegmentsResponse_Item , len ( items ) )
for i , item := range items {
segmentItems [ i ] = & pb . ListSegmentsResponse_Item {
Path : [ ] byte ( item . Path ) ,
Pointer : item . Pointer ,
IsPrefix : item . IsPrefix ,
}
}
return & pb . ListSegmentsResponse { Items : segmentItems , More : more } , nil
}
2019-03-28 20:09:23 +00:00
func createBucketID ( projectID uuid . UUID , bucket [ ] byte ) [ ] byte {
entries := make ( [ ] string , 0 )
entries = append ( entries , projectID . String ( ) )
entries = append ( entries , string ( bucket ) )
return [ ] byte ( storj . JoinPaths ( entries ... ) )
}
2019-06-04 12:55:38 +01:00
func ( endpoint * Endpoint ) filterValidPieces ( ctx context . Context , pointer * pb . Pointer ) ( err error ) {
defer mon . Task ( ) ( & ctx ) ( & err )
2019-03-18 10:55:06 +00:00
if pointer . Type == pb . Pointer_REMOTE {
var remotePieces [ ] * pb . RemotePiece
remote := pointer . Remote
for _ , piece := range remote . RemotePieces {
// TODO enable verification
// err := auth.VerifyMsg(piece.Hash, piece.NodeId)
// if err == nil {
// // set to nil after verification to avoid storing in DB
// piece.Hash = nil
// remotePieces = append(remotePieces, piece)
// } else {
// // TODO satellite should send Delete request for piece that failed
// s.logger.Warn("unable to verify piece hash: %v", zap.Error(err))
// }
remotePieces = append ( remotePieces , piece )
}
2019-05-17 20:02:40 +01:00
// we repair when the number of healthy files is less than or equal to the repair threshold
// except for the case when the repair and success thresholds are the same (a case usually seen during testing)
2019-06-19 21:13:11 +01:00
if int32 ( len ( remotePieces ) ) <= remote . Redundancy . RepairThreshold && int32 ( len ( remotePieces ) ) < remote . Redundancy . SuccessThreshold {
return Error . New ( "Number of valid pieces (%d) is less than or equal to the repair threshold (%d)" ,
2019-03-18 10:55:06 +00:00
len ( remotePieces ) ,
2019-04-05 11:19:20 +01:00
remote . Redundancy . RepairThreshold ,
2019-03-18 10:55:06 +00:00
)
}
remote . RemotePieces = remotePieces
}
return nil
}
2019-04-02 15:55:58 +01:00
// CreatePath will create a Segment path
2019-06-04 12:55:38 +01:00
func CreatePath ( ctx context . Context , projectID uuid . UUID , segmentIndex int64 , bucket , path [ ] byte ) ( _ storj . Path , err error ) {
defer mon . Task ( ) ( & ctx ) ( & err )
2019-04-02 15:55:58 +01:00
if segmentIndex < - 1 {
return "" , errors . New ( "invalid segment index" )
}
segment := "l"
if segmentIndex > - 1 {
segment = "s" + strconv . FormatInt ( segmentIndex , 10 )
}
entries := make ( [ ] string , 0 )
entries = append ( entries , projectID . String ( ) )
entries = append ( entries , segment )
if len ( bucket ) != 0 {
entries = append ( entries , string ( bucket ) )
}
if len ( path ) != 0 {
entries = append ( entries , string ( path ) )
}
return storj . JoinPaths ( entries ... ) , nil
}
2019-06-13 02:35:37 +01:00
2019-06-19 13:02:37 +01:00
// SetAttribution tries to add attribution to the bucket.
func ( endpoint * Endpoint ) SetAttribution ( ctx context . Context , req * pb . SetAttributionRequest ) ( _ * pb . SetAttributionResponse , err error ) {
defer mon . Task ( ) ( & ctx ) ( & err )
2019-06-21 20:14:34 +01:00
// try to add an attribution that doesn't exist
partnerID , err := bytesToUUID ( req . GetPartnerId ( ) )
2019-06-19 13:02:37 +01:00
if err != nil {
2019-06-21 20:14:34 +01:00
return nil , Error . Wrap ( err )
2019-06-19 13:02:37 +01:00
}
2019-06-13 02:35:37 +01:00
keyInfo , err := endpoint . validateAuth ( ctx , macaroon . Action {
Op : macaroon . ActionList ,
Bucket : req . BucketName ,
EncryptedPath : [ ] byte ( "" ) ,
Time : time . Now ( ) ,
} )
if err != nil {
2019-06-21 20:14:34 +01:00
return nil , status . Errorf ( codes . Unauthenticated , err . Error ( ) )
}
// check if attribution is set for given bucket
_ , err = endpoint . partnerinfo . Get ( ctx , keyInfo . ProjectID , req . GetBucketName ( ) )
if err == nil {
return nil , Error . New ( "Bucket(%s) , PartnerID(%s) cannot be attributed" , string ( req . BucketName ) , string ( req . PartnerId ) )
}
if ! attribution . ErrBucketNotAttributed . Has ( err ) {
// try only to set the attribution, when it's missing
return nil , Error . Wrap ( err )
2019-06-13 02:35:37 +01:00
}
prefix , err := CreatePath ( ctx , keyInfo . ProjectID , - 1 , req . BucketName , [ ] byte ( "" ) )
if err != nil {
2019-06-21 20:14:34 +01:00
return nil , Error . Wrap ( err )
2019-06-13 02:35:37 +01:00
}
2019-06-21 20:14:34 +01:00
items , _ , err := endpoint . metainfo . List ( ctx , prefix , "" , "" , true , 1 , 0 )
2019-06-13 02:35:37 +01:00
if err != nil {
2019-06-21 20:14:34 +01:00
return nil , Error . Wrap ( err )
2019-06-13 02:35:37 +01:00
}
if len ( items ) > 0 {
2019-06-21 20:14:34 +01:00
return nil , Error . New ( "Bucket(%q) , PartnerID(%s) cannot be attributed" , req . BucketName , req . PartnerId )
}
_ , err = endpoint . partnerinfo . Insert ( ctx , & attribution . Info {
ProjectID : keyInfo . ProjectID ,
BucketName : req . GetBucketName ( ) ,
PartnerID : partnerID ,
} )
if err != nil {
return nil , Error . Wrap ( err )
}
return & pb . SetAttributionResponse { } , nil
}
// bytesToUUID is used to convert []byte to UUID
func bytesToUUID ( data [ ] byte ) ( uuid . UUID , error ) {
var id uuid . UUID
copy ( id [ : ] , data )
if len ( id ) != len ( data ) {
return uuid . UUID { } , errs . New ( "Invalid uuid" )
2019-06-13 02:35:37 +01:00
}
2019-06-21 20:14:34 +01:00
return id , nil
2019-06-13 02:35:37 +01:00
}
2019-06-24 18:15:45 +01:00
// ProjectInfo returns allowed ProjectInfo for the provided API key
func ( endpoint * Endpoint ) ProjectInfo ( ctx context . Context , req * pb . ProjectInfoRequest ) ( _ * pb . ProjectInfoResponse , err error ) {
defer mon . Task ( ) ( & ctx ) ( & err )
keyInfo , err := endpoint . validateAuth ( ctx , macaroon . Action {
Op : macaroon . ActionProjectInfo ,
Time : time . Now ( ) ,
} )
if err != nil {
return nil , status . Errorf ( codes . Unauthenticated , err . Error ( ) )
}
salt := sha256 . Sum256 ( keyInfo . ProjectID [ : ] )
return & pb . ProjectInfoResponse {
ProjectSalt : salt [ : ] ,
} , nil
}