Skip to content

Commit 3df8a49

Browse files
authored
Migrate from ADAL to MSAL for TestFx (#23730)
1 parent b0cd118 commit 3df8a49

File tree

14 files changed

+116
-229
lines changed

14 files changed

+116
-229
lines changed

src/Accounts/Accounts.Test/Mocks/MockSubscriptionClientFactoryVersion2019.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@
1616
using Microsoft.Azure.Management.ResourceManager.Version2021_01_01.Models;
1717
using Microsoft.Rest.Azure;
1818
using Moq;
19+
using Newtonsoft.Json;
1920
using System;
2021
using System.Collections.Generic;
2122
using System.Linq;
22-
using System.Management.Automation.Language;
2323
using System.Threading;
2424
using System.Threading.Tasks;
25-
using Newtonsoft.Json;
26-
using Microsoft.IdentityModel.Tokens;
2725

2826
namespace Microsoft.Azure.Commands.ResourceManager.Common.Test
2927
{

src/Accounts/Accounts.Test/SubscriptionClientSwitchTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public void SubscriptionClientListTenantFallback()
123123
{
124124
subscriptionClients.Dequeue();
125125
}
126-
throw e;
126+
throw;
127127
}
128128
return Task.FromResult(result);
129129
};
@@ -193,7 +193,7 @@ public void SubscriptionClientGetSubFallback()
193193
{
194194
subscriptionClients.Dequeue();
195195
}
196-
throw e;
196+
throw;
197197
}
198198
return Task.FromResult(result);
199199
};
@@ -265,7 +265,7 @@ public void SubscriptionClientListSubFallback()
265265
{
266266
subscriptionClients.Dequeue();
267267
}
268-
throw e;
268+
throw;
269269
}
270270
return Task.FromResult(result);
271271
};

src/Attestation/Attestation.Test/ScenarioTests/AttestationTestRunner.cs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using Microsoft.Azure.Attestation;
16+
using Microsoft.Azure.Commands.TestFx;
1617
using Microsoft.Azure.Management.Attestation;
17-
using Microsoft.Azure.Test.HttpRecorder;
18-
using System;
19-
using System.Collections.Generic;
2018
using Microsoft.Azure.Management.Internal.Resources;
21-
using Microsoft.IdentityModel.Clients.ActiveDirectory;
19+
using Microsoft.Azure.Test.HttpRecorder;
2220
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
23-
using Microsoft.Azure.Commands.TestFx;
21+
using System.Collections.Generic;
2422
using Xunit.Abstractions;
2523

