Skip to content

Commit 927fa79

Browse files
authored
Upgrade Azure.Identity to 1.10.3 and other dependencies (#23957)
* Release an Preview Az.Accounts for Updating Az.Identity to 1.10.3 (#23766) * Upgrade Azure.Identity to 1.10.0 Update Azure.Identity.Broker to 1.0.0-beta.5 Migrate Token Cache from Name without Suffix to Name with Suffix Set CAEenabled to true in TokenCredential Fix bugs Update Change Log Fix the issue that token cache cannot be migrated in Mac and Linux Integrate the Source Codes of Azure.Identity 1.10.0 to Enable CAE for Client Assertion Authencation * Try to fix Build Issue * Fix token acquisition error of Service Principal when upgrading Az.Accounts (#23841) #23831 * Polish change log * Address review comments
1 parent 1816872 commit 927fa79

File tree

80 files changed

+5089
-208
lines changed

Some content is hidden

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

80 files changed

+5089
-208
lines changed

src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ public void OnImport()
678678
#endif
679679
AzureSessionInitializer.InitializeAzureSession(WriteInitializationWarnings);
680680
AzureSessionInitializer.MigrateAdalCache(AzureSession.Instance, GetAzureContextContainer, WriteInitializationWarnings);
681+
AzureSessionInitializer.MigrateMsalCacheWithoutSuffix(AzureSession.Instance, WriteInitializationWarnings);
681682
#if DEBUG
682683
if (!TestMockSupport.RunningMocked)
683684
{

src/Accounts/Accounts/ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
-->
2020

2121
## Upcoming Release
22+
* Upgraded Azure.Identity to 1.10.3 [#23018].
23+
- Renamed token cache from `msal.cache` to `msal.cache.cae` or `masl.cache.nocae`.
24+
* Enabled Continue Access Evalution (CAE) for all Service Principals login methods.
2225
* Adjusted output format to be more user-friendly for `Get-AzContext/Tenant/Subscription` and `Invoke-AzRestMethod`
2326
* Fixed the multiple `x-ms-unique-id` values issue
2427

src/Accounts/AssemblyLoading/ConditionalAssemblyProvider.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ public static void Initialize(string rootPath, IConditionalAssemblyContext conte
4343
// todo: consider moving the list to a standalone config file
4444
#region AssemblyList
4545
CreateAssembly("netcoreapp2.1", "Azure.Core", "1.35.0.0").WithPowerShellCore(),
46-
CreateAssembly("netcoreapp2.1", "Microsoft.Identity.Client", "4.49.1.0").WithPowerShellCore(),
47-
CreateAssembly("netcoreapp3.1", "Microsoft.Identity.Client.Extensions.Msal", "2.23.0.0").WithPowerShellCore(),
4846

49-
CreateAssembly("netstandard2.0", "Azure.Identity", "1.6.1.0"),
50-
CreateAssembly("netstandard2.0", "Azure.Identity.BrokeredAuthentication", "1.0.0.0"),
47+
CreateAssembly("netstandard2.0", "Azure.Identity", "1.10.3.0"),
48+
CreateAssembly("netstandard2.0", "Azure.Identity.Broker", "1.0.0.0"),
5149
CreateAssembly("netstandard2.0", "Microsoft.Bcl.AsyncInterfaces", "1.0.0.0"),
52-
CreateAssembly("netstandard2.0", "Microsoft.Identity.Client.Broker", "4.49.1.0"),
53-
CreateAssembly("netstandard2.0", "Microsoft.Identity.Client.NativeInterop", "0.13.3.0"),
50+
CreateAssembly("netstandard2.0", "Microsoft.Identity.Client", "4.56.0.0"),
51+
CreateAssembly("netstandard2.0", "Microsoft.Identity.Client.Extensions.Msal", "4.56.0.0"),
52+
CreateAssembly("netstandard2.0", "Microsoft.Identity.Client.Broker", "4.56.0.0"),
53+
CreateAssembly("netstandard2.0", "Microsoft.Identity.Client.NativeInterop", "0.13.8.0"),
5454
CreateAssembly("netstandard2.0", "Microsoft.IdentityModel.Abstractions", "6.22.1.0"),
5555
CreateAssembly("netstandard2.0", "System.Memory.Data", "1.0.2.0"),
5656
CreateAssembly("netstandard2.0", "System.Text.Json", "4.0.1.2"),
@@ -65,14 +65,13 @@ public static void Initialize(string rootPath, IConditionalAssemblyContext conte
6565
CreateAssembly("netstandard2.0", "System.Threading.Tasks.Extensions", "4.2.0.1").WithWindowsPowerShell(),
6666

6767
CreateAssembly("netfx", "Azure.Core", "1.35.0.0").WithWindowsPowerShell(),
68-
CreateAssembly("netfx", "Microsoft.Identity.Client", "4.49.1.0").WithWindowsPowerShell(),
69-
CreateAssembly("netfx", "Microsoft.Identity.Client.Extensions.Msal", "2.23.0.0").WithWindowsPowerShell(),
7068
CreateAssembly("netfx", "Newtonsoft.Json", "13.0.0.0").WithWindowsPowerShell(),
7169
CreateAssembly("netfx", "System.Diagnostics.DiagnosticSource", "6.0.0.1").WithWindowsPowerShell(),
7270
CreateAssembly("netfx", "System.Numerics.Vectors", "4.1.4.0").WithWindowsPowerShell(),
7371
CreateAssembly("netfx", "System.Reflection.DispatchProxy", "4.0.4.0").WithWindowsPowerShell(),
7472
CreateAssembly("netfx", "System.Runtime.CompilerServices.Unsafe", "6.0.0.0").WithWindowsPowerShell(),
7573
CreateAssembly("netfx", "System.Security.Cryptography.Cng", "4.3.0.0").WithWindowsPowerShell(),
74+
CreateAssembly("netfx", "System.Security.Cryptography.ProtectedData", "4.5.0.0").WithWindowsPowerShell(),
7675
CreateAssembly("netfx", "System.Text.Encodings.Web", "4.0.5.1").WithWindowsPowerShell(),
7776
CreateAssembly("netfx", "System.Xml.ReaderWriter", "4.1.0.0").WithWindowsPowerShell(),
7877
#endregion

src/Accounts/Authentication/Authentication.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
</PropertyGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="Azure.Identity" Version="1.6.1" />
16-
<PackageReference Include="Azure.Identity.BrokeredAuthentication" Version="1.0.0-beta.3" />
17-
<PackageReference Include="Microsoft.Identity.Client" Version="4.49.1" />
18-
<PackageReference Include="Microsoft.Identity.Client.Extensions.Msal" Version="2.23.0" />
15+
<PackageReference Include="Azure.Identity" Version="1.10.3" />
16+
<PackageReference Include="Azure.Identity.Broker" Version="1.0.0-beta.5" />
17+
<PackageReference Include="Microsoft.Identity.Client" Version="4.56.0" />
18+
<PackageReference Include="Microsoft.Identity.Client.Extensions.Msal" Version="4.56.0" />
1919
</ItemGroup>
2020

2121
<ItemGroup>

src/Accounts/Authentication/Authentication/TokenCache/AdalTokenMigrator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public AdalTokenMigrator(byte[] adalToken, Func<IAzureContextContainer> getConte
3838
ContextContainerInitializer = new Lazy<IAzureContextContainer>(getContextContainer);
3939
}
4040

41-
public void MigrateFromAdalToMsal()
41+
public void MigrateFromAdalToMsal(string tokenCacheFile)
4242
{
4343
MsalCacheHelper cacheHelper = null;
4444
var builder = PublicClientApplicationBuilder.Create(Constants.PowerShellClientId);
@@ -61,7 +61,7 @@ public void MigrateFromAdalToMsal()
6161
if (!HasRegistered)
6262
{
6363
HasRegistered = true;
64-
cacheHelper = MsalCacheHelperProvider.GetCacheHelper();
64+
cacheHelper = MsalCacheHelperProvider.GetCacheHelper(tokenCacheFile);
6565
cacheHelper.RegisterCache(clientApplication.UserTokenCache);
6666
}
6767
}

src/Accounts/Authentication/Authentication/TokenCache/PowerShellTokenCacheProvider.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
using Microsoft.Azure.PowerShell.Common.Config;
2828
using Microsoft.Identity.Client;
2929
using Microsoft.Identity.Client.Broker;
30-
using Microsoft.Rest;
3130

3231
namespace Microsoft.Azure.Commands.Common.Authentication
3332
{
@@ -174,7 +173,7 @@ public virtual IPublicClientApplication CreatePublicClient(string authority = nu
174173
if (AzureSession.Instance.TryGetComponent<IConfigManager>(nameof(IConfigManager), out var config)
175174
&& config.GetConfigValue<bool>(ConfigKeys.EnableLoginByWam))
176175
{
177-
builder = builder.WithBrokerPreview();
176+
builder = builder.WithBroker(new BrokerOptions(BrokerOptions.OperatingSystems.Windows));
178177
}
179178
if (!string.IsNullOrEmpty(authority))
180179
{

src/Accounts/Authentication/Authentication/TokenCache/SharedTokenCacheProvider.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public SharedTokenCacheProvider(byte[] adalTokenCache = null)
3434
AdalTokenCache = adalTokenCache;
3535
TokenCachePersistenceOptions = new TokenCachePersistenceOptions()
3636
{
37-
UnsafeAllowUnencryptedStorage = true
37+
UnsafeAllowUnencryptedStorage = true,
38+
Name = MsalCacheHelperProvider.GetTokenCacheNameWithoutSuffix(AzureSession.Instance.TokenCacheFile)
3839
};
3940
}
4041

@@ -136,7 +137,7 @@ private static MsalCacheHelper GetCacheHelper()
136137

137138
private static MsalCacheHelper CreateCacheHelper()
138139
{
139-
return MsalCacheHelperProvider.GetCacheHelper();
140+
return MsalCacheHelperProvider.GetCacheHelper(AzureSession.Instance.TokenCacheFile);
140141
}
141142

142143
public override TokenCachePersistenceOptions GetTokenCachePersistenceOptions()

src/Accounts/Authentication/AzureSessionInitializer.cs

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,31 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using System;
16-
using System.Diagnostics;
17-
using System.IO;
18-
using System.Linq;
19-
2015
using Hyak.Common;
2116

2217
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
2318
using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Core;
2419
using Microsoft.Azure.Commands.Common.Authentication.Authentication.TokenCache;
25-
using Microsoft.Azure.Commands.Common.Authentication.Factories;
26-
using Microsoft.Azure.Commands.Common.Authentication.Properties;
2720
using Microsoft.Azure.Commands.Common.Authentication.Config;
28-
using Newtonsoft.Json;
21+
using Microsoft.Azure.Commands.Common.Authentication.Factories;
2922
using Microsoft.Azure.Commands.Common.Authentication.Models;
23+
using Microsoft.Azure.Commands.Common.Authentication.Properties;
24+
using Microsoft.Azure.Commands.Common.Authentication.Utilities;
25+
using Microsoft.Identity.Client.Extensions.Msal;
26+
using Microsoft.WindowsAzure.Commands.Common;
27+
using Microsoft.WindowsAzure.Commands.Common.Utilities;
28+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
3029

30+
using Newtonsoft.Json;
3131

32-
using TraceLevel = System.Diagnostics.TraceLevel;
32+
using System;
3333
using System.Collections.Generic;
34+
using System.Diagnostics;
35+
using System.IO;
36+
using System.Linq;
3437
using System.Threading;
35-
using Microsoft.WindowsAzure.Commands.Utilities.Common;
36-
using Microsoft.Azure.Commands.Common.Authentication.Utilities;
37-
using Microsoft.WindowsAzure.Commands.Common.Utilities;
38-
using Microsoft.WindowsAzure.Commands.Common;
38+
39+
using TraceLevel = System.Diagnostics.TraceLevel;
3940

4041
namespace Microsoft.Azure.Commands.Common.Authentication
4142
{
@@ -119,7 +120,7 @@ public static void MigrateAdalCache(IAzureSession session, Func<IAzureContextCon
119120
}
120121

121122
var adalCachePath = Path.Combine(session.ProfileDirectory, "TokenCache.dat");
122-
var msalCachePath = Path.Combine(session.TokenCacheDirectory, "msal.cache");
123+
var msalCachePath = Path.Combine(session.TokenCacheDirectory, session.TokenCacheFile);
123124
var store = session.DataStore;
124125
if (!store.FileExists(adalCachePath) || store.FileExists(msalCachePath))
125126
{
@@ -142,7 +143,7 @@ public static void MigrateAdalCache(IAzureSession session, Func<IAzureContextCon
142143

143144
if (adalData != null && adalData.Length > 0)
144145
{
145-
new AdalTokenMigrator(adalData, getContextContainer).MigrateFromAdalToMsal();
146+
new AdalTokenMigrator(adalData, getContextContainer).MigrateFromAdalToMsal(session.TokenCacheFile);
146147
}
147148
}
148149
catch (Exception e)
@@ -151,6 +152,35 @@ public static void MigrateAdalCache(IAzureSession session, Func<IAzureContextCon
151152
}
152153
}
153154

155+
public static void MigrateMsalCacheWithoutSuffix(IAzureSession session, Action<string> writeWarning)
156+
{
157+
try
158+
{
159+
if (session.ARMContextSaveMode == ContextSaveMode.CurrentUser)
160+
{
161+
var oldMsalCachePath = Path.Combine(MsalCacheHelperProvider.MsalTokenCachePath, MsalCacheHelperProvider.LegacyTokenCacheName);
162+
var newMsalCachePath = Path.Combine(MsalCacheHelperProvider.MsalTokenCachePath, session.TokenCacheFile);
163+
var store = session.DataStore;
164+
if (store.FileExists(oldMsalCachePath) && !store.FileExists(newMsalCachePath))
165+
{
166+
MsalCacheHelperProvider.Reset();
167+
MsalCacheHelper oldCacheHelper = MsalCacheHelperProvider.GetCacheHelper(MsalCacheHelperProvider.LegacyTokenCacheName);
168+
var data = oldCacheHelper.LoadUnencryptedTokenCache();
169+
if (data != null && data.Length > 0)
170+
{
171+
MsalCacheHelperProvider.Reset();
172+
MsalCacheHelper newCacheHelper = MsalCacheHelperProvider.GetCacheHelper(session.TokenCacheFile);
173+
newCacheHelper.SaveUnencryptedTokenCache(data);
174+
}
175+
}
176+
}
177+
}
178+
catch (Exception e)
179+
{
180+
writeWarning(Resources.FailedToMigrateMsalCacheWithLegayName.FormatInvariant(e.Message));
181+
}
182+
}
183+
154184
static ContextAutosaveSettings InitializeSessionSettings(IDataStore store, string profileDirectory, string settingsFile, bool migrated = false)
155185
{
156186
return InitializeSessionSettings(store, profileDirectory, profileDirectory, settingsFile, migrated);
@@ -163,7 +193,7 @@ static ContextAutosaveSettings InitializeSessionSettings(IDataStore store, strin
163193
CacheDirectory = cacheDirectory,
164194
ContextDirectory = profileDirectory,
165195
Mode = ContextSaveMode.Process,
166-
CacheFile = "msal.cache",
196+
CacheFile = MsalCacheHelperProvider.LegacyTokenCacheName,
167197
ContextFile = "AzureRmContext.json"
168198
};
169199

@@ -181,6 +211,7 @@ static ContextAutosaveSettings InitializeSessionSettings(IDataStore store, strin
181211
result.Mode = settings.Mode;
182212
result.ContextFile = settings.ContextFile ?? result.ContextFile;
183213
result.Settings = settings.Settings;
214+
result.KeyStoreFile = settings.KeyStoreFile;
184215
bool updateSettings = false;
185216
if (!settings.Settings.ContainsKey("InstallationId"))
186217
{
@@ -271,8 +302,8 @@ static IAzureSession CreateInstance(IDataStore dataStore = null, Action<string>
271302
session.ARMProfileDirectory = autoSave.ContextDirectory;
272303
session.ARMProfileFile = autoSave.ContextFile;
273304
session.TokenCacheDirectory = autoSave.CacheDirectory;
274-
session.TokenCacheFile = autoSave.CacheFile;
275-
session.KeyStoreFile = "keystore.cache";
305+
session.TokenCacheFile = MsalCacheHelperProvider.GetTokenCacheName(autoSave.CacheFile, true);
306+
session.KeyStoreFile = autoSave.KeyStoreFile ?? "keystore.cache";
276307
autoSave.Settings.TryGetValue("InstallationId", out string installationId);
277308
session.ExtendedProperties.Add("InstallationId", installationId);
278309
InitializeConfigs(session, profilePath, writeWarning);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
//
15+
16+
using System.Threading;
17+
using System.Threading.Tasks;
18+
using Microsoft.Identity.Client;
19+
20+
namespace Microsoft.Azure.PowerShell.Authenticators.Identity
21+
{
22+
internal static class AbstractAcquireTokenParameterBuilderExtensions
23+
{
24+
public static async ValueTask<AuthenticationResult> ExecuteAsync<T>(this AbstractAcquireTokenParameterBuilder<T> builder, bool async, CancellationToken cancellationToken)
25+
where T : AbstractAcquireTokenParameterBuilder<T>
26+
{
27+
Microsoft.Identity.Client.AuthenticationResult result = async
28+
? await builder.ExecuteAsync(cancellationToken).ConfigureAwait(false)
29+
#pragma warning disable AZC0102 // Do not use GetAwaiter().GetResult(). Use the TaskExtensions.EnsureCompleted() extension method instead.
30+
: builder.ExecuteAsync(cancellationToken).GetAwaiter().GetResult();
31+
#pragma warning restore AZC0102 // Do not use GetAwaiter().GetResult(). Use the TaskExtensions.EnsureCompleted() extension method instead.
32+
33+
return result;
34+
}
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// Copyright Microsoft Corporation
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
// ----------------------------------------------------------------------------------
13+
//
14+
using Microsoft.Identity.Client;
15+
16+
namespace Microsoft.Azure.PowerShell.Authenticators.Identity
17+
{
18+
internal class AuthenticationAccount : IAccount
19+
{
20+
private AuthenticationRecord _profile;
21+
22+
internal AuthenticationAccount(AuthenticationRecord profile)
23+
{
24+
_profile = profile;
25+
}
26+
27+
string IAccount.Username => _profile.Username;
28+
29+
string IAccount.Environment => _profile.Authority;
30+
31+
AccountId IAccount.HomeAccountId => _profile.AccountId;
32+
33+
public static explicit operator AuthenticationAccount(AuthenticationRecord profile) => new AuthenticationAccount(profile);
34+
public static explicit operator AuthenticationRecord(AuthenticationAccount account) => account._profile;
35+
}
36+
}

0 commit comments

Comments
 (0)