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
1 change: 1 addition & 0 deletions src/ResourceManager/Profile/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Additional information about change #1
-->
## Current Release
* Create a context for each subscription when running `Connect-AzureRmAccount` with no previous context

## Version 5.2.0
* Added the following three values to the telemetry:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using Microsoft.Azure.Commands.ResourceManager.Common;
using Microsoft.Azure.Commands.ScenarioTest;
using System.Linq;

namespace Microsoft.Azure.Commands.Profile.Test
{
Expand Down Expand Up @@ -327,7 +328,7 @@ public void LoginWithCredentialParameterAndMSA()
"For more information, please refer to http://go.microsoft.com/fwlink/?linkid=331007&clcid=0x409 " +
"for more information about the difference between an organizational account and a Microsoft account.",
ex.Message);
}
}
}

[Fact]
Expand All @@ -353,6 +354,43 @@ public void LoginWithAccessToken()
Assert.NotNull(AzureRmProfileProvider.Instance.Profile.DefaultContext);
}

[Fact]
[Trait(Category.RunType, Category.LiveOnly)]
public void LoginPopulatesContextList()
{
// Before running this test, make sure to clear the contexts on your machine by removing the following two files:
// - %APPDATA%/Windows Azure Powershell/AzureRmContext.json
// - %APPDATA%/Windows Azure Powershell/AzureRmContextSettings.json
// This will clear all existing contexts on your machine so that this test can re-populate the list with a context for each subscription

var cmdlt = new ConnectAzureRmAccountCommand();
// Setup
cmdlt.CommandRuntime = commandRuntimeMock;

// Act
cmdlt.InvokeBeginProcessing();
cmdlt.ExecuteCmdlet();
cmdlt.InvokeEndProcessing();

var profile = AzureRmProfileProvider.Instance.Profile as AzureRmProfile;
Assert.NotNull(profile);
Assert.NotNull(profile.Contexts);
Assert.NotNull(profile.Subscriptions);
Assert.True(profile.Contexts.Count > 1);
Assert.True(profile.Subscriptions.Count() > 1);
Assert.Equal(profile.Subscriptions.Count(), profile.Contexts.Count);

foreach (var sub in profile.Subscriptions)
{
var contextName = string.Format("{0} - {1}", sub.Name, sub.Id);
Assert.True(profile.Contexts.ContainsKey(contextName));
var context = profile.Contexts[contextName];
Assert.NotNull(context);
Assert.Equal(sub.Id, context.Subscription.Id);
Assert.Equal(sub.GetTenant(), context.Tenant.Id);
}
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void ThrowOnUnknownEnvironment()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class ConnectAzureRmAccountCommand : AzureContextModificationCmdlet, IMod
[Alias("EnvironmentName")]
[ValidateNotNullOrEmpty]
public string Environment { get; set; }

#if !NETSTANDARD
[Parameter(ParameterSetName = UserParameterSet,
Mandatory = false, HelpMessage = "Optional credential", Position = 0)]
Expand All @@ -59,35 +59,35 @@ public class ConnectAzureRmAccountCommand : AzureContextModificationCmdlet, IMod
Mandatory = true, HelpMessage = "Credential")]
public PSCredential Credential { get; set; }

[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet,
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet,
Mandatory = true, HelpMessage = "Certificate Hash (Thumbprint)")]
public string CertificateThumbprint { get; set; }
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet,

[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet,
Mandatory = true, HelpMessage = "SPN")]
public string ApplicationId { get; set; }

