-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Client Assertion Credential (#26900)
- Loading branch information
Showing
11 changed files
with
180 additions
and
36 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
sdk/identity/azure-identity/src/main/java/com/azure/identity/AksExchangeTokenCredential.java
This file contains 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,38 @@ | ||
// 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.core.util.logging.ClientLogger; | ||
import com.azure.identity.implementation.IdentityClient; | ||
|
||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* Authenticates a service principal with AAD using a client assertion. | ||
*/ | ||
class AksExchangeTokenCredential extends ManagedIdentityServiceCredential { | ||
private final ClientLogger logger = new ClientLogger(AksExchangeTokenCredential.class); | ||
|
||
/** | ||
* Creates an instance of AksExchangeTokenCredential. | ||
* | ||
* @param clientId the client id of user assigned or system assigned identity. | ||
* @param identityClient the identity client to acquire a token with. | ||
*/ | ||
AksExchangeTokenCredential(String clientId, IdentityClient identityClient) { | ||
super(clientId, identityClient, "AZURE AKS TOKEN EXCHANGE"); | ||
} | ||
|
||
@Override | ||
public Mono<AccessToken> authenticate(TokenRequestContext request) { | ||
if (this.getClientId() == null) { | ||
return Mono.error(logger.logExceptionAsError(new IllegalStateException("The client id is not configured via" | ||
+ " 'AZURE_CLIENT_ID' environment variable or through the credential builder." | ||
+ " Please ensure client id is provided to authenticate via token exchange in AKS environment."))); | ||
} | ||
return identityClient.authenticatewithExchangeToken(request); | ||
} | ||
} |
This file contains 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
75 changes: 75 additions & 0 deletions
75
...ity/azure-identity/src/main/java/com/azure/identity/ClientAssertionCredentialBuilder.java
This file contains 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,75 @@ | ||
// 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.RegionalAuthority; | ||
import com.azure.identity.implementation.util.ValidationUtil; | ||
|
||
import java.util.HashMap; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Fluent credential builder for instantiating a {@link ClientAssertionCredential}. | ||
* | ||
* @see ClientAssertionCredential | ||
*/ | ||
public class ClientAssertionCredentialBuilder extends AadCredentialBuilderBase<ClientAssertionCredentialBuilder> { | ||
private Supplier<String> clientAssertionSupplier; | ||
private final ClientLogger logger = new ClientLogger(ClientAssertionCredentialBuilder.class); | ||
|
||
/** | ||
* Sets the supplier containing the logic to supply the client assertion when invoked. | ||
* | ||
* @param clientAssertionSupplier the supplier supplying client assertion. | ||
* @return An updated instance of this builder. | ||
*/ | ||
public ClientAssertionCredentialBuilder clientAssertion(Supplier<String> clientAssertionSupplier) { | ||
this.clientAssertionSupplier = clientAssertionSupplier; | ||
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 ClientAssertionCredentialBuilder tokenCachePersistenceOptions(TokenCachePersistenceOptions | ||
tokenCachePersistenceOptions) { | ||
this.identityClientOptions.setTokenCacheOptions(tokenCachePersistenceOptions); | ||
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. | ||
*/ | ||
ClientAssertionCredentialBuilder regionalAuthority(RegionalAuthority regionalAuthority) { | ||
this.identityClientOptions.setRegionalAuthority(regionalAuthority); | ||
return this; | ||
} | ||
|
||
/** | ||
* Creates a new {@link ClientAssertionCredential} with the current configurations. | ||
* | ||
* @return a {@link ClientAssertionCredential} with the current configurations. | ||
* @throws IllegalArgumentException if either of clientId, tenantId or clientAssertion is not present. | ||
*/ | ||
public ClientAssertionCredential build() { | ||
ValidationUtil.validate(getClass().getSimpleName(), new HashMap<String, Object>() {{ | ||
put("clientId", clientId); | ||
put("tenantId", tenantId); | ||
put("clientAssertion", clientAssertionSupplier); | ||
}}); | ||
|
||
return new ClientAssertionCredential(clientId, tenantId, clientAssertionSupplier, identityClientOptions); | ||
} | ||
} |
This file contains 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 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 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 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.