Skip to content

Commit

Permalink
Make saved snapshot readable by user only
Browse files Browse the repository at this point in the history
Fixes #9976
  • Loading branch information
Daniel Lipovetsky committed Aug 2, 2018
1 parent 93be31d commit 8ac1e51
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clientv3/snapshot/v3_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (s *v3Manager) Save(ctx context.Context, cfg clientv3.Config, dbPath string
defer os.RemoveAll(partpath)

var f *os.File
f, err = os.Create(partpath)
f, err = os.OpenFile(partpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fileutil.PrivateFileMode)
if err != nil {
return fmt.Errorf("could not open %s (%v)", partpath, err)
}
Expand Down
21 changes: 21 additions & 0 deletions clientv3/snapshot/v3_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"testing"
"time"

"github.com/coreos/etcd/pkg/fileutil"

"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/embed"
"github.com/coreos/etcd/pkg/testutil"
Expand Down Expand Up @@ -141,6 +143,25 @@ func TestSnapshotV3RestoreMulti(t *testing.T) {
}
}

// TestSnapshotFilePermissions ensures that the snapshot is saved with
// the correct file permissions.
func TestSnapshotFilePermissions(t *testing.T) {
expectedFileMode := os.FileMode(fileutil.PrivateFileMode)
kvs := []kv{{"foo1", "bar1"}, {"foo2", "bar2"}, {"foo3", "bar3"}}
dbPath := createSnapshotFile(t, kvs)
defer os.RemoveAll(dbPath)

dbInfo, err := os.Stat(dbPath)
if err != nil {
t.Fatalf("failed to get test snapshot file status: %v", err)
}
actualFileMode := dbInfo.Mode()

if expectedFileMode != actualFileMode {
t.Fatalf("expected test snapshot file mode %s, got %s:", expectedFileMode, actualFileMode)
}
}

type kv struct {
k, v string
}
Expand Down

0 comments on commit 8ac1e51

Please sign in to comment.