2019-04-03 09:46:21 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package uplink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-06-26 17:22:01 +01:00
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
2019-04-03 09:46:21 +01:00
|
|
|
"github.com/vivint/infectious"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/encryption"
|
|
|
|
"storj.io/common/memory"
|
|
|
|
"storj.io/common/rpc"
|
|
|
|
"storj.io/common/storj"
|
2020-01-08 13:40:19 +00:00
|
|
|
"storj.io/uplink/ecclient"
|
|
|
|
"storj.io/uplink/eestream"
|
|
|
|
"storj.io/uplink/metainfo"
|
|
|
|
"storj.io/uplink/metainfo/kvmetainfo"
|
|
|
|
"storj.io/uplink/storage/segments"
|
|
|
|
"storj.io/uplink/storage/streams"
|
2019-04-03 09:46:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Project represents a specific project access session.
|
|
|
|
type Project struct {
|
2019-11-22 21:00:04 +00:00
|
|
|
uplinkCfg *Config
|
|
|
|
dialer rpc.Dialer
|
|
|
|
metainfo *metainfo.Client
|
|
|
|
project *kvmetainfo.Project
|
2019-04-03 09:46:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// BucketConfig holds information about a bucket's configuration. This is
|
|
|
|
// filled in by the caller for use with CreateBucket(), or filled in by the
|
|
|
|
// library as Bucket.Config when a bucket is returned from OpenBucket().
|
|
|
|
type BucketConfig struct {
|
|
|
|
// PathCipher indicates which cipher suite is to be used for path
|
|
|
|
// encryption within the new Bucket. If not set, AES-GCM encryption
|
|
|
|
// will be used.
|
|
|
|
PathCipher storj.CipherSuite
|
|
|
|
|
|
|
|
// EncryptionParameters specifies the default encryption parameters to
|
|
|
|
// be used for data encryption of new Objects in this bucket.
|
|
|
|
EncryptionParameters storj.EncryptionParameters
|
|
|
|
|
|
|
|
// Volatile groups config values that are likely to change semantics
|
|
|
|
// or go away entirely between releases. Be careful when using them!
|
|
|
|
Volatile struct {
|
|
|
|
// RedundancyScheme defines the default Reed-Solomon and/or
|
|
|
|
// Forward Error Correction encoding parameters to be used by
|
|
|
|
// objects in this Bucket.
|
|
|
|
RedundancyScheme storj.RedundancyScheme
|
2019-04-10 23:27:04 +01:00
|
|
|
// SegmentsSize is the default segment size to use for new
|
2019-04-03 09:46:21 +01:00
|
|
|
// objects in this Bucket.
|
2019-04-10 23:27:04 +01:00
|
|
|
SegmentsSize memory.Size
|
2019-04-03 09:46:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 18:15:41 +01:00
|
|
|
func (cfg *BucketConfig) clone() *BucketConfig {
|
|
|
|
clone := *cfg
|
|
|
|
return &clone
|
|
|
|
}
|
|
|
|
|
2019-06-13 16:09:05 +01:00
|
|
|
// TODO: is this the best way to do this?
|
2019-04-26 18:15:41 +01:00
|
|
|
func (cfg *BucketConfig) setDefaults() {
|
|
|
|
if cfg.PathCipher == storj.EncUnspecified {
|
|
|
|
cfg.PathCipher = defaultCipher
|
2019-04-03 09:46:21 +01:00
|
|
|
}
|
2019-04-26 18:15:41 +01:00
|
|
|
if cfg.EncryptionParameters.CipherSuite == storj.EncUnspecified {
|
|
|
|
cfg.EncryptionParameters.CipherSuite = defaultCipher
|
2019-04-03 09:46:21 +01:00
|
|
|
}
|
2019-04-26 18:15:41 +01:00
|
|
|
if cfg.Volatile.RedundancyScheme.RequiredShares == 0 {
|
|
|
|
cfg.Volatile.RedundancyScheme.RequiredShares = 29
|
2019-04-03 09:46:21 +01:00
|
|
|
}
|
2019-04-26 18:15:41 +01:00
|
|
|
if cfg.Volatile.RedundancyScheme.RepairShares == 0 {
|
|
|
|
cfg.Volatile.RedundancyScheme.RepairShares = 35
|
2019-04-03 09:46:21 +01:00
|
|
|
}
|
2019-04-26 18:15:41 +01:00
|
|
|
if cfg.Volatile.RedundancyScheme.OptimalShares == 0 {
|
|
|
|
cfg.Volatile.RedundancyScheme.OptimalShares = 80
|
2019-04-03 09:46:21 +01:00
|
|
|
}
|
2019-04-26 18:15:41 +01:00
|
|
|
if cfg.Volatile.RedundancyScheme.TotalShares == 0 {
|
2019-10-31 19:04:33 +00:00
|
|
|
cfg.Volatile.RedundancyScheme.TotalShares = 95
|
2019-04-03 09:46:21 +01:00
|
|
|
}
|
2019-04-26 18:15:41 +01:00
|
|
|
if cfg.Volatile.RedundancyScheme.ShareSize == 0 {
|
2019-06-28 11:23:59 +01:00
|
|
|
cfg.Volatile.RedundancyScheme.ShareSize = 256 * memory.B.Int32()
|
2019-04-03 09:46:21 +01:00
|
|
|
}
|
2019-06-20 22:50:13 +01:00
|
|
|
if cfg.EncryptionParameters.BlockSize == 0 {
|
|
|
|
cfg.EncryptionParameters.BlockSize = cfg.Volatile.RedundancyScheme.ShareSize * int32(cfg.Volatile.RedundancyScheme.RequiredShares)
|
|
|
|
}
|
2019-04-26 18:15:41 +01:00
|
|
|
if cfg.Volatile.SegmentsSize.Int() == 0 {
|
|
|
|
cfg.Volatile.SegmentsSize = 64 * memory.MiB
|
2019-04-03 09:46:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateBucket creates a new bucket if authorized.
|
2019-06-06 19:55:10 +01:00
|
|
|
func (p *Project) CreateBucket(ctx context.Context, name string, cfg *BucketConfig) (bucket storj.Bucket, err error) {
|
2019-04-03 09:46:21 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
if cfg == nil {
|
|
|
|
cfg = &BucketConfig{}
|
|
|
|
}
|
2019-04-26 18:15:41 +01:00
|
|
|
cfg = cfg.clone()
|
2019-04-03 09:46:21 +01:00
|
|
|
cfg.setDefaults()
|
2019-06-06 19:55:10 +01:00
|
|
|
|
2019-11-26 11:12:37 +00:00
|
|
|
var partnerID uuid.UUID
|
|
|
|
if p.uplinkCfg.Volatile.PartnerID != "" {
|
|
|
|
id, err := uuid.Parse(p.uplinkCfg.Volatile.PartnerID)
|
|
|
|
if err != nil {
|
|
|
|
return storj.Bucket{}, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
partnerID = *id
|
|
|
|
}
|
|
|
|
|
2019-06-06 19:55:10 +01:00
|
|
|
bucket = storj.Bucket{
|
2019-11-26 11:12:37 +00:00
|
|
|
PartnerID: partnerID,
|
2019-07-08 23:32:18 +01:00
|
|
|
PathCipher: cfg.PathCipher,
|
|
|
|
DefaultEncryptionParameters: cfg.EncryptionParameters,
|
|
|
|
DefaultRedundancyScheme: cfg.Volatile.RedundancyScheme,
|
|
|
|
DefaultSegmentsSize: cfg.Volatile.SegmentsSize.Int64(),
|
2019-04-10 23:27:04 +01:00
|
|
|
}
|
2019-06-06 19:55:10 +01:00
|
|
|
return p.project.CreateBucket(ctx, name, &bucket)
|
2019-04-03 09:46:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBucket deletes a bucket if authorized. If the bucket contains any
|
|
|
|
// Objects at the time of deletion, they may be lost permanently.
|
|
|
|
func (p *Project) DeleteBucket(ctx context.Context, bucket string) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
return p.project.DeleteBucket(ctx, bucket)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BucketListOptions controls options to the ListBuckets() call.
|
|
|
|
type BucketListOptions = storj.BucketListOptions
|
|
|
|
|
|
|
|
// ListBuckets will list authorized buckets.
|
|
|
|
func (p *Project) ListBuckets(ctx context.Context, opts *BucketListOptions) (bl storj.BucketList, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
if opts == nil {
|
2019-06-13 16:09:05 +01:00
|
|
|
opts = &BucketListOptions{Direction: storj.Forward}
|
2019-04-03 09:46:21 +01:00
|
|
|
}
|
|
|
|
return p.project.ListBuckets(ctx, *opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucketInfo returns info about the requested bucket if authorized.
|
|
|
|
func (p *Project) GetBucketInfo(ctx context.Context, bucket string) (b storj.Bucket, bi *BucketConfig, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
b, err = p.project.GetBucket(ctx, bucket)
|
|
|
|
if err != nil {
|
|
|
|
return b, nil, err
|
|
|
|
}
|
2019-04-10 23:27:04 +01:00
|
|
|
cfg := &BucketConfig{
|
2019-07-03 19:07:44 +01:00
|
|
|
PathCipher: b.PathCipher,
|
2019-07-08 23:32:18 +01:00
|
|
|
EncryptionParameters: b.DefaultEncryptionParameters,
|
2019-04-10 23:27:04 +01:00
|
|
|
}
|
2019-07-08 23:32:18 +01:00
|
|
|
cfg.Volatile.RedundancyScheme = b.DefaultRedundancyScheme
|
|
|
|
cfg.Volatile.SegmentsSize = memory.Size(b.DefaultSegmentsSize)
|
2019-04-03 09:46:21 +01:00
|
|
|
return b, cfg, nil
|
|
|
|
}
|
|
|
|
|
2019-06-25 16:36:23 +01:00
|
|
|
// TODO: move the bucket related OpenBucket to bucket.go
|
|
|
|
|
2019-04-03 09:46:21 +01:00
|
|
|
// OpenBucket returns a Bucket handle with the given EncryptionAccess
|
|
|
|
// information.
|
2019-06-28 06:18:24 +01:00
|
|
|
func (p *Project) OpenBucket(ctx context.Context, bucketName string, access *EncryptionAccess) (b *Bucket, err error) {
|
2019-04-03 09:46:21 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-06-28 00:25:36 +01:00
|
|
|
bucketInfo, cfg, err := p.GetBucketInfo(ctx, bucketName)
|
2019-06-26 17:22:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-19 16:17:34 +01:00
|
|
|
// partnerID set and bucket's attribution is not set
|
2019-11-26 11:12:37 +00:00
|
|
|
if bucketInfo.PartnerID.IsZero() {
|
|
|
|
err = p.trySetBucketAttribution(ctx, bucketName)
|
2019-07-19 16:17:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2019-07-03 19:07:44 +01:00
|
|
|
encryptionParameters := cfg.EncryptionParameters
|
2019-04-03 09:46:21 +01:00
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
ec := ecclient.NewClient(p.uplinkCfg.Volatile.Log.Named("ecclient"), p.dialer, p.uplinkCfg.Volatile.MaxMemory.Int())
|
2019-04-03 09:46:21 +01:00
|
|
|
fc, err := infectious.NewFEC(int(cfg.Volatile.RedundancyScheme.RequiredShares), int(cfg.Volatile.RedundancyScheme.TotalShares))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rs, err := eestream.NewRedundancyStrategy(
|
|
|
|
eestream.NewRSScheme(fc, int(cfg.Volatile.RedundancyScheme.ShareSize)),
|
|
|
|
int(cfg.Volatile.RedundancyScheme.RepairShares),
|
|
|
|
int(cfg.Volatile.RedundancyScheme.OptimalShares))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-04-10 23:27:04 +01:00
|
|
|
maxEncryptedSegmentSize, err := encryption.CalcEncryptedSize(cfg.Volatile.SegmentsSize.Int64(),
|
2019-07-03 19:07:44 +01:00
|
|
|
cfg.EncryptionParameters)
|
2019-04-03 09:46:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-29 15:49:16 +00:00
|
|
|
segmentStore := segments.NewSegmentStore(p.metainfo, ec, rs)
|
2019-04-03 09:46:21 +01:00
|
|
|
|
2019-11-22 21:00:04 +00:00
|
|
|
streamStore, err := streams.NewStreamStore(p.metainfo, segmentStore, cfg.Volatile.SegmentsSize.Int64(), access.store, int(encryptionParameters.BlockSize), encryptionParameters.CipherSuite, p.uplinkCfg.Volatile.MaxInlineSize.Int(), maxEncryptedSegmentSize)
|
2019-04-03 09:46:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Bucket{
|
2019-04-10 23:27:04 +01:00
|
|
|
BucketConfig: *cfg,
|
|
|
|
Name: bucketInfo.Name,
|
|
|
|
Created: bucketInfo.Created,
|
|
|
|
bucket: bucketInfo,
|
2019-06-28 06:18:24 +01:00
|
|
|
metainfo: kvmetainfo.New(p.project, p.metainfo, streamStore, segmentStore, access.store),
|
2019-04-10 23:27:04 +01:00
|
|
|
streams: streamStore,
|
2019-04-03 09:46:21 +01:00
|
|
|
}, nil
|
|
|
|
}
|
2019-06-26 17:22:01 +01:00
|
|
|
|
2019-06-27 18:36:51 +01:00
|
|
|
func (p *Project) retrieveSalt(ctx context.Context) (salt []byte, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
info, err := p.metainfo.GetProjectInfo(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return info.ProjectSalt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaltedKeyFromPassphrase returns a key generated from the given passphrase using a stable, project-specific salt
|
|
|
|
func (p *Project) SaltedKeyFromPassphrase(ctx context.Context, passphrase string) (_ *storj.Key, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
salt, err := p.retrieveSalt(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-22 21:00:04 +00:00
|
|
|
key, err := encryption.DeriveRootKey([]byte(passphrase), salt, "", uint8(p.uplinkCfg.Volatile.PBKDFConcurrency))
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-09 05:07:03 +01:00
|
|
|
return key, nil
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 11:12:37 +00:00
|
|
|
// trySetBucketAttribution sets the bucket attribution if PartnerID or UserAgent is available.
|
|
|
|
func (p *Project) trySetBucketAttribution(ctx context.Context, bucketName string) (err error) {
|
2019-06-26 18:24:50 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-11-26 11:12:37 +00:00
|
|
|
if p.uplinkCfg.Volatile.PartnerID == "" && p.uplinkCfg.Volatile.UserAgent == "" {
|
2019-06-26 17:22:01 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-26 11:12:37 +00:00
|
|
|
var partnerID uuid.UUID
|
|
|
|
if p.uplinkCfg.Volatile.PartnerID != "" {
|
|
|
|
id, err := uuid.Parse(p.uplinkCfg.Volatile.PartnerID)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
partnerID = *id
|
2019-06-26 17:22:01 +01:00
|
|
|
}
|
|
|
|
|
2019-11-26 11:12:37 +00:00
|
|
|
// UserAgent is sent via RequestHeader
|
2019-10-23 07:59:56 +01:00
|
|
|
return p.metainfo.SetBucketAttribution(ctx, metainfo.SetBucketAttributionParams{
|
|
|
|
Bucket: bucketName,
|
2019-11-26 11:12:37 +00:00
|
|
|
PartnerID: partnerID,
|
2019-10-23 07:59:56 +01:00
|
|
|
})
|
2019-06-26 17:22:01 +01:00
|
|
|
}
|