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

Backport of vault-23135 - fix modify storage keys ending with .temp causes overwr… into release/1.15.x #25445

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
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)
}
}
Loading