|
8 | 8 | import com.azure.communication.identity.models.CommunicationTokenScope; |
9 | 9 | import com.azure.core.credential.AccessToken; |
10 | 10 | import com.azure.core.credential.AzureKeyCredential; |
| 11 | +import com.azure.core.credential.TokenCredential; |
11 | 12 | import com.azure.core.http.HttpClient; |
12 | 13 | import com.azure.core.http.HttpPipelineNextPolicy; |
13 | 14 | import com.azure.core.http.HttpResponse; |
|
19 | 20 | import com.azure.core.test.models.TestProxySanitizerType; |
20 | 21 | import com.azure.core.test.utils.MockTokenCredential; |
21 | 22 | import com.azure.core.util.Configuration; |
| 23 | +import com.azure.core.util.CoreUtils; |
22 | 24 | import com.azure.core.util.logging.ClientLogger; |
23 | 25 | import com.azure.core.util.logging.LogLevel; |
24 | | -import com.azure.identity.DefaultAzureCredentialBuilder; |
| 26 | +import com.azure.identity.*; |
25 | 27 | import reactor.core.publisher.Mono; |
26 | 28 |
|
27 | 29 | import java.util.ArrayList; |
@@ -97,15 +99,10 @@ protected CommunicationIdentityClientBuilder createClientBuilderUsingManagedIden |
97 | 99 | .endpoint(communicationEndpoint) |
98 | 100 | .httpClient(httpClient); |
99 | 101 |
|
100 | | - if (interceptorManager.isPlaybackMode()) { |
101 | | - builder.credential(new MockTokenCredential()); |
102 | | - } else { |
103 | | - builder.credential(new DefaultAzureCredentialBuilder().build()); |
104 | | - if (interceptorManager.isRecordMode()) { |
105 | | - builder.addPolicy(interceptorManager.getRecordPolicy()); |
106 | | - } |
| 102 | + builder.credential(getIdentityTestCredential(interceptorManager)); |
| 103 | + if (interceptorManager.isRecordMode()) { |
| 104 | + builder.addPolicy(interceptorManager.getRecordPolicy()); |
107 | 105 | } |
108 | | - |
109 | 106 | addTestProxyTestSanitizersAndMatchers(interceptorManager); |
110 | 107 | return builder; |
111 | 108 | } |
@@ -182,4 +179,39 @@ protected void verifyUserNotEmpty(CommunicationUserIdentifier userIdentifier) { |
182 | 179 | assertFalse(userIdentifier.getId().isEmpty()); |
183 | 180 | } |
184 | 181 |
|
| 182 | + public static TokenCredential getIdentityTestCredential(InterceptorManager interceptorManager) { |
| 183 | + if (interceptorManager.isPlaybackMode()) { |
| 184 | + return new MockTokenCredential(); |
| 185 | + } |
| 186 | + |
| 187 | + Configuration config = Configuration.getGlobalConfiguration(); |
| 188 | + |
| 189 | + ChainedTokenCredentialBuilder builder = new ChainedTokenCredentialBuilder() |
| 190 | + .addLast(new EnvironmentCredentialBuilder().build()) |
| 191 | + .addLast(new AzureCliCredentialBuilder().build()) |
| 192 | + .addLast(new AzureDeveloperCliCredentialBuilder().build()); |
| 193 | + |
| 194 | + |
| 195 | + String serviceConnectionId = config.get("AZURESUBSCRIPTION_SERVICE_CONNECTION_ID"); |
| 196 | + String clientId = config.get("AZURESUBSCRIPTION_CLIENT_ID"); |
| 197 | + String tenantId = config.get("AZURESUBSCRIPTION_TENANT_ID"); |
| 198 | + String systemAccessToken = config.get("SYSTEM_ACCESSTOKEN"); |
| 199 | + |
| 200 | + if (!CoreUtils.isNullOrEmpty(serviceConnectionId) |
| 201 | + && !CoreUtils.isNullOrEmpty(clientId) |
| 202 | + && !CoreUtils.isNullOrEmpty(tenantId) |
| 203 | + && !CoreUtils.isNullOrEmpty(systemAccessToken)) { |
| 204 | + |
| 205 | + builder.addLast(new AzurePipelinesCredentialBuilder() |
| 206 | + .systemAccessToken(systemAccessToken) |
| 207 | + .clientId(clientId) |
| 208 | + .tenantId(tenantId) |
| 209 | + .serviceConnectionId(serviceConnectionId) |
| 210 | + .build()); |
| 211 | + } |
| 212 | + |
| 213 | + builder.addLast(new AzurePowerShellCredentialBuilder().build()); |
| 214 | + return builder.build(); |
| 215 | + } |
| 216 | + |
185 | 217 | } |
0 commit comments