Skip to content

Commit 769fbbb

Browse files
erich-wangisra-felYeming Liu
authored
Add MSAL support for Az.Accounts (#13225)
* Add MSAL support based on Azure.Identity * temp fix for adfs * fix cache * code refactoring * code refactoring * add Azure.Identity local package * update test cases * temporarily fix static analysis errors for eng bits * fix spn+cert flow * fix msal cache helper * code refactoring * update Azure.Identity to 1.4.0-beta.1 * refacotring code per review comments * fix build error * fix test case errors * fix build error on MacOS * update common assemblies * update static analysis * update test cases * fix typo * update test cases Co-authored-by: Yeming Liu <[email protected]> Co-authored-by: Yeming Liu <[email protected]>
1 parent 09d7f9b commit 769fbbb

File tree

111 files changed

+3354
-1918
lines changed

Some content is hidden

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

111 files changed

+3354
-1918
lines changed

src/Accounts/Accounts.Test/AutoSaveSettingOnLoadingTests.cs

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

1515
using Microsoft.Azure.Commands.Common.Authentication;
1616
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
17-
using Microsoft.Azure.Commands.Common.Authentication.Core;
1817
using Microsoft.Azure.Commands.Common.Authentication.Models;
1918
using Microsoft.Azure.Commands.Common.Authentication.Properties;
2019
using Microsoft.Azure.Commands.ResourceManager.Common;
@@ -111,7 +110,7 @@ public void DisableAutoSaveWhenSettingFileBreaks()
111110
var newSetting = JsonConvert.DeserializeObject<ContextAutosaveSettings>(afterModified) as ContextAutosaveSettings;
112111
Assert.NotNull(newSetting);
113112
Assert.Equal(ContextSaveMode.CurrentUser, newSetting.Mode);
114-
Assert.Equal(typeof(AuthenticationStoreTokenCache), AzureSession.Instance.TokenCache.GetType());
113+
//Assert.Equal(typeof(AzureTokenCache), AzureSession.Instance.TokenCache.GetType());
115114
}
116115
finally
117116
{

src/Accounts/Accounts.Test/AutosaveTests.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using Microsoft.Azure.Commands.Common.Authentication;
16-
// TODO: Remove IfDef
17-
#if NETSTANDARD
18-
using Microsoft.Azure.Commands.Common.Authentication.Core;
19-
#endif
2016
using Microsoft.Azure.Commands.Common.Authentication.Models;
2117
using Microsoft.Azure.ServiceManagement.Common.Models;
2218
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
@@ -49,11 +45,15 @@ void ResetState()
4945

5046
TestExecutionHelpers.SetUpSessionAndProfile();
5147
ResourceManagerProfileProvider.InitializeResourceManagerProfile(true);
48+
// prevent token acquisition
49+
AzureRmProfileProvider.Instance.GetProfile<AzureRmProfile>().ShouldRefreshContextsFromCache = false;
5250
AzureSession.Instance.DataStore = dataStore;
5351
AzureSession.Instance.ARMContextSaveMode = ContextSaveMode.Process;
5452
AzureSession.Instance.AuthenticationFactory = new MockTokenAuthenticationFactory();
55-
AzureSession.Instance.TokenCache = new AuthenticationStoreTokenCache(new AzureTokenCache());
5653
Environment.SetEnvironmentVariable("Azure_PS_Data_Collection", "false");
54+
PowerShellTokenCacheProvider tokenProvider = new InMemoryTokenCacheProvider();
55+
AzureSession.Instance.RegisterComponent(PowerShellTokenCacheProvider.PowerShellTokenCacheProviderKey, () => tokenProvider);
56+
AzureSession.Instance.RegisterComponent(nameof(PowerShellTokenCache), () => tokenProvider.GetTokenCache());
5757
}
5858

