Repository navigation
fix(helper): serialize keystore to memory before writing the file - #520
Conversation
maxlambrecht
left a comment
There was a problem hiding this comment.
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.
1b9d726 to
1087b47
Compare
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>
|
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>
1087b47 to
65e2808
Compare
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>
|
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. |
Relates to #490
flush()used to open the keystore withFiles.newOutputStream, which truncates it beforestoreruns, 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.