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-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-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/identity"
|
|
|
|
"storj.io/common/macaroon"
|
|
|
|
"storj.io/common/peertls/tlsopts"
|
|
|
|
"storj.io/common/rpc"
|
|
|
|
"storj.io/common/storj"
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2020-03-23 19:18:20 +00:00
|
|
|
"storj.io/private/cfgstruct"
|
2019-07-23 15:58:45 +01:00
|
|
|
libuplink "storj.io/storj/lib/uplink"
|
2020-03-30 10:08:02 +01:00
|
|
|
"storj.io/uplink"
|
2020-02-21 14:07:29 +00:00
|
|
|
"storj.io/uplink/private/metainfo"
|
|
|
|
"storj.io/uplink/private/piecestore"
|
2019-02-04 16:56:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Uplink is a general purpose
|
|
|
|
type Uplink struct {
|
|
|
|
Log *zap.Logger
|
|
|
|
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
|
|
|
|
2020-04-16 13:12:46 +01:00
|
|
|
APIKey map[storj.NodeID]*macaroon.APIKey
|
|
|
|
|
|
|
|
// Projects is indexed by the satellite number.
|
|
|
|
Projects []*Project
|
|
|
|
}
|
|
|
|
|
|
|
|
// Project contains all necessary information about a user.
|
|
|
|
type Project struct {
|
|
|
|
client *Uplink
|
|
|
|
|
|
|
|
ID uuid.UUID
|
|
|
|
Owner ProjectOwner
|
|
|
|
|
|
|
|
Satellite Peer
|
|
|
|
APIKey string
|
|
|
|
|
|
|
|
RawAPIKey *macaroon.APIKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProjectOwner contains information about the project owner.
|
|
|
|
type ProjectOwner struct {
|
|
|
|
ID uuid.UUID
|
|
|
|
Email string
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialMetainfo dials the satellite with the appropriate api key.
|
|
|
|
func (project *Project) DialMetainfo(ctx context.Context) (*metainfo.Client, error) {
|
|
|
|
return project.client.DialMetainfo(ctx, project.Satellite, project.RawAPIKey)
|
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) {
|
2020-04-06 19:29:32 +01:00
|
|
|
ctx := context.TODO()
|
|
|
|
|
2019-02-04 16:56:10 +00:00
|
|
|
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{
|
2020-04-16 13:12:46 +01:00
|
|
|
Log: planet.log.Named(name),
|
|
|
|
Identity: identity,
|
|
|
|
StorageNodeCount: storageNodeCount,
|
|
|
|
APIKey: map[storj.NodeID]*macaroon.APIKey{},
|
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
|
|
|
|
2019-02-05 17:22:17 +00:00
|
|
|
for j, satellite := range planet.Satellites {
|
2020-05-19 14:36:01 +01:00
|
|
|
console := satellite.API.Console
|
2019-02-05 17:22:17 +00:00
|
|
|
|
|
|
|
projectName := fmt.Sprintf("%s_%d", name, j)
|
2020-05-19 14:36:01 +01:00
|
|
|
user, err := satellite.AddUser(
|
|
|
|
ctx,
|
|
|
|
fmt.Sprintf("User %s", projectName),
|
|
|
|
fmt.Sprintf("user@%s.test", projectName),
|
|
|
|
10,
|
2020-04-06 19:29:32 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-19 14:36:01 +01:00
|
|
|
project, err := satellite.AddProject(ctx, user.ID, projectName)
|
2020-04-06 19:29:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-19 14:36:01 +01:00
|
|
|
authCtx, err := satellite.authenticatedContext(ctx, user.ID)
|
2019-02-05 17:22:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-19 14:36:01 +01:00
|
|
|
_, apiKey, err := console.Service.CreateAPIKey(authCtx, project.ID, "root")
|
2019-02-05 17:22:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-19 14:36:01 +01:00
|
|
|
uplink.APIKey[satellite.ID()] = apiKey
|
2020-04-16 13:12:46 +01:00
|
|
|
|
|
|
|
uplink.Projects = append(uplink.Projects, &Project{
|
|
|
|
client: uplink,
|
|
|
|
|
|
|
|
ID: project.ID,
|
|
|
|
Owner: ProjectOwner{
|
2020-05-19 14:36:01 +01:00
|
|
|
ID: user.ID,
|
|
|
|
Email: user.Email,
|
2020-04-16 13:12:46 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
Satellite: satellite,
|
2020-05-19 14:36:01 +01:00
|
|
|
APIKey: apiKey.Serialize(),
|
2020-04-16 13:12:46 +01:00
|
|
|
|
2020-05-19 14:36:01 +01:00
|
|
|
RawAPIKey: apiKey,
|
2020-04-16 13:12:46 +01:00
|
|
|
})
|
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
|
2020-05-20 14:11:54 +01:00
|
|
|
func (client *Uplink) ID() storj.NodeID { return client.Identity.ID }
|
2019-02-04 16:56:10 +00:00
|
|
|
|
|
|
|
// Addr returns uplink address
|
2020-05-20 14:11:54 +01:00
|
|
|
func (client *Uplink) Addr() string { return "" }
|
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) {
|
2020-05-19 16:49:13 +01:00
|
|
|
return metainfo.DialNodeURL(ctx, client.Dialer, destination.NodeURL().String(), apikey, "Test/1.0")
|
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) {
|
2020-05-19 16:49:13 +01:00
|
|
|
return piecestore.DialNodeURL(ctx, client.Dialer, destination.NodeURL(), 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
|
2020-03-27 14:46:40 +00:00
|
|
|
func (client *Uplink) Upload(ctx context.Context, satellite *Satellite, 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
|
2020-03-27 14:46:40 +00:00
|
|
|
func (client *Uplink) UploadWithExpiration(ctx context.Context, satellite *Satellite, bucketName string, path storj.Path, data []byte, expiration time.Time) error {
|
2020-03-30 10:08:02 +01:00
|
|
|
project, err := client.GetNewProject(ctx, satellite)
|
2019-03-21 14:26:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-30 10:08:02 +01:00
|
|
|
defer func() { err = errs.Combine(err, project.Close()) }()
|
2019-03-21 14:26:56 +00:00
|
|
|
|
2020-03-30 10:08:02 +01:00
|
|
|
_, err = project.EnsureBucket(ctx, bucketName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2020-03-30 10:08:02 +01:00
|
|
|
upload, err := project.UploadObject(ctx, bucketName, path, &uplink.UploadOptions{
|
|
|
|
Expires: expiration,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-07-23 15:58:45 +01:00
|
|
|
return err
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 10:08:02 +01:00
|
|
|
_, err = io.Copy(upload, bytes.NewReader(data))
|
|
|
|
if err != nil {
|
|
|
|
abortErr := upload.Abort()
|
|
|
|
err = errs.Combine(err, abortErr)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return upload.Commit()
|
2019-07-23 15:58:45 +01:00
|
|
|
}
|
|
|
|
|
2019-08-30 22:30:18 +01:00
|
|
|
// UploadWithClientConfig uploads data to specific satellite with custom client configuration
|
2020-03-27 14:46:40 +00:00
|
|
|
func (client *Uplink) UploadWithClientConfig(ctx context.Context, satellite *Satellite, clientConfig UplinkConfig, 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
|
2020-03-27 14:46:40 +00:00
|
|
|
func (client *Uplink) Download(ctx context.Context, satellite *Satellite, bucketName string, path storj.Path) ([]byte, error) {
|
2020-03-30 10:08:02 +01:00
|
|
|
project, err := client.GetNewProject(ctx, 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
|
|
|
}
|
2020-03-30 10:08:02 +01:00
|
|
|
defer func() { err = errs.Combine(err, project.Close()) }()
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2020-03-30 10:08:02 +01:00
|
|
|
download, err := project.DownloadObject(ctx, bucketName, path, nil)
|
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
|
|
|
}
|
2020-03-30 10:08:02 +01:00
|
|
|
defer func() { err = errs.Combine(err, download.Close()) }()
|
2019-07-01 15:35:10 +01:00
|
|
|
|
2020-03-30 10:08:02 +01:00
|
|
|
data, err := ioutil.ReadAll(download)
|
2019-07-23 15:58:45 +01:00
|
|
|
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
|
2020-03-27 14:46:40 +00:00
|
|
|
func (client *Uplink) DownloadStream(ctx context.Context, satellite *Satellite, bucketName string, path storj.Path) (_ io.ReadCloser, cleanup func() error, err error) {
|
2020-03-30 10:08:02 +01:00
|
|
|
project, err := client.GetNewProject(ctx, 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(),
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2020-03-30 10:08:02 +01:00
|
|
|
downloader, err := project.DownloadObject(ctx, bucketName, path, nil)
|
2019-08-13 18:29:02 +01:00
|
|
|
return downloader, cleanup, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DownloadStreamRange returns stream for downloading data
|
2020-03-27 14:46:40 +00:00
|
|
|
func (client *Uplink) DownloadStreamRange(ctx context.Context, satellite *Satellite, bucketName string, path storj.Path, start, limit int64) (_ io.ReadCloser, cleanup func() error, err error) {
|
2020-03-30 10:08:02 +01:00
|
|
|
project, err := client.GetNewProject(ctx, satellite)
|
2019-08-13 18:29:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup = func() error {
|
|
|
|
err = errs.Combine(err,
|
|
|
|
project.Close(),
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-30 10:08:02 +01:00
|
|
|
downloader, err := project.DownloadObject(ctx, bucketName, path, &uplink.DownloadOptions{
|
|
|
|
Offset: start,
|
|
|
|
Length: limit,
|
|
|
|
})
|
2019-07-23 15:58:45 +01:00
|
|
|
return downloader, cleanup, err
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 12:18:18 +00:00
|
|
|
// DeleteObject deletes an object at the path in a bucket
|
2020-03-27 14:46:40 +00:00
|
|
|
func (client *Uplink) DeleteObject(ctx context.Context, satellite *Satellite, bucketName string, path storj.Path) error {
|
2020-03-30 10:08:02 +01:00
|
|
|
project, err := client.GetNewProject(ctx, 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
|
|
|
}
|
2020-03-30 10:08:02 +01:00
|
|
|
defer func() { err = errs.Combine(err, project.Close()) }()
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2020-03-30 10:08:02 +01:00
|
|
|
_, err = project.DeleteObject(ctx, bucketName, 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
|
|
|
}
|
2020-03-30 10:08:02 +01:00
|
|
|
return err
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 15:58:45 +01:00
|
|
|
// CreateBucket creates a new bucket
|
2020-03-27 14:46:40 +00:00
|
|
|
func (client *Uplink) CreateBucket(ctx context.Context, satellite *Satellite, bucketName string) error {
|
2020-03-30 10:08:02 +01:00
|
|
|
project, err := client.GetNewProject(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
|
|
|
|
2020-03-30 10:08:02 +01:00
|
|
|
_, err = project.CreateBucket(ctx, bucketName)
|
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
|
|
|
}
|
|
|
|
|
2020-02-10 12:18:18 +00:00
|
|
|
// DeleteBucket deletes a bucket.
|
2020-03-27 14:46:40 +00:00
|
|
|
func (client *Uplink) DeleteBucket(ctx context.Context, satellite *Satellite, bucketName string) error {
|
2020-03-30 10:08:02 +01:00
|
|
|
project, err := client.GetNewProject(ctx, satellite)
|
2020-02-10 12:18:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, project.Close()) }()
|
|
|
|
|
2020-03-30 10:08:02 +01:00
|
|
|
_, err = project.DeleteBucket(ctx, bucketName)
|
2020-02-10 12:18:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-30 11:21:49 +00:00
|
|
|
// GetConfig returns a default config for a given satellite.
|
2020-03-27 14:46:40 +00:00
|
|
|
func (client *Uplink) GetConfig(satellite *Satellite) UplinkConfig {
|
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{})
|
2020-02-11 08:25:31 +00:00
|
|
|
encAccess.SetDefaultPathCipher(storj.EncAESGCM)
|
2019-08-05 18:01:20 +01:00
|
|
|
|
2020-01-09 12:24:49 +00:00
|
|
|
accessData, err := (&libuplink.Scope{
|
2020-04-16 13:12:46 +01:00
|
|
|
SatelliteAddr: satellite.URL(),
|
2019-08-05 18:01:20 +01:00
|
|
|
APIKey: apiKey,
|
|
|
|
EncryptionAccess: encAccess,
|
|
|
|
}).Serialize()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-01-09 12:24:49 +00:00
|
|
|
config.Access = accessData
|
2019-08-05 18:01:20 +01:00
|
|
|
|
|
|
|
// 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.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
|
|
|
}
|
|
|
|
|
2020-03-09 10:25:15 +00:00
|
|
|
func getDefaultConfig() UplinkConfig {
|
|
|
|
config := UplinkConfig{}
|
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
|
|
|
|
|
|
|
|
return libuplink.NewUplink(ctx, libuplinkCfg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetProject returns a libuplink.Project which allows interactions with a specific project
|
2020-03-27 14:46:40 +00:00
|
|
|
func (client *Uplink) GetProject(ctx context.Context, satellite *Satellite) (*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
|
|
|
|
2020-01-09 12:24:49 +00:00
|
|
|
access, err := client.GetConfig(satellite).GetAccess()
|
2019-07-23 15:58:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-27 18:36:51 +01:00
|
|
|
|
2020-01-09 12:24:49 +00:00
|
|
|
project, err := testLibuplink.OpenProject(ctx, access.SatelliteAddr, access.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
|
|
|
|
2020-03-30 10:08:02 +01:00
|
|
|
// GetNewProject returns a uplink.Project which allows interactions with a specific project
|
|
|
|
func (client *Uplink) GetNewProject(ctx context.Context, satellite *Satellite) (*uplink.Project, error) {
|
|
|
|
oldAccess, err := client.GetConfig(satellite).GetAccess()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
serializedOldAccess, err := oldAccess.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
access, err := uplink.ParseAccess(serializedOldAccess)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := uplink.OpenProject(ctx, access)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return project, nil
|
|
|
|
}
|
|
|
|
|
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
|
2020-03-27 14:46:40 +00:00
|
|
|
func (client *Uplink) GetProjectAndBucket(ctx context.Context, satellite *Satellite, bucketName string, clientCfg UplinkConfig) (_ *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
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-01-09 12:24:49 +00:00
|
|
|
access, err := client.GetConfig(satellite).GetAccess()
|
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
|
|
|
}
|
|
|
|
|
2020-01-09 12:24:49 +00:00
|
|
|
bucket, err := project.OpenBucket(ctx, bucketName, access.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
|
|
|
|
2020-03-09 10:25:15 +00:00
|
|
|
func createBucket(ctx context.Context, config UplinkConfig, project libuplink.Project, bucketName string) error {
|
2019-07-23 15:58:45 +01:00
|
|
|
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
|
|
|
}
|