Skip to content

Commit f2780bd

Browse files
authored
Update Identity to Use Static ClientLoggers (Azure#27374)
1 parent 9d8bb10 commit f2780bd

File tree

49 files changed

+399
-357
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+399
-357
lines changed

sdk/identity/azure-identity/src/main/java/com/azure/identity/AadCredentialBuilderBase.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
package com.azure.identity;
55

6+
import com.azure.core.util.logging.ClientLogger;
67
import com.azure.identity.implementation.util.ValidationUtil;
78

89
import java.util.concurrent.ExecutorService;
@@ -13,6 +14,8 @@
1314
* @param <T> the type of the credential builder
1415
*/
1516
public abstract class AadCredentialBuilderBase<T extends AadCredentialBuilderBase<T>> extends CredentialBuilderBase<T> {
17+
private static final ClientLogger LOGGER = new ClientLogger(AadCredentialBuilderBase.class);
18+
1619
String clientId;
1720
String tenantId;
1821

@@ -23,7 +26,7 @@ public abstract class AadCredentialBuilderBase<T extends AadCredentialBuilderBas
2326
*/
2427
@SuppressWarnings("unchecked")
2528
public T authorityHost(String authorityHost) {
26-
ValidationUtil.validateAuthHost(getClass().getSimpleName(), authorityHost);
29+
ValidationUtil.validateAuthHost(authorityHost, LOGGER);
2730
this.identityClientOptions.setAuthorityHost(authorityHost);
2831
return (T) this;
2932
}
@@ -48,7 +51,7 @@ public T clientId(String clientId) {
4851
*/
4952
@SuppressWarnings("unchecked")
5053
public T tenantId(String tenantId) {
51-
ValidationUtil.validateTenantIdCharacterRange(getClass().getSimpleName(), tenantId);
54+
ValidationUtil.validateTenantIdCharacterRange(tenantId, LOGGER);
5255
this.tenantId = tenantId;
5356
return (T) this;
5457
}

sdk/identity/azure-identity/src/main/java/com/azure/identity/AksExchangeTokenCredential.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* Authenticates a service principal with AAD using a client assertion.
1515
*/
1616
class AksExchangeTokenCredential extends ManagedIdentityServiceCredential {
17-
private final ClientLogger logger = new ClientLogger(AksExchangeTokenCredential.class);
17+
private static final ClientLogger LOGGER = new ClientLogger(AksExchangeTokenCredential.class);
1818

1919
/**
2020
* Creates an instance of AksExchangeTokenCredential.
@@ -29,10 +29,10 @@ class AksExchangeTokenCredential extends ManagedIdentityServiceCredential {
2929
@Override
3030
public Mono<AccessToken> authenticate(TokenRequestContext request) {
3131
if (this.getClientId() == null) {
32-
return Mono.error(logger.logExceptionAsError(new IllegalStateException("The client id is not configured via"
32+
return Mono.error(LOGGER.logExceptionAsError(new IllegalStateException("The client id is not configured via"
3333
+ " 'AZURE_CLIENT_ID' environment variable or through the credential builder."
3434
+ " Please ensure client id is provided to authenticate via token exchange in AKS environment.")));
3535
}
36-
return identityClient.authenticatewithExchangeToken(request);
36+
return identityClient.authenticateWithExchangeToken(request);
3737
}
3838
}

sdk/identity/azure-identity/src/main/java/com/azure/identity/AppServiceMsiCredential.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
*/
1717
@Immutable
1818
class AppServiceMsiCredential extends ManagedIdentityServiceCredential {
19+
private static final ClientLogger LOGGER = new ClientLogger(AppServiceMsiCredential.class);
20+
1921
private final String identityEndpoint;
2022
private final String identityHeader;
21-
private final ClientLogger logger = new ClientLogger(AppServiceMsiCredential.class);
2223

2324
/**
2425
* Creates an instance of {@link AppServiceMsiCredential}.
@@ -32,7 +33,7 @@ class AppServiceMsiCredential extends ManagedIdentityServiceCredential {
3233
this.identityEndpoint = configuration.get(Configuration.PROPERTY_IDENTITY_ENDPOINT);
3334
this.identityHeader = configuration.get(Configuration.PROPERTY_IDENTITY_HEADER);
3435
if (identityEndpoint != null) {
35-
validateEndpointProtocol(this.identityEndpoint, "MSI", logger);
36+
validateEndpointProtocol(this.identityEndpoint, "MSI", LOGGER);
3637
}
3738
}
3839

sdk/identity/azure-identity/src/main/java/com/azure/identity/ArcIdentityCredential.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
*/
1818
@Immutable
1919
class ArcIdentityCredential extends ManagedIdentityServiceCredential {
20+
private static final ClientLogger LOGGER = new ClientLogger(ArcIdentityCredential.class);
21+
2022
private final String identityEndpoint;
21-
private final ClientLogger logger = new ClientLogger(ArcIdentityCredential.class);
2223

2324
/**
2425
* Creates an instance of {@link ArcIdentityCredential}.
@@ -31,7 +32,7 @@ class ArcIdentityCredential extends ManagedIdentityServiceCredential {
3132
Configuration configuration = Configuration.getGlobalConfiguration().clone();
3233
this.identityEndpoint = configuration.get(Configuration.PROPERTY_IDENTITY_ENDPOINT);
3334
if (identityEndpoint != null) {
34-
validateEndpointProtocol(this.identityEndpoint, "Identity", logger);
35+
validateEndpointProtocol(this.identityEndpoint, "Identity", LOGGER);
3536
}
3637
}
3738

@@ -43,7 +44,7 @@ class ArcIdentityCredential extends ManagedIdentityServiceCredential {
4344
*/
4445
public Mono<AccessToken> authenticate(TokenRequestContext request) {
4546
if (getClientId() != null) {
46-
return Mono.error(logger.logExceptionAsError(new ClientAuthenticationException(
47+
return Mono.error(LOGGER.logExceptionAsError(new ClientAuthenticationException(
4748
"User assigned identity is not supported by the Azure Arc Managed Identity Endpoint. To authenticate "
4849
+ "with the system assigned identity omit the client id when constructing the"
4950
+ " ManagedIdentityCredential.", null)));

sdk/identity/azure-identity/src/main/java/com/azure/identity/AuthorizationCodeCredential.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424
*/
2525
@Immutable
2626
public class AuthorizationCodeCredential implements TokenCredential {
27+
private static final ClientLogger LOGGER = new ClientLogger(AuthorizationCodeCredential.class);
28+
2729
private final String authCode;
2830
private final URI redirectUri;
2931
private final IdentityClient identityClient;
3032
private final AtomicReference<MsalAuthenticationAccount> cachedToken;
31-
private final ClientLogger logger = new ClientLogger(AuthorizationCodeCredential.class);
3233

3334
/**
3435
* Creates an AuthorizationCodeCredential with the given identity client options.
@@ -70,8 +71,8 @@ public Mono<AccessToken> getToken(TokenRequestContext request) {
7071
identityClient.getTenantId(), identityClient.getClientId())));
7172
return (AccessToken) msalToken;
7273
})
73-
.doOnNext(token -> LoggingUtil.logTokenSuccess(logger, request))
74-
.doOnError(error -> LoggingUtil.logTokenError(logger, identityClient.getIdentityClientOptions(),
74+
.doOnNext(token -> LoggingUtil.logTokenSuccess(LOGGER, request))
75+
.doOnError(error -> LoggingUtil.logTokenError(LOGGER, identityClient.getIdentityClientOptions(),
7576
request, error));
7677
}
7778
}

sdk/identity/azure-identity/src/main/java/com/azure/identity/AuthorizationCodeCredentialBuilder.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* @see AuthorizationCodeCredential
1717
*/
1818
public class AuthorizationCodeCredentialBuilder extends AadCredentialBuilderBase<AuthorizationCodeCredentialBuilder> {
19-
private final ClientLogger logger = new ClientLogger(AuthorizationCodeCredentialBuilder.class);
19+
private static final ClientLogger LOGGER = new ClientLogger(AuthorizationCodeCredentialBuilder.class);
2020

2121
private String authCode;
2222
private String redirectUrl;
@@ -36,7 +36,7 @@ public AuthorizationCodeCredentialBuilder authorizationCode(String authCode) {
3636
/**
3737
* Sets redirect URL for the Oauth 2.0 login request, which must be
3838
* registered as a valid redirect URL on the application. The authorization code
39-
* will be sent to this URL so it must be listening on this server and is able
39+
* will be sent to this URL, so it must be listening on this server and is able
4040
* to complete the {@link AuthorizationCodeCredential} construction from there.
4141
* This is also called Reply URLs in some contexts.
4242
*
@@ -70,12 +70,12 @@ public AuthorizationCodeCredential build() {
7070
put("clientId", clientId);
7171
put("authorizationCode", authCode);
7272
put("redirectUrl", redirectUrl);
73-
}});
73+
}}, LOGGER);
7474
try {
7575
return new AuthorizationCodeCredential(clientId, clientSecret, tenantId, authCode, new URI(redirectUrl),
7676
identityClientOptions);
7777
} catch (URISyntaxException e) {
78-
throw logger.logExceptionAsError(new RuntimeException(e));
78+
throw LOGGER.logExceptionAsError(new RuntimeException(e));
7979
}
8080
}
8181
}

sdk/identity/azure-identity/src/main/java/com/azure/identity/AzureApplicationCredentialBuilder.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
* @see AzureApplicationCredential
1818
*/
1919
class AzureApplicationCredentialBuilder extends CredentialBuilderBase<AzureApplicationCredentialBuilder> {
20+
private static final ClientLogger LOGGER = new ClientLogger(AzureApplicationCredentialBuilder.class);
21+
2022
private String managedIdentityClientId;
21-
private String managedIdentityResouceId;
22-
private final ClientLogger logger = new ClientLogger(AzureApplicationCredentialBuilder.class);
23+
private String managedIdentityResourceId;
2324

2425
/**
2526
* Creates an instance of a AzureApplicationCredentialBuilder.
@@ -62,7 +63,7 @@ public AzureApplicationCredentialBuilder managedIdentityClientId(String clientId
6263
* @return An updated instance of this builder with the managed identity client id set as specified.
6364
*/
6465
public AzureApplicationCredentialBuilder managedIdentityResourceId(String resourceId) {
65-
this.managedIdentityResouceId = resourceId;
66+
this.managedIdentityResourceId = resourceId;
6667
return this;
6768
}
6869

@@ -93,8 +94,8 @@ public AzureApplicationCredentialBuilder executorService(ExecutorService executo
9394
* @throws IllegalStateException if clientId and resourceId are both set.
9495
*/
9596
public AzureApplicationCredential build() {
96-
if (managedIdentityClientId != null && managedIdentityResouceId != null) {
97-
throw logger.logExceptionAsError(
97+
if (managedIdentityClientId != null && managedIdentityResourceId != null) {
98+
throw LOGGER.logExceptionAsError(
9899
new IllegalStateException("Only one of managedIdentityClientId and managedIdentityResourceId can be specified."));
99100
}
100101

@@ -104,7 +105,7 @@ public AzureApplicationCredential build() {
104105
private ArrayList<TokenCredential> getCredentialsChain() {
105106
ArrayList<TokenCredential> output = new ArrayList<TokenCredential>(2);
106107
output.add(new EnvironmentCredential(identityClientOptions));
107-
output.add(new ManagedIdentityCredential(managedIdentityClientId, managedIdentityResouceId, identityClientOptions));
108+
output.add(new ManagedIdentityCredential(managedIdentityClientId, managedIdentityResourceId, identityClientOptions));
108109
return output;
109110
}
110111
}

sdk/identity/azure-identity/src/main/java/com/azure/identity/AzureCliCredential.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
*/
2121
@Immutable
2222
public class AzureCliCredential implements TokenCredential {
23+
private static final ClientLogger LOGGER = new ClientLogger(AzureCliCredential.class);
24+
2325
private final IdentityClient identityClient;
24-
private final ClientLogger logger = new ClientLogger(AzureCliCredential.class);
2526

2627
/**
2728
* Creates an AzureCliSecretCredential with default identity client options.
@@ -37,7 +38,8 @@ public class AzureCliCredential implements TokenCredential {
3738
@Override
3839
public Mono<AccessToken> getToken(TokenRequestContext request) {
3940
return identityClient.authenticateWithAzureCli(request)
40-
.doOnNext(token -> LoggingUtil.logTokenSuccess(logger, request))
41-
.doOnError(error -> LoggingUtil.logTokenError(logger, identityClient.getIdentityClientOptions(), request, error));
41+
.doOnNext(token -> LoggingUtil.logTokenSuccess(LOGGER, request))
42+
.doOnError(error -> LoggingUtil.logTokenError(LOGGER, identityClient.getIdentityClientOptions(), request,
43+
error));
4244
}
4345
}

sdk/identity/azure-identity/src/main/java/com/azure/identity/AzureCliCredentialBuilder.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
package com.azure.identity;
55

6+
import com.azure.core.util.logging.ClientLogger;
67
import com.azure.identity.implementation.util.ValidationUtil;
78

89
/**
@@ -11,6 +12,8 @@
1112
* @see AzureCliCredential
1213
*/
1314
public class AzureCliCredentialBuilder extends CredentialBuilderBase<AzureCliCredentialBuilder> {
15+
private static final ClientLogger LOGGER = new ClientLogger(AzureCliCredentialBuilder.class);
16+
1417
private String tenantId;
1518

1619
/**
@@ -20,7 +23,7 @@ public class AzureCliCredentialBuilder extends CredentialBuilderBase<AzureCliCre
2023
* @return An updated instance of this builder with the tenant id set as specified.
2124
*/
2225
public AzureCliCredentialBuilder tenantId(String tenantId) {
23-
ValidationUtil.validateTenantIdCharacterRange(getClass().getSimpleName(), tenantId);
26+
ValidationUtil.validateTenantIdCharacterRange(tenantId, LOGGER);
2427
this.tenantId = tenantId;
2528
return this;
2629
}

sdk/identity/azure-identity/src/main/java/com/azure/identity/AzurePowerShellCredential.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
import reactor.core.publisher.Mono;
1616

1717
/**
18-
* A credential provider that provides token credentials based on Azure Power Shell command.
18+
* A credential provider that provides token credentials based on Azure PowerShell command.
1919
*/
2020
@Immutable
2121
public class AzurePowerShellCredential implements TokenCredential {
22+
private static final ClientLogger LOGGER = new ClientLogger(AzurePowerShellCredential.class);
23+
2224
private final IdentityClient identityClient;
23-
private final ClientLogger logger = new ClientLogger(AzurePowerShellCredential.class);
2425

2526
AzurePowerShellCredential(String tenantId, IdentityClientOptions options) {
2627
identityClient = new IdentityClientBuilder()
@@ -32,7 +33,8 @@ public class AzurePowerShellCredential implements TokenCredential {
3233
@Override
3334
public Mono<AccessToken> getToken(TokenRequestContext request) {
3435
return identityClient.authenticateWithAzurePowerShell(request)
35-
.doOnNext(token -> LoggingUtil.logTokenSuccess(logger, request))
36-
.doOnError(error -> LoggingUtil.logTokenError(logger, identityClient.getIdentityClientOptions(), request, error));
36+
.doOnNext(token -> LoggingUtil.logTokenSuccess(LOGGER, request))
37+
.doOnError(error -> LoggingUtil.logTokenError(LOGGER, identityClient.getIdentityClientOptions(), request,
38+
error));
3739
}
3840
}

0 commit comments

Comments
 (0)