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

Ensure consistent file permissions on broken WAL files #18574

Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion server/storage/wal/repair.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func Repair(lg *zap.Logger, dirpath string) bool {

case errors.Is(err, io.ErrUnexpectedEOF):
brokenName := f.Name() + ".broken"
bf, bferr := os.Create(brokenName)
bf, bferr := os.OpenFile(brokenName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fileutil.PrivateFileMode)
if bferr != nil {
lg.Warn("failed to create backup file", zap.String("path", brokenName), zap.Error(bferr))
return false
Expand Down
10 changes: 10 additions & 0 deletions server/storage/wal/repair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"

"go.etcd.io/etcd/client/pkg/v3/fileutil"
"go.etcd.io/etcd/server/v3/storage/wal/walpb"
"go.etcd.io/raft/v3/raftpb"
)
Expand Down Expand Up @@ -77,6 +79,14 @@ func testRepair(t *testing.T, ents [][]raftpb.Entry, corrupt corruptFunc, expect
// repair the wal
require.True(t, Repair(lg, p), "'Repair' returned 'false', want 'true'")

// verify the broken wal has correct permissions
bf := filepath.Join(p, filepath.Base(w.tail().Name())+".broken")
fi, err := os.Stat(bf)
require.NoError(t, err)
expectedPerms := fmt.Sprintf("%o", os.FileMode(fileutil.PrivateFileMode))
actualPerms := fmt.Sprintf("%o", fi.Mode().Perm())
require.Equal(t, expectedPerms, actualPerms, "unexpected file permissions on .broken wal")

// read it back
w, err = Open(lg, p, walpb.Snapshot{})
require.NoError(t, err)
Expand Down
Loading