2624
namespace Microsoft.Azure.Commands.Attestation.Test.ScenarioTests
@@ -76,22 +74,12 @@ private static AttestationManagementClient GetAttestationManagementClient(MockCo
7674

7775
private static AttestationClient GetAttestationClient(MockContext context)
7876
{
79-
string environmentConnectionString = Environment.GetEnvironmentVariable("TEST_CSM_ORGID_AUTHENTICATION");
8077
string accessToken = "fakefakefake";
8178

8279
// When recording, we should have a connection string passed into the code from the environment
83-
if (!string.IsNullOrEmpty(environmentConnectionString))
80+
if (HttpMockServer.Mode == HttpRecorderMode.Record)
8481
{
85-
// Gather test client credential information from the environment
86-
var connectionInfo = new ConnectionString(Environment.GetEnvironmentVariable("TEST_CSM_ORGID_AUTHENTICATION"));
87-
string servicePrincipal = connectionInfo.GetValue<string>(ConnectionStringKeys.ServicePrincipalKey);
88-
string servicePrincipalSecret = connectionInfo.GetValue<string>(ConnectionStringKeys.ServicePrincipalSecretKey);
89-
string aadTenant = connectionInfo.GetValue<string>(ConnectionStringKeys.TenantIdKey);
90-
91-
// Create credentials
92-
var clientCredentials = new ClientCredential(servicePrincipal, servicePrincipalSecret);
93-
var authContext = new AuthenticationContext($"https://login.windows.net/{aadTenant}", TokenCache.DefaultShared);
94-
accessToken = authContext.AcquireTokenAsync("https://attest.azure.net", clientCredentials).Result.AccessToken;
82+
accessToken = TestEnvironmentFactory.GetTestEnvironment().GetServicePrincipalAccessToken(new[] { "https://attest.azure.net/.default" });
9583
}
9684

9785
return new AttestationClient(new AttestationCredentials(accessToken), HttpMockServer.CreateInstance());

src/Attestation/Attestation.sln

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,7 @@ Global
8484
{3B31A547-8D98-4DE2-B314-885C82474A42} = {C9F34834-F918-4085-A8DE-F9E38FB03AF5}
8585
{D3F5BCD1-1B75-421E-8C3A-B16F211680F6} = {C9F34834-F918-4085-A8DE-F9E38FB03AF5}
8686
{A52A877E-B6BA-410D-ADEE-0EE969734C50} = {C9F34834-F918-4085-A8DE-F9E38FB03AF5}
87-
{0F29534D-1078-4FBC-A65D-913CC6D62D7A} = {E3DA12B6-0025-4624-805C-F3406CA90122}
8887
{31B49528-4241-4DDB-8BB9-28F95148C602} = {E3DA12B6-0025-4624-805C-F3406CA90122}
89-
{B73D384F-62CA-4FD2-89C6-04DB2DC5AB9D} = {E3DA12B6-0025-4624-805C-F3406CA90122}
9088
{D8EAB1F1-D2A9-49F9-BC09-7397F9CCFF10} = {E3DA12B6-0025-4624-805C-F3406CA90122}
9189
EndGlobalSection
9290
EndGlobal

src/Batch/Batch.Test/ScenarioTests/ScenarioTestHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public static void CreatePoolIfNotExists(
238238
{
239239
if (e.RequestInformation.BatchError.Code != "PoolExists")
240240
{
241-
throw e;
241+
throw;
242242
}
243243
}
244244
}

src/Network/Network.Test/ScenarioTests/NetworkTestRunner.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
using Microsoft.Azure.Commands.TestFx;
1616
using Microsoft.Azure.KeyVault;
1717
using Microsoft.Azure.Test.HttpRecorder;
18-
using Microsoft.IdentityModel.Clients.ActiveDirectory;
1918
using Microsoft.Rest;
2019
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
2120
using System;
@@ -77,26 +76,12 @@ protected NetworkTestRunner(ITestOutputHelper output)
7776

7877
private static KeyVaultClient GetKeyVaultClient(MockContext context)
7978
{
80-
string environmentConnectionString = Environment.GetEnvironmentVariable("TEST_CSM_ORGID_AUTHENTICATION");
8179
string accessToken = "fakefakefake";
8280

8381
// When recording, we should have a connection string passed into the code from the environment
84-
if (!string.IsNullOrEmpty(environmentConnectionString))
82+
if (HttpMockServer.Mode == HttpRecorderMode.Record)
8583
{
86-
// Gather test client credential information from the environment
87-
var connectionInfo = new ConnectionString(Environment.GetEnvironmentVariable("TEST_CSM_ORGID_AUTHENTICATION"));
88-
var mode = connectionInfo.GetValue<string>(ConnectionStringKeys.HttpRecorderModeKey);
89-
if (mode == HttpRecorderMode.Record.ToString())
90-
{
91-
string servicePrincipal = connectionInfo.GetValue<string>(ConnectionStringKeys.ServicePrincipalKey);
92-
string servicePrincipalSecret = connectionInfo.GetValue<string>(ConnectionStringKeys.ServicePrincipalSecretKey);
93-
string aadTenant = connectionInfo.GetValue<string>(ConnectionStringKeys.TenantIdKey);
94-
95-
// Create credentials
96-
var clientCredentials = new ClientCredential(servicePrincipal, servicePrincipalSecret);
97-
var authContext = new AuthenticationContext($"https://login.windows.net/{aadTenant}", TokenCache.DefaultShared);
98-
accessToken = authContext.AcquireTokenAsync("https://vault.azure.net", clientCredentials).Result.AccessToken;
99-
}
84+
accessToken = TestEnvironmentFactory.GetTestEnvironment().GetServicePrincipalAccessToken(new[] { "https://vault.azure.net/.default" });
10085
}
10186

10287
return new KeyVaultClient(new TokenCredentials(accessToken), HttpMockServer.CreateInstance());

src/Resources/Resources.Test/ScenarioTests/ResourcesTestRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected ResourcesTestRunner(ITestOutputHelper output)
6262
? new Func<ServiceClientCredentials>(() =>
6363
{
6464
var testEnvironment = TestEnvironmentFactory.GetTestEnvironment();
65-
return testEnvironment.TokenInfo[TokenAudience.Management];
65+
return testEnvironment.TokenInfo[TokenAudience.Graph];
6666
})()
6767
: new TokenCredentials("foo");
6868

src/Synapse/Synapse.Test/ScenarioTests/SynapseTestRunner.cs

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,12 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
15+
using Microsoft.Azure.Commands.TestFx;
16+
using Microsoft.Azure.Synapse;
1617
using Microsoft.Azure.Test.HttpRecorder;
18+
using Microsoft.Rest;
1719
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
18-
using System;
1920
using System.Collections.Generic;
20-
using NewResourceManagementClient = Microsoft.Azure.Management.Internal.Resources.ResourceManagementClient;
21-
using Microsoft.Azure.Management.Synapse;
22-
using Microsoft.Azure.Synapse;
23-
using Microsoft.IdentityModel.Clients.ActiveDirectory;
24-
using Microsoft.Rest;
25-
using NewStorageManagementClient = Microsoft.Azure.Management.Storage.Version2017_10_01.StorageManagementClient;
26-
using Microsoft.Azure.Management.Storage;
27-
using SDKMonitor = Microsoft.Azure.Management.Monitor;
28-
using CommonMonitor = Microsoft.Azure.Management.Monitor.Version2018_09_01;
29-
using Microsoft.Azure.Management.OperationalInsights;
30-
using Microsoft.Azure.Management.EventHub;
31-
using Microsoft.Azure.Commands.TestFx;
3221
using Xunit.Abstractions;
3322

3423
namespace Microsoft.Azure.Commands.Synapse.Test.ScenarioTests
@@ -89,22 +78,12 @@ protected SynapseTestRunner(ITestOutputHelper output)
8978

9079
protected static SynapseClient GetSynapseClient(MockContext context)
9180
{
92-
string environmentConnectionString = Environment.GetEnvironmentVariable("TEST_CSM_ORGID_AUTHENTICATION");
9381
string accessToken = "fakefakefake";
9482

9583
// When recording, we should have a connection string passed into the code from the environment
96-
if (!string.IsNullOrEmpty(environmentConnectionString))
84+
if (HttpMockServer.Mode == HttpRecorderMode.Record)
9785
{
98-
// Gather test client credential information from the environment
99-
var connectionInfo = new ConnectionString(Environment.GetEnvironmentVariable("TEST_CSM_ORGID_AUTHENTICATION"));
100-
string servicePrincipal = connectionInfo.GetValue<string>(ConnectionStringKeys.ServicePrincipalKey);
101-
string servicePrincipalSecret = connectionInfo.GetValue<string>(ConnectionStringKeys.ServicePrincipalSecretKey);
102-
string aadTenant = connectionInfo.GetValue<string>(ConnectionStringKeys.TenantIdKey);
103-
104-
// Create credentials
105-
var clientCredentials = new ClientCredential(servicePrincipal, servicePrincipalSecret);
106-
var authContext = new AuthenticationContext($"https://login.windows.net/{aadTenant}", TokenCache.DefaultShared);
107-
accessToken = authContext.AcquireTokenAsync("https://dev.azuresynapse.net", clientCredentials).Result.AccessToken;
86+
accessToken = TestEnvironmentFactory.GetTestEnvironment().GetServicePrincipalAccessToken(new[] { "https://dev.azuresynapse.net/.default" });
10887
}
10988

11089
return new SynapseClient(new TokenCredentials(accessToken), HttpMockServer.CreateInstance());

tools/Common.Netcore.Dependencies.Test.targets

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
<ItemGroup>
1212
<PackageReference Include="Microsoft.Azure.Management.ResourceManager" Version="3.17.3-preview" />
13-
<PackageReference Include="Microsoft.Rest.ClientRuntime.Azure.Authentication" Version="2.3.7" />
1413
<PackageReference Include="xunit" Version="2.4.2" />
1514
</ItemGroup>
1615

@@ -40,7 +39,7 @@
4039
</ItemGroup>
4140

4241
<ItemGroup>
43-
<PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="3.19.2" />
4442
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
43+
<PackageReference Include="Microsoft.Identity.Client" Version="4.49.1" />
4544
</ItemGroup>
4645
</Project>

tools/TestFx/EnvironmentSetupHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ public void SetupAzureEnvironmentFromEnvironmentVariables(AzureModule mode)
406406
environment.StorageEndpointSuffix = AzureEnvironmentConstants.AzureStorageEndpointSuffix;
407407
environment.AzureKeyVaultDnsSuffix = AzureEnvironmentConstants.AzureKeyVaultDnsSuffix;
408408
environment.AzureKeyVaultServiceEndpointResourceId = AzureEnvironmentConstants.AzureKeyVaultServiceEndpointResourceId;
409+
environment.ExtendedProperties.SetProperty(AzureEnvironment.ExtendedEndpoint.MicrosoftGraphUrl, currentEnvironment.Endpoints.GraphUri.AbsoluteUri);
409410
environment.ExtendedProperties.SetProperty(AzureEnvironment.ExtendedEndpoint.OperationalInsightsEndpoint, "https://api.loganalytics.io/v1");
410411
environment.ExtendedProperties.SetProperty(AzureEnvironment.ExtendedEndpoint.OperationalInsightsEndpointResourceId, "https://api.loganalytics.io");
411412
if (!AzureRmProfileProvider.Instance.GetProfile<AzureRmProfile>().EnvironmentTable.ContainsKey(TestEnvironmentName))

0 commit comments

Comments
 (0)