5959
[Fact]
@@ -70,9 +70,12 @@ public void EnableAutosaveWhenDisabled()
7070
cmdlet.ExecuteCmdlet();
7171
cmdlet.InvokeEndProcessing();
7272
Assert.Equal(ContextSaveMode.CurrentUser, AzureSession.Instance.ARMContextSaveMode);
73-
Assert.Equal(typeof(ProtectedFileTokenCache), AzureSession.Instance.TokenCache.GetType());
7473
Assert.Equal(typeof(ProtectedProfileProvider), AzureRmProfileProvider.Instance.GetType());
7574
}
75+
catch (PlatformNotSupportedException)
76+
{
77+
// swallow exception when test env (Linux server) doesn't support token cache persistence
78+
}
7679
finally
7780
{
7881
ResetState();
@@ -94,9 +97,12 @@ public void EnableAutosaveWhenEnabled()
9497
cmdlet.ExecuteCmdlet();
9598
cmdlet.InvokeEndProcessing();
9699
Assert.Equal(ContextSaveMode.CurrentUser, AzureSession.Instance.ARMContextSaveMode);
97-
Assert.Equal(typeof(ProtectedFileTokenCache), AzureSession.Instance.TokenCache.GetType());
98100
Assert.Equal(typeof(ProtectedProfileProvider), AzureRmProfileProvider.Instance.GetType());
99101
}
102+
catch (PlatformNotSupportedException)
103+
{
104+
// swallow exception when test env (Linux server) doesn't support token cache persistence
105+
}
100106
finally
101107
{
102108
ResetState();
@@ -119,7 +125,8 @@ public void DisableAutosaveWhenEnabled()
119125
cmdlet.ExecuteCmdlet();
120126
cmdlet.InvokeEndProcessing();
121127
Assert.Equal(ContextSaveMode.Process, AzureSession.Instance.ARMContextSaveMode);
122-
Assert.Equal(typeof(AuthenticationStoreTokenCache), AzureSession.Instance.TokenCache.GetType());
128+
Assert.True(AzureSession.Instance.TryGetComponent(PowerShellTokenCacheProvider.PowerShellTokenCacheProviderKey, out PowerShellTokenCacheProvider factory));
129+
Assert.Equal(typeof(InMemoryTokenCacheProvider), factory.GetType());
123130
Assert.Equal(typeof(ResourceManagerProfileProvider), AzureRmProfileProvider.Instance.GetType());
124131
}
125132
finally
@@ -142,7 +149,8 @@ public void DisableAutoSsaveWhenDisabled()
142149
cmdlet.ExecuteCmdlet();
143150
cmdlet.InvokeEndProcessing();
144151
Assert.Equal(ContextSaveMode.Process, AzureSession.Instance.ARMContextSaveMode);
145-
Assert.Equal(typeof(AuthenticationStoreTokenCache), AzureSession.Instance.TokenCache.GetType());
152+
Assert.True(AzureSession.Instance.TryGetComponent(PowerShellTokenCacheProvider.PowerShellTokenCacheProviderKey, out PowerShellTokenCacheProvider factory));
153+
Assert.Equal(typeof(InMemoryTokenCacheProvider), factory.GetType());
146154
Assert.Equal(typeof(ResourceManagerProfileProvider), AzureRmProfileProvider.Instance.GetType());
147155
}
148156
finally

src/Accounts/Accounts.Test/AzureRMProfileTests.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using Microsoft.Azure.Commands.Common.Authentication;
16-
// TODO: Remove IfDef
17-
#if NETSTANDARD
18-
using Microsoft.Azure.Commands.Common.Authentication.Core;
19-
#endif
2016
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
2117
using Microsoft.Azure.Commands.Common.Authentication.Factories;
2218
using Microsoft.Azure.Commands.Common.Authentication.Models;
@@ -946,7 +942,7 @@ public void SavingProfileWorks()
946942
};
947943
profile.DefaultContext = new AzureContext(sub, account, environment, tenant);
948944
profile.EnvironmentTable[environment.Name] = environment;
949-
profile.DefaultContext.TokenCache = new AuthenticationStoreTokenCache(new AzureTokenCache { CacheData = new byte[] { 1, 2, 3, 4, 5, 6, 8, 9, 0 } });
945+
//profile.DefaultContext.TokenCache = new AuthenticationStoreTokenCache(new AzureTokenCache { CacheData = new byte[] { 1, 2, 3, 4, 5, 6, 8, 9, 0 } });
950946
profile.Save();
951947
string actual = dataStore.ReadFileAsText(path).Substring(1).TrimEnd(new[] { '\0' });
952948
#if NETSTANDARD
@@ -1019,7 +1015,6 @@ public void LoadingProfileWorks()
10191015
Assert.Equal("testCloud", profile.DefaultContext.Environment.Name);
10201016
Assert.Equal("[email protected]", profile.DefaultContext.Account.Id);
10211017
Assert.Equal(AzureAccount.AccountType.User, profile.DefaultContext.Account.Type);
1022-
Assert.Equal(expectedArray, profile.DefaultContext.TokenCache.CacheData);
10231018
Assert.Equal(path, profile.ProfilePath);
10241019
}
10251020

