Skip to content

Commit

Permalink
Added 3 configs to config framework (#18162)
Browse files Browse the repository at this point in the history
* add configs to config framework

* use signed common build

* Fix configs set on process scope get lost

* Get-AzConfig supports filter by Scope

* use official common build v1.3.58
  • Loading branch information
isra-fel authored May 17, 2022
1 parent 38d92a8 commit 5ea071f
Show file tree
Hide file tree
Showing 48 changed files with 979 additions and 314 deletions.
34 changes: 22 additions & 12 deletions src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
using Microsoft.Azure.Commands.Profile.Properties;
using Microsoft.Azure.Commands.ResourceManager.Common;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Commands.Shared.Config;
using Microsoft.Azure.PowerShell.Authenticators;
using Microsoft.Azure.PowerShell.Authenticators.Factories;
using Microsoft.Azure.PowerShell.Common.Config;
using Microsoft.Identity.Client;
using Microsoft.WindowsAzure.Commands.Common;
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
Expand Down Expand Up @@ -316,11 +318,27 @@ public override void ExecuteCmdlet()
}

}
else if (AzureSession.Instance.TryGetComponent<IConfigManager>(nameof(IConfigManager), out var configManager))
{
string subscriptionFromConfig = configManager.GetConfigValue<string>(ConfigKeys.DefaultSubscriptionForLogin);
if (!string.IsNullOrEmpty(subscriptionFromConfig))
{
// user doesn't specify subscript; but DefaultSubscriptionForLogin is found in config
WriteDebugWithTimestamp($"[ConnectAzureRmAccountCommand] Using default subscription \"{subscriptionFromConfig}\" from config.");
if (Guid.TryParse(subscriptionFromConfig, out subscriptionIdGuid))
{
subscriptionId = subscriptionFromConfig;
}
else
{
subscriptionName = subscriptionFromConfig;
}
}
}

if(ClientAssertionParameterSet.Equals(ParameterSetName, StringComparison.OrdinalIgnoreCase))
{
string suppressWarningOrErrorValue = System.Environment.GetEnvironmentVariable(BreakingChangeAttributeHelper.SUPPRESS_ERROR_OR_WARNING_MESSAGE_ENV_VARIABLE_NAME);
bool.TryParse(suppressWarningOrErrorValue, out bool suppressWarningOrError);
bool suppressWarningOrError = AzureSession.Instance.TryGetComponent<IConfigManager>(nameof(IConfigManager), out var configManager) && configManager.GetConfigValue<bool>(ConfigKeys.DisplayBreakingChangeWarning);
if (!suppressWarningOrError)
{
WriteWarning("The feature related to parameter name 'FederatedToken' is under preview.");
Expand Down Expand Up @@ -416,16 +434,8 @@ public override void ExecuteCmdlet()
&& SendCertificateChain)
{
azureAccount.SetProperty(AzureAccount.Property.SendCertificateChain, SendCertificateChain.ToString());
bool supressWarningOrError = false;
try
{
supressWarningOrError = bool.Parse(System.Environment.GetEnvironmentVariable(BreakingChangeAttributeHelper.SUPPRESS_ERROR_OR_WARNING_MESSAGE_ENV_VARIABLE_NAME));
}
catch
{
//if value of env variable is invalid, use default value of supressWarningOrError
}
if (!supressWarningOrError)
bool suppressWarningOrError = AzureSession.Instance.TryGetComponent<IConfigManager>(nameof(IConfigManager), out var configManager) && configManager.GetConfigValue<bool>(ConfigKeys.DisplayBreakingChangeWarning);
if (!suppressWarningOrError)
{
WriteWarning(Resources.PreviewFunctionMessage);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Accounts/Accounts/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
-->

## Upcoming Release
* Added a preview feature allowing user to control the configurations of Azure PowerShell by using the following cmdlets:
- `Get-AzConfig`
- `Update-AzConfig`
- `Clear-AzConfig`
* Added a preview feature allowing user to control the following configurations by using `Get-AzConfig`, `Update-AzConfig` and `Clear-AzConfig`:
- `DefaultSubscriptionForLogin`: Subscription name or GUID. Sets the default context for Azure PowerShell when logging in without specifying a subscription.
- `DisplayBreakingChangeWarning`: Controls if warning messages for breaking changes are displayed or suppressed.
- `EnableDataCollection`: When enabled, Azure PowerShell cmdlets send telemetry data to Microsoft to improve the customer experience.
* Upgraded System.Reflection.DispatchProxy on Windows PowerShell [#17856]
* Upgraded Azure.Identity to 1.6.0 and Azure.Core to 1.24.0

Expand Down
2 changes: 1 addition & 1 deletion src/Accounts/Accounts/CommonModule/TelemetryProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private static AzurePSDataCollectionProfile CreateDataCollectionProfile(Action<s
}

warningLogger(Resources.DataCollectionEnabledWarning);
return new AzurePSDataCollectionProfile(true);
return new AzurePSDataCollectionProfile();
}

public void Dispose()
Expand Down
24 changes: 16 additions & 8 deletions src/Accounts/Accounts/Config/ClearConfigCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.Common.Exceptions;
using Microsoft.Azure.PowerShell.Common.Config;
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
Expand All @@ -23,21 +24,19 @@

namespace Microsoft.Azure.Commands.Common.Authentication.Config
{
[Cmdlet("Clear", "AzConfig", SupportsShouldProcess = true)]
[Cmdlet("Clear", "AzConfig", SupportsShouldProcess = true, DefaultParameterSetName = ClearAll)]
[OutputType(typeof(bool))]
[CmdletPreview(PreviewMessage)]
public class ClearConfigCommand : ConfigCommandBase, IDynamicParameters
{
private const string ClearByKey = "ClearByKey";
private const string ClearAll = "ClearAll";

private const string ProcessMessage = "Clear the configs that apply to \"{0}\" by the following keys: {1}.";
private const string ProcessMessage = "Clear the configs that apply to {0} by the following keys: {1}.";

private string ContinueMessage => $"Clear all the configs that apply to \"{AppliesTo}\" in scope {Scope}?";
private string ProcessTarget => $"Configs in scope {Scope}";
private string ContinueMessageForClearAll => $"Clear all the configs that apply to {AppliesTo ?? "all the modules and cmdlets"} in scope {Scope}.";

[Parameter(ParameterSetName = ClearAll, Mandatory = true, HelpMessage = "Clear all configs.")]
public SwitchParameter All { get; set; }
private string ProcessTarget => $"{Scope} scope";

[Parameter(ParameterSetName = ClearAll, HelpMessage = "Do not ask for confirmation when clearing all configs.")]
public SwitchParameter Force { get; set; }
Expand All @@ -59,6 +58,15 @@ public class ClearConfigCommand : ConfigCommandBase, IDynamicParameters
}));
}

protected override void ValidateParameters()
{
base.ValidateParameters();
if (Scope != ConfigScope.Process && Scope != ConfigScope.CurrentUser)
{
throw new AzPSArgumentException($"When clearing configs, {nameof(Scope)} must be either {ConfigScope.Process} or {ConfigScope.CurrentUser}", nameof(Scope));
}
}

public override void ExecuteCmdlet()
{
switch (ParameterSetName)
Expand Down Expand Up @@ -87,7 +95,7 @@ private void ClearConfigByKey()
return;
}
base.ConfirmAction(
string.Format(ProcessMessage, AppliesTo, string.Join(", ", configKeysFromInput)),
string.Format(ProcessMessage, AppliesTo ?? "all the modules and cmdlets", string.Join(", ", configKeysFromInput)),
ProcessTarget,
() => configKeysFromInput.ForEach(ClearConfigByKey));
}
Expand All @@ -102,7 +110,7 @@ private void ClearConfigByKey(string key)

