Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

snapshot: read meta.json correctly. #5193

Merged
merged 2 commits into from
Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions snapshot/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"
"hash"
"io"
"io/ioutil"
"time"

"github.com/hashicorp/raft"
Expand Down Expand Up @@ -193,8 +194,11 @@ func read(in io.Reader, metadata *raft.SnapshotMeta, snap io.Writer) error {

switch hdr.Name {
case "meta.json":
dec := json.NewDecoder(io.TeeReader(archive, metaHash))
if err := dec.Decode(&metadata); err != nil {
buf, err := ioutil.ReadAll(io.TeeReader(archive, metaHash))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worth commenting on why this is needed. The root cause seems to be something like:

// json.Decode will stop reading as soon as the initial JSON element (the object) is closed 
// (i.e. at the closing brace) which means the final newline, if any, is not read and so is not 
// delivered to `metaHash`. This means that the hash calculated is not the same as the
// one in SHA256SUMS which was calculated over the raw bytes in the file. By explicitly 
// reading the whole thing first we ensure that we calculate the hash over the exact
// same bytes regardless of whitespace.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I will add a comment!

if err != nil {
return fmt.Errorf("failed to read snapshot metadata: %v", err)
}
if err := json.Unmarshal(buf, &metadata); err != nil {
return fmt.Errorf("failed to decode snapshot metadata: %v", err)
}

Expand Down
19 changes: 19 additions & 0 deletions snapshot/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ func TestArchive(t *testing.T) {
}
}

func TestArchive_GoodData(t *testing.T) {
paths := []string{
"../test/snapshot/spaces-meta.tar",
}
for i, p := range paths {
f, err := os.Open(p)
if err != nil {
t.Fatalf("err: %v", err)
}
defer f.Close()

var metadata raft.SnapshotMeta
err = read(f, &metadata, ioutil.Discard)
if err != nil {
t.Fatalf("case %d: should've read the snapshot, but didn't: %v", i, err)
}
}
}

func TestArchive_BadData(t *testing.T) {
cases := []struct {
Name string
Expand Down
Binary file added test/snapshot/spaces-meta.tar
Binary file not shown.