src/Accounts/Accounts.sln

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
2-
# Visual Studio 15
3-
VisualStudioVersion = 15.0.27130.2027
2+
# Visual Studio Version 16
3+
VisualStudioVersion = 16.0.30503.244
44
MinimumVisualStudioVersion = 10.0.40219.1
55
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Accounts", "Accounts\Accounts.csproj", "{142D7B0B-388A-4CEB-A228-7F6D423C5C2E}"
66
EndProject
@@ -16,10 +16,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ScenarioTest.ResourceManage
1616
EndProject
1717
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestFx", "..\..\tools\TestFx\TestFx.csproj", "{BC80A1D0-FFA4-43D9-AA74-799F5CB54B58}"
1818
EndProject
19-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Authentication.Test", "Authentication.Test\Authentication.Test.csproj", "{78D9B754-6A18-4125-80CC-63437BDE3244}"
20-
EndProject
2119
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Authenticators", "Authenticators\Authenticators.csproj", "{6BD6B80A-06AF-4B5B-9230-69CCFC6C8D64}"
2220
EndProject
21+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Authentication.Test", "Authentication.Test\Authentication.Test.csproj", "{43BE9983-BD21-4474-92E5-1FFC0220B2AD}"
22+
EndProject
2323
Global
2424
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2525
Debug|Any CPU = Debug|Any CPU
@@ -50,20 +50,21 @@ Global
5050
{BC80A1D0-FFA4-43D9-AA74-799F5CB54B58}.Debug|Any CPU.Build.0 = Debug|Any CPU
5151
{BC80A1D0-FFA4-43D9-AA74-799F5CB54B58}.Release|Any CPU.ActiveCfg = Release|Any CPU
5252
{BC80A1D0-FFA4-43D9-AA74-799F5CB54B58}.Release|Any CPU.Build.0 = Release|Any CPU
53-
{78D9B754-6A18-4125-80CC-63437BDE3244}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
54-
{78D9B754-6A18-4125-80CC-63437BDE3244}.Debug|Any CPU.Build.0 = Debug|Any CPU
55-
{78D9B754-6A18-4125-80CC-63437BDE3244}.Release|Any CPU.ActiveCfg = Release|Any CPU
56-
{78D9B754-6A18-4125-80CC-63437BDE3244}.Release|Any CPU.Build.0 = Release|Any CPU
5753
{6BD6B80A-06AF-4B5B-9230-69CCFC6C8D64}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
5854
{6BD6B80A-06AF-4B5B-9230-69CCFC6C8D64}.Debug|Any CPU.Build.0 = Debug|Any CPU
5955
{6BD6B80A-06AF-4B5B-9230-69CCFC6C8D64}.Release|Any CPU.ActiveCfg = Release|Any CPU
6056
{6BD6B80A-06AF-4B5B-9230-69CCFC6C8D64}.Release|Any CPU.Build.0 = Release|Any CPU
57+
{43BE9983-BD21-4474-92E5-1FFC0220B2AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
58+
{43BE9983-BD21-4474-92E5-1FFC0220B2AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
59+
{43BE9983-BD21-4474-92E5-1FFC0220B2AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
60+
{43BE9983-BD21-4474-92E5-1FFC0220B2AD}.Release|Any CPU.Build.0 = Release|Any CPU
6161
EndGlobalSection
6262
GlobalSection(SolutionProperties) = preSolution
6363
HideSolutionNode = FALSE
6464
EndGlobalSection
6565
GlobalSection(NestedProjects) = preSolution
6666
{152D78F0-A642-4D0E-B3A8-2FC64FFA9714} = {95C16AED-FD57-42A0-86C3-2CF4300A4817}
67+
{43BE9983-BD21-4474-92E5-1FFC0220B2AD} = {95C16AED-FD57-42A0-86C3-2CF4300A4817}
6768
EndGlobalSection
6869
GlobalSection(ExtensibilityGlobals) = postSolution
6970
SolutionGuid = {AA51E4F8-AA75-429D-9626-12C7B4305D72}

0 commit comments

Comments
 (0)