Skip to content
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
6 changes: 4 additions & 2 deletions src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,13 @@ private void SetContextWithOverwritePrompt(Action<AzureRmProfile, RMProfileClien
}

AzureRmProfile profile = null;
bool? originalShouldRefreshContextsFromCache = null;
try
{
profile = DefaultProfile as AzureRmProfile;
if (profile != null)
{
originalShouldRefreshContextsFromCache = profile.ShouldRefreshContextsFromCache;
profile.ShouldRefreshContextsFromCache = false;
}
if (!CheckForExistingContext(profile, name)
Expand All @@ -435,9 +437,9 @@ private void SetContextWithOverwritePrompt(Action<AzureRmProfile, RMProfileClien
}
finally
{
if(profile != null)
if(profile != null && originalShouldRefreshContextsFromCache.HasValue)
{
profile.ShouldRefreshContextsFromCache = true;
profile.ShouldRefreshContextsFromCache = originalShouldRefreshContextsFromCache.Value;
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/Accounts/Accounts/Context/GetAzureRMContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Collections.ObjectModel;
using Microsoft.Azure.Commands.Profile.Properties;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using Microsoft.Azure.Commands.Common.Authentication;

namespace Microsoft.Azure.Commands.Profile
{
Expand Down Expand Up @@ -63,13 +64,32 @@ protected override IAzureContext DefaultContext
[Parameter(Mandatory =true, ParameterSetName = ListAllParameterSet, HelpMessage ="List all available contexts in the current session.")]
public SwitchParameter ListAvailable { get; set; }

[Parameter(Mandatory = false, ParameterSetName = ListAllParameterSet, HelpMessage = "Refresh contexts from token cache")]
public SwitchParameter RefreshContextFromTokenCache { get; set; }
Copy link
Member

Choose a reason for hiding this comment

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

I just want to confirm that eventually we shall get rid of this parameter. Is that correct?


protected override void BeginProcessing()
{
// Skip BeginProcessing()
}

public override void ExecuteCmdlet()
{
if(ListAvailable.IsPresent && RefreshContextFromTokenCache.IsPresent)
{
try
{
var defaultProfile = DefaultProfile as AzureRmProfile;
if (defaultProfile != null && string.Equals(AzureSession.Instance?.ARMContextSaveMode, "CurrentUser"))
{
defaultProfile.RefreshContextsFromCache();
}
}
catch(Exception e)
{
WriteWarning(e.ToString());
}
}

// If no context is found, return
if (DefaultContext == null && !this.IsParameterBound(c => c.ListAvailable))
{
Expand Down
23 changes: 12 additions & 11 deletions src/Accounts/Authentication.ResourceManager/AzureRmProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class AzureRmProfile : IAzureContextContainer, IProfileOperations

[JsonIgnore]
[XmlIgnore]
public bool ShouldRefreshContextsFromCache { get; set; } = true;
public bool ShouldRefreshContextsFromCache { get; set; } = false;

/// <summary>
/// Gets the path of the profile file.
Expand All @@ -65,6 +65,7 @@ public virtual IAzureContext DefaultContext
{
get
{
//TODO: remove calling RefreshContextsFromCache
if (ShouldRefreshContextsFromCache && AzureSession.Instance != null && AzureSession.Instance.ARMContextSaveMode == "CurrentUser")
{
// If context autosave is enabled, try reading from the cache, updating the contexts, and writing them out
Expand Down Expand Up @@ -267,7 +268,7 @@ public void Save()
/// Writes profile to a specified path.
/// </summary>
/// <param name="path">File path on disk to save profile to</param>
public void Save(string path)
public void Save(string path, bool serializeCache = true)
{
if (string.IsNullOrEmpty(path))
{
Expand All @@ -276,7 +277,7 @@ public void Save(string path)

using (var provider = ProtectedFileProvider.CreateFileProvider(path, FileProtection.ExclusiveWrite))
{
Save(provider);
Save(provider, serializeCache);
}
}

Expand Down Expand Up @@ -675,16 +676,16 @@ private void WriteWarningMessage(string message)
}
}

private void WriteDebugMessage(string message)
private void EnqueueDebugMessage(string message)
{
EventHandler<StreamEventArgs> writeDebugEvent;
if(AzureSession.Instance.TryGetComponent(AzureRMCmdlet.WriteDebugKey, out writeDebugEvent))
EventHandler<StreamEventArgs> enqueueDebugEvent;
if(AzureSession.Instance.TryGetComponent(AzureRMCmdlet.EnqueueDebugKey, out enqueueDebugEvent))
{
writeDebugEvent(this, new StreamEventArgs() { Message = message });
enqueueDebugEvent(this, new StreamEventArgs() { Message = message });
}
}

private void RefreshContextsFromCache()
public void RefreshContextsFromCache()
{
// Authentication factory is already registered in `OnImport()`
AzureSession.Instance.TryGetComponent(
Expand Down Expand Up @@ -775,12 +776,12 @@ private void RefreshContextsFromCache()
{
tokens = authenticationClientFactory.GetTenantTokensForAccount(account, environment, WriteWarningMessage);
}
catch (Exception e)
catch(Exception e)
{
//In SSO scenario, if the account from token cache has multiple tenants, e.g. MSA account, MSAL randomly picks up
//one tenant to ask for token, MSAL will throw exception if MSA home tenant is chosen. The exception is swallowed here as short term fix.
WriteWarningMessage(string.Format(Resources.NoTokenFoundWarning, account.Username));
WriteDebugMessage(e.ToString());
EnqueueDebugMessage(e.ToString());
continue;
}

Expand Down Expand Up @@ -822,7 +823,7 @@ private void RefreshContextsFromCache()
}
}

Save(ProfilePath);
Save(ProfilePath, false);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
<value>Please connect to internet before executing this cmdlet</value>
</data>
<data name="NoTokenFoundWarning" xml:space="preserve">
<value>Failed to get token for account '{0}', please run Connect-AzAccount to login.</value>
<value>Failed to get token for account '{0}', please run Connect-AzAccount to login for {0} if you need to use this account.</value>
</data>
<data name="NullDataStore" xml:space="preserve">
<value>A valid implementation of IDataStore must be provided.</value>
Expand Down
32 changes: 16 additions & 16 deletions tools/Common.Netcore.Dependencies.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
<ItemGroup >
<PackageReference Include="Microsoft.Rest.ClientRuntime" Version="2.3.20"/>
<PackageReference Include="Microsoft.Rest.ClientRuntime.Azure" Version="3.3.19"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Aks" Version="1.0.44-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Authentication.Abstractions" Version="1.0.44-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Authorization" Version="1.0.44-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Common" Version="1.0.44-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Compute" Version="1.0.44-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Graph.Rbac" Version="1.0.44-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.KeyVault" Version="1.0.44-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Monitor" Version="1.0.44-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Network" Version="1.0.44-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.PolicyInsights" Version="1.0.44-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.ResourceManager" Version="1.0.44-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Storage" Version="1.0.44-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Storage.Management" Version="1.0.44-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Strategies" Version="1.0.44-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Websites" Version="1.0.44-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Aks" Version="1.0.46-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Authentication.Abstractions" Version="1.0.46-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Authorization" Version="1.0.46-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Common" Version="1.0.46-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Compute" Version="1.0.46-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Graph.Rbac" Version="1.0.46-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.KeyVault" Version="1.0.46-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Monitor" Version="1.0.46-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Network" Version="1.0.46-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.PolicyInsights" Version="1.0.46-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.ResourceManager" Version="1.0.46-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Storage" Version="1.0.46-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Storage.Management" Version="1.0.46-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Strategies" Version="1.0.46-preview"/>
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Websites" Version="1.0.46-preview"/>
</ItemGroup>
<ItemGroup Condition="'$(IsTestProject)' != 'true'">
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.4.0">
Expand All @@ -32,7 +32,7 @@
<PackageReference Include="PowerShellStandard.Library" Version="5.1.0" PrivateAssets="All" />
</ItemGroup>
<PropertyGroup>
<StorageToolsPath>$(NugetPackageRoot)microsoft.azure.powershell.storage\1.0.44-preview\tools\</StorageToolsPath>
<StorageToolsPath>$(NugetPackageRoot)microsoft.azure.powershell.storage\1.0.46-preview\tools\</StorageToolsPath>
</PropertyGroup>
<ItemGroup Condition="'$(OmitJsonPackage)' != 'true'">
<PackageReference Include="Newtonsoft.Json" Version="10.0.3"/>
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.