2019-02-04 16:56:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information
|
|
|
|
|
|
|
|
package testplanet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-08-13 18:29:02 +01:00
|
|
|
"io"
|
2019-02-04 16:56:10 +00:00
|
|
|
"io/ioutil"
|
2019-04-09 18:01:45 +01:00
|
|
|
"strconv"
|
2019-04-25 09:17:26 +01:00
|
|
|
"time"
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2019-09-12 11:38:49 +01:00
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
2019-02-08 12:57:35 +00:00
|
|
|
"github.com/spf13/pflag"
|
2019-02-04 16:56:10 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
libuplink "storj.io/storj/lib/uplink"
|
2019-02-08 12:57:35 +00:00
|
|
|
"storj.io/storj/pkg/cfgstruct"
|
2019-02-04 16:56:10 +00:00
|
|
|
"storj.io/storj/pkg/identity"
|
2019-05-24 17:51:27 +01:00
|
|
|
"storj.io/storj/pkg/macaroon"
|
2019-02-04 16:56:10 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
2019-02-11 11:17:32 +00:00
|
|
|
"storj.io/storj/pkg/peertls/tlsopts"
|
2019-09-19 05:46:39 +01:00
|
|
|
"storj.io/storj/pkg/rpc"
|
2019-02-04 16:56:10 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-02-05 17:22:17 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2019-02-08 12:57:35 +00:00
|
|
|
"storj.io/storj/uplink"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/uplink/metainfo"
|
|
|
|
"storj.io/storj/uplink/piecestore"
|
2019-02-04 16:56:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Uplink is a general purpose
|
|
|
|
type Uplink struct {
|
|
|
|
Log *zap.Logger
|
|
|
|
Info pb.Node
|
|
|
|
Identity *identity.FullIdentity
|
2019-09-19 05:46:39 +01:00
|
|
|
Dialer rpc.Dialer
|
2019-02-04 16:56:10 +00:00
|
|
|
StorageNodeCount int
|
2019-09-12 11:38:49 +01:00
|
|
|
|
2019-09-19 17:19:29 +01:00
|
|
|
APIKey map[storj.NodeID]*macaroon.APIKey
|
2019-09-12 11:38:49 +01:00
|
|
|
ProjectID map[storj.NodeID]uuid.UUID
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 14:39:43 +01:00
|
|
|
// newUplinks creates initializes uplinks, requires peer to have at least one satellite
|
|
|
|
func (planet *Planet) newUplinks(prefix string, count, storageNodeCount int) ([]*Uplink, error) {
|
|
|
|
var xs []*Uplink
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
uplink, err := planet.newUplink(prefix+strconv.Itoa(i), storageNodeCount)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
xs = append(xs, uplink)
|
|
|
|
}
|
|
|
|
|
|
|
|
return xs, nil
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:56:10 +00:00
|
|
|
// newUplink creates a new uplink
|
|
|
|
func (planet *Planet) newUplink(name string, storageNodeCount int) (*Uplink, error) {
|
|
|
|
identity, err := planet.NewIdentity()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
tlsOptions, err := tlsopts.NewOptions(identity, tlsopts.Config{
|
2019-04-09 18:01:45 +01:00
|
|
|
PeerIDVersions: strconv.Itoa(int(planet.config.IdentityVersion.Number)),
|
2019-08-19 23:10:38 +01:00
|
|
|
}, nil)
|
2019-02-11 11:17:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:56:10 +00:00
|
|
|
uplink := &Uplink{
|
|
|
|
Log: planet.log.Named(name),
|
|
|
|
Identity: identity,
|
|
|
|
StorageNodeCount: storageNodeCount,
|
2019-09-19 17:19:29 +01:00
|
|
|
APIKey: map[storj.NodeID]*macaroon.APIKey{},
|
2019-09-12 11:38:49 +01:00
|
|
|
ProjectID: map[storj.NodeID]uuid.UUID{},
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uplink.Log.Debug("id=" + identity.ID.String())
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
uplink.Dialer = rpc.NewDefaultDialer(tlsOptions)
|
2019-02-04 16:56:10 +00:00
|
|
|
|
|
|
|
uplink.Info = pb.Node{
|
2019-04-22 10:07:50 +01:00
|
|
|
Id: uplink.Identity.ID,
|
2019-02-04 16:56:10 +00:00
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Transport: pb.NodeTransport_TCP_TLS_GRPC,
|
|
|
|
Address: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-02-05 17:22:17 +00:00
|
|
|
for j, satellite := range planet.Satellites {
|
|
|
|
// TODO: find a nicer way to do this
|
|
|
|
// populate satellites console with example
|
|
|
|
// project and API key and pass that to uplinks
|
|
|
|
consoleDB := satellite.DB.Console()
|
|
|
|
|
|
|
|
projectName := fmt.Sprintf("%s_%d", name, j)
|
2019-05-24 17:51:27 +01:00
|
|
|
key, err := macaroon.NewAPIKey([]byte("testSecret"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-05 17:22:17 +00:00
|
|
|
|
|
|
|
project, err := consoleDB.Projects().Insert(
|
|
|
|
context.Background(),
|
|
|
|
&console.Project{
|
|
|
|
Name: projectName,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = consoleDB.APIKeys().Create(
|
|
|
|
context.Background(),
|
2019-05-24 17:51:27 +01:00
|
|
|
key.Head(),
|
2019-02-05 17:22:17 +00:00
|
|
|
console.APIKeyInfo{
|
|
|
|
Name: "root",
|
|
|
|
ProjectID: project.ID,
|
2019-05-24 17:51:27 +01:00
|
|
|
Secret: []byte("testSecret"),
|
2019-02-05 17:22:17 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-19 17:19:29 +01:00
|
|
|
uplink.APIKey[satellite.ID()] = key
|
2019-09-12 11:38:49 +01:00
|
|
|
uplink.ProjectID[satellite.ID()] = project.ID
|
2019-02-05 17:22:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 16:56:10 +00:00
|
|
|
planet.uplinks = append(planet.uplinks, uplink)
|
|
|
|
|
|
|
|
return uplink, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns uplink id
|
2019-07-23 15:58:45 +01:00
|
|
|
func (client *Uplink) ID() storj.NodeID { return client.Info.Id }
|
2019-02-04 16:56:10 +00:00
|
|
|
|
|
|
|
// Addr returns uplink address
|
2019-07-23 15:58:45 +01:00
|
|
|
func (client *Uplink) Addr() string { return client.Info.Address.Address }
|
2019-02-04 16:56:10 +00:00
|
|
|
|
|
|
|
// Local returns uplink info
|
2019-07-23 15:58:45 +01:00
|
|
|
func (client *Uplink) Local() pb.Node { return client.Info }
|
2019-02-04 16:56:10 +00:00
|
|
|
|
|
|
|
// Shutdown shuts down all uplink dependencies
|
2019-07-23 15:58:45 +01:00
|
|
|
func (client *Uplink) Shutdown() error { return nil }
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
// DialMetainfo dials destination with apikey and returns metainfo Client
|
2019-09-19 17:19:29 +01:00
|
|
|
func (client *Uplink) DialMetainfo(ctx context.Context, destination Peer, apikey *macaroon.APIKey) (*metainfo.Client, error) {
|
2019-09-19 05:46:39 +01:00
|
|
|
return metainfo.Dial(ctx, client.Dialer, destination.Addr(), apikey)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DialPiecestore dials destination storagenode and returns a piecestore client.
|
2019-07-23 15:58:45 +01:00
|
|
|
func (client *Uplink) DialPiecestore(ctx context.Context, destination Peer) (*piecestore.Client, error) {
|
2019-03-18 10:55:06 +00:00
|
|
|
node := destination.Local()
|
2019-09-19 05:46:39 +01:00
|
|
|
return piecestore.Dial(ctx, client.Dialer, &node.Node, client.Log.Named("uplink>piecestore"), piecestore.DefaultConfig)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 16:56:10 +00:00
|
|
|
// Upload data to specific satellite
|
2019-09-17 21:14:49 +01:00
|
|
|
func (client *Uplink) Upload(ctx context.Context, satellite *SatelliteSystem, bucket string, path storj.Path, data []byte) error {
|
2019-07-23 15:58:45 +01:00
|
|
|
return client.UploadWithExpiration(ctx, satellite, bucket, path, data, time.Time{})
|
2019-05-08 12:11:59 +01:00
|
|
|
}
|
|
|
|
|
2019-06-19 21:13:11 +01:00
|
|
|
// UploadWithExpiration data to specific satellite and expiration time
|
2019-09-17 21:14:49 +01:00
|
|
|
func (client *Uplink) UploadWithExpiration(ctx context.Context, satellite *SatelliteSystem, bucket string, path storj.Path, data []byte, expiration time.Time) error {
|
2019-07-23 15:58:45 +01:00
|
|
|
return client.UploadWithExpirationAndConfig(ctx, satellite, nil, bucket, path, data, expiration)
|
2019-03-21 14:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UploadWithConfig uploads data to specific satellite with configured values
|
2019-09-17 21:14:49 +01:00
|
|
|
func (client *Uplink) UploadWithConfig(ctx context.Context, satellite *SatelliteSystem, redundancy *uplink.RSConfig, bucket string, path storj.Path, data []byte) error {
|
2019-07-23 15:58:45 +01:00
|
|
|
return client.UploadWithExpirationAndConfig(ctx, satellite, redundancy, bucket, path, data, time.Time{})
|
2019-06-19 21:13:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// UploadWithExpirationAndConfig uploads data to specific satellite with configured values and expiration time
|
2019-09-17 21:14:49 +01:00
|
|
|
func (client *Uplink) UploadWithExpirationAndConfig(ctx context.Context, satellite *SatelliteSystem, redundancy *uplink.RSConfig, bucketName string, path storj.Path, data []byte, expiration time.Time) (err error) {
|
2019-07-23 15:58:45 +01:00
|
|
|
config := client.GetConfig(satellite)
|
2019-03-21 14:26:56 +00:00
|
|
|
if redundancy != nil {
|
2019-06-03 10:17:09 +01:00
|
|
|
if redundancy.MinThreshold > 0 {
|
|
|
|
config.RS.MinThreshold = redundancy.MinThreshold
|
|
|
|
}
|
|
|
|
if redundancy.RepairThreshold > 0 {
|
|
|
|
config.RS.RepairThreshold = redundancy.RepairThreshold
|
|
|
|
}
|
|
|
|
if redundancy.SuccessThreshold > 0 {
|
|
|
|
config.RS.SuccessThreshold = redundancy.SuccessThreshold
|
|
|
|
}
|
|
|
|
if redundancy.MaxThreshold > 0 {
|
|
|
|
config.RS.MaxThreshold = redundancy.MaxThreshold
|
|
|
|
}
|
|
|
|
if redundancy.ErasureShareSize > 0 {
|
|
|
|
config.RS.ErasureShareSize = redundancy.ErasureShareSize
|
|
|
|
}
|
2019-03-21 14:26:56 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
project, bucket, err := client.GetProjectAndBucket(ctx, satellite, bucketName, config)
|
2019-03-21 14:26:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
defer func() { err = errs.Combine(err, bucket.Close(), project.Close()) }()
|
2019-03-21 14:26:56 +00:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
opts := &libuplink.UploadOptions{}
|
|
|
|
opts.Expires = expiration
|
|
|
|
opts.Volatile.RedundancyScheme = config.GetRedundancyScheme()
|
|
|
|
opts.Volatile.EncryptionParameters = config.GetEncryptionParameters()
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
reader := bytes.NewReader(data)
|
|
|
|
if err := bucket.UploadObject(ctx, path, reader, opts); err != nil {
|
|
|
|
return err
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-30 22:30:18 +01:00
|
|
|
// UploadWithClientConfig uploads data to specific satellite with custom client configuration
|
2019-09-17 21:14:49 +01:00
|
|
|
func (client *Uplink) UploadWithClientConfig(ctx context.Context, satellite *SatelliteSystem, clientConfig uplink.Config, bucketName string, path storj.Path, data []byte) (err error) {
|
2019-08-30 22:30:18 +01:00
|
|
|
project, bucket, err := client.GetProjectAndBucket(ctx, satellite, bucketName, clientConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, bucket.Close(), project.Close()) }()
|
|
|
|
|
|
|
|
opts := &libuplink.UploadOptions{}
|
|
|
|
opts.Volatile.RedundancyScheme = clientConfig.GetRedundancyScheme()
|
|
|
|
opts.Volatile.EncryptionParameters = clientConfig.GetEncryptionParameters()
|
|
|
|
|
|
|
|
reader := bytes.NewReader(data)
|
|
|
|
if err := bucket.UploadObject(ctx, path, reader, opts); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
// Download data from specific satellite
|
2019-09-17 21:14:49 +01:00
|
|
|
func (client *Uplink) Download(ctx context.Context, satellite *SatelliteSystem, bucketName string, path storj.Path) ([]byte, error) {
|
2019-07-23 15:58:45 +01:00
|
|
|
project, bucket, err := client.GetProjectAndBucket(ctx, satellite, bucketName, client.GetConfig(satellite))
|
2019-02-04 16:56:10 +00:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil, err
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
defer func() { err = errs.Combine(err, project.Close(), bucket.Close()) }()
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
object, err := bucket.OpenObject(ctx, path)
|
2019-02-04 16:56:10 +00:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil, err
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
rc, err := object.DownloadRange(ctx, 0, object.Meta.Size)
|
2019-07-01 15:35:10 +01:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil, err
|
2019-07-01 15:35:10 +01:00
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
defer func() { err = errs.Combine(err, rc.Close()) }()
|
2019-07-01 15:35:10 +01:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
data, err := ioutil.ReadAll(rc)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
return data, nil
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
// DownloadStream returns stream for downloading data
|
2019-09-17 21:14:49 +01:00
|
|
|
func (client *Uplink) DownloadStream(ctx context.Context, satellite *SatelliteSystem, bucketName string, path storj.Path) (_ io.ReadCloser, cleanup func() error, err error) {
|
2019-07-23 15:58:45 +01:00
|
|
|
project, bucket, err := client.GetProjectAndBucket(ctx, satellite, bucketName, client.GetConfig(satellite))
|
2019-02-04 16:56:10 +00:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil, nil, err
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
cleanup = func() error {
|
|
|
|
err = errs.Combine(err,
|
|
|
|
project.Close(),
|
|
|
|
bucket.Close(),
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2019-08-13 18:29:02 +01:00
|
|
|
downloader, err := bucket.Download(ctx, path)
|
|
|
|
return downloader, cleanup, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DownloadStreamRange returns stream for downloading data
|
2019-09-17 21:14:49 +01:00
|
|
|
func (client *Uplink) DownloadStreamRange(ctx context.Context, satellite *SatelliteSystem, bucketName string, path storj.Path, start, limit int64) (_ io.ReadCloser, cleanup func() error, err error) {
|
2019-08-13 18:29:02 +01:00
|
|
|
project, bucket, err := client.GetProjectAndBucket(ctx, satellite, bucketName, client.GetConfig(satellite))
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup = func() error {
|
|
|
|
err = errs.Combine(err,
|
|
|
|
project.Close(),
|
|
|
|
bucket.Close(),
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
downloader, err := bucket.DownloadRange(ctx, path, start, limit)
|
2019-07-23 15:58:45 +01:00
|
|
|
return downloader, cleanup, err
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
// Delete deletes an object at the path in a bucket
|
2019-09-17 21:14:49 +01:00
|
|
|
func (client *Uplink) Delete(ctx context.Context, satellite *SatelliteSystem, bucketName string, path storj.Path) error {
|
2019-07-23 15:58:45 +01:00
|
|
|
project, bucket, err := client.GetProjectAndBucket(ctx, satellite, bucketName, client.GetConfig(satellite))
|
2019-02-04 16:56:10 +00:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return err
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
defer func() { err = errs.Combine(err, project.Close(), bucket.Close()) }()
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
err = bucket.DeleteObject(ctx, path)
|
2019-02-04 16:56:10 +00:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return err
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
// CreateBucket creates a new bucket
|
2019-09-17 21:14:49 +01:00
|
|
|
func (client *Uplink) CreateBucket(ctx context.Context, satellite *SatelliteSystem, bucketName string) error {
|
2019-07-23 15:58:45 +01:00
|
|
|
project, err := client.GetProject(ctx, satellite)
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return err
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
defer func() { err = errs.Combine(err, project.Close()) }()
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
clientCfg := client.GetConfig(satellite)
|
|
|
|
bucketCfg := &libuplink.BucketConfig{}
|
|
|
|
bucketCfg.PathCipher = clientCfg.GetPathCipherSuite()
|
|
|
|
bucketCfg.EncryptionParameters = clientCfg.GetEncryptionParameters()
|
|
|
|
bucketCfg.Volatile.RedundancyScheme = clientCfg.GetRedundancyScheme()
|
|
|
|
bucketCfg.Volatile.SegmentsSize = clientCfg.GetSegmentSize()
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
_, err = project.CreateBucket(ctx, bucketName, bucketCfg)
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-03-30 11:21:49 +00:00
|
|
|
// GetConfig returns a default config for a given satellite.
|
2019-09-17 21:14:49 +01:00
|
|
|
func (client *Uplink) GetConfig(satellite *SatelliteSystem) uplink.Config {
|
2019-02-08 12:57:35 +00:00
|
|
|
config := getDefaultConfig()
|
2019-08-05 18:01:20 +01:00
|
|
|
|
2019-09-19 17:19:29 +01:00
|
|
|
// client.APIKey[satellite.ID()] is a *macaroon.APIKey, but we want a
|
|
|
|
// *libuplink.APIKey, so, serialize and parse for now
|
|
|
|
apiKey, err := libuplink.ParseAPIKey(client.APIKey[satellite.ID()].Serialize())
|
2019-08-05 18:01:20 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
encAccess := libuplink.NewEncryptionAccess()
|
|
|
|
encAccess.SetDefaultKey(storj.Key{})
|
|
|
|
|
|
|
|
scopeData, err := (&libuplink.Scope{
|
|
|
|
SatelliteAddr: satellite.Addr(),
|
|
|
|
APIKey: apiKey,
|
|
|
|
EncryptionAccess: encAccess,
|
|
|
|
}).Serialize()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Scope = scopeData
|
|
|
|
|
|
|
|
// Support some legacy stuff
|
|
|
|
config.Legacy.Client.APIKey = apiKey.Serialize()
|
|
|
|
config.Legacy.Client.SatelliteAddr = satellite.Addr()
|
|
|
|
|
2019-05-10 12:26:25 +01:00
|
|
|
config.Client.RequestTimeout = 10 * time.Second
|
|
|
|
config.Client.DialTimeout = 10 * time.Second
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
config.RS.MinThreshold = atLeastOne(client.StorageNodeCount * 1 / 5) // 20% of storage nodes
|
|
|
|
config.RS.RepairThreshold = atLeastOne(client.StorageNodeCount * 2 / 5) // 40% of storage nodes
|
|
|
|
config.RS.SuccessThreshold = atLeastOne(client.StorageNodeCount * 3 / 5) // 60% of storage nodes
|
|
|
|
config.RS.MaxThreshold = atLeastOne(client.StorageNodeCount * 4 / 5) // 80% of storage nodes
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2019-02-11 11:17:32 +00:00
|
|
|
config.TLS.UsePeerCAWhitelist = false
|
|
|
|
config.TLS.Extensions.Revocation = false
|
|
|
|
config.TLS.Extensions.WhitelistSignedLeaf = false
|
|
|
|
|
2019-02-08 12:57:35 +00:00
|
|
|
return config
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 12:57:35 +00:00
|
|
|
func getDefaultConfig() uplink.Config {
|
|
|
|
config := uplink.Config{}
|
2019-04-19 19:17:30 +01:00
|
|
|
cfgstruct.Bind(&pflag.FlagSet{}, &config, cfgstruct.UseDevDefaults())
|
2019-02-08 12:57:35 +00:00
|
|
|
return config
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
2019-04-17 15:00:00 +01:00
|
|
|
|
|
|
|
// atLeastOne returns 1 if value < 1, or value otherwise.
|
|
|
|
func atLeastOne(value int) int {
|
|
|
|
if value < 1 {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
2019-06-27 18:36:51 +01:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
// NewLibuplink creates a libuplink.Uplink object with the testplanet Uplink config
|
|
|
|
func (client *Uplink) NewLibuplink(ctx context.Context) (*libuplink.Uplink, error) {
|
|
|
|
config := getDefaultConfig()
|
|
|
|
libuplinkCfg := &libuplink.Config{}
|
2019-08-01 12:14:09 +01:00
|
|
|
libuplinkCfg.Volatile.Log = client.Log
|
2019-07-23 15:58:45 +01:00
|
|
|
libuplinkCfg.Volatile.MaxInlineSize = config.Client.MaxInlineSize
|
|
|
|
libuplinkCfg.Volatile.MaxMemory = config.RS.MaxBufferMem
|
|
|
|
libuplinkCfg.Volatile.PeerIDVersion = config.TLS.PeerIDVersions
|
|
|
|
libuplinkCfg.Volatile.TLS.SkipPeerCAWhitelist = !config.TLS.UsePeerCAWhitelist
|
|
|
|
libuplinkCfg.Volatile.TLS.PeerCAWhitelistPath = config.TLS.PeerCAWhitelistPath
|
|
|
|
libuplinkCfg.Volatile.DialTimeout = config.Client.DialTimeout
|
|
|
|
libuplinkCfg.Volatile.RequestTimeout = config.Client.RequestTimeout
|
|
|
|
|
|
|
|
return libuplink.NewUplink(ctx, libuplinkCfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetProject returns a libuplink.Project which allows interactions with a specific project
|
2019-09-17 21:14:49 +01:00
|
|
|
func (client *Uplink) GetProject(ctx context.Context, satellite *SatelliteSystem) (*libuplink.Project, error) {
|
2019-07-23 15:58:45 +01:00
|
|
|
testLibuplink, err := client.NewLibuplink(ctx)
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil, err
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
defer func() { err = errs.Combine(err, testLibuplink.Close()) }()
|
2019-06-27 18:36:51 +01:00
|
|
|
|
2019-08-05 18:01:20 +01:00
|
|
|
scope, err := client.GetConfig(satellite).GetScope()
|
2019-07-23 15:58:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-27 18:36:51 +01:00
|
|
|
|
2019-08-05 18:01:20 +01:00
|
|
|
project, err := testLibuplink.OpenProject(ctx, scope.SatelliteAddr, scope.APIKey)
|
2019-07-23 15:58:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
return project, nil
|
|
|
|
}
|
2019-06-27 18:36:51 +01:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
// GetProjectAndBucket returns a libuplink.Project and Bucket which allows interactions with a specific project and its buckets
|
2019-09-17 21:14:49 +01:00
|
|
|
func (client *Uplink) GetProjectAndBucket(ctx context.Context, satellite *SatelliteSystem, bucketName string, clientCfg uplink.Config) (_ *libuplink.Project, _ *libuplink.Bucket, err error) {
|
2019-07-23 15:58:45 +01:00
|
|
|
project, err := client.GetProject(ctx, satellite)
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil, nil, err
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
err = errs.Combine(err, project.Close())
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-08-05 18:01:20 +01:00
|
|
|
scope, err := client.GetConfig(satellite).GetScope()
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil, nil, err
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
// Check if the bucket exists, if not then create it
|
|
|
|
_, _, err = project.GetBucketInfo(ctx, bucketName)
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
if storj.ErrBucketNotFound.Has(err) {
|
|
|
|
err := createBucket(ctx, clientCfg, *project, bucketName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
|
2019-08-05 18:01:20 +01:00
|
|
|
bucket, err := project.OpenBucket(ctx, bucketName, scope.EncryptionAccess)
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil, nil, err
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
return project, bucket, nil
|
|
|
|
}
|
2019-06-27 18:36:51 +01:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
func createBucket(ctx context.Context, config uplink.Config, project libuplink.Project, bucketName string) error {
|
|
|
|
bucketCfg := &libuplink.BucketConfig{}
|
|
|
|
bucketCfg.PathCipher = config.GetPathCipherSuite()
|
|
|
|
bucketCfg.EncryptionParameters = config.GetEncryptionParameters()
|
|
|
|
bucketCfg.Volatile.RedundancyScheme = config.GetRedundancyScheme()
|
|
|
|
bucketCfg.Volatile.SegmentsSize = config.GetSegmentSize()
|
2019-06-27 18:36:51 +01:00
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
_, err := project.CreateBucket(ctx, bucketName, bucketCfg)
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return err
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
2019-07-23 15:58:45 +01:00
|
|
|
return nil
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|