Skip to content

Commit

Permalink
Fix keyring file missing after Vault restart (#15946)
Browse files Browse the repository at this point in the history
  • Loading branch information
shujun10086 authored and raskchanky committed Jun 21, 2022
1 parent e63837e commit 45b0234
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
3 changes: 3 additions & 0 deletions changelog/15946.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
core/seal: Fix possible keyring truncation when using the file backend.
```
11 changes: 8 additions & 3 deletions sdk/physical/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,9 @@ 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(
fullPath,
tempPath,
os.O_CREATE|os.O_TRUNC|os.O_WRONLY,
0o600)
if err != nil {
Expand All @@ -262,6 +263,10 @@ func (b *FileBackend) PutInternal(ctx context.Context, entry *physical.Entry) er
})
f.Close()
if encErr == nil {
err = os.Rename(tempPath, fullPath)
if err != nil {
return err
}
return nil
}

Expand All @@ -270,15 +275,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(fullPath)
fi, err := os.Stat(tempPath)
if err != nil {
return encErr
}
if fi == nil {
return encErr
}
if fi.Size() == 0 {
os.Remove(fullPath)
os.Remove(tempPath)
}
return encErr
}
Expand Down

0 comments on commit 45b0234

Please sign in to comment.