Skip to content

refactor: Add pkg/fileutil and move fstab writes onto it - #314

Draft
lauri-amd wants to merge 1 commit into
mainfrom
refactor-shared-atomic-write
Draft

refactor: Add pkg/fileutil and move fstab writes onto it#314
lauri-amd wants to merge 1 commit into
mainfrom
refactor-shared-atomic-write

Conversation

@lauri-amd

Copy link
Copy Markdown
Contributor

Replacing a file in place truncates it for the duration of the write, so an interrupt in that window leaves it empty. fstab.go had grown a private atomicWriteFile to avoid exactly that, and an in-flight fix for the ephemeral ssh key handling needs the same guarantee for authorized_keys, where an emptied file locks the operator out of the node.

Promote the helper into pkg/fileutil rather than keep a second copy of it, and add the one capability fstab never needed: WriteAtomicallyOwned sets uid and gid, because authorized_keys has to keep its owner. WriteAtomically is the mode-only form, and /etc/fstab and its backup now use it.

Two details changed in the move. The mode is applied after the write rather than before, so content is never briefly readable at a wider mode than intended, and the temporary file is removed on every error path instead of only those before the rename. The tests assert the guarantee deterministically instead of racing the writer: a reader that opened the file beforehand must still see the complete old content, and the target must be a different inode afterwards.

pkg/fileutil carries no build tag, because pkg/ssh is untagged and so cannot import the linux-only runtime package. The ssh key handling moves onto pkg/fileutil in a follow-up PR.

Replacing a file in place truncates it for the duration of the write, so an
interrupt in that window leaves it empty. fstab.go had grown a private
atomicWriteFile to avoid exactly that, and an in-flight fix for the ephemeral
ssh key handling needs the same guarantee for authorized_keys, where an
emptied file locks the operator out of the node.

Promote the helper into pkg/fileutil rather than keep a second copy of it, and
add the one capability fstab never needed: WriteAtomicallyOwned sets uid and
gid, because authorized_keys has to keep its owner. WriteAtomically is the
mode-only form, and /etc/fstab and its backup now use it.

Two details changed in the move. The mode is applied after the write rather
than before, so content is never briefly readable at a wider mode than
intended, and the temporary file is removed on every error path instead of
only those before the rename. The tests assert the guarantee deterministically
instead of racing the writer: a reader that opened the file beforehand must
still see the complete old content, and the target must be a different inode
afterwards.

pkg/fileutil carries no build tag, because pkg/ssh is untagged and so cannot
import the linux-only runtime package. The ssh key handling moves onto
pkg/fileutil in a follow-up PR.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant