Merge pull request #18574 from lucasrod16/18571-ensure-consistent-permissions-for-broken-WAL-files

Ensure consistent file permissions on broken WAL files
This commit is contained in:
Marek Siarkowicz 2024-09-17 09:54:59 +02:00 committed by GitHub
commit 2ed418c191
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 1 deletions

View File

@ -67,7 +67,7 @@ func Repair(lg *zap.Logger, dirpath string) bool {
case errors.Is(err, io.ErrUnexpectedEOF): case errors.Is(err, io.ErrUnexpectedEOF):
brokenName := f.Name() + ".broken" 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 { if bferr != nil {
lg.Warn("failed to create backup file", zap.String("path", brokenName), zap.Error(bferr)) lg.Warn("failed to create backup file", zap.String("path", brokenName), zap.Error(bferr))
return false return false

View File

@ -18,12 +18,14 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"path/filepath"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest" "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/etcd/server/v3/storage/wal/walpb"
"go.etcd.io/raft/v3/raftpb" "go.etcd.io/raft/v3/raftpb"
) )
@ -77,6 +79,14 @@ func testRepair(t *testing.T, ents [][]raftpb.Entry, corrupt corruptFunc, expect
// repair the wal // repair the wal
require.True(t, Repair(lg, p), "'Repair' returned 'false', want 'true'") 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 // read it back
w, err = Open(lg, p, walpb.Snapshot{}) w, err = Open(lg, p, walpb.Snapshot{})
require.NoError(t, err) require.NoError(t, err)