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"
|
2020-10-29 11:58:36 +00:00
|
|
|
"runtime/pprof"
|
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
|
|
|
|
|
|
|
"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-07-24 10:40:17 +01:00
|
|
|
"storj.io/storj/satellite/console"
|
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"
|
2020-05-26 09:05:43 +01:00
|
|
|
"storj.io/uplink/private/testuplink"
|
2019-02-04 16:56:10 +00:00
|
|
|
)
|
|
|
|
|
2020-07-16 16:27:24 +01:00
|
|
|
// Uplink is a registered user on all satellites,
|
|
|
|
// which contains the necessary accesses and project info.
|
2019-02-04 16:56:10 +00:00
|
|
|
type Uplink struct {
|
2020-05-21 12:22:37 +01:00
|
|
|
Log *zap.Logger
|
|
|
|
Identity *identity.FullIdentity
|
|
|
|
Dialer rpc.Dialer
|
2020-07-20 10:40:12 +01:00
|
|
|
Config uplink.Config
|
2019-09-12 11:38:49 +01:00
|
|
|
|
2020-04-16 13:12:46 +01:00
|
|
|
APIKey map[storj.NodeID]*macaroon.APIKey
|
2020-05-26 09:05:43 +01:00
|
|
|
Access map[storj.NodeID]*uplink.Access
|
2020-04-16 13:12:46 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2020-05-21 12:22:37 +01:00
|
|
|
// newUplinks creates initializes uplinks, requires peer to have at least one satellite.
|
2020-10-29 11:58:36 +00:00
|
|
|
func (planet *Planet) newUplinks(ctx context.Context, prefix string, count int) ([]*Uplink, error) {
|
2019-06-21 14:39:43 +01:00
|
|
|
var xs []*Uplink
|
|
|
|
for i := 0; i < count; i++ {
|
2020-10-29 11:58:36 +00:00
|
|
|
name := prefix + strconv.Itoa(i)
|
|
|
|
var uplink *Uplink
|
|
|
|
var err error
|
|
|
|
pprof.Do(ctx, pprof.Labels("peer", name), func(ctx context.Context) {
|
|
|
|
uplink, err = planet.newUplink(ctx, name)
|
|
|
|
})
|
2019-06-21 14:39:43 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
xs = append(xs, uplink)
|
|
|
|
}
|
|
|
|
|
|
|
|
return xs, nil
|
|
|
|
}
|
|
|
|
|
2020-05-21 12:22:37 +01:00
|
|
|
// newUplink creates a new uplink.
|
2020-10-29 11:58:36 +00:00
|
|
|
func (planet *Planet) newUplink(ctx context.Context, name string) (*Uplink, error) {
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
planetUplink := &Uplink{
|
2020-05-21 12:22:37 +01:00
|
|
|
Log: planet.log.Named(name),
|
|
|
|
Identity: identity,
|
|
|
|
APIKey: map[storj.NodeID]*macaroon.APIKey{},
|
2020-05-26 09:05:43 +01:00
|
|
|
Access: map[storj.NodeID]*uplink.Access{},
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
planetUplink.Log.Debug("id=" + identity.ID.String())
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
planetUplink.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-07-24 10:40:17 +01:00
|
|
|
consoleAPI := satellite.API.Console
|
2019-02-05 17:22:17 +00:00
|
|
|
|
|
|
|
projectName := fmt.Sprintf("%s_%d", name, j)
|
2020-07-24 10:40:17 +01:00
|
|
|
user, err := satellite.AddUser(ctx, console.CreateUser{
|
|
|
|
FullName: fmt.Sprintf("User %s", projectName),
|
|
|
|
Email: 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-07-24 10:40:17 +01:00
|
|
|
authCtx, err := satellite.AuthenticatedContext(ctx, user.ID)
|
2019-02-05 17:22:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-24 10:40:17 +01:00
|
|
|
_, apiKey, err := consoleAPI.Service.CreateAPIKey(authCtx, project.ID, "root")
|
2019-02-05 17:22:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
planetUplink.APIKey[satellite.ID()] = apiKey
|
2020-04-16 13:12:46 +01:00
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
planetUplink.Projects = append(planetUplink.Projects, &Project{
|
|
|
|
client: planetUplink,
|
2020-04-16 13:12:46 +01:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
planet.Uplinks = append(planet.Uplinks, planetUplink)
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
return planetUplink, nil
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// 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
|
|
|
|
2020-07-16 15:18:02 +01: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
|
|
|
|
2020-07-16 15:18:02 +01: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
|
|
|
|
2020-07-16 15:18:02 +01: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
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01: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
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +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-05-26 09:05:43 +01:00
|
|
|
_, found := testuplink.GetMaxSegmentSize(ctx)
|
|
|
|
if !found {
|
|
|
|
ctx = testuplink.WithMaxSegmentSize(ctx, satellite.Config.Metainfo.MaxSegmentSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
project, err := client.GetProject(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
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +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-05-26 09:05:43 +01:00
|
|
|
project, err := client.GetProject(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
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +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-05-26 09:05:43 +01:00
|
|
|
project, err := client.GetProject(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
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// 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-05-26 09:05:43 +01:00
|
|
|
project, err := client.GetProject(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-07-16 15:18:02 +01: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-05-26 09:05:43 +01:00
|
|
|
project, err := client.GetProject(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
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +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-05-26 09:05:43 +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
|
|
|
|
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-05-26 09:05:43 +01:00
|
|
|
project, err := client.GetProject(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
|
|
|
|
}
|
|
|
|
|
2020-05-26 09:05:43 +01:00
|
|
|
// GetProject returns a uplink.Project which allows interactions with a specific project.
|
|
|
|
func (client *Uplink) GetProject(ctx context.Context, satellite *Satellite) (*uplink.Project, error) {
|
|
|
|
access := client.Access[satellite.ID()]
|
2020-03-30 10:08:02 +01:00
|
|
|
|
2020-07-20 10:40:12 +01:00
|
|
|
project, err := client.Config.OpenProject(ctx, access)
|
2020-03-30 10:08:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return project, nil
|
|
|
|
}
|