Skip to content

Commit

Permalink
backport of commit a4be0c9
Browse files Browse the repository at this point in the history
  • Loading branch information
hghaf099 committed Feb 15, 2024
1 parent a5a0146 commit b7b365c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
3 changes: 3 additions & 0 deletions changelog/25395.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
storage/file: Fixing spuriously deleting storage keys ending with .temp
```
20 changes: 12 additions & 8 deletions sdk/physical/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,17 +245,21 @@ func (b *FileBackend) PutInternal(ctx context.Context, entry *physical.Entry) er

// JSON encode the entry and write it
fullPath := filepath.Join(path, key)
tempPath := fullPath + ".temp"
f, err := os.OpenFile(
tempPath,
os.O_CREATE|os.O_TRUNC|os.O_WRONLY,
0o600)
f, err := os.CreateTemp(path, key)
if err != nil {
if f != nil {
f.Close()
}
return err
}

if err = os.Chmod(f.Name(), 0o600); err != nil {
if f != nil {
f.Close()
}
return err
}

if f == nil {
return errors.New("could not successfully get a file handle")
}
Expand All @@ -266,7 +270,7 @@ func (b *FileBackend) PutInternal(ctx context.Context, entry *physical.Entry) er
})
f.Close()
if encErr == nil {
err = os.Rename(tempPath, fullPath)
err = os.Rename(f.Name(), fullPath)
if err != nil {
return err
}
Expand All @@ -278,15 +282,15 @@ func (b *FileBackend) PutInternal(ctx context.Context, entry *physical.Entry) er
// See if we ended up with a zero-byte file and if so delete it, might be a
// case of disk being full but the file info is in metadata that is
// reserved.
fi, err := os.Stat(tempPath)
fi, err := os.Stat(f.Name())
if err != nil {
return encErr
}
if fi == nil {
return encErr
}
if fi.Size() == 0 {
os.Remove(tempPath)
os.Remove(f.Name())
}
return encErr
}
Expand Down
51 changes: 51 additions & 0 deletions sdk/physical/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,54 @@ func TestFileBackend(t *testing.T) {

physical.ExerciseBackend_ListPrefix(t, b)
}

func TestFileBackendCreateTempKey(t *testing.T) {
dir := t.TempDir()

logger := logging.NewVaultLogger(log.Debug)

b, err := NewFileBackend(map[string]string{
"path": dir,
}, logger)
if err != nil {
t.Fatalf("err: %s", err)
}
temp := &physical.Entry{Key: "example.temp", Value: []byte("tempfoo")}
err = b.Put(context.Background(), temp)
if err != nil {
t.Fatalf("err: %v", err)
}

nonTemp := &physical.Entry{Key: "example", Value: []byte("foobar")}
err = b.Put(context.Background(), nonTemp)
if err != nil {
t.Fatalf("err: %v", err)
}

vals, err := b.List(context.Background(), "")
if err != nil {
t.Fatal(err)
}
if len(vals) != 2 || vals[0] == vals[1] {
t.Fatalf("bad: %v", vals)
}
for _, val := range vals {
if val != "example.temp" && val != "example" {
t.Fatalf("bad val: %v", val)
}
}
out, err := b.Get(context.Background(), "example")
if err != nil {
t.Fatalf("err: %v", err)
}
if !reflect.DeepEqual(out, nonTemp) {
t.Fatalf("bad: %v expected: %v", out, nonTemp)
}
out, err = b.Get(context.Background(), "example.temp")
if err != nil {
t.Fatalf("err: %v", err)
}
if !reflect.DeepEqual(out, temp) {
t.Fatalf("bad: %v expected: %v", out, temp)
}
}

0 comments on commit b7b365c

Please sign in to comment.