Return an error when trying to store a too-large key with Raft (#13282)

This commit is contained in:
Nick Cabatoff 2021-11-25 14:07:03 -05:00 committed by GitHub
parent 8b6477a0f0
commit f85908e1df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 0 deletions

3
changelog/13282.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
storage/raft: Fix a panic when trying to write a key > 32KB
```

View File

@ -1288,6 +1288,9 @@ func (b *RaftBackend) Get(ctx context.Context, path string) (*physical.Entry, er
// or if the call to applyLog fails.
func (b *RaftBackend) Put(ctx context.Context, entry *physical.Entry) error {
defer metrics.MeasureSince([]string{"raft-storage", "put"}, time.Now())
if len(entry.Key) > bolt.MaxKeySize {
return fmt.Errorf("%s, max key size for integrated storage is %d", physical.ErrKeyTooLarge, bolt.MaxKeySize)
}
if err := ctx.Err(); err != nil {
return err

View File

@ -15,6 +15,8 @@ import (
"testing"
"time"
"github.com/hashicorp/go-secure-stdlib/base62"
"github.com/go-test/deep"
"github.com/golang/protobuf/proto"
hclog "github.com/hashicorp/go-hclog"
@ -224,6 +226,34 @@ func TestRaft_Backend(t *testing.T) {
physical.ExerciseBackend(t, b)
}
func TestRaft_Backend_LargeKey(t *testing.T) {
b, dir := getRaft(t, true, true)
defer os.RemoveAll(dir)
key, err := base62.Random(bolt.MaxKeySize + 1)
if err != nil {
t.Fatal(err)
}
entry := &physical.Entry{Key: key, Value: []byte(key)}
err = b.Put(context.Background(), entry)
if err == nil {
t.Fatal("expected error for put entry")
}
if !strings.Contains(err.Error(), physical.ErrKeyTooLarge) {
t.Fatalf("expected %q, got %v", physical.ErrKeyTooLarge, err)
}
out, err := b.Get(context.Background(), entry.Key)
if err != nil {
t.Fatalf("unexpected error after failed put: %v", err)
}
if out != nil {
t.Fatal("expected response entry to be nil after a failed put")
}
}
func TestRaft_Backend_LargeValue(t *testing.T) {
b, dir := getRaft(t, true, true)
defer os.RemoveAll(dir)

View File

@ -21,6 +21,7 @@ const (
const (
ErrValueTooLarge = "put failed due to value being too large"
ErrKeyTooLarge = "put failed due to key being too large"
)
// Backend is the interface required for a physical