util: Fix AdvancedTlsX509KeyManager reloading credentials on every refresh - #13033
Open
TimurRakhmatullin86 wants to merge 1 commit into
Open
Conversation
…fresh The UpdateResult constructor takes (success, certTime, keyTime), but readAndUpdate constructed it as (success, newKeyTime, newCertTime), so the returned certTime/keyTime were swapped. LoadFilePathExecution then stored each into the oppositely named field, so on the next refresh readAndUpdate compared the key file's mtime against the cert file's last-seen mtime and vice versa. With two files of different mtimes (the normal case) that cross-comparison is always true, so the scheduled reloader re-read the key and certificates and rotated the alias on every refresh interval even when nothing changed. That breaks the method's documented 'only update when both files changed' invariant and continuously invalidates the per-alias key-material cache that the alias rotation was introduced to enable. Pass the times in the order the constructor expects. Add a test that advances a FakeClock over several refresh intervals with the files untouched and asserts the alias is stable; it fails before this change (the alias keeps rotating). Signed-off-by: Timur Rakhmatullin <174210871+TimurRakhmatullin86@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
AdvancedTlsX509KeyManager's scheduled credential reloader reloads the key and certificates and rotates the TLS alias on every refresh interval, even when neither file has changed.UpdateResult's constructor takes(boolean success, long certTime, long keyTime), butreadAndUpdatebuilds it as:So
result.certTimeactually holds the key file's mtime andresult.keyTimethe cert file's mtime.LoadFilePathExecution.run()then stores each into the oppositely named field (currentCertTime = result.certTime,currentKeyTime = result.keyTime), so the persistedcurrentKeyTime/currentCertTimeare crossed. On the next tick,readAndUpdatecompares the key file's current mtime against the cert file's last-seen mtime and vice versa:With two files whose mtimes differ — the normal case for a cert and a key written at different instants —
K != Cis always true, so it reloads every interval.Impact
key-<N>alias on every interval, continuously invalidating the per-alias encoded-key-material cache that the alias rotation (util: update AdvancedTlsX509KeyManager to support key alias for reloaded cert #12686) was introduced to enable — the opposite of the intended optimization.Introduced by #11385, which reordered the
UpdateResultconstructor parameters but left these two call sites unchanged.Fix
Pass the times in the order the constructor expects (
certTime, keyTime).Test
scheduledReload_doesNotReloadWhenFilesAreUnchangedgives the cert and key files distinct mtimes, schedules the reloader on aFakeClock, lets one refresh cycle run, then advances several more intervals with the files untouched and asserts the alias is unchanged. It fails before this change (expected:<key-2> but was:<key-3>— the alias keeps rotating) and passes after.