mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-01 19:17:58 +00:00
vault-23135 - fix modify storage keys ending with .temp causes overwr… (#25395)
* vault-23135 - fix modify storage keys ending with .temp causes overwriting * CL * use t.TempDir
This commit is contained in:
3
changelog/25395.txt
Normal file
3
changelog/25395.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
```release-note:bug
|
||||||
|
storage/file: Fixing spuriously deleting storage keys ending with .temp
|
||||||
|
```
|
||||||
@@ -244,17 +244,21 @@ func (b *FileBackend) PutInternal(ctx context.Context, entry *physical.Entry) er
|
|||||||
|
|
||||||
// JSON encode the entry and write it
|
// JSON encode the entry and write it
|
||||||
fullPath := filepath.Join(path, key)
|
fullPath := filepath.Join(path, key)
|
||||||
tempPath := fullPath + ".temp"
|
f, err := os.CreateTemp(path, key)
|
||||||
f, err := os.OpenFile(
|
|
||||||
tempPath,
|
|
||||||
os.O_CREATE|os.O_TRUNC|os.O_WRONLY,
|
|
||||||
0o600)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if f != nil {
|
if f != nil {
|
||||||
f.Close()
|
f.Close()
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err = os.Chmod(f.Name(), 0o600); err != nil {
|
||||||
|
if f != nil {
|
||||||
|
f.Close()
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if f == nil {
|
if f == nil {
|
||||||
return errors.New("could not successfully get a file handle")
|
return errors.New("could not successfully get a file handle")
|
||||||
}
|
}
|
||||||
@@ -265,7 +269,7 @@ func (b *FileBackend) PutInternal(ctx context.Context, entry *physical.Entry) er
|
|||||||
})
|
})
|
||||||
f.Close()
|
f.Close()
|
||||||
if encErr == nil {
|
if encErr == nil {
|
||||||
err = os.Rename(tempPath, fullPath)
|
err = os.Rename(f.Name(), fullPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -277,7 +281,7 @@ 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
|
// 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
|
// case of disk being full but the file info is in metadata that is
|
||||||
// reserved.
|
// reserved.
|
||||||
fi, err := os.Stat(tempPath)
|
fi, err := os.Stat(f.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return encErr
|
return encErr
|
||||||
}
|
}
|
||||||
@@ -285,7 +289,7 @@ func (b *FileBackend) PutInternal(ctx context.Context, entry *physical.Entry) er
|
|||||||
return encErr
|
return encErr
|
||||||
}
|
}
|
||||||
if fi.Size() == 0 {
|
if fi.Size() == 0 {
|
||||||
os.Remove(tempPath)
|
os.Remove(f.Name())
|
||||||
}
|
}
|
||||||
return encErr
|
return encErr
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -240,3 +240,54 @@ func TestFileBackend(t *testing.T) {
|
|||||||
|
|
||||||
physical.ExerciseBackend_ListPrefix(t, b)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user