2019-04-10 23:27:04 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-07-05 21:02:49 +01:00
|
|
|
package uplink_test
|
2019-04-10 23:27:04 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-08-01 12:14:09 +01:00
|
|
|
"go.uber.org/zap/zaptest"
|
2019-04-10 23:27:04 +01:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/memory"
|
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2019-07-05 21:02:49 +01:00
|
|
|
"storj.io/storj/lib/uplink"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/testplanet"
|
2019-04-10 23:27:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type testConfig struct {
|
2019-07-05 21:02:49 +01:00
|
|
|
uplinkCfg uplink.Config
|
2019-04-10 23:27:04 +01:00
|
|
|
}
|
|
|
|
|
2019-06-24 03:06:14 +01:00
|
|
|
func testPlanetWithLibUplink(t *testing.T, cfg testConfig,
|
2019-07-05 21:02:49 +01:00
|
|
|
testFunc func(*testing.T, *testcontext.Context, *testplanet.Planet, *uplink.Project)) {
|
2019-04-22 10:07:50 +01:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 5, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
// we only use testUplink for the free API key, until such time
|
|
|
|
// as testplanet makes it easy to get another way :D
|
|
|
|
testUplink := planet.Uplinks[0]
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
|
2019-09-19 17:19:29 +01:00
|
|
|
apiKey, err := uplink.ParseAPIKey(testUplink.APIKey[satellite.ID()].Serialize())
|
2019-04-22 10:07:50 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not parse API key from testplanet: %v", err)
|
|
|
|
}
|
2019-07-05 21:02:49 +01:00
|
|
|
up, err := uplink.NewUplink(ctx, &cfg.uplinkCfg)
|
2019-04-22 10:07:50 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not create new Uplink object: %v", err)
|
|
|
|
}
|
2019-07-05 21:02:49 +01:00
|
|
|
defer ctx.Check(up.Close)
|
|
|
|
proj, err := up.OpenProject(ctx, satellite.Addr(), apiKey)
|
2019-04-22 10:07:50 +01:00
|
|
|
if err != nil {
|
2019-07-05 21:02:49 +01:00
|
|
|
t.Fatalf("could not open project from uplink under testplanet: %v", err)
|
2019-04-22 10:07:50 +01:00
|
|
|
}
|
|
|
|
defer ctx.Check(proj.Close)
|
|
|
|
|
|
|
|
testFunc(t, ctx, planet, proj)
|
|
|
|
})
|
2019-04-10 23:27:04 +01:00
|
|
|
}
|
|
|
|
|
2019-07-19 16:17:34 +01:00
|
|
|
// check that partner bucket attributes are stored and retrieved correctly.
|
2019-11-26 11:12:37 +00:00
|
|
|
func TestBucket_PartnerAttribution(t *testing.T) {
|
2019-07-19 16:17:34 +01:00
|
|
|
var (
|
|
|
|
access = uplink.NewEncryptionAccessWithDefaultKey(storj.Key{0, 1, 2, 3, 4})
|
|
|
|
bucketName = "mightynein"
|
|
|
|
)
|
|
|
|
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
2019-11-26 11:12:37 +00:00
|
|
|
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 1,
|
2019-07-19 16:17:34 +01:00
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
satellite := planet.Satellites[0]
|
2019-09-19 17:19:29 +01:00
|
|
|
apikey, err := uplink.ParseAPIKey(planet.Uplinks[0].APIKey[satellite.ID()].Serialize())
|
2019-07-19 16:17:34 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-04-02 15:18:08 +01:00
|
|
|
partnerID := testrand.UUID()
|
2019-07-19 16:17:34 +01:00
|
|
|
|
|
|
|
t.Run("without partner id", func(t *testing.T) {
|
|
|
|
config := uplink.Config{}
|
2019-08-01 12:14:09 +01:00
|
|
|
config.Volatile.Log = zaptest.NewLogger(t)
|
2019-07-19 16:17:34 +01:00
|
|
|
config.Volatile.TLS.SkipPeerCAWhitelist = true
|
|
|
|
|
|
|
|
up, err := uplink.NewUplink(ctx, &config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(up.Close)
|
|
|
|
|
|
|
|
project, err := up.OpenProject(ctx, satellite.Addr(), apikey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(project.Close)
|
|
|
|
|
|
|
|
bucketInfo, err := project.CreateBucket(ctx, bucketName, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.True(t, bucketInfo.PartnerID.IsZero())
|
|
|
|
|
|
|
|
_, err = project.CreateBucket(ctx, bucketName, nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("open with partner id", func(t *testing.T) {
|
|
|
|
config := uplink.Config{}
|
2019-08-01 12:14:09 +01:00
|
|
|
config.Volatile.Log = zaptest.NewLogger(t)
|
2019-07-19 16:17:34 +01:00
|
|
|
config.Volatile.TLS.SkipPeerCAWhitelist = true
|
|
|
|
config.Volatile.PartnerID = partnerID.String()
|
|
|
|
|
|
|
|
up, err := uplink.NewUplink(ctx, &config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(up.Close)
|
|
|
|
|
|
|
|
project, err := up.OpenProject(ctx, satellite.Addr(), apikey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(project.Close)
|
|
|
|
|
|
|
|
bucket, err := project.OpenBucket(ctx, bucketName, access)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(bucket.Close)
|
|
|
|
|
|
|
|
bucketInfo, _, err := project.GetBucketInfo(ctx, bucketName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, bucketInfo.PartnerID.String(), config.Volatile.PartnerID)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("open with different partner id", func(t *testing.T) {
|
|
|
|
config := uplink.Config{}
|
2019-08-01 12:14:09 +01:00
|
|
|
config.Volatile.Log = zaptest.NewLogger(t)
|
2019-07-19 16:17:34 +01:00
|
|
|
config.Volatile.TLS.SkipPeerCAWhitelist = true
|
2020-04-02 15:18:08 +01:00
|
|
|
config.Volatile.PartnerID = testrand.UUID().String()
|
2019-07-19 16:17:34 +01:00
|
|
|
|
|
|
|
up, err := uplink.NewUplink(ctx, &config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(up.Close)
|
|
|
|
|
|
|
|
project, err := up.OpenProject(ctx, satellite.Addr(), apikey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(project.Close)
|
|
|
|
|
|
|
|
bucket, err := project.OpenBucket(ctx, bucketName, access)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(bucket.Close)
|
|
|
|
|
|
|
|
bucketInfo, _, err := project.GetBucketInfo(ctx, bucketName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotEqual(t, bucketInfo.PartnerID.String(), config.Volatile.PartnerID)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-26 11:12:37 +00:00
|
|
|
// check that partner bucket attributes are stored and retrieved correctly.
|
|
|
|
func TestBucket_UserAgent(t *testing.T) {
|
|
|
|
var (
|
|
|
|
access = uplink.NewEncryptionAccessWithDefaultKey(storj.Key{0, 1, 2, 3, 4})
|
|
|
|
bucketName = "mightynein"
|
|
|
|
)
|
|
|
|
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 5, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
apikey, err := uplink.ParseAPIKey(planet.Uplinks[0].APIKey[satellite.ID()].Serialize())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
t.Run("without user agent", func(t *testing.T) {
|
|
|
|
config := uplink.Config{}
|
|
|
|
config.Volatile.Log = zaptest.NewLogger(t)
|
|
|
|
config.Volatile.TLS.SkipPeerCAWhitelist = true
|
2020-03-30 15:19:36 +01:00
|
|
|
// we include the uplink version, which should be ignored by partner id handling
|
|
|
|
config.Volatile.UserAgent = "uplink/v1.0.0 (drpc/v0.10.0 common/v0.0.0-00010101000000-000000000000)"
|
2019-11-26 11:12:37 +00:00
|
|
|
|
|
|
|
up, err := uplink.NewUplink(ctx, &config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(up.Close)
|
|
|
|
|
|
|
|
project, err := up.OpenProject(ctx, satellite.Addr(), apikey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(project.Close)
|
|
|
|
|
|
|
|
bucketInfo, err := project.CreateBucket(ctx, bucketName, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.True(t, bucketInfo.PartnerID.IsZero())
|
|
|
|
|
|
|
|
_, err = project.CreateBucket(ctx, bucketName, nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("open with user agent", func(t *testing.T) {
|
|
|
|
config := uplink.Config{}
|
|
|
|
config.Volatile.Log = zaptest.NewLogger(t)
|
|
|
|
config.Volatile.TLS.SkipPeerCAWhitelist = true
|
2020-03-30 15:19:36 +01:00
|
|
|
// we also include the uplink version, which should be ignored by partner id handling
|
|
|
|
config.Volatile.UserAgent = "Zenko uplink/v1.0.0 (drpc/v0.10.0 common/v0.0.0-00010101000000-000000000000)"
|
2019-11-26 11:12:37 +00:00
|
|
|
|
|
|
|
up, err := uplink.NewUplink(ctx, &config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(up.Close)
|
|
|
|
|
|
|
|
project, err := up.OpenProject(ctx, satellite.Addr(), apikey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(project.Close)
|
|
|
|
|
|
|
|
bucket, err := project.OpenBucket(ctx, bucketName, access)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(bucket.Close)
|
|
|
|
|
|
|
|
bucketInfo, _, err := project.GetBucketInfo(ctx, bucketName)
|
|
|
|
require.NoError(t, err)
|
2020-04-02 13:30:43 +01:00
|
|
|
partnerID, err := uuid.FromString("8cd605fa-ad00-45b6-823e-550eddc611d6")
|
2019-11-26 11:12:37 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-02 15:18:08 +01:00
|
|
|
assert.Equal(t, partnerID, bucketInfo.PartnerID)
|
2019-11-26 11:12:37 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("open with different user agent", func(t *testing.T) {
|
|
|
|
config := uplink.Config{}
|
|
|
|
config.Volatile.Log = zaptest.NewLogger(t)
|
|
|
|
config.Volatile.TLS.SkipPeerCAWhitelist = true
|
|
|
|
config.Volatile.UserAgent = "Temporal"
|
|
|
|
|
|
|
|
up, err := uplink.NewUplink(ctx, &config)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(up.Close)
|
|
|
|
|
|
|
|
project, err := up.OpenProject(ctx, satellite.Addr(), apikey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(project.Close)
|
|
|
|
|
|
|
|
bucket, err := project.OpenBucket(ctx, bucketName, access)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(bucket.Close)
|
|
|
|
|
|
|
|
bucketInfo, _, err := project.GetBucketInfo(ctx, bucketName)
|
|
|
|
require.NoError(t, err)
|
2020-04-02 13:30:43 +01:00
|
|
|
partnerID, err := uuid.FromString("8cd605fa-ad00-45b6-823e-550eddc611d6")
|
2019-11-26 11:12:37 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-02 15:18:08 +01:00
|
|
|
assert.Equal(t, partnerID, bucketInfo.PartnerID)
|
2019-11-26 11:12:37 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-10 23:27:04 +01:00
|
|
|
// check that bucket attributes are stored and retrieved correctly.
|
|
|
|
func TestBucketAttrs(t *testing.T) {
|
|
|
|
var (
|
2019-07-05 21:02:49 +01:00
|
|
|
access = uplink.NewEncryptionAccessWithDefaultKey(storj.Key{0, 1, 2, 3, 4})
|
2019-06-11 18:14:05 +01:00
|
|
|
bucketName = "mightynein"
|
|
|
|
shareSize = memory.KiB.Int32()
|
|
|
|
requiredShares = 2
|
|
|
|
stripeSize = shareSize * int32(requiredShares)
|
|
|
|
stripesPerBlock = 2
|
2019-07-05 21:02:49 +01:00
|
|
|
inBucketConfig = uplink.BucketConfig{
|
2019-04-10 23:27:04 +01:00
|
|
|
PathCipher: storj.EncSecretBox,
|
|
|
|
EncryptionParameters: storj.EncryptionParameters{
|
|
|
|
CipherSuite: storj.EncAESGCM,
|
2019-06-11 18:14:05 +01:00
|
|
|
BlockSize: int32(stripesPerBlock) * stripeSize,
|
2019-04-10 23:27:04 +01:00
|
|
|
},
|
|
|
|
Volatile: struct {
|
|
|
|
RedundancyScheme storj.RedundancyScheme
|
|
|
|
SegmentsSize memory.Size
|
|
|
|
}{
|
|
|
|
RedundancyScheme: storj.RedundancyScheme{
|
|
|
|
Algorithm: storj.ReedSolomon,
|
2019-06-06 19:55:10 +01:00
|
|
|
ShareSize: shareSize,
|
|
|
|
RequiredShares: int16(requiredShares),
|
2019-04-10 23:27:04 +01:00
|
|
|
RepairShares: 3,
|
|
|
|
OptimalShares: 4,
|
|
|
|
TotalShares: 5,
|
|
|
|
},
|
|
|
|
SegmentsSize: 688894,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-12-17 15:33:23 +00:00
|
|
|
cfg := testConfig{}
|
|
|
|
cfg.uplinkCfg.Volatile.TLS.SkipPeerCAWhitelist = true
|
|
|
|
|
|
|
|
testPlanetWithLibUplink(t, cfg,
|
2019-07-05 21:02:49 +01:00
|
|
|
func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet, proj *uplink.Project) {
|
2019-04-10 23:27:04 +01:00
|
|
|
before := time.Now()
|
|
|
|
bucket, err := proj.CreateBucket(ctx, bucketName, &inBucketConfig)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, bucketName, bucket.Name)
|
|
|
|
assert.Falsef(t, bucket.Created.Before(before), "impossible creation time %v", bucket.Created)
|
|
|
|
|
2019-06-27 18:36:51 +01:00
|
|
|
got, err := proj.OpenBucket(ctx, bucketName, access)
|
2019-04-10 23:27:04 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(got.Close)
|
|
|
|
|
|
|
|
assert.Equal(t, bucketName, got.Name)
|
|
|
|
assert.Equal(t, inBucketConfig.PathCipher, got.PathCipher)
|
|
|
|
assert.Equal(t, inBucketConfig.EncryptionParameters, got.EncryptionParameters)
|
|
|
|
assert.Equal(t, inBucketConfig.Volatile.SegmentsSize, got.Volatile.SegmentsSize)
|
2020-01-28 13:44:47 +00:00
|
|
|
// ignore RS values because satellite will override it
|
2019-04-10 23:27:04 +01:00
|
|
|
|
|
|
|
err = proj.DeleteBucket(ctx, bucketName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// check that when uploading objects without any specific RS or encryption
|
|
|
|
// config, the bucket attributes apply. also when uploading objects _with_ more
|
|
|
|
// specific config, the specific config applies and not the bucket attrs.
|
|
|
|
func TestBucketAttrsApply(t *testing.T) {
|
|
|
|
var (
|
2019-06-11 18:14:05 +01:00
|
|
|
bucketName = "dodecahedron"
|
|
|
|
objectPath1 = "vax/vex/vox"
|
|
|
|
objectContents = "Willingham,Ray,Jaffe,Johnson,Riegel,O'Brien,Bailey,Mercer"
|
|
|
|
shareSize = 3 * memory.KiB.Int32()
|
|
|
|
requiredShares = 3
|
|
|
|
stripeSize = shareSize * int32(requiredShares)
|
|
|
|
stripesPerBlock = 2
|
2019-07-05 21:02:49 +01:00
|
|
|
inBucketConfig = uplink.BucketConfig{
|
2019-04-10 23:27:04 +01:00
|
|
|
PathCipher: storj.EncSecretBox,
|
|
|
|
EncryptionParameters: storj.EncryptionParameters{
|
|
|
|
CipherSuite: storj.EncSecretBox,
|
2019-06-11 18:14:05 +01:00
|
|
|
BlockSize: int32(stripesPerBlock) * stripeSize,
|
2019-04-10 23:27:04 +01:00
|
|
|
},
|
|
|
|
Volatile: struct {
|
|
|
|
RedundancyScheme storj.RedundancyScheme
|
|
|
|
SegmentsSize memory.Size
|
|
|
|
}{
|
|
|
|
RedundancyScheme: storj.RedundancyScheme{
|
|
|
|
Algorithm: storj.ReedSolomon,
|
2019-06-06 19:55:10 +01:00
|
|
|
ShareSize: shareSize,
|
|
|
|
RequiredShares: int16(requiredShares),
|
2019-04-10 23:27:04 +01:00
|
|
|
RepairShares: 4,
|
|
|
|
OptimalShares: 5,
|
|
|
|
TotalShares: 5,
|
|
|
|
},
|
|
|
|
SegmentsSize: 1536,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
testConfig testConfig
|
|
|
|
)
|
2020-02-11 08:25:31 +00:00
|
|
|
access := uplink.NewEncryptionAccessWithDefaultKey(storj.Key{0, 1, 2, 3, 4})
|
|
|
|
access.SetDefaultPathCipher(storj.EncAESGCM)
|
2019-12-17 15:33:23 +00:00
|
|
|
|
2019-04-10 23:27:04 +01:00
|
|
|
// so our test object will not be inlined (otherwise it will lose its RS params)
|
|
|
|
testConfig.uplinkCfg.Volatile.MaxInlineSize = 1
|
2019-12-17 15:33:23 +00:00
|
|
|
testConfig.uplinkCfg.Volatile.TLS.SkipPeerCAWhitelist = true
|
2019-04-10 23:27:04 +01:00
|
|
|
|
2019-06-24 03:06:14 +01:00
|
|
|
testPlanetWithLibUplink(t, testConfig,
|
2019-07-05 21:02:49 +01:00
|
|
|
func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet, proj *uplink.Project) {
|
2019-04-10 23:27:04 +01:00
|
|
|
_, err := proj.CreateBucket(ctx, bucketName, &inBucketConfig)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-06-27 18:36:51 +01:00
|
|
|
bucket, err := proj.OpenBucket(ctx, bucketName, access)
|
2019-04-10 23:27:04 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(bucket.Close)
|
|
|
|
|
|
|
|
{
|
|
|
|
buf := bytes.NewBufferString(objectContents)
|
|
|
|
err := bucket.UploadObject(ctx, objectPath1, buf, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
readBack, err := bucket.OpenObject(ctx, objectPath1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(readBack.Close)
|
|
|
|
|
|
|
|
assert.Equal(t, inBucketConfig.EncryptionParameters, readBack.Meta.Volatile.EncryptionParameters)
|
|
|
|
assert.Equal(t, inBucketConfig.Volatile.SegmentsSize.Int64(), readBack.Meta.Volatile.SegmentsSize)
|
|
|
|
|
|
|
|
strm, err := readBack.DownloadRange(ctx, 0, -1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(strm.Close)
|
|
|
|
|
|
|
|
contents, err := ioutil.ReadAll(strm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, string(contents), objectContents)
|
|
|
|
})
|
|
|
|
}
|