private void ClearAllConfigs()
{
ConfirmAction(Force, ContinueMessage, ContinueMessage, ProcessTarget, () =>
ConfirmAction(Force, ContinueMessageForClearAll, ContinueMessageForClearAll, ProcessTarget, () =>
{
ConfigManager.ClearConfig(new ClearConfigOptions(null, Scope)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Accounts/Accounts/Config/ConfigCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public ConfigCommandBase() : base()
ConfigManager = configManager;
}

[Parameter(HelpMessage = "Specifies what part of Azure PowerShell the config applies to. Possible values are:\n- \"" + ConfigFilter.GlobalAppliesTo + "\": the config applies to all modules and cmdlets of Azure PowerShell. \n- Module name: the config applies to a certain module of Azure PowerShell. For example, \"Az.Storage\".\n- Cmdlet name: the config applies to a certain cmdlet of Azure PowerShell. For example, \"Get-AzKeyVault\".\nIf not specified, when getting configs, output will be all of the above; when updating, it defaults to \"" + ConfigFilter.GlobalAppliesTo + "\"; when clearing, configs applying to any targets are cleared.")]
[Parameter(HelpMessage = "Specifies what part of Azure PowerShell the config applies to. Possible values are:\n- \"" + ConfigFilter.GlobalAppliesTo + "\": the config applies to all modules and cmdlets of Azure PowerShell.\n- Module name: the config applies to a certain module of Azure PowerShell. For example, \"Az.Storage\".\n- Cmdlet name: the config applies to a certain cmdlet of Azure PowerShell. For example, \"Get-AzKeyVault\".\nIf not specified, when getting or clearing configs, it defaults to all the above; when updating, it defaults to \"" + ConfigFilter.GlobalAppliesTo + "\".")]
[ValidateNotNullOrEmpty]
public string AppliesTo { get; set; }

Expand Down
5 changes: 5 additions & 0 deletions src/Accounts/Accounts/Config/GetConfigCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.Azure.Commands.ResourceManager.Common;
using Microsoft.Azure.PowerShell.Common.Config;
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
Expand Down Expand Up @@ -57,6 +58,10 @@ public override void ExecuteCmdlet()
private ConfigFilter CreateConfigFilter()
{
ConfigFilter filter = new ConfigFilter() { AppliesTo = AppliesTo };
if (this.IsParameterBound(c => c.Scope))
{
filter.Scope = Scope;
}
IEnumerable<string> configKeysFromInput = GetConfigsSpecifiedByUser()
.Where(x => (SwitchParameter)x.Value)
.Select(x => x.Key);
Expand Down
10 changes: 10 additions & 0 deletions src/Accounts/Accounts/Config/UpdateConfigCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.Common.Exceptions;
using Microsoft.Azure.Commands.Profile.Models;
using Microsoft.Azure.PowerShell.Common.Config;
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
Expand Down Expand Up @@ -49,6 +50,15 @@ protected override void BeginProcessing()
}
}

protected override void ValidateParameters()
{
base.ValidateParameters();
if (Scope != ConfigScope.Process && Scope != ConfigScope.CurrentUser)
{
throw new AzPSArgumentException($"When updating configs, {nameof(Scope)} must be either {ConfigScope.Process} or {ConfigScope.CurrentUser}", nameof(Scope));
}
}

public override void ExecuteCmdlet()
{
var configsFromInput = GetConfigsSpecifiedByUser();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using Microsoft.Azure.Commands.Profile.Properties;
using Microsoft.Azure.Commands.ResourceManager.Common;
using Microsoft.WindowsAzure.Commands.Common;
using Microsoft.Azure.Commands.Shared.Config;
using Microsoft.Azure.PowerShell.Common.Config;
using System.Management.Automation;

namespace Microsoft.Azure.Commands.Profile
Expand All @@ -31,7 +32,7 @@ protected override void BeginProcessing()

public override void ExecuteCmdlet()
{
if (ShouldProcess(Resources.EnableDataCollection, Resources.DataCollectionEnabledWarning,
if (ShouldProcess(Resources.EnableDataCollection, Resources.DataCollectionEnabledWarning,
string.Empty))
{

Expand All @@ -41,11 +42,10 @@ public override void ExecuteCmdlet()

protected void SetDataCollectionProfile(bool enable)
{
var profile = _dataCollectionProfile;
profile.EnableAzureDataCollection = enable;
// update config
var session = AzureSession.Instance;
DataCollectionController.WritePSDataCollectionProfile(session, profile);
AzureSession.Instance.RegisterComponent(DataCollectionController.RegistryKey, () => DataCollectionController.Create(profile), true);
session.TryGetComponent<IConfigManager>(nameof(IConfigManager), out var configManager);
configManager.UpdateConfig(ConfigKeys.EnableDataCollection, enable, ConfigScope.CurrentUser);
}
}
}
5 changes: 0 additions & 5 deletions src/Accounts/Accounts/Feedback/SendFeedback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ private bool CheckIfInteractive()
interactive = false;
}
}

if (!interactive && _dataCollectionProfile != null && !_dataCollectionProfile.EnableAzureDataCollection.HasValue)
{
_dataCollectionProfile.EnableAzureDataCollection = false;
}
return interactive;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Accounts/Accounts/Models/RMProfileClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,11 @@ public AzureRmProfile Login(
{
if (subscriptionId != null)
{
throw new PSInvalidOperationException(String.Format(ResourceMessages.SubscriptionIdNotFound, account.Id, subscriptionId));
throw new PSInvalidOperationException(String.Format(ResourceMessages.SubscriptionIdNotFound, account.Id, subscriptionId) + " " + ProfileMessages.SubscriptionNotFouldPleaseCheckConfig);
}
else if (subscriptionName != null)
{
throw new PSInvalidOperationException(String.Format(ResourceMessages.SubscriptionNameNotFound, account.Id, subscriptionName));
throw new PSInvalidOperationException(String.Format(ResourceMessages.SubscriptionNameNotFound, account.Id, subscriptionName) + " " + ProfileMessages.SubscriptionNotFouldPleaseCheckConfig);
}

var newContext = new AzureContext(account, environment, newTenant);
Expand Down
9 changes: 9 additions & 0 deletions src/Accounts/Accounts/Properties/Resources.Designer.cs

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

5 changes: 4 additions & 1 deletion src/Accounts/Accounts/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@
</data>
<data name="ClientAssertionWarning" xml:space="preserve">
<value>The provided client id and assertion will be included in the '{0}' file found in the user profile ( {1} ). Please ensure that this directory has appropriate protections.</value>
</data>
</data>
<data name="MSITenantDomainNotFound" xml:space="preserve">
<value>Please ensure that the managed service identity found on this machine has proper permissions to the provided tenant domain.</value>
</data>
Expand Down Expand Up @@ -537,4 +537,7 @@
<data name="TokenCachePersistenceCheckError" xml:space="preserve">
<value>Persistence check fails due to unknown error</value>
</data>
<data name="SubscriptionNotFouldPleaseCheckConfig" xml:space="preserve">
<value>If a subscription is not specified, please check the configs by `Get-AzConfig`.</value>
</data>
</root>
Loading

0 comments on commit 5ea071f

Please sign in to comment.