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

Fix keyring file missing after Vault restart #15946

Merged
merged 4 commits into from Jun 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/15946.txt
@@ -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
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