Skip to content

fix(helper): adopt keystore updates only after the write succeeds - #525

Merged
maxlambrecht merged 2 commits into
spiffe:mainfrom
cvas22:helper-candidate-truststore
Nov 24, 2026
Merged

maxlambrecht merged 2 commits into
spiffe:mainfrom
cvas22:helper-candidate-truststore

Conversation

@cvas22

@cvas22 cvas22 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Part of #490

#492 already stages the trust bundle replacement in a copy and adopts it after the flush, but storePrivateKeyEntry and storeAuthorityEntry still changed the live KeyStore first. If the write failed, the change stayed in memory and the next successful write persisted it. An entry that can't be serialized was worse, it stayed in the live store and made every later write fail.

I moved all three methods onto one update path: copy the live store, apply the change to the copy, flush it, and only then swap it in. Files are still written in place like before, so the temp file and rename question from #520 stays out of this, and an I/O error or crash mid-write is still open under #490.

Tests run on JKS and PKCS12. They break the write by swapping the file for a directory, or break serialization with a cert that can't be encoded, then check the live store kept its entries, the other trust domains and the private key are still there, and the next update goes through. Also updated the README note and the changelog.

storePrivateKeyEntry and storeAuthorityEntry changed the live in-memory
KeyStore before flushing it. If the flush failed, the change stayed in
memory and the next successful write persisted it, and an entry that
couldn't be serialized made every later write fail.

Route every update through one path that copies the live store, applies
the change to the copy, writes it and only then adopts it, the same way
replaceAuthorityEntries already did. Files are still written in place.

Add JKS and PKCS12 tests that inject write and serialization failures
and check that the live store, other trust domains and the private key
survive and that the next update works.

Signed-off-by: Srinivas Chippagiri <cvas22@gmail.com>
Two overlapping updates could write one candidate to the file and keep the other as the live store. Hold the instance lock for the whole copy, write and assignment.

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

@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 a10159f into spiffe:main Nov 24, 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