-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add OBO Credential && Token Exchange Support #23866
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
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
da3a5b7
update
g2vinay a12a83e
update
g2vinay 9b8852e
update
g2vinay 2c3f2bf
update
g2vinay 00ea675
update
g2vinay e1a9a8a
Merge remote-tracking branch 'upstream/main' into add-obo-credential-…
g2vinay 720392c
update
g2vinay 7c7879b
update
g2vinay 75856c2
Merge remote-tracking branch 'upstream/main' into add-managed-identit…
g2vinay 87d1cb2
Merge remote-tracking branch 'upstream/main' into add-obo-credential-…
g2vinay a01c6a8
Merge branch 'add-managed-identity-aks-k8-token-support' into add-obo…
g2vinay 3506fb3
updates
g2vinay 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
31 changes: 31 additions & 0 deletions
31
sdk/identity/azure-identity/src/main/java/com/azure/identity/ClientAssertionCredential.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,31 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.identity; | ||
|
|
||
| import com.azure.core.credential.AccessToken; | ||
| import com.azure.core.credential.TokenRequestContext; | ||
| import com.azure.identity.implementation.IdentityClient; | ||
|
|
||
| import reactor.core.publisher.Mono; | ||
|
|
||
| /** | ||
| * Authenticates a service principal with AAD using a client assertion. | ||
| */ | ||
| class ClientAssertionCredential extends ManagedIdentityServiceCredential { | ||
|
|
||
| /** | ||
| * Creates an instance of ClientAssertionCredential. | ||
| * | ||
| * @param clientId the client id of user assigned or system assigned identity. | ||
| * @param identityClient the identity client to acquire a token with. | ||
| */ | ||
| ClientAssertionCredential(String clientId, IdentityClient identityClient) { | ||
| super(clientId, identityClient, "AZURE AKS TOKEN EXCHANGE"); | ||
| } | ||
|
|
||
| @Override | ||
| public Mono<AccessToken> authenticate(TokenRequestContext request) { | ||
| return identityClient.authenticatewithExchangeToken(request); | ||
| } | ||
| } |
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
58 changes: 58 additions & 0 deletions
58
sdk/identity/azure-identity/src/main/java/com/azure/identity/OnBehalfOfCredential.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,58 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.identity; | ||
|
|
||
| import com.azure.core.credential.AccessToken; | ||
| import com.azure.core.credential.TokenCredential; | ||
| import com.azure.core.credential.TokenRequestContext; | ||
| import com.azure.core.util.logging.ClientLogger; | ||
| import com.azure.identity.implementation.IdentityClient; | ||
| import com.azure.identity.implementation.IdentityClientBuilder; | ||
| import com.azure.identity.implementation.IdentityClientOptions; | ||
| import com.azure.identity.implementation.util.LoggingUtil; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| import java.time.Duration; | ||
|
|
||
| /** | ||
| * An AAD credential that acquires a token with a client secret and user assertion for an AAD application | ||
| * on behalf of a user principal. | ||
| */ | ||
| public class OnBehalfOfCredential implements TokenCredential { | ||
| private final IdentityClient identityClient; | ||
| private final ClientLogger logger = new ClientLogger(OnBehalfOfCredential.class); | ||
|
|
||
|
|
||
| /** | ||
| * Creates OnBehalfOfCredential with the specified AAD application details and client options. | ||
| * | ||
| * @param tenantId the tenant ID of the application | ||
| * @param clientId the client ID of the application | ||
| * @param clientSecret the secret value of the AAD application. | ||
| * @param certificatePath the PEM file or PFX file containing the certificate | ||
| * @param certificatePassword the password protecting the PFX file | ||
| * @param identityClientOptions the options for configuring the identity client | ||
| */ | ||
| public OnBehalfOfCredential(String clientId, String tenantId, String clientSecret, String certificatePath, | ||
| String certificatePassword, IdentityClientOptions identityClientOptions) { | ||
| this.identityClient = new IdentityClientBuilder() | ||
| .tenantId(tenantId) | ||
| .clientId(clientId) | ||
| .clientSecret(clientSecret) | ||
| .certificatePath(certificatePath) | ||
| .certificatePassword(certificatePassword) | ||
| .identityClientOptions(identityClientOptions) | ||
| .confidentialClientCacheTimeout(Duration.ofMinutes(5)) | ||
| .build(); | ||
| } | ||
|
|
||
| @Override | ||
| public Mono<AccessToken> getToken(TokenRequestContext request) { | ||
| return Mono.deferContextual(ctx -> identityClient.authenticateWithConfidentialClientCache(request) | ||
| .onErrorResume(t -> Mono.empty()) | ||
| .switchIfEmpty(Mono.defer(() -> identityClient.authenticateWithOBO(request))) | ||
| .doOnNext(token -> LoggingUtil.logTokenSuccess(logger, request)) | ||
| .doOnError(error -> LoggingUtil.logTokenError(logger, request, error))); | ||
| } | ||
| } |
127 changes: 127 additions & 0 deletions
127
...identity/azure-identity/src/main/java/com/azure/identity/OnBehalfOfCredentialBuilder.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,127 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package com.azure.identity; | ||
|
|
||
| import com.azure.core.util.logging.ClientLogger; | ||
| import com.azure.identity.implementation.util.ValidationUtil; | ||
|
|
||
| import java.util.HashMap; | ||
|
|
||
| /** | ||
| * Fluent credential builder for instantiating a {@link OnBehalfOfCredential}. | ||
| * | ||
| * @see OnBehalfOfCredential | ||
| */ | ||
| public class OnBehalfOfCredentialBuilder extends AadCredentialBuilderBase<OnBehalfOfCredentialBuilder> { | ||
| private String clientSecret; | ||
| private String clientCertificatePath; | ||
| private String clientCertificatePassword; | ||
| private final ClientLogger logger = new ClientLogger(OnBehalfOfCredentialBuilder.class); | ||
|
|
||
| /** | ||
| * Sets the client secret for the authentication. | ||
| * @param clientSecret the secret value of the AAD application. | ||
| * @return An updated instance of this builder. | ||
| */ | ||
| public OnBehalfOfCredentialBuilder clientSecret(String clientSecret) { | ||
| this.clientSecret = clientSecret; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Configures the persistent shared token cache options and enables the persistent token cache which is disabled | ||
| * by default. If configured, the credential will store tokens in a cache persisted to the machine, protected to | ||
| * the current user, which can be shared by other credentials and processes. | ||
| * | ||
| * @param tokenCachePersistenceOptions the token cache configuration options | ||
| * @return An updated instance of this builder with the token cache options configured. | ||
| */ | ||
| public OnBehalfOfCredentialBuilder tokenCachePersistenceOptions(TokenCachePersistenceOptions | ||
| tokenCachePersistenceOptions) { | ||
| this.identityClientOptions.setTokenCacheOptions(tokenCachePersistenceOptions); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the path and password of the PFX certificate for authenticating to AAD. | ||
| * | ||
| * @param certificatePath the password protected PFX file containing the certificate | ||
| * @param clientCertificatePassword the password protecting the PFX file | ||
| * @return An updated instance of this builder. | ||
| */ | ||
| public OnBehalfOfCredentialBuilder pfxCertificate(String certificatePath, | ||
| String clientCertificatePassword) { | ||
| this.clientCertificatePath = certificatePath; | ||
| this.clientCertificatePassword = clientCertificatePassword; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Specifies if the x5c claim (public key of the certificate) should be sent as part of the authentication request | ||
| * and enable subject name / issuer based authentication. The default value is false. | ||
| * | ||
| * @param sendCertificateChain the flag to indicate if certificate chain should be sent as part of authentication | ||
| * request. | ||
| * @return An updated instance of this builder. | ||
| */ | ||
| public OnBehalfOfCredentialBuilder sendCertificateChain(boolean sendCertificateChain) { | ||
| this.identityClientOptions.setIncludeX5c(sendCertificateChain); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Specifies either the specific regional authority, or use {@link RegionalAuthority#AUTO_DISCOVER_REGION} to | ||
| * attempt to auto-detect the region. If unset, a non-regional authority will be used. This argument should be used | ||
| * only by applications deployed to Azure VMs. | ||
| * | ||
| * @param regionalAuthority the regional authority | ||
| * @return An updated instance of this builder with the regional authority configured. | ||
| */ | ||
| public OnBehalfOfCredentialBuilder regionalAuthority(RegionalAuthority regionalAuthority) { | ||
| this.identityClientOptions.setRegionalAuthority(regionalAuthority); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Configure the User Assertion Scope to be used for OnBehalfOf Authentication request. | ||
| * | ||
| * @param userAssertion the user assertion access token to be used for On behalf Of authentication flow | ||
| * @return An updated instance of this builder with the user assertion scope configured. | ||
| */ | ||
| public OnBehalfOfCredentialBuilder userAssertion(String userAssertion) { | ||
| this.identityClientOptions.userAssertion(userAssertion); | ||
| return this; | ||
| } | ||
|
g2vinay marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Creates a new {@link OnBehalfOfCredential} with the current configurations. | ||
| * | ||
| * @return a {@link OnBehalfOfCredential} with the current configurations. | ||
| * @throws IllegalArgumentException if eiter both the client secret and certificate are configured or none of them | ||
| * are configured. | ||
| */ | ||
| public OnBehalfOfCredential build() { | ||
| ValidationUtil.validate(getClass().getSimpleName(), new HashMap<String, Object>() { | ||
| { | ||
| put("clientId", clientId); | ||
| put("tenantId", tenantId); | ||
| } | ||
| }); | ||
|
|
||
| if (clientSecret == null && clientCertificatePath == null) { | ||
| throw logger.logExceptionAsWarning(new IllegalArgumentException("Atleast client secret or certificate " | ||
| + "path should provided in OnBhealfOfCredentialBuilder. Only one of them should " | ||
| + "be provided.")); | ||
| } | ||
|
|
||
| if (clientCertificatePath != null && clientSecret != null) { | ||
| throw logger.logExceptionAsWarning(new IllegalArgumentException("Both client secret and certificate " | ||
| + "path are provided in OnBhealfCredentialBuilder. Only one of them should " | ||
| + "be provided.")); | ||
| } | ||
|
|
||
| return new OnBehalfOfCredential(clientId, tenantId, clientSecret, clientCertificatePath, | ||
| clientCertificatePassword, identityClientOptions); | ||
| } | ||
| } | ||
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.