-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Support token credential cache for azure-identity-extension #43659
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
Merged
moarychan
merged 16 commits into
Azure:main
from
moarychan:feature/support-cache-for-identity-extension
Jan 8, 2025
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
497dfb3
Support cache for identity extension
moarychan 7bc1390
test
moarychan 2639fbb
Improve
moarychan 1d6853b
Improve
moarychan f9ee905
Resolve comments
moarychan f588eac
Resolve comments
moarychan 44b40b1
refactor
saragluna 3b83e04
Resolve comments
moarychan dcfcdb9
Fix
moarychan c440e00
Fix pipeline failures
moarychan 49e61f0
Improve test
moarychan e3535d8
refactor
saragluna a7d7c67
spotless apply
moarychan 0112840
Resolve comments
moarychan 24c68f5
Resolve comments
moarychan 2e2bf95
spotless apply
moarychan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
84 changes: 84 additions & 0 deletions
84
...dentity/extensions/implementation/credential/provider/CachingTokenCredentialProvider.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.identity.extensions.implementation.credential.provider; | ||
|
|
||
| import com.azure.core.credential.TokenCredential; | ||
| import com.azure.core.util.logging.ClientLogger; | ||
| import com.azure.identity.extensions.implementation.credential.TokenCredentialProviderOptions; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Caching tokenCredentialProvider implementation that provides tokenCredential instance. | ||
| */ | ||
| public class CachingTokenCredentialProvider implements TokenCredentialProvider { | ||
|
|
||
| private static final ClientLogger LOGGER = new ClientLogger(CachingTokenCredentialProvider.class); | ||
|
|
||
| private static final ConcurrentHashMap<String, TokenCredential> CACHE = new ConcurrentHashMap<>(); | ||
|
|
||
| private final TokenCredentialProviderOptions defaultOptions; | ||
|
|
||
| private final TokenCredentialProvider delegate; | ||
|
|
||
| /** | ||
| * CachingTokenCredentialProvider constructor. | ||
| * @param defaultOptions the {@link TokenCredentialProviderOptions} for the delegate {@link TokenCredentialProvider} initialization. | ||
| * @param tokenCredentialProvider the delegate {@link TokenCredentialProvider}. | ||
| */ | ||
| public CachingTokenCredentialProvider(TokenCredentialProviderOptions defaultOptions, | ||
| TokenCredentialProvider tokenCredentialProvider) { | ||
| this.defaultOptions = defaultOptions; | ||
| this.delegate = tokenCredentialProvider; | ||
| } | ||
|
|
||
| @Override | ||
| public TokenCredential get() { | ||
| return getOrCreate(CACHE, this.defaultOptions, this.delegate, | ||
| tokenCredentialProvider -> tokenCredentialProvider.get()); | ||
| } | ||
|
|
||
| @Override | ||
| public TokenCredential get(TokenCredentialProviderOptions options) { | ||
| return getOrCreate(CACHE, options, this.delegate, | ||
| tokenCredentialProvider -> tokenCredentialProvider.get(options)); | ||
| } | ||
|
|
||
| private static TokenCredential getOrCreate(Map<String, TokenCredential> cache, | ||
| TokenCredentialProviderOptions options, TokenCredentialProvider delegate, | ||
| Function<TokenCredentialProvider, TokenCredential> fn) { | ||
| String tokenCredentialCacheKey = convertToTokenCredentialCacheKey(options); | ||
|
|
||
| if (cache.containsKey(tokenCredentialCacheKey)) { | ||
| LOGGER.verbose("Retrieving token credential from cache."); | ||
| } else { | ||
| LOGGER.verbose("Caching token credential."); | ||
| cache.put(tokenCredentialCacheKey, fn.apply(delegate)); | ||
| } | ||
|
|
||
| return cache.get(tokenCredentialCacheKey); | ||
| } | ||
|
|
||
| private static String convertToTokenCredentialCacheKey(TokenCredentialProviderOptions options) { | ||
| if (options == null) { | ||
| return CachingTokenCredentialProvider.class.getSimpleName(); | ||
| } | ||
|
|
||
| return Arrays | ||
| .stream(new String[] { | ||
| options.getTenantId(), | ||
| options.getClientId(), | ||
| options.getClientCertificatePath(), | ||
| options.getUsername(), | ||
| String.valueOf(options.isManagedIdentityEnabled()), | ||
| options.getTokenCredentialProviderClassName(), | ||
| options.getTokenCredentialBeanName() }) | ||
| .map(option -> option == null ? "" : option) | ||
| .collect(Collectors.joining(",")); | ||
| } | ||
| } |
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
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
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
112 changes: 112 additions & 0 deletions
112
...ity/extensions/implementation/credential/provider/CachingTokenCredentialProviderTest.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.identity.extensions.implementation.credential.provider; | ||
|
|
||
| import com.azure.core.credential.TokenCredential; | ||
| import com.azure.identity.DefaultAzureCredential; | ||
| import com.azure.identity.ManagedIdentityCredential; | ||
| import com.azure.identity.extensions.implementation.credential.TokenCredentialProviderOptions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| class CachingTokenCredentialProviderTest { | ||
|
|
||
| @Test | ||
| void returnCacheUsingDefaultAuthMethodViaDifferentProviderInstances() { | ||
| DefaultTokenCredentialProvider defaultTokenCredentialProvider1 = new DefaultTokenCredentialProvider(null); | ||
| CachingTokenCredentialProvider provider1 | ||
| = new CachingTokenCredentialProvider(null, defaultTokenCredentialProvider1); | ||
| TokenCredential tokenCredential1 = provider1.get(); | ||
|
|
||
| DefaultTokenCredentialProvider defaultTokenCredentialProvider2 = new DefaultTokenCredentialProvider(null); | ||
| CachingTokenCredentialProvider provider2 | ||
| = new CachingTokenCredentialProvider(null, defaultTokenCredentialProvider2); | ||
|
|
||
| TokenCredential tokenCredential2 = provider2.get(); | ||
| assertTrue(tokenCredential1 instanceof DefaultAzureCredential); | ||
| assertTrue(tokenCredential2 instanceof DefaultAzureCredential); | ||
| assertTrue(tokenCredential1 == tokenCredential2); | ||
| } | ||
|
|
||
| @Test | ||
| void returnCacheUsingSameAuthMethodViaDifferentProviderInstances() { | ||
| TokenCredentialProviderOptions customOptions = getSystemManagedIdentityCredentialProviderOptions(); | ||
|
|
||
| DefaultTokenCredentialProvider defaultTokenCredentialProvider1 | ||
| = new DefaultTokenCredentialProvider(customOptions); | ||
| CachingTokenCredentialProvider cachingProvider1 | ||
| = new CachingTokenCredentialProvider(customOptions, defaultTokenCredentialProvider1); | ||
| TokenCredential tokenCredential1 = cachingProvider1.get(); | ||
|
|
||
| TokenCredentialProviderOptions customOptions2 = getSystemManagedIdentityCredentialProviderOptions(); | ||
| DefaultTokenCredentialProvider defaultTokenCredentialProvider2 | ||
| = new DefaultTokenCredentialProvider(customOptions2); | ||
| CachingTokenCredentialProvider cachingProvider2 | ||
| = new CachingTokenCredentialProvider(customOptions2, defaultTokenCredentialProvider2); | ||
|
|
||
| TokenCredential tokenCredential2 = cachingProvider2.get(); | ||
| assertTrue(tokenCredential1 instanceof ManagedIdentityCredential); | ||
| assertTrue(tokenCredential2 instanceof ManagedIdentityCredential); | ||
| assertTrue(tokenCredential1 == tokenCredential2); | ||
| } | ||
|
|
||
| @Test | ||
| void returnCacheUsingSameAuthMethodAndInvokingDifferentGetMethods() { | ||
| TokenCredentialProviderOptions customOptions = getSystemManagedIdentityCredentialProviderOptions(); | ||
|
|
||
| DefaultTokenCredentialProvider defaultTokenCredentialProvider1 | ||
| = new DefaultTokenCredentialProvider(customOptions); | ||
| CachingTokenCredentialProvider cachingProvider1 | ||
| = new CachingTokenCredentialProvider(customOptions, defaultTokenCredentialProvider1); | ||
| TokenCredential tokenCredential1 = cachingProvider1.get(); | ||
|
|
||
| TokenCredentialProviderOptions customOptions2 = getSystemManagedIdentityCredentialProviderOptions(); | ||
| DefaultTokenCredentialProvider defaultTokenCredentialProvider2 | ||
| = new DefaultTokenCredentialProvider(customOptions2); | ||
| CachingTokenCredentialProvider cachingProvider2 | ||
| = new CachingTokenCredentialProvider(customOptions2, defaultTokenCredentialProvider2); | ||
|
|
||
| TokenCredential tokenCredential2 = cachingProvider2.get(customOptions2); | ||
| assertTrue(tokenCredential1 instanceof ManagedIdentityCredential); | ||
| assertTrue(tokenCredential2 instanceof ManagedIdentityCredential); | ||
| assertTrue(tokenCredential1 == tokenCredential2); | ||
| } | ||
|
|
||
| @Test | ||
| void returnDifferentCachesUsingDifferentAuthenticationMethods() { | ||
| TokenCredentialProviderOptions customOptions = getSystemManagedIdentityCredentialProviderOptions(); | ||
|
|
||
| DefaultTokenCredentialProvider defaultTokenCredentialProvider1 | ||
| = new DefaultTokenCredentialProvider(customOptions); | ||
| CachingTokenCredentialProvider cachingProvider1 | ||
| = new CachingTokenCredentialProvider(customOptions, defaultTokenCredentialProvider1); | ||
| TokenCredential tokenCredential1 = cachingProvider1.get(); | ||
|
|
||
| TokenCredentialProviderOptions customOptions2 | ||
| = getUserManagedIdentityCredentialProviderOptions("test-client-id"); | ||
| DefaultTokenCredentialProvider defaultTokenCredentialProvider2 | ||
| = new DefaultTokenCredentialProvider(customOptions2); | ||
| CachingTokenCredentialProvider cachingProvider2 | ||
| = new CachingTokenCredentialProvider(customOptions2, defaultTokenCredentialProvider2); | ||
|
|
||
| TokenCredential tokenCredential2 = cachingProvider2.get(customOptions2); | ||
| assertTrue(tokenCredential1 instanceof ManagedIdentityCredential); | ||
| assertTrue(tokenCredential2 instanceof ManagedIdentityCredential); | ||
| assertTrue(tokenCredential1 != tokenCredential2); | ||
| } | ||
|
|
||
| private static TokenCredentialProviderOptions getUserManagedIdentityCredentialProviderOptions(String clientId) { | ||
| TokenCredentialProviderOptions customOptions = new TokenCredentialProviderOptions(); | ||
| customOptions.setManagedIdentityEnabled(true); | ||
| customOptions.setClientId(clientId); | ||
| return customOptions; | ||
| } | ||
|
|
||
| private static TokenCredentialProviderOptions getSystemManagedIdentityCredentialProviderOptions() { | ||
| TokenCredentialProviderOptions customOptions = new TokenCredentialProviderOptions(); | ||
| customOptions.setManagedIdentityEnabled(true); | ||
| return customOptions; | ||
| } | ||
| } |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.