Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Importing Az in Parallel #18483

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ public void OnImport()
try
{
#endif
AzureSessionInitializer.InitializeAzureSession();
AzureSessionInitializer.InitializeAzureSession(WriteInitializationWarnings);
AzureSessionInitializer.MigrateAdalCache(AzureSession.Instance, GetAzureContextContainer, WriteInitializationWarnings);
#if DEBUG
if (!TestMockSupport.RunningMocked)
Expand Down
5 changes: 3 additions & 2 deletions src/Accounts/Accounts/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
-->

## Upcoming Release
* Fixed incorrect access token [#18105]
* Supported exporting and importing configurations by `Export-AzConfig` and `Import-AzConfig`.
* Upgraded version of Microsoft.Identity.Client for .NET Framework [#18495]
* Fixed an issue that Az.Accounts may fail to be imported in parallel PowerShell processes. [#18321]
* Fixed incorrect access token [#18105]
* Upgraded version of Microsoft.Identity.Client for .NET Framework. [#18495]
* Fixed an issue that Az.Accounts failed to be imported if multiple environment variables, which only differ by case, are set. [#18304]

## Version 2.8.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.Common.Exceptions;
using Microsoft.Azure.Commands.Common.Authentication.Config;
using Microsoft.Azure.Commands.Common.Exceptions;
using Microsoft.Azure.PowerShell.Common.Config;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Xunit;
using System.Linq;
using Xunit;

namespace Microsoft.Azure.Authentication.Test.Config
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,21 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.Common.Exceptions;
using Microsoft.Azure.Commands.Common.Authentication.Config;
using Microsoft.Azure.Commands.Common.Exceptions;
using Microsoft.Azure.PowerShell.Common.Config;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using Microsoft.WindowsAzure.Commands.Common;
using System;
using System.Linq;
using Xunit;
using Microsoft.Azure.PowerShell.Common.Config;

namespace Microsoft.Azure.Authentication.Test.Config
{
public class ConfigDefinitionTests : ConfigTestsBase
{
[Fact]
[Trait(TestTraits.AcceptanceType, TestTraits.CheckIn)]
public void CanValidateInput() {
public void CanValidateInput()
{
const string boolKey = "BoolKey";
var boolConfig = new SimpleTypedConfig<bool>(boolKey, "", false);
var rangedIntConfig = new RangedConfig();
Expand Down
17 changes: 10 additions & 7 deletions src/Accounts/Authentication.Test/ConfigTests/ConfigTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,19 @@ public class ConfigTestsBase
/// <returns>A config manager with initial state, ready to use.</returns>
internal IConfigManager GetConfigManager(InitSettings settings, params ConfigDefinition[] config)
{
var dataStore = settings?.DataStore ?? new MockDataStore();
var configPath = settings?.Path ?? Path.GetRandomFileName();
var dataStore = settings?.DataStore;
if (dataStore == null)
{
dataStore = new MockDataStore();
}
Comment on lines +49 to +53
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why previous code didn't work? var dataStore = settings?.DataStore ?? new MockDataStore();

if (!dataStore.FileExists(configPath))
{
dataStore.WriteFile(configPath, @"{}");
}
var environmentVariableProvider = settings?.EnvironmentVariableProvider ?? new MockEnvironmentVariableProvider();

ConfigInitializer ci = new ConfigInitializer(new List<string>() { configPath })
{
DataStore = dataStore,
EnvironmentVariableProvider = environmentVariableProvider
};
IConfigManager icm = ci.GetConfigManager();
IConfigManager icm = new DefaultConfigManager(configPath, dataStore, environmentVariableProvider);
foreach (var configDefinition in config)
{
icm.RegisterConfig(configDefinition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.Common.Exceptions;
using Microsoft.Azure.Commands.Common.Authentication.Config;
using Microsoft.Azure.Commands.Common.Exceptions;
using Microsoft.Azure.PowerShell.Common.Config;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using System.Linq;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.Common.Exceptions;
using Microsoft.Azure.Commands.Common.Authentication.Config;
using Microsoft.Azure.Commands.Common.Exceptions;
using Microsoft.Azure.PowerShell.Common.Config;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using System;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.Common.Exceptions;
using Microsoft.Azure.Commands.Common.Authentication.Config;
using Microsoft.Azure.Commands.Common.Exceptions;
using Microsoft.Azure.PowerShell.Common.Config;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using Moq;
Expand Down
46 changes: 38 additions & 8 deletions src/Accounts/Authentication/AzureSessionInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

using TraceLevel = System.Diagnostics.TraceLevel;
using System.Collections.Generic;
using System.Threading;

namespace Microsoft.Azure.Commands.Common.Authentication
{
Expand All @@ -43,9 +44,9 @@ public static class AzureSessionInitializer
/// <summary>
/// Initialize the azure session if it is not already initialized
/// </summary>
public static void InitializeAzureSession()
public static void InitializeAzureSession(Action<string> writeWarning = null)
{
AzureSession.Initialize(() => CreateInstance());
AzureSession.Initialize(() => CreateInstance(null, writeWarning));
}

/// <summary>
Expand Down Expand Up @@ -137,7 +138,7 @@ public static void MigrateAdalCache(IAzureSession session, Func<IAzureContextCon
new AdalTokenMigrator(adalData, getContextContainer).MigrateFromAdalToMsal();
}
}
catch(Exception e)
catch (Exception e)
{
writeWarning(Resources.FailedToMigrateAdal2Msal.FormatInvariant(e.Message));
}
Expand Down Expand Up @@ -206,7 +207,7 @@ static void InitializeDataCollection(IAzureSession session)
session.RegisterComponent(DataCollectionController.RegistryKey, () => DataCollectionController.Create(session));
}

static IAzureSession CreateInstance(IDataStore dataStore = null)
static IAzureSession CreateInstance(IDataStore dataStore = null, Action<string> writeWarning = null)
{
string profilePath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
Expand Down Expand Up @@ -239,23 +240,52 @@ static IAzureSession CreateInstance(IDataStore dataStore = null)
session.TokenCacheDirectory = autoSave.CacheDirectory;
session.TokenCacheFile = autoSave.CacheFile;

InitializeConfigs(session, profilePath);
InitializeConfigs(session, profilePath, writeWarning);
InitializeDataCollection(session);
session.RegisterComponent(HttpClientOperationsFactory.Name, () => HttpClientOperationsFactory.Create());
session.TokenCache = session.TokenCache ?? new AzureTokenCache();
return session;
}

private static void InitializeConfigs(AzureSession session, string profilePath)
private static void InitializeConfigs(AzureSession session, string profilePath, Action<string> writeWarning)
{
if (writeWarning == null) { writeWarning = (string s) => { }; };
var fallbackList = new List<string>()
{
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".Azure", "PSConfig.json"),
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), ".Azure", "PSConfig.json")
};
ConfigInitializer configInitializer = new ConfigInitializer(fallbackList);
configInitializer.MigrateConfigs(profilePath);
configInitializer.InitializeForAzureSession(session);

using (var mutex = new Mutex(false, @"Global\AzurePowerShellConfigInit"))
{
if (mutex.WaitOne(1000))
{
// regular initialization
try
{
configInitializer.MigrateConfigs(profilePath);
configInitializer.Initialize(session);
return; // done, return.
}
catch (Exception ex)
{
writeWarning($"[AzureSessionInitializer] Failed to initialize the configs, reason: {ex.Message}.");
}
finally
{
mutex.ReleaseMutex();
}
}
else
{
writeWarning($"[AzureSessionInitializer] Timed out when initializing the configs.");
}

// initialize in safe mode and do not try to migrate configs
writeWarning($"[AzureSessionInitializer] Config manager will be re-initialized in safe mode. All configs will have only default values.");
configInitializer.SafeInitialize(session);
}
}

public class AdalSession : AzureSession
Expand Down
82 changes: 46 additions & 36 deletions src/Accounts/Authentication/Config/ConfigInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ namespace Microsoft.Azure.Commands.Common.Authentication.Config
internal class ConfigInitializer
{
internal IDataStore DataStore { get; set; } = new DiskDataStore();
private static readonly object _fsLock = new object();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lock is no longer needed because the initialization is guarded by a mutex


internal IEnvironmentVariableProvider EnvironmentVariableProvider { get; set; } = new DefaultEnvironmentVariableProvider();

public string ConfigPath
private string ConfigPath
{
get
{
Expand All @@ -47,6 +46,10 @@ public string ConfigPath
}
return _cachedConfigPath;
}
set
{
_cachedConfigPath = value;
}
}
private string _cachedConfigPath;
private readonly IEnumerable<string> _configPathCandidates;
Expand Down Expand Up @@ -87,16 +90,7 @@ private string GetPathToConfigFile(IEnumerable<string> paths)
continue;
}
}
throw new ApplicationException($"Failed to store the config file. Please make sure any one of the following paths is accessible: {string.Join(", ", paths)}");
}

internal IConfigManager GetConfigManager()
{
lock (_fsLock)
{
ValidateConfigFile();
}
return new ConfigManager(ConfigPath, DataStore, EnvironmentVariableProvider);
throw new ApplicationException($"Failed to create the config file. Please make sure any one of the following paths is accessible: {string.Join(", ", paths)}");
}

private void ValidateConfigFile()
Expand All @@ -122,46 +116,62 @@ private void ResetConfigFileToDefault()
}
}

// todo: tests initializes configs in a different way. Maybe there should be an abstraction IConfigInitializer and two concrete classes ConfigInitializer + TestConfigInitializer
internal void InitializeForAzureSession(AzureSession session)
/// <summary>
/// Initializes the config manager and registers it to <see cref="AzureSession"/>.
/// </summary>
/// <param name="session"></param>
public void Initialize(IAzureSession session)
{
ValidateConfigFile();
Initialize(session, new DefaultConfigManager(ConfigPath, DataStore, EnvironmentVariableProvider));
}

private void Initialize(IAzureSession session, IConfigManager configManager)
{
IConfigManager configManager = GetConfigManager();
session.RegisterComponent(nameof(IConfigManager), () => configManager);
session.RegisterComponent(nameof(IConfigManager), () => configManager, true);
RegisterConfigs(configManager);
configManager.BuildConfig();
}

/// <summary>
/// Initializes the config manager with limited capabilities but in a safe manner.
/// Registers it to <see cref="AzureSession"/>.
/// </summary>
/// <param name="session"></param>
public void SafeInitialize(IAzureSession session)
{
ConfigPath = "";
Initialize(session, new SafeConfigManager());
}

/// <summary>
/// Migrates independent configs to the new config framework.
/// </summary>
/// <param name="profilePath">Path of session profile where old config files are stored.</param>
internal void MigrateConfigs(string profilePath)
{
lock (_fsLock)
// Migrate data collection config
string dataCollectionConfigPath = Path.Combine(profilePath, AzurePSDataCollectionProfile.DefaultFileName);
const string legacyConfigKey = "enableAzureDataCollection";
// Migrate only when:
// 1. Old config file exists
// 2. New config file does not exist
if (DataStore.FileExists(dataCollectionConfigPath) && _configPathCandidates.All(path => !DataStore.FileExists(path)))
{
// Migrate data collection config
string dataCollectionConfigPath = Path.Combine(profilePath, AzurePSDataCollectionProfile.DefaultFileName);
const string legacyConfigKey = "enableAzureDataCollection";
// Migrate only when:
// 1. Old config file exists
// 2. New config file does not exist
if (DataStore.FileExists(dataCollectionConfigPath) && _configPathCandidates.All(path => !DataStore.FileExists(path)))
try
{
try
string json = DataStore.ReadFileAsText(dataCollectionConfigPath);
JObject root = JObject.Parse(json);
if (root.TryGetValue(legacyConfigKey, out JToken jToken))
{
string json = DataStore.ReadFileAsText(dataCollectionConfigPath);
JObject root = JObject.Parse(json);
if (root.TryGetValue(legacyConfigKey, out JToken jToken))
{
bool enabled = ((bool)jToken);
new JsonConfigHelper(ConfigPath, DataStore).Update(ConfigPathHelper.GetPathOfConfig(ConfigKeys.EnableDataCollection), enabled);
}
}
catch (Exception)
{
// do not throw for file IO exceptions
bool enabled = ((bool)jToken);
new JsonConfigHelper(ConfigPath, DataStore).Update(ConfigPathHelper.GetPathOfConfig(ConfigKeys.EnableDataCollection), enabled);
}
}
catch (Exception)
{
// do not throw for file IO exceptions
}
}
}

Expand Down
Loading