Skip to content

fix(helper): serialize keystore to memory before writing the file - #520

Merged
maxlambrecht merged 4 commits into
spiffe:mainfrom
cvas22:helper-atomic-store-write
Nov 18, 2026
Merged

maxlambrecht merged 4 commits into
spiffe:mainfrom
cvas22:helper-atomic-store-write

Conversation

@cvas22

@cvas22 cvas22 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Relates to #490

flush() used to open the keystore with Files.newOutputStream, which truncates it before store runs, so a store or encoding failure left an empty or partial file. Now it serializes the KeyStore to a byte array first and only then writes the bytes into the existing file in place. A serialization failure never touches the file, and since it's still a plain in-place write, symlinks, owner, group, mode and ACLs behave exactly like before.

This doesn't cover an I/O error or a crash halfway through the write, that part of #490 stays open. Tests cover a failed serialization leaving the keystore loadable and unchanged followed by a good update, symlinks including a dangling one, and 0600 surviving an update on POSIX.

@maxlambrecht maxlambrecht left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The helper now needs permission to create and rename files in the parent directory. Could you update the README’s permissions guidance? It currently says write access to the keystore files is sufficient, and that setup can stop working with this change.

@cvas22
cvas22 force-pushed the helper-atomic-store-write branch from 1b9d726 to 1087b47 Compare November 18, 2026 02:33
KeyStore.flush opened the destination with Files.newOutputStream, which
truncates it before KeyStore.store runs. A serialization or I/O failure
left an empty or partial keystore or truststore on disk.

Write to a temporary file next to the destination, sync it, and move it
over the old file, atomically where supported. On failure the temp file
is removed and the old file stays as it was. The temp file is created
with the existing file's POSIX permissions, owner and group are copied
where allowed, and symlinked paths replace the link target.

Refs spiffe#490

Signed-off-by: Srinivas Chippagiri <cvas22@gmail.com>
…missing

Signed-off-by: Srinivas Chippagiri <cvas22@gmail.com>
@cvas22

cvas22 commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

thx for digging into these. The ownership and dangling symlink ones I can fix: create the temp file 0600, set the mode only after the owner and group are in place, don't replace when they can't be matched, and resolve a dangling link to its target before writing. The ACL one is the hard part. On Linux and macOS the JDK doesn't expose ACLs at all (AclFileAttributeView is Windows only), so the rename can't tell whether it's about to drop one, and to be safe it would have to fall back every time.

So I'm thinking of flipping it around: serialize the store to memory first, then write the bytes into the existing file in place. A store or encoding failure then can't touch the file, owner, group, mode, ACLs and links stay exactly as they are today, and no directory permission is needed, so the README stays right. What's left is an I/O error or crash halfway through the write, which the rename would have covered. Would that work for you, or would you rather keep the rename with the in place write as the fallback?

…symlinks

The temporary file is now created owner-only and gets the destination's group, owner, ACL and permissions before the move. If they can't be reproduced, the destination is overwritten in place instead. A symlink to a file that doesn't exist yet is followed so the target is created and the link stays.

Signed-off-by: Srinivas Chippagiri <cvas22@gmail.com>
@cvas22
cvas22 force-pushed the helper-atomic-store-write branch from 1087b47 to 65e2808 Compare November 18, 2026 03:23
@maxlambrecht

Copy link
Copy Markdown
Member

thx for digging into these. The ownership and dangling symlink ones I can fix: create the temp file 0600, set the mode only after the owner and group are in place, don't replace when they can't be matched, and resolve a dangling link to its target before writing. The ACL one is the hard part. On Linux and macOS the JDK doesn't expose ACLs at all (AclFileAttributeView is Windows only), so the rename can't tell whether it's about to drop one, and to be safe it would have to fall back every time.

So I'm thinking of flipping it around: serialize the store to memory first, then write the bytes into the existing file in place. A store or encoding failure then can't touch the file, owner, group, mode, ACLs and links stay exactly as they are today, and no directory permission is needed, so the README stays right. What's left is an I/O error or crash halfway through the write, which the rename would have covered. Would that work for you, or would you rather keep the rename with the in place write as the fallback?

Yes, serializing to memory first would work for me. I’d prefer that to keeping the rename with an in-place fallback: it gives us a clear guarantee for serialization failures while preserving the existing filesystem behavior.

Let’s take that path and remove the temporary-file, fallback, and custom link-resolution code. I/O failures and crashes during the write would remain unresolved under #490. Please adjust the title, description, changelog, and flush() comment to reflect that narrower guarantee.

For tests, cover a serialization failure leaving the existing keystore loadable and unchanged, followed by a successful update. Keep the existing and dangling symlink coverage, and check that 0600 permissions survive an update.

Drop the temporary file and rename. flush() now serializes the KeyStore
to a byte array first and then writes the bytes into the existing file,
so a store or encoding failure never touches the file, and links, owner,
group, mode and ACLs stay as they are. An I/O error or crash halfway
through the write can still leave a partial file.

Signed-off-by: Srinivas Chippagiri <cvas22@gmail.com>
@cvas22 cvas22 changed the title fix(helper): write keystore files atomically fix(helper): serialize keystore to memory before writing the file Nov 18, 2026
@cvas22

cvas22 commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Switched to serializing in memory and writing in place in 47c3c81, the temp file, fallback and link resolution code are gone and the title, description and changelog now say it only covers serialization failures. thx for the call on this one.

@maxlambrecht maxlambrecht left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@maxlambrecht
maxlambrecht merged commit 1f1b000 into spiffe:main Nov 18, 2026
8 checks passed
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.

2 participants