[Parameter(ParameterSetName = ServicePrincipalParameterSet,
[Parameter(ParameterSetName = ServicePrincipalParameterSet,
Mandatory = true)]
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet,
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet,
Mandatory = true)]
public SwitchParameter ServicePrincipal { get; set; }
[Parameter(ParameterSetName = UserParameterSet,

[Parameter(ParameterSetName = UserParameterSet,
Mandatory = false, HelpMessage = "Optional tenant name or ID")]
[Parameter(ParameterSetName = ServicePrincipalParameterSet,
[Parameter(ParameterSetName = ServicePrincipalParameterSet,
Mandatory = true, HelpMessage = "Tenant name or ID")]
[Parameter(ParameterSetName = AccessTokenParameterSet,
[Parameter(ParameterSetName = AccessTokenParameterSet,
Mandatory = false, HelpMessage = "Tenant name or ID")]
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet,
[Parameter(ParameterSetName = ServicePrincipalCertificateParameterSet,
Mandatory = true, HelpMessage = "Tenant name or ID")]
[Parameter(ParameterSetName = ManagedServiceParameterSet,
Mandatory = false, HelpMessage = "Optional tenant name or ID")]
[Alias("Domain")]
[ValidateNotNullOrEmpty]
public string TenantId { get; set; }
[Parameter(ParameterSetName = AccessTokenParameterSet,

[Parameter(ParameterSetName = AccessTokenParameterSet,
Mandatory = true, HelpMessage = "AccessToken for Azure Resource Manager")]
[ValidateNotNullOrEmpty]
public string AccessToken { get; set; }
Expand All @@ -101,8 +101,8 @@ public class ConnectAzureRmAccountCommand : AzureContextModificationCmdlet, IMod
Mandatory = false, HelpMessage = "AccessToken for KeyVault Service")]
[ValidateNotNullOrEmpty]
public string KeyVaultAccessToken { get; set; }
[Parameter(ParameterSetName = AccessTokenParameterSet,

[Parameter(ParameterSetName = AccessTokenParameterSet,
Mandatory = true, HelpMessage = "Account Id for access token")]
[Parameter(ParameterSetName = ManagedServiceParameterSet,
Mandatory = false, HelpMessage = "Account Id for managed service. Can be a managed service resource Id, or the associated client id. To use the SyatemAssigned identity, leave this field blank.")]
Expand Down Expand Up @@ -148,6 +148,9 @@ public class ConnectAzureRmAccountCommand : AzureContextModificationCmdlet, IMod
Mandatory = false, HelpMessage = "Skip validation for access token")]
public SwitchParameter SkipValidation { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Skips context population if no contexts are found.")]
public SwitchParameter SkipContextPopulation { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Overwrite the existing context with the same name, if any.")]
public SwitchParameter Force { get; set; }

Expand Down Expand Up @@ -214,8 +217,8 @@ public override void ExecuteCmdlet()
builder.Port = ManagedServicePort;
builder.Path = "/oauth2/token";

string msiSecret = this.IsBound(nameof(ManagedServiceSecret))
? ManagedServiceSecret.ConvertToString()
string msiSecret = this.IsBound(nameof(ManagedServiceSecret))
? ManagedServiceSecret.ConvertToString()
: System.Environment.GetEnvironmentVariable(MSISecretVariable);

string suppliedUri = this.IsBound(nameof(ManagedServiceHostName))
Expand All @@ -226,7 +229,7 @@ public override void ExecuteCmdlet()
{
azureAccount.SetProperty(AzureAccount.Property.MSILoginSecret, msiSecret);
}

if (!string.IsNullOrWhiteSpace(suppliedUri))
{
azureAccount.SetProperty(AzureAccount.Property.MSILoginUri, suppliedUri);
Expand Down Expand Up @@ -255,7 +258,7 @@ public override void ExecuteCmdlet()
{
azureAccount.Id = ApplicationId;
}

if (!string.IsNullOrWhiteSpace(CertificateThumbprint))
{
azureAccount.SetThumbprint(CertificateThumbprint);
Expand Down Expand Up @@ -284,7 +287,8 @@ public override void ExecuteCmdlet()
password,
SkipValidation,
(s) => WriteWarning(s),
name));
name,
!this.SkipContextPopulation.IsPresent));
});
}
}
Expand Down Expand Up @@ -330,7 +334,7 @@ public void OnImport()
#if DEBUG
}
#endif

bool autoSaveEnabled = AzureSession.Instance.ARMContextSaveMode == ContextSaveMode.CurrentUser;
var autosaveVariable = System.Environment.GetEnvironmentVariable(AzureProfileConstants.AzureAutosaveVariable);
bool localAutosave;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ public AzureRmProfile Login(
SecureString password,
bool skipValidation,
Action<string> promptAction,
string name = null)
string name = null,
bool shouldPopulateContextList = true)
{
IAzureSubscription newSubscription = null;
IAzureTenant newTenant = null;
Expand Down Expand Up @@ -226,6 +227,7 @@ public AzureRmProfile Login(
}
}

shouldPopulateContextList &= _profile.DefaultContext?.Account == null;
if (newSubscription == null)
{
if (subscriptionId != null)
Expand Down Expand Up @@ -260,6 +262,35 @@ public AzureRmProfile Login(
}

_profile.DefaultContext.TokenCache = _cache;
if (shouldPopulateContextList)
Copy link
Member

Choose a reason for hiding this comment

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

Were we going to add an option in to the login cmdlet that would disable populating the contexts? Also, I think we should set a default limit to the number of populated contexts (say, 25).

{
var defaultContext = _profile.DefaultContext;
var subscriptions = ListSubscriptions(tenantId).Take(25);
foreach (var subscription in subscriptions)
{
IAzureTenant tempTenant = new AzureTenant()
{
Id = subscription.GetProperty(AzureSubscription.Property.Tenants)
};

var tempContext = new AzureContext(subscription, account, environment, tempTenant);
tempContext.TokenCache = _cache;
string tempName = null;
if (!_profile.TryGetContextName(tempContext, out tempName))
{
WriteWarningMessage(string.Format(Resources.CannotGetContextName, subscription.Id));
continue;
}

if (!_profile.TrySetContext(tempName, tempContext))
{
WriteWarningMessage(string.Format(Resources.CannotCreateContext, subscription.Id));
}
}

_profile.TrySetDefaultContext(defaultContext);
_profile.TryRemoveContext("Default");
}

return _profile.ToProfile();
}
Expand Down

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 @@ -406,4 +406,12 @@
<data name="RunLoginCmdlet" xml:space="preserve">
<value>Run Connect-AzureRmAccount to login.</value>
</data>
<data name="CannotCreateContext" xml:space="preserve">
<value>Cannot create a context for subscription with id '{0}'.</value>
<comment>{0} = subscription id</comment>
</data>
<data name="CannotGetContextName" xml:space="preserve">
<value>Unable to get context name for subscription with id '{0}'.</value>
<comment>{0} = subscription id</comment>
</data>
</root>
Loading