adding expiration date check on satellite's CreateSegment (#2476)

* adding expiration check on satellite createsegment
This commit is contained in:
aligeti 2019-07-08 14:10:16 -04:00 committed by GitHub
parent dcf8e2936b
commit a554752ce0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 8 deletions

View File

@ -10,6 +10,7 @@ import (
"strconv"
"time"
"github.com/golang/protobuf/ptypes"
"github.com/skyrings/skyring-common/tools/uuid"
"github.com/zeebo/errs"
"go.uber.org/zap"
@ -128,6 +129,17 @@ func (endpoint *Endpoint) SegmentInfoOld(ctx context.Context, req *pb.SegmentInf
func (endpoint *Endpoint) CreateSegmentOld(ctx context.Context, req *pb.SegmentWriteRequestOld) (resp *pb.SegmentWriteResponseOld, err error) {
defer mon.Task()(&ctx)(&err)
if req.Expiration != nil {
exp, err := ptypes.Timestamp(req.Expiration)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, err.Error())
}
if !exp.After(time.Now()) {
return nil, errs.New("Invalid expiration time")
}
}
keyInfo, err := endpoint.validateAuth(ctx, macaroon.Action{
Op: macaroon.ActionWrite,
Bucket: req.Bucket,

View File

@ -56,7 +56,7 @@ func TestInvalidAPIKey(t *testing.T) {
require.NoError(t, err)
defer ctx.Check(client.Close)
_, _, err = client.CreateSegment(ctx, "hello", "world", 1, &pb.RedundancyScheme{}, 123, time.Now())
_, _, err = client.CreateSegment(ctx, "hello", "world", 1, &pb.RedundancyScheme{}, 123, time.Now().Add(time.Hour))
assertUnauthenticated(t, err, false)
_, err = client.CommitSegment(ctx, "testbucket", "testpath", 0, &pb.Pointer{}, nil)
@ -170,7 +170,7 @@ func TestRestrictedAPIKey(t *testing.T) {
require.NoError(t, err)
defer ctx.Check(client.Close)
_, _, err = client.CreateSegment(ctx, "testbucket", "testpath", 1, &pb.RedundancyScheme{}, 123, time.Now())
_, _, err = client.CreateSegment(ctx, "testbucket", "testpath", 1, &pb.RedundancyScheme{}, 123, time.Now().Add(time.Hour))
assertUnauthenticated(t, err, test.CreateSegmentAllowed)
_, err = client.CommitSegment(ctx, "testbucket", "testpath", 0, &pb.Pointer{}, nil)
@ -310,7 +310,7 @@ func TestCommitSegment(t *testing.T) {
Total: 4,
ErasureShareSize: 256,
}
expirationDate := time.Now()
expirationDate := time.Now().Add(time.Hour)
addresedLimits, rootPieceID, err := metainfo.CreateSegment(ctx, "bucket", "path", -1, redundancy, 1000, expirationDate)
require.NoError(t, err)
@ -444,7 +444,7 @@ func TestCreateSegment(t *testing.T) {
fail: false,
},
} {
_, _, err := metainfo.CreateSegment(ctx, "bucket", "path", -1, r.rs, 1000, time.Now())
_, _, err := metainfo.CreateSegment(ctx, "bucket", "path", -1, r.rs, 1000, time.Now().Add(time.Hour))
if r.fail {
require.Error(t, err)
} else {
@ -454,6 +454,49 @@ func TestCreateSegment(t *testing.T) {
})
}
func TestExpirationTimeSegment(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 6, UplinkCount: 1,
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
apiKey := planet.Uplinks[0].APIKey[planet.Satellites[0].ID()]
metainfo, err := planet.Uplinks[0].DialMetainfo(ctx, planet.Satellites[0], apiKey)
require.NoError(t, err)
defer ctx.Check(metainfo.Close)
pointer := createTestPointer(t)
for _, r := range []struct {
expirationDate time.Time
errFlag bool
}{
{ // expiration time not set
time.Time{},
false,
},
{ // 10 days into future
time.Now().AddDate(0, 0, 10),
false,
},
{ // current time
time.Now(),
true,
},
{ // 10 days into past
time.Now().AddDate(0, 0, -10),
true,
},
} {
_, _, err := metainfo.CreateSegment(ctx, "my-bucket-name", "file/path", -1, pointer.Remote.Redundancy, memory.MiB.Int64(), r.expirationDate)
if err != nil {
assert.True(t, r.errFlag)
} else {
assert.False(t, r.errFlag)
}
}
})
}
func TestDoubleCommitSegment(t *testing.T) {
testplanet.Run(t, testplanet.Config{
SatelliteCount: 1, StorageNodeCount: 6, UplinkCount: 1,
@ -699,7 +742,9 @@ func createTestPointer(t *testing.T) *pb.Pointer {
require.NoError(t, err)
segmentSize := 4 * memory.KiB.Int64()
pieceSize := eestream.CalcPieceSize(segmentSize, redundancy)
timestamp := time.Now()
timestamp := time.Now().Add(time.Hour)
expiration, err := ptypes.TimestampProto(timestamp)
require.NoError(t, err)
pointer := &pb.Pointer{
Type: pb.Pointer_REMOTE,
SegmentSize: segmentSize,
@ -722,7 +767,7 @@ func createTestPointer(t *testing.T) *pb.Pointer {
},
},
},
ExpirationDate: ptypes.TimestampNow(),
ExpirationDate: expiration,
}
return pointer
}
@ -755,7 +800,7 @@ func TestBucketNameValidation(t *testing.T) {
"testbucket-63-0123456789012345678901234567890123456789012345abc",
}
for _, name := range validNames {
_, _, err = metainfo.CreateSegment(ctx, name, "", -1, rs, 1, time.Now())
_, _, err = metainfo.CreateSegment(ctx, name, "", -1, rs, 1, time.Now().Add(time.Hour))
require.NoError(t, err, "bucket name: %v", name)
}
@ -769,7 +814,7 @@ func TestBucketNameValidation(t *testing.T) {
"testbucket-64-0123456789012345678901234567890123456789012345abcd",
}
for _, name := range invalidNames {
_, _, err = metainfo.CreateSegment(ctx, name, "", -1, rs, 1, time.Now())
_, _, err = metainfo.CreateSegment(ctx, name, "", -1, rs, 1, time.Now().Add(time.Hour))
require.Error(t, err, "bucket name: %v", name)
}
})