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"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
|
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-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/pkg/auth/signing"
|
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"
|
|
|
|
"storj.io/storj/pkg/pb"
|
2019-02-11 11:17:32 +00:00
|
|
|
"storj.io/storj/pkg/peertls/tlsopts"
|
2019-02-04 16:56:10 +00:00
|
|
|
"storj.io/storj/pkg/storage/streams"
|
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
"storj.io/storj/pkg/stream"
|
|
|
|
"storj.io/storj/pkg/transport"
|
|
|
|
"storj.io/storj/satellite"
|
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
|
|
|
|
Transport transport.Client
|
|
|
|
StorageNodeCount int
|
2019-02-05 17:22:17 +00:00
|
|
|
APIKey map[storj.NodeID]string
|
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-02-11 11:17:32 +00:00
|
|
|
tlsOpts, err := tlsopts.NewOptions(identity, tlsopts.Config{})
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
uplink.Log.Debug("id=" + identity.ID.String())
|
|
|
|
|
2019-02-11 11:17:32 +00:00
|
|
|
uplink.Transport = transport.NewClient(tlsOpts)
|
2019-02-04 16:56:10 +00:00
|
|
|
|
|
|
|
uplink.Info = pb.Node{
|
|
|
|
Id: uplink.Identity.ID,
|
|
|
|
Type: pb.NodeType_UPLINK,
|
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Transport: pb.NodeTransport_TCP_TLS_GRPC,
|
|
|
|
Address: "",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-02-05 17:22:17 +00:00
|
|
|
apiKeys := make(map[storj.NodeID]string)
|
|
|
|
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)
|
|
|
|
key := console.APIKeyFromBytes([]byte(projectName))
|
|
|
|
|
|
|
|
project, err := consoleDB.Projects().Insert(
|
|
|
|
context.Background(),
|
|
|
|
&console.Project{
|
|
|
|
Name: projectName,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = consoleDB.APIKeys().Create(
|
|
|
|
context.Background(),
|
|
|
|
*key,
|
|
|
|
console.APIKeyInfo{
|
|
|
|
Name: "root",
|
|
|
|
ProjectID: project.ID,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
apiKeys[satellite.ID()] = key.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
uplink.APIKey = apiKeys
|
2019-02-04 16:56:10 +00:00
|
|
|
planet.uplinks = append(planet.uplinks, uplink)
|
|
|
|
|
|
|
|
return uplink, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID returns uplink id
|
|
|
|
func (uplink *Uplink) ID() storj.NodeID { return uplink.Info.Id }
|
|
|
|
|
|
|
|
// Addr returns uplink address
|
|
|
|
func (uplink *Uplink) Addr() string { return uplink.Info.Address.Address }
|
|
|
|
|
|
|
|
// Local returns uplink info
|
|
|
|
func (uplink *Uplink) Local() pb.Node { return uplink.Info }
|
|
|
|
|
|
|
|
// Shutdown shuts down all uplink dependencies
|
|
|
|
func (uplink *Uplink) Shutdown() error { return nil }
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
// DialMetainfo dials destination with apikey and returns metainfo Client
|
|
|
|
func (uplink *Uplink) DialMetainfo(ctx context.Context, destination Peer, apikey string) (metainfo.Client, error) {
|
|
|
|
// TODO: handle disconnect
|
|
|
|
return metainfo.NewClient(ctx, uplink.Transport, destination.Addr(), apikey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialPiecestore dials destination storagenode and returns a piecestore client.
|
|
|
|
func (uplink *Uplink) DialPiecestore(ctx context.Context, destination Peer) (*piecestore.Client, error) {
|
|
|
|
node := destination.Local()
|
|
|
|
|
|
|
|
conn, err := uplink.Transport.DialNode(ctx, &node)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
signer := signing.SignerFromFullIdentity(uplink.Transport.Identity())
|
|
|
|
|
|
|
|
return piecestore.NewClient(uplink.Log.Named("uplink>piecestore"), signer, conn, piecestore.DefaultConfig), nil
|
|
|
|
}
|
|
|
|
|
2019-02-04 16:56:10 +00:00
|
|
|
// Upload data to specific satellite
|
|
|
|
func (uplink *Uplink) Upload(ctx context.Context, satellite *satellite.Peer, bucket string, path storj.Path, data []byte) error {
|
2019-03-30 11:21:49 +00:00
|
|
|
config := uplink.GetConfig(satellite)
|
2019-02-08 12:57:35 +00:00
|
|
|
metainfo, streams, err := config.GetMetainfo(ctx, uplink.Identity)
|
2019-02-04 16:56:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-08 12:57:35 +00:00
|
|
|
encScheme := config.GetEncryptionScheme()
|
|
|
|
redScheme := config.GetRedundancyScheme()
|
2019-03-21 14:26:56 +00:00
|
|
|
|
|
|
|
// create bucket if not exists
|
|
|
|
_, err = metainfo.GetBucket(ctx, bucket)
|
|
|
|
if err != nil {
|
|
|
|
if storj.ErrBucketNotFound.Has(err) {
|
|
|
|
_, err := metainfo.CreateBucket(ctx, bucket, &storj.Bucket{PathCipher: encScheme.Cipher})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
createInfo := storj.CreateObject{
|
|
|
|
RedundancyScheme: redScheme,
|
|
|
|
EncryptionScheme: encScheme,
|
|
|
|
}
|
|
|
|
obj, err := metainfo.CreateObject(ctx, bucket, path, &createInfo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reader := bytes.NewReader(data)
|
|
|
|
err = uploadStream(ctx, streams, obj, reader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UploadWithConfig uploads data to specific satellite with configured values
|
|
|
|
func (uplink *Uplink) UploadWithConfig(ctx context.Context, satellite *satellite.Peer, redundancy *uplink.RSConfig, bucket string, path storj.Path, data []byte) error {
|
2019-03-30 11:21:49 +00:00
|
|
|
config := uplink.GetConfig(satellite)
|
2019-03-21 14:26:56 +00:00
|
|
|
if redundancy != nil {
|
|
|
|
config.RS.MinThreshold = redundancy.MinThreshold
|
|
|
|
config.RS.RepairThreshold = redundancy.RepairThreshold
|
|
|
|
config.RS.SuccessThreshold = redundancy.SuccessThreshold
|
|
|
|
config.RS.MaxThreshold = redundancy.MaxThreshold
|
|
|
|
}
|
|
|
|
|
|
|
|
metainfo, streams, err := config.GetMetainfo(ctx, uplink.Identity)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
encScheme := config.GetEncryptionScheme()
|
|
|
|
redScheme := config.GetRedundancyScheme()
|
2019-02-04 16:56:10 +00:00
|
|
|
|
|
|
|
// create bucket if not exists
|
|
|
|
_, err = metainfo.GetBucket(ctx, bucket)
|
|
|
|
if err != nil {
|
|
|
|
if storj.ErrBucketNotFound.Has(err) {
|
|
|
|
_, err := metainfo.CreateBucket(ctx, bucket, &storj.Bucket{PathCipher: encScheme.Cipher})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
createInfo := storj.CreateObject{
|
|
|
|
RedundancyScheme: redScheme,
|
|
|
|
EncryptionScheme: encScheme,
|
|
|
|
}
|
|
|
|
obj, err := metainfo.CreateObject(ctx, bucket, path, &createInfo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reader := bytes.NewReader(data)
|
|
|
|
err = uploadStream(ctx, streams, obj, reader)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func uploadStream(ctx context.Context, streams streams.Store, mutableObject storj.MutableObject, reader io.Reader) error {
|
|
|
|
mutableStream, err := mutableObject.CreateStream(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
upload := stream.NewUpload(ctx, mutableStream, streams)
|
|
|
|
|
|
|
|
_, err = io.Copy(upload, reader)
|
|
|
|
|
|
|
|
return errs.Combine(err, upload.Close())
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
// DownloadStream returns stream for downloading data.
|
|
|
|
func (uplink *Uplink) DownloadStream(ctx context.Context, satellite *satellite.Peer, bucket string, path storj.Path) (*stream.Download, error) {
|
2019-03-30 11:21:49 +00:00
|
|
|
config := uplink.GetConfig(satellite)
|
2019-02-08 12:57:35 +00:00
|
|
|
metainfo, streams, err := config.GetMetainfo(ctx, uplink.Identity)
|
2019-02-04 16:56:10 +00:00
|
|
|
if err != nil {
|
2019-03-18 10:55:06 +00:00
|
|
|
return nil, err
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
readOnlyStream, err := metainfo.GetObjectStream(ctx, bucket, path)
|
|
|
|
if err != nil {
|
2019-03-18 10:55:06 +00:00
|
|
|
return nil, err
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
return stream.NewDownload(ctx, readOnlyStream, streams), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Download data from specific satellite
|
|
|
|
func (uplink *Uplink) Download(ctx context.Context, satellite *satellite.Peer, bucket string, path storj.Path) ([]byte, error) {
|
|
|
|
download, err := uplink.DownloadStream(ctx, satellite, bucket, path)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
2019-02-04 16:56:10 +00:00
|
|
|
defer func() { err = errs.Combine(err, download.Close()) }()
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(download)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
// Delete data to specific satellite
|
|
|
|
func (uplink *Uplink) Delete(ctx context.Context, satellite *satellite.Peer, bucket string, path storj.Path) error {
|
2019-03-30 11:21:49 +00:00
|
|
|
config := uplink.GetConfig(satellite)
|
2019-03-18 10:55:06 +00:00
|
|
|
metainfo, _, err := config.GetMetainfo(ctx, uplink.Identity)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return metainfo.DeleteObject(ctx, bucket, path)
|
|
|
|
}
|
|
|
|
|
2019-03-30 11:21:49 +00:00
|
|
|
// GetConfig returns a default config for a given satellite.
|
|
|
|
func (uplink *Uplink) GetConfig(satellite *satellite.Peer) uplink.Config {
|
2019-02-08 12:57:35 +00:00
|
|
|
config := getDefaultConfig()
|
2019-03-22 09:01:49 +00:00
|
|
|
config.Client.SatelliteAddr = satellite.Addr()
|
2019-02-08 12:57:35 +00:00
|
|
|
config.Client.APIKey = uplink.APIKey[satellite.ID()]
|
2019-02-04 16:56:10 +00:00
|
|
|
|
2019-02-08 12:57:35 +00:00
|
|
|
config.RS.MinThreshold = 1 * uplink.StorageNodeCount / 5
|
|
|
|
config.RS.RepairThreshold = 2 * uplink.StorageNodeCount / 5
|
|
|
|
config.RS.SuccessThreshold = 3 * uplink.StorageNodeCount / 5
|
|
|
|
config.RS.MaxThreshold = 4 * uplink.StorageNodeCount / 5
|
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-03-12 12:51:06 +00:00
|
|
|
cfgstruct.Bind(&pflag.FlagSet{}, &config, true)
|
2019-02-08 12:57:35 +00:00
|
|
|
return config
|
2019-02-04 16:56:10 +00:00
|
|
|
}
|