diff --git a/src/Accounts/Accounts.Test/AzureRMProfileTestsForMultitenant.cs b/src/Accounts/Accounts.Test/AzureRMProfileTestsForMultitenant.cs index baadaae82d36..3570ac4be80c 100644 --- a/src/Accounts/Accounts.Test/AzureRMProfileTestsForMultitenant.cs +++ b/src/Accounts/Accounts.Test/AzureRMProfileTestsForMultitenant.cs @@ -318,7 +318,8 @@ public void LoginByTenant() null, false, mockOpenIDConfig.Object, - null); + null, + IsInteractiveContextSelectionEnabled: false); Assert.Equal("2021-01-01", client.SubscriptionAndTenantClient.ApiVersion); Assert.Equal(tenantA, azureRmProfile.DefaultContext.Tenant.Id.ToString()); diff --git a/src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs b/src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs index 59dcacf9536a..bad56c2f73ca 100644 --- a/src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs +++ b/src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs @@ -538,7 +538,7 @@ public override void ExecuteCmdlet() } profileClient.WarningLog = (message) => _tasks.Enqueue(new Task(() => this.WriteWarning(message))); - profileClient.InformationLog = (message) => _tasks.Enqueue(new Task(() => WriteInteractiveInformation(message))); + profileClient.InteractiveInformationLog = (message) => _tasks.Enqueue(new Task(() => WriteInteractiveInformation(message))); profileClient.DebugLog = (message) => _tasks.Enqueue(new Task(() => this.WriteDebugWithTimestamp(message))); profileClient.PromptAndReadLine = (message) => { @@ -561,7 +561,6 @@ public override void ExecuteCmdlet() shouldPopulateContextList, MaxContextPopulation, resourceId, - IsInteractiveAuthenticationFlow(), IsInteractiveContextSelectionEnabled())); task.Start(); while (!task.IsCompleted) @@ -614,11 +613,6 @@ private bool IsInteractiveContextSelectionEnabled() return AzureSession.Instance.TryGetComponent(nameof(IConfigManager), out IConfigManager configManager) ? configManager.GetConfigValue(ConfigKeys.LoginExperienceV2).Equals(LoginExperienceConfig.On) : true; } - private bool IsInteractiveAuthenticationFlow() - { - return ParameterSetName.Equals(UserParameterSet); - } - private bool IsPopUpInteractiveAuthenticationFlow() { return ParameterSetName.Equals(UserParameterSet) && UseDeviceAuthentication.IsPresent == false; @@ -634,7 +628,7 @@ private void ValidateActionRequiredMessageCanBePresented() private void WriteInteractiveInformation(string message) { - if (IsInteractiveAuthenticationFlow()) + if (ParameterSetName.Equals(UserParameterSet)) { this.WriteInformation(message, false); } diff --git a/src/Accounts/Accounts/ChangeLog.md b/src/Accounts/Accounts/ChangeLog.md index 293484b5fc4c..2d6145d492c2 100644 --- a/src/Accounts/Accounts/ChangeLog.md +++ b/src/Accounts/Accounts/ChangeLog.md @@ -19,6 +19,7 @@ --> ## Upcoming Release +* Removed the informational table about selected context to avoid duplication with output table. ## Version 3.0.2 * Fixed bug handling GUID type subscription Id. diff --git a/src/Accounts/Accounts/Models/RMProfileClient.cs b/src/Accounts/Accounts/Models/RMProfileClient.cs index e32c1c096fe8..285d323d7e47 100644 --- a/src/Accounts/Accounts/Models/RMProfileClient.cs +++ b/src/Accounts/Accounts/Models/RMProfileClient.cs @@ -11,17 +11,13 @@ // See the License for the specific language governing permissions and // limitations under the License. // ---------------------------------------------------------------------------------- -using Microsoft.Azure.Commands.Common; using Microsoft.Azure.Commands.Common.Authentication; using Microsoft.Azure.Commands.Common.Authentication.Abstractions; using Microsoft.Azure.Commands.Common.Authentication.Models; using Microsoft.Azure.Commands.Common.Authentication.ResourceManager; -using Microsoft.Azure.Commands.Common.Exceptions; using Microsoft.Azure.Commands.Profile.Models; using Microsoft.Azure.Commands.Profile.Properties; using Microsoft.Azure.Commands.Profile.Utilities; -using Microsoft.Azure.Management.Profiles.Storage.Version2019_06_01.Models; -using Microsoft.Identity.Client.NativeInterop; using Microsoft.Rest.Azure; using Microsoft.WindowsAzure.Commands.Common; @@ -29,10 +25,7 @@ using System.Collections.Generic; using System.Linq; using System.Management.Automation; -using System.Runtime.InteropServices; using System.Security; -using System.Security.Authentication.ExtendedProtection; -using System.Text; using AuthenticationMessages = Microsoft.Azure.Commands.Common.Authentication.Properties.Resources; using ProfileMessages = Microsoft.Azure.Commands.Profile.Properties.Resources; @@ -46,7 +39,7 @@ public class RMProfileClient private IAzureTokenCache _cache; public Action WarningLog; public Action DebugLog; - public Action InformationLog; + public Action InteractiveInformationLog; internal Func PromptAndReadLine; private List _queriedTenants = new List(); @@ -137,10 +130,10 @@ public AzureRmProfile Login( bool shouldPopulateContextList = true, int maxContextPopulation = Profile.ConnectAzureRmAccountCommand.DefaultMaxContextPopulation, string authScope = null, - bool isInteractiveAuthenticationFlow = false, bool IsInteractiveContextSelectionEnabled = true) { - if(isInteractiveAuthenticationFlow) WriteInformationMessage($"{PSStyle.ForegroundColor.BrightYellow}{Resources.PleaseSelectAccount}{PSStyle.Reset}{System.Environment.NewLine}"); + + WriteInteractiveInformationMessage($"{PSStyle.ForegroundColor.BrightYellow}{Resources.PleaseSelectAccount}{PSStyle.Reset}{System.Environment.NewLine}"); IAzureSubscription defaultSubscription = null; IAzureTenant defaultTenant = null; @@ -148,7 +141,10 @@ public AzureRmProfile Login( List tempSubscriptions = null; string tenantName = null; - bool selectSubscriptionFromList = isInteractiveAuthenticationFlow && IsInteractiveContextSelectionEnabled && string.IsNullOrEmpty(subscriptionId) && string.IsNullOrEmpty(subscriptionName); + bool selectSubscriptionFromList = AzureAccount.AccountType.User.Equals(account.Type) && + IsInteractiveContextSelectionEnabled && + string.IsNullOrEmpty(subscriptionId) && + string.IsNullOrEmpty(subscriptionName); var lastUsedSubscription = selectSubscriptionFromList ? _profile?.DefaultContext?.Subscription : null; string promptBehavior = @@ -242,7 +238,7 @@ public AzureRmProfile Login( throw new ArgumentNullException(string.Format($"{e.Message}{Environment.NewLine}{baseMessage} {typeMessage}"), e); } - WriteInformationMessage(Resources.RetrievingSubscription); + WriteInteractiveInformationMessage(Resources.RetrievingSubscription); tempSubscriptions = null; if (TryGetTenantSubscription( token, @@ -253,8 +249,7 @@ public AzureRmProfile Login( true, out defaultSubscription, out defaultTenant, - out tempSubscriptions, - isInteractiveAuthenticationFlow)) + out tempSubscriptions)) { account.SetOrAppendProperty(AzureAccount.Property.Tenants, new[] { defaultTenant.Id.ToString() }); @@ -267,7 +262,7 @@ public AzureRmProfile Login( { InteractiveSubscriptionSelectionHelper.SelectSubscriptionFromList( subscriptions, _queriedTenants, tenantIdOrName, tenantName, lastUsedSubscription, - Prompt, WriteInformationMessage, + Prompt, WriteInteractiveInformationMessage, ref defaultSubscription, ref defaultTenant); } } @@ -283,7 +278,7 @@ public AzureRmProfile Login( IAzureSubscription tempSubscription = null; tempSubscriptions = null; - WriteInformationMessage(Resources.RetrievingSubscription); + WriteInteractiveInformationMessage(Resources.RetrievingSubscription); foreach (var tenant in _queriedTenants) { @@ -323,7 +318,7 @@ public AzureRmProfile Login( if (token != null && (defaultTenant == null || selectSubscriptionFromList) && - TryGetTenantSubscription(token, account, environment, subscriptionId, subscriptionName, false, out tempSubscription, out tempTenant, out tempSubscriptions, isInteractiveAuthenticationFlow)) + TryGetTenantSubscription(token, account, environment, subscriptionId, subscriptionName, false, out tempSubscription, out tempTenant, out tempSubscriptions)) { // If no subscription found for the given token/tenant,discard tempTenant value. // Continue to look for matched subscripitons until one subscription retrived by its home tenant is found. @@ -350,7 +345,7 @@ public AzureRmProfile Login( { InteractiveSubscriptionSelectionHelper.SelectSubscriptionFromList( subscriptions, _queriedTenants, tenantIdOrName, tenantName, lastUsedSubscription, - Prompt, WriteInformationMessage, + Prompt, WriteInteractiveInformationMessage, ref defaultSubscription, ref defaultTenant); } } @@ -385,7 +380,7 @@ public AzureRmProfile Login( WriteWarningMessage(string.Format(ProfileMessages.CannotSetDefaultContext, defaultContext.ToString())); } - if (!skipValidation && !defaultSubscription.State.Equals("Enabled", StringComparison.OrdinalIgnoreCase)) + if (!skipValidation && null != defaultSubscription.State && !defaultSubscription.State.Equals("Enabled", StringComparison.OrdinalIgnoreCase)) { WriteWarningMessage(string.Format( ProfileMessages.SelectedSubscriptionNotActive, @@ -707,9 +702,9 @@ private bool TryGetTenantSubscription(IAccessToken accessToken, bool isTenantPresent, out IAzureSubscription subscription, out IAzureTenant tenant, - out List subscriptions, - bool isInteractiveAuthentication) + out List subscriptions) { + bool isInteractiveAuthentication = AzureAccount.AccountType.User.Equals(account.Type); subscriptions = new List(); subscription = null; if (accessToken != null) @@ -881,11 +876,11 @@ private void WriteDebugMessage(string message) DebugLog(message); } } - private void WriteInformationMessage(string message) + private void WriteInteractiveInformationMessage(string message) { - if (InformationLog != null) + if (InteractiveInformationLog != null) { - InformationLog(message); + InteractiveInformationLog(message); } } diff --git a/src/Accounts/Accounts/Utilities/InteractiveSubscriptionSelectionHelper.cs b/src/Accounts/Accounts/Utilities/InteractiveSubscriptionSelectionHelper.cs index 05d745ce5e34..a01f19fea050 100644 --- a/src/Accounts/Accounts/Utilities/InteractiveSubscriptionSelectionHelper.cs +++ b/src/Accounts/Accounts/Utilities/InteractiveSubscriptionSelectionHelper.cs @@ -76,9 +76,6 @@ internal static void SelectSubscriptionFromList(IEnumerable { defaultTenant.ExtendedProperties.Add(AzureTenant.Property.DisplayName, tenantName); } - WriteSelectedSubscriptionTable(defaultSubscription?.Name ?? defaultSubscription?.Id, - defaultTenant?.GetProperty(AzureTenant.Property.DisplayName) ?? tenantName ?? defaultTenant?.Id, - outputAction, columnSubNameWidth, columnTenantWidth, columnIndentsWidth); } catch (ArgumentOutOfRangeException) { @@ -149,26 +146,6 @@ private static void WriteSubscriptionSelectionTableRow(int subIndex, IAzureSubsc } - private static void WriteSelectedSubscriptionTable(string subscription, string tenant, - Action outputAction, int columnSubNameWidth, int columnTenantWidth, int columnIndentsWidth) - { - string columnSubNameTab = "Subscription name", columnTenantTab = "Tenant name"; - string separator = "-", - ColumSubNameSeparator = new StringBuilder().Insert(0, separator, columnSubNameWidth).ToString(), - ColumTenantSeparator = new StringBuilder().Insert(0, separator, columnTenantWidth).ToString(); - - outputAction(""); - outputAction($"{String.Format($"{{0,-{columnSubNameWidth + columnIndentsWidth}}}", columnSubNameTab)}" + - $"{String.Format($"{{0,-{columnTenantWidth + columnIndentsWidth}}}", columnTenantTab)}"); - outputAction($"{String.Format($"{{0,-{columnSubNameWidth + columnIndentsWidth}}}", ColumSubNameSeparator)}" + - $"{String.Format($"{{0,-{columnTenantWidth}}}", ColumTenantSeparator)}"); - string truncatedSubName = subscription?.Length > columnSubNameWidth ? $"{subscription.Substring(0, columnSubNameWidth - 3)}..." : subscription; - string truncatedTenantName = tenant?.Length > columnTenantWidth ? $"{tenant.Substring(0, columnTenantWidth - 3)}..." : tenant; - string subNameRowValue = String.Format($"{{0,-{columnSubNameWidth + columnIndentsWidth}}}", truncatedSubName), - tenantDomainNameRowValue = String.Format($"{{0,-{columnTenantWidth}}}", truncatedTenantName); - outputAction($"{subNameRowValue}{tenantDomainNameRowValue}"); - } - public static IAzureTenant GetDetailedTenantFromQueryHistory(List queriedTenants, string tenantId) { return queriedTenants?.Where(t => t.Id.Equals(tenantId, StringComparison.OrdinalIgnoreCase))?.FirstOrDefault();