2019-12-11 17:44:13 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package metainfo_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2022-01-10 17:36:15 +00:00
|
|
|
"time"
|
2019-12-11 17:44:13 +00:00
|
|
|
|
2021-07-02 13:39:46 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-12-11 17:44:13 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-01-26 11:10:28 +00:00
|
|
|
"go.uber.org/zap"
|
2019-12-11 17:44:13 +00:00
|
|
|
|
2022-01-10 17:36:15 +00:00
|
|
|
"storj.io/common/errs2"
|
2022-01-26 11:10:28 +00:00
|
|
|
"storj.io/common/macaroon"
|
2020-08-06 02:23:45 +01:00
|
|
|
"storj.io/common/pb"
|
2022-01-10 17:36:15 +00:00
|
|
|
"storj.io/common/rpc/rpcstatus"
|
|
|
|
"storj.io/common/signing"
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
2019-12-11 17:44:13 +00:00
|
|
|
"storj.io/storj/private/testplanet"
|
2022-01-26 11:10:28 +00:00
|
|
|
"storj.io/storj/satellite"
|
|
|
|
"storj.io/storj/satellite/internalpb"
|
|
|
|
"storj.io/storj/satellite/metainfo"
|
2021-04-20 09:06:56 +01:00
|
|
|
"storj.io/uplink"
|
2022-01-10 17:36:15 +00:00
|
|
|
"storj.io/uplink/private/metaclient"
|
2019-12-11 17:44:13 +00:00
|
|
|
)
|
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
func TestEndpoint_NoStorageNodes(t *testing.T) {
|
2022-01-26 11:10:28 +00:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
2022-02-04 10:27:38 +00:00
|
|
|
SatelliteCount: 1, UplinkCount: 3,
|
2022-01-26 11:10:28 +00:00
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
2022-02-04 10:27:38 +00:00
|
|
|
t.Run("revoke access", func(t *testing.T) {
|
|
|
|
accessIssuer := planet.Uplinks[0].Access[planet.Satellites[0].ID()]
|
|
|
|
accessUser1, err := accessIssuer.Share(uplink.Permission{
|
|
|
|
AllowDownload: true,
|
|
|
|
AllowUpload: true,
|
|
|
|
AllowList: true,
|
|
|
|
AllowDelete: true,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
accessUser2, err := accessUser1.Share(uplink.Permission{
|
|
|
|
AllowDownload: true,
|
|
|
|
AllowUpload: true,
|
|
|
|
AllowList: true,
|
|
|
|
AllowDelete: true,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2019-12-11 17:44:13 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
projectUser2, err := uplink.OpenProject(ctx, accessUser2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(projectUser2.Close)
|
2020-12-08 08:08:02 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
// confirm that we can create a bucket
|
|
|
|
_, err = projectUser2.CreateBucket(ctx, "bob")
|
|
|
|
require.NoError(t, err)
|
2021-01-11 12:12:54 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
// we shouldn't be allowed to revoke ourselves or our parent
|
|
|
|
err = projectUser2.RevokeAccess(ctx, accessUser2)
|
|
|
|
assert.True(t, errs2.IsRPC(err, rpcstatus.PermissionDenied))
|
|
|
|
err = projectUser2.RevokeAccess(ctx, accessUser1)
|
|
|
|
assert.True(t, errs2.IsRPC(err, rpcstatus.PermissionDenied))
|
2021-01-11 12:12:54 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
projectIssuer, err := uplink.OpenProject(ctx, accessIssuer)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(projectIssuer.Close)
|
2021-01-11 12:12:54 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
projectUser1, err := uplink.OpenProject(ctx, accessUser1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(projectUser1.Close)
|
2020-01-21 10:38:41 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
// I should be able to revoke with accessIssuer
|
|
|
|
err = projectIssuer.RevokeAccess(ctx, accessUser1)
|
|
|
|
require.NoError(t, err)
|
2022-01-26 11:10:28 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
// should no longer be able to create bucket with access 2 or 3
|
|
|
|
_, err = projectUser2.CreateBucket(ctx, "bob1")
|
|
|
|
assert.True(t, errs2.IsRPC(err, rpcstatus.PermissionDenied))
|
|
|
|
_, err = projectUser1.CreateBucket(ctx, "bob1")
|
|
|
|
assert.True(t, errs2.IsRPC(err, rpcstatus.PermissionDenied))
|
|
|
|
})
|
2019-12-11 17:44:13 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
t.Run("revoke macaroon", func(t *testing.T) {
|
|
|
|
// I want the api key for the single satellite in this test
|
|
|
|
up := planet.Uplinks[2]
|
|
|
|
apiKey := up.APIKey[planet.Satellites[0].ID()]
|
2022-01-26 11:10:28 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
client, err := up.DialMetainfo(ctx, planet.Satellites[0], apiKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(client.Close)
|
|
|
|
|
|
|
|
// Sanity check: it should work before revoke
|
|
|
|
_, err = client.ListBuckets(ctx, metaclient.ListBucketsParams{
|
2022-07-08 13:17:19 +01:00
|
|
|
ListOpts: metaclient.BucketListOptions{
|
2022-02-04 10:27:38 +00:00
|
|
|
Cursor: "",
|
|
|
|
Direction: storj.Forward,
|
|
|
|
Limit: 10,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2022-01-26 11:10:28 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
err = planet.Satellites[0].API.DB.Revocation().Revoke(ctx, apiKey.Tail(), []byte("apikey"))
|
|
|
|
require.NoError(t, err)
|
2021-01-11 10:08:18 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
_, err = client.ListBuckets(ctx, metaclient.ListBucketsParams{})
|
|
|
|
assert.True(t, errs2.IsRPC(err, rpcstatus.PermissionDenied))
|
2022-01-26 11:10:28 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
_, err = client.BeginObject(ctx, metaclient.BeginObjectParams{})
|
|
|
|
assert.True(t, errs2.IsRPC(err, rpcstatus.PermissionDenied))
|
2022-01-26 11:10:28 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
_, err = client.BeginDeleteObject(ctx, metaclient.BeginDeleteObjectParams{})
|
|
|
|
assert.True(t, errs2.IsRPC(err, rpcstatus.PermissionDenied))
|
2022-01-26 11:10:28 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
_, err = client.ListBuckets(ctx, metaclient.ListBucketsParams{})
|
|
|
|
assert.True(t, errs2.IsRPC(err, rpcstatus.PermissionDenied))
|
2022-01-26 11:10:28 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
_, _, err = client.ListObjects(ctx, metaclient.ListObjectsParams{})
|
|
|
|
assert.True(t, errs2.IsRPC(err, rpcstatus.PermissionDenied))
|
2022-01-26 11:10:28 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
_, err = client.CreateBucket(ctx, metaclient.CreateBucketParams{})
|
|
|
|
assert.True(t, errs2.IsRPC(err, rpcstatus.PermissionDenied))
|
2022-01-26 11:10:28 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
_, err = client.DeleteBucket(ctx, metaclient.DeleteBucketParams{})
|
|
|
|
assert.True(t, errs2.IsRPC(err, rpcstatus.PermissionDenied))
|
2022-01-26 11:10:28 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
_, err = client.BeginDeleteObject(ctx, metaclient.BeginDeleteObjectParams{})
|
|
|
|
assert.True(t, errs2.IsRPC(err, rpcstatus.PermissionDenied))
|
2022-01-26 11:10:28 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
_, err = client.GetBucket(ctx, metaclient.GetBucketParams{})
|
|
|
|
assert.True(t, errs2.IsRPC(err, rpcstatus.PermissionDenied))
|
2022-01-26 11:10:28 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
_, err = client.GetObject(ctx, metaclient.GetObjectParams{})
|
|
|
|
assert.True(t, errs2.IsRPC(err, rpcstatus.PermissionDenied))
|
2022-01-26 11:10:28 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
_, err = client.GetProjectInfo(ctx)
|
|
|
|
assert.True(t, errs2.IsRPC(err, rpcstatus.PermissionDenied))
|
2022-01-26 11:10:28 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
signer := signing.SignerFromFullIdentity(planet.Satellites[0].Identity)
|
|
|
|
satStreamID := &internalpb.StreamID{
|
|
|
|
CreationDate: time.Now(),
|
|
|
|
}
|
|
|
|
signedStreamID, err := metainfo.SignStreamID(ctx, signer, satStreamID)
|
|
|
|
require.NoError(t, err)
|
2022-01-26 11:10:28 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
encodedStreamID, err := pb.Marshal(signedStreamID)
|
|
|
|
require.NoError(t, err)
|
2021-01-11 10:08:18 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
err = client.CommitObject(ctx, metaclient.CommitObjectParams{StreamID: encodedStreamID})
|
|
|
|
assert.True(t, errs2.IsRPC(err, rpcstatus.PermissionDenied))
|
|
|
|
|
|
|
|
_, err = client.BeginSegment(ctx, metaclient.BeginSegmentParams{StreamID: encodedStreamID})
|
|
|
|
assert.True(t, errs2.IsRPC(err, rpcstatus.PermissionDenied))
|
2021-01-11 10:08:18 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
err = client.MakeInlineSegment(ctx, metaclient.MakeInlineSegmentParams{StreamID: encodedStreamID})
|
|
|
|
assert.True(t, errs2.IsRPC(err, rpcstatus.PermissionDenied))
|
2021-01-11 10:08:18 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
_, err = client.DownloadSegmentWithRS(ctx, metaclient.DownloadSegmentParams{StreamID: encodedStreamID})
|
|
|
|
assert.True(t, errs2.IsRPC(err, rpcstatus.PermissionDenied))
|
2021-01-11 10:08:18 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
_, err = client.ListSegments(ctx, metaclient.ListSegmentsParams{StreamID: encodedStreamID})
|
|
|
|
assert.True(t, errs2.IsRPC(err, rpcstatus.PermissionDenied))
|
2021-01-11 10:08:18 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
// these methods needs SegmentID
|
2021-01-11 10:08:18 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
signedSegmentID, err := metainfo.SignSegmentID(ctx, signer, &internalpb.SegmentID{
|
|
|
|
StreamId: satStreamID,
|
|
|
|
CreationDate: time.Now(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2022-01-26 11:10:28 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
encodedSegmentID, err := pb.Marshal(signedSegmentID)
|
|
|
|
require.NoError(t, err)
|
2022-01-26 11:10:28 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
segmentID, err := storj.SegmentIDFromBytes(encodedSegmentID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = client.CommitSegment(ctx, metaclient.CommitSegmentParams{SegmentID: segmentID})
|
|
|
|
assert.True(t, errs2.IsRPC(err, rpcstatus.PermissionDenied))
|
2022-01-26 11:10:28 +00:00
|
|
|
})
|
2021-01-11 10:08:18 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
t.Run("invalid api key", func(t *testing.T) {
|
|
|
|
throwawayKey, err := macaroon.NewAPIKey([]byte("secret"))
|
|
|
|
require.NoError(t, err)
|
2021-01-11 10:08:18 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
for _, invalidAPIKey := range []string{"", "invalid", "testKey"} {
|
|
|
|
func() {
|
|
|
|
client, err := planet.Uplinks[0].DialMetainfo(ctx, planet.Satellites[0], throwawayKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(client.Close)
|
2021-01-11 10:08:18 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
client.SetRawAPIKey([]byte(invalidAPIKey))
|
2021-01-11 10:08:18 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
_, err = client.BeginObject(ctx, metaclient.BeginObjectParams{})
|
|
|
|
assertInvalidArgument(t, err, false)
|
2020-12-03 18:04:01 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
_, err = client.BeginDeleteObject(ctx, metaclient.BeginDeleteObjectParams{})
|
|
|
|
assertInvalidArgument(t, err, false)
|
|
|
|
|
|
|
|
_, err = client.ListBuckets(ctx, metaclient.ListBucketsParams{})
|
|
|
|
assertInvalidArgument(t, err, false)
|
|
|
|
|
|
|
|
_, _, err = client.ListObjects(ctx, metaclient.ListObjectsParams{})
|
|
|
|
assertInvalidArgument(t, err, false)
|
|
|
|
|
|
|
|
_, err = client.CreateBucket(ctx, metaclient.CreateBucketParams{})
|
|
|
|
assertInvalidArgument(t, err, false)
|
2020-12-03 18:04:01 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
_, err = client.DeleteBucket(ctx, metaclient.DeleteBucketParams{})
|
|
|
|
assertInvalidArgument(t, err, false)
|
2020-12-03 18:04:01 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
_, err = client.BeginDeleteObject(ctx, metaclient.BeginDeleteObjectParams{})
|
|
|
|
assertInvalidArgument(t, err, false)
|
2020-12-03 18:04:01 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
_, err = client.GetBucket(ctx, metaclient.GetBucketParams{})
|
|
|
|
assertInvalidArgument(t, err, false)
|
2020-12-03 18:04:01 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
_, err = client.GetObject(ctx, metaclient.GetObjectParams{})
|
|
|
|
assertInvalidArgument(t, err, false)
|
2020-12-03 18:04:01 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
_, err = client.GetProjectInfo(ctx)
|
|
|
|
assertInvalidArgument(t, err, false)
|
2020-12-03 18:04:01 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
// these methods needs StreamID to do authentication
|
2020-12-03 18:04:01 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
signer := signing.SignerFromFullIdentity(planet.Satellites[0].Identity)
|
|
|
|
satStreamID := &internalpb.StreamID{
|
|
|
|
CreationDate: time.Now(),
|
|
|
|
}
|
|
|
|
signedStreamID, err := metainfo.SignStreamID(ctx, signer, satStreamID)
|
|
|
|
require.NoError(t, err)
|
2020-12-03 18:04:01 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
encodedStreamID, err := pb.Marshal(signedStreamID)
|
|
|
|
require.NoError(t, err)
|
2020-12-03 18:04:01 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
streamID, err := storj.StreamIDFromBytes(encodedStreamID)
|
|
|
|
require.NoError(t, err)
|
2020-12-03 18:04:01 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
err = client.CommitObject(ctx, metaclient.CommitObjectParams{StreamID: streamID})
|
|
|
|
assertInvalidArgument(t, err, false)
|
2020-12-03 18:04:01 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
_, err = client.BeginSegment(ctx, metaclient.BeginSegmentParams{StreamID: streamID})
|
|
|
|
assertInvalidArgument(t, err, false)
|
2020-12-03 18:04:01 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
err = client.MakeInlineSegment(ctx, metaclient.MakeInlineSegmentParams{StreamID: streamID})
|
|
|
|
assertInvalidArgument(t, err, false)
|
|
|
|
|
|
|
|
_, err = client.DownloadSegmentWithRS(ctx, metaclient.DownloadSegmentParams{StreamID: streamID})
|
|
|
|
assertInvalidArgument(t, err, false)
|
|
|
|
|
|
|
|
_, err = client.ListSegments(ctx, metaclient.ListSegmentsParams{StreamID: streamID})
|
|
|
|
assertInvalidArgument(t, err, false)
|
|
|
|
|
|
|
|
// these methods needs SegmentID
|
|
|
|
|
|
|
|
signedSegmentID, err := metainfo.SignSegmentID(ctx, signer, &internalpb.SegmentID{
|
|
|
|
StreamId: satStreamID,
|
|
|
|
CreationDate: time.Now(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
encodedSegmentID, err := pb.Marshal(signedSegmentID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
segmentID, err := storj.SegmentIDFromBytes(encodedSegmentID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = client.CommitSegment(ctx, metaclient.CommitSegmentParams{SegmentID: segmentID})
|
|
|
|
assertInvalidArgument(t, err, false)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("get project info", func(t *testing.T) {
|
|
|
|
apiKey0 := planet.Uplinks[0].APIKey[planet.Satellites[0].ID()]
|
|
|
|
apiKey1 := planet.Uplinks[1].APIKey[planet.Satellites[0].ID()]
|
|
|
|
|
|
|
|
metainfo0, err := planet.Uplinks[0].DialMetainfo(ctx, planet.Satellites[0], apiKey0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
metainfo1, err := planet.Uplinks[0].DialMetainfo(ctx, planet.Satellites[0], apiKey1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
info0, err := metainfo0.GetProjectInfo(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, info0.ProjectSalt)
|
|
|
|
|
|
|
|
info1, err := metainfo1.GetProjectInfo(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, info1.ProjectSalt)
|
|
|
|
|
|
|
|
// Different projects should have different salts
|
|
|
|
require.NotEqual(t, info0.ProjectSalt, info1.ProjectSalt)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("check IDs", func(t *testing.T) {
|
|
|
|
apiKey := planet.Uplinks[0].APIKey[planet.Satellites[0].ID()]
|
|
|
|
metainfoClient, err := planet.Uplinks[0].DialMetainfo(ctx, planet.Satellites[0], apiKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ctx.Check(metainfoClient.Close)
|
2020-12-03 18:04:01 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
{
|
|
|
|
streamID := testrand.StreamID(256)
|
|
|
|
err = metainfoClient.CommitObject(ctx, metaclient.CommitObjectParams{
|
|
|
|
StreamID: streamID,
|
|
|
|
})
|
|
|
|
require.Error(t, err) // invalid streamID
|
|
|
|
|
|
|
|
segmentID := testrand.SegmentID(512)
|
|
|
|
err = metainfoClient.CommitSegment(ctx, metaclient.CommitSegmentParams{
|
|
|
|
SegmentID: segmentID,
|
|
|
|
})
|
|
|
|
require.Error(t, err) // invalid segmentID
|
|
|
|
}
|
|
|
|
|
|
|
|
satellitePeer := signing.SignerFromFullIdentity(planet.Satellites[0].Identity)
|
|
|
|
|
|
|
|
{ // streamID expired
|
|
|
|
signedStreamID, err := metainfo.SignStreamID(ctx, satellitePeer, &internalpb.StreamID{
|
|
|
|
CreationDate: time.Now().Add(-36 * time.Hour),
|
|
|
|
})
|
2022-01-26 11:10:28 +00:00
|
|
|
require.NoError(t, err)
|
2020-12-03 18:04:01 +00:00
|
|
|
|
2022-01-26 11:10:28 +00:00
|
|
|
encodedStreamID, err := pb.Marshal(signedStreamID)
|
|
|
|
require.NoError(t, err)
|
2020-12-03 18:04:01 +00:00
|
|
|
|
2022-01-26 11:10:28 +00:00
|
|
|
streamID, err := storj.StreamIDFromBytes(encodedStreamID)
|
2021-09-27 12:28:57 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
err = metainfoClient.CommitObject(ctx, metaclient.CommitObjectParams{
|
|
|
|
StreamID: streamID,
|
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
2021-09-27 12:28:57 +01:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
{ // segment id missing stream id
|
|
|
|
signedSegmentID, err := metainfo.SignSegmentID(ctx, satellitePeer, &internalpb.SegmentID{
|
|
|
|
CreationDate: time.Now().Add(-1 * time.Hour),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2021-09-27 12:28:57 +01:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
encodedSegmentID, err := pb.Marshal(signedSegmentID)
|
|
|
|
require.NoError(t, err)
|
2022-01-26 11:10:28 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
segmentID, err := storj.SegmentIDFromBytes(encodedSegmentID)
|
|
|
|
require.NoError(t, err)
|
2022-01-26 11:10:28 +00:00
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
err = metainfoClient.CommitSegment(ctx, metaclient.CommitSegmentParams{
|
|
|
|
SegmentID: segmentID,
|
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // segmentID expired
|
|
|
|
signedSegmentID, err := metainfo.SignSegmentID(ctx, satellitePeer, &internalpb.SegmentID{
|
|
|
|
CreationDate: time.Now().Add(-36 * time.Hour),
|
|
|
|
StreamId: &internalpb.StreamID{
|
|
|
|
CreationDate: time.Now(),
|
|
|
|
},
|
2022-01-26 11:10:28 +00:00
|
|
|
})
|
2021-09-27 12:28:57 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-01-26 11:10:28 +00:00
|
|
|
encodedSegmentID, err := pb.Marshal(signedSegmentID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
segmentID, err := storj.SegmentIDFromBytes(encodedSegmentID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-02-04 10:27:38 +00:00
|
|
|
err = metainfoClient.CommitSegment(ctx, metaclient.CommitSegmentParams{
|
|
|
|
SegmentID: segmentID,
|
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
})
|
2020-12-03 18:04:01 +00:00
|
|
|
})
|
|
|
|
}
|
2021-01-11 12:12:54 +00:00
|
|
|
|
2022-01-26 11:10:28 +00:00
|
|
|
func assertInvalidArgument(t *testing.T, err error, allowed bool) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
// If it's allowed, we allow any non-unauthenticated error because
|
|
|
|
// some calls error after authentication checks.
|
|
|
|
if !allowed {
|
|
|
|
assert.True(t, errs2.IsRPC(err, rpcstatus.InvalidArgument))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRateLimit(t *testing.T) {
|
|
|
|
rateLimit := 2
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.Metainfo.RateLimiter.Rate = float64(rateLimit)
|
|
|
|
config.Metainfo.RateLimiter.CacheExpiration = 500 * time.Millisecond
|
2020-08-06 02:23:45 +01:00
|
|
|
},
|
2022-01-26 11:10:28 +00:00
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
ul := planet.Uplinks[0]
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
|
|
|
|
// TODO find a way to reset limiter before test is executed, currently
|
|
|
|
// testplanet is doing one additional request to get access
|
|
|
|
time.Sleep(1 * time.Second)
|
2020-08-06 02:23:45 +01:00
|
|
|
|
2022-01-26 11:10:28 +00:00
|
|
|
var group errs2.Group
|
|
|
|
for i := 0; i <= rateLimit; i++ {
|
|
|
|
group.Go(func() error {
|
|
|
|
return ul.CreateBucket(ctx, satellite, testrand.BucketName())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
groupErrs := group.Wait()
|
|
|
|
require.Len(t, groupErrs, 1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRateLimit_Disabled(t *testing.T) {
|
|
|
|
rateLimit := 2
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.Metainfo.RateLimiter.Enabled = false
|
|
|
|
config.Metainfo.RateLimiter.Rate = float64(rateLimit)
|
2020-08-06 02:23:45 +01:00
|
|
|
},
|
2022-01-26 11:10:28 +00:00
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
ul := planet.Uplinks[0]
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
|
|
|
|
var group errs2.Group
|
|
|
|
for i := 0; i <= rateLimit; i++ {
|
|
|
|
group.Go(func() error {
|
|
|
|
return ul.CreateBucket(ctx, satellite, testrand.BucketName())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
groupErrs := group.Wait()
|
|
|
|
require.Len(t, groupErrs, 0)
|
2020-08-06 02:23:45 +01:00
|
|
|
})
|
|
|
|
}
|
2021-04-08 09:35:40 +01:00
|
|
|
|
2022-01-26 11:10:28 +00:00
|
|
|
func TestRateLimit_ProjectRateLimitOverride(t *testing.T) {
|
2022-01-10 17:36:15 +00:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 1, UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
2022-01-26 11:10:28 +00:00
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.Metainfo.RateLimiter.Rate = 2
|
|
|
|
config.Metainfo.RateLimiter.CacheExpiration = 500 * time.Millisecond
|
|
|
|
},
|
2022-01-10 17:36:15 +00:00
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
2022-01-26 11:10:28 +00:00
|
|
|
ul := planet.Uplinks[0]
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
|
|
|
|
// TODO find a way to reset limiter before test is executed, currently
|
|
|
|
// testplanet is doing one additional request to get access
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
projects, err := satellite.DB.Console().Projects().GetAll(ctx)
|
2022-01-10 17:36:15 +00:00
|
|
|
require.NoError(t, err)
|
2022-01-26 11:10:28 +00:00
|
|
|
require.Len(t, projects, 1)
|
2022-01-10 17:36:15 +00:00
|
|
|
|
2022-01-26 11:10:28 +00:00
|
|
|
rateLimit := 3
|
|
|
|
projects[0].RateLimit = &rateLimit
|
|
|
|
|
|
|
|
err = satellite.DB.Console().Projects().Update(ctx, &projects[0])
|
2022-01-10 17:36:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-01-26 11:10:28 +00:00
|
|
|
var group errs2.Group
|
|
|
|
for i := 0; i <= rateLimit; i++ {
|
|
|
|
group.Go(func() error {
|
|
|
|
return ul.CreateBucket(ctx, satellite, testrand.BucketName())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
groupErrs := group.Wait()
|
|
|
|
require.Len(t, groupErrs, 1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRateLimit_ProjectRateLimitOverrideCachedExpired(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 0, UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.Metainfo.RateLimiter.Rate = 2
|
|
|
|
config.Metainfo.RateLimiter.CacheExpiration = time.Second
|
2022-01-10 17:36:15 +00:00
|
|
|
},
|
2022-01-26 11:10:28 +00:00
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
ul := planet.Uplinks[0]
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
|
|
|
|
// TODO find a way to reset limiter before test is executed, currently
|
|
|
|
// testplanet is doing one additional request to get access
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
projects, err := satellite.DB.Console().Projects().GetAll(ctx)
|
2022-01-10 17:36:15 +00:00
|
|
|
require.NoError(t, err)
|
2022-01-26 11:10:28 +00:00
|
|
|
require.Len(t, projects, 1)
|
2022-01-10 17:36:15 +00:00
|
|
|
|
2022-01-26 11:10:28 +00:00
|
|
|
rateLimit := 3
|
|
|
|
projects[0].RateLimit = &rateLimit
|
|
|
|
|
|
|
|
err = satellite.DB.Console().Projects().Update(ctx, &projects[0])
|
2022-01-10 17:36:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-01-26 11:10:28 +00:00
|
|
|
var group1 errs2.Group
|
|
|
|
|
|
|
|
for i := 0; i <= rateLimit; i++ {
|
|
|
|
group1.Go(func() error {
|
|
|
|
return ul.CreateBucket(ctx, satellite, testrand.BucketName())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
group1Errs := group1.Wait()
|
|
|
|
require.Len(t, group1Errs, 1)
|
|
|
|
|
|
|
|
rateLimit = 1
|
|
|
|
projects[0].RateLimit = &rateLimit
|
|
|
|
|
|
|
|
err = satellite.DB.Console().Projects().Update(ctx, &projects[0])
|
2022-01-10 17:36:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-01-26 11:10:28 +00:00
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
var group2 errs2.Group
|
|
|
|
|
|
|
|
for i := 0; i <= rateLimit; i++ {
|
|
|
|
group2.Go(func() error {
|
|
|
|
return ul.CreateBucket(ctx, satellite, testrand.BucketName())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
group2Errs := group2.Wait()
|
|
|
|
require.Len(t, group2Errs, 1)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRateLimit_ExceededBurstLimit(t *testing.T) {
|
|
|
|
burstLimit := 2
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 1, UplinkCount: 1,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.Metainfo.RateLimiter.Rate = float64(burstLimit)
|
|
|
|
config.Metainfo.RateLimiter.CacheExpiration = 500 * time.Millisecond
|
2022-01-10 17:36:15 +00:00
|
|
|
},
|
2022-01-26 11:10:28 +00:00
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
ul := planet.Uplinks[0]
|
|
|
|
satellite := planet.Satellites[0]
|
|
|
|
|
|
|
|
// TODO find a way to reset limiter before test is executed, currently
|
|
|
|
// testplanet is doing one additional request to get access
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
var group errs2.Group
|
|
|
|
for i := 0; i <= burstLimit; i++ {
|
|
|
|
group.Go(func() error {
|
|
|
|
return ul.CreateBucket(ctx, satellite, testrand.BucketName())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
groupErrs := group.Wait()
|
|
|
|
require.Len(t, groupErrs, 1)
|
|
|
|
|
|
|
|
projects, err := satellite.DB.Console().Projects().GetAll(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, projects, 1)
|
|
|
|
|
|
|
|
zeroRateLimit := 0
|
|
|
|
err = satellite.DB.Console().Projects().UpdateBurstLimit(ctx, projects[0].ID, zeroRateLimit)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
var group2 errs2.Group
|
|
|
|
for i := 0; i <= burstLimit; i++ {
|
|
|
|
group2.Go(func() error {
|
|
|
|
return ul.CreateBucket(ctx, satellite, testrand.BucketName())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
group2Errs := group2.Wait()
|
|
|
|
require.Len(t, group2Errs, burstLimit+1)
|
|
|
|
|
2022-01-10 17:36:15 +00:00
|
|
|
})
|
|
|
|
}
|