-
Notifications
You must be signed in to change notification settings - Fork 46
Improve wrapped key loader #2659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
❌ Work item link check failed. Description does not contain AB#{ID}. Click here to Learn more. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR centralizes secret key caching across all AndroidWrappedKeyProvider instances using a static ConcurrentHashMap, and ensures the Android KeyStore is reloaded on each access rather than statically cached.
- Use a shared static
ConcurrentHashMap<String, SecretKey>inAndroidWrappedKeyProviderfor effective cross-instance caching - Remove per-instance
CachedDataand introducegetKeyFromCache/clearKeyFromCachehelpers - Update
AndroidKeyStoreUtilto always load a freshKeyStoreon demand - Adjust Android test suites and update
changelog.txtaccordingly
Comments suppressed due to low confidence (3)
common/src/main/java/com/microsoft/identity/common/crypto/AndroidWrappedKeyProvider.java:186
- [nitpick] The log message "Key not in cache is empty, loading key from storage" is confusing. Consider renaming it to something clearer like "Cache miss: loading key from storage".
Logger.info(methodTag, "Key not in cache is empty, loading key from storage");
common/src/main/java/com/microsoft/identity/common/crypto/AndroidWrappedKeyProvider.java:176
- Add a unit test to verify that cache entries are evicted when
AndroidKeyStoreUtil.canLoadKey(...)returns false or when the key file is missing, to ensure the invalidation logic is correct.
if (!sSkipKeyInvalidationCheck &&
common/src/main/java/com/microsoft/identity/common/crypto/AndroidWrappedKeyProvider.java:132
- [nitpick] Consider adding Javadoc to
getKeyFromCache()(andclearKeyFromCache()) to clarify their intended use and thread-safety guarantees.
/* package */ SecretKey getKeyFromCache() {
common/src/main/java/com/microsoft/identity/common/crypto/AndroidWrappedKeyProvider.java
Outdated
Show resolved
Hide resolved
common/src/main/java/com/microsoft/identity/common/crypto/AndroidWrappedKeyProvider.java
Show resolved
Hide resolved
…oidWrappedKeyProvider.java Co-authored-by: Sowmya Malayanur <[email protected]>
AndroidWrappedKeyLoader currently has code cache secret key in memory, but there are multiple instances of AndroidWrappedKeyLoader active (one per BrokerPlatformComponents). So, each object gets is own cache and there's no benefit of caching. Moreover, this makes keystore reads more frequent and concurrent.
Changes:
Update caching logic in AndroidWrappedKeyLoader by static ConcurrentHashMap so cache is shared across all ConcurrentHashMap. This fixes caching (effective) logic and reduces keystore unwrap and decrypt calls. A good side effect of this would be lesser number of failures due to Keystore unwrap calls that we have seen.
AndroidKeyStoreUtil changes:
Load keystore always instead of caching, as it is recommended to not cache it statically.