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

Enable passing a tenant domain to Connect-AzureRmAccount #6822

Merged
merged 2 commits into from
Aug 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ private AuthenticationContext CreateContext(AdalConfiguration config)
// We have to run this in a separate thread to guarantee that it's STA. This method
// handles the threading details.
private AuthenticationResult AcquireToken(
AdalConfiguration config,
string promptBehavior,
Action<string> promptAction,
AdalConfiguration config,
string promptBehavior,
Action<string> promptAction,
string userId,
SecureString password)
{
Expand Down Expand Up @@ -239,6 +239,21 @@ private AuthenticationResult DoAcquireToken(
AdalTokenCache.ClearCookies();
}

Guid tempGuid = Guid.Empty;
if (!string.Equals(config.AdDomain, "Common", StringComparison.OrdinalIgnoreCase) && !Guid.TryParse(config.AdDomain, out tempGuid))
{
var tempResult = context.AcquireToken(
config.ResourceClientUri,
config.ClientId,
config.ClientRedirectUri,
promptBehavior,
UserIdentifier.AnyUser,
AdalConfiguration.EnableEbdMagicCookie);
config.AdDomain = tempResult.TenantId;
context = CreateContext(config);
promptBehavior = PromptBehavior.Never;
}

result = context.AcquireToken(
config.ResourceClientUri,
config.ClientId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,26 @@ public void LoginWithNoSubscriptionAndTenant()
Assert.Equal("microsoft.com", AzureRmProfileProvider.Instance.Profile.DefaultContext.Tenant.Directory);
}

[Fact]
[Trait(Category.RunType, Category.LiveOnly)]
public void LoginWithNoSubscriptionAndTenantDomain()
{
var cmdlt = new ConnectAzureRmAccountCommand();
// Setup
cmdlt.CommandRuntime = commandRuntimeMock;
cmdlt.TenantId = "microsoft.onmicrosoft.com";
cmdlt.SetParameterSet("UserWithSubscriptionId");

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

Assert.NotNull(AzureRmProfileProvider.Instance.Profile.DefaultContext);
Assert.Equal("microsoft.com", AzureRmProfileProvider.Instance.Profile.DefaultContext.Tenant.Directory);
Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", AzureRmProfileProvider.Instance.Profile.DefaultContext.Tenant.Id);
}

[Fact]
[Trait(Category.RunType, Category.LiveOnly)]
public void LoginWithSubscriptionname()
Expand Down
33 changes: 18 additions & 15 deletions src/ResourceManager/Profile/Commands.Profile/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
-->
## Current Release
* Fix issues with Clear-AzureRmContext that caused issues with selecting a context #6398
* Enable tenant domain to be passed to `-TenantId` parameter for `Connect-AzureRmAccount`
- https://github.com/Azure/azure-powershell/issues/3974
- https://github.com/Azure/azure-powershell/issues/6709

## Version 5.3.4
* Updated Common.Strategy library to be able to validate that the current config for a resource is compatible with the target resource. Default is always true, individual resources and overridet the default.
Expand Down Expand Up @@ -79,7 +82,7 @@
* 'Remove-AzureRmAccount' has been renamed as 'Disconnect-AzureRmAccount'; an alias has been added for the old cmdlet name, and other aliases ('Logout-AzAccount' and 'Logout-AzureRmAccount') have been redirected to the new cmdlet name.
* Corrected Resource Strings to use Connect-AzureRmAccount instead of Login-AzureRmAccount
* Add-AzureRmEnvironment and Set-AzureRmEnvironment
- Added -AzureOperationalInsightsEndpoint and -AzureOperationalInsightsEndpointResourceId as parameters for use with OperationalInsights data plane RP.
- Added -AzureOperationalInsightsEndpoint and -AzureOperationalInsightsEndpointResourceId as parameters for use with OperationalInsights data plane RP.

## Version 4.2.0
* Added ResourceGroup Completer to -ResourceGroup parameters allowing tab completion through resource groups in current subscription
Expand Down Expand Up @@ -114,22 +117,22 @@
- Use this feature by adding LocationCompleter(string[] validResourceTypes) onto the Location parameter

## Version 3.4.0
* Start-Job Support for AzureRm cmdlets.
* All AzureRmCmdlets add -AzureRmContext parameter, which can accept a context (output of a Context cmdlet).
* Start-Job Support for AzureRm cmdlets.
* All AzureRmCmdlets add -AzureRmContext parameter, which can accept a context (output of a Context cmdlet).
- Common pattern for jobs with context persistence DISABLED: ```Start-Job {param ($context) New-AzureRmVM -AzureRmContext $context [... other parameters]} -ArgumentList (Get-AzureRmContext)```
- Common pattern for jobs with context persistence ENABLED:```Start-Job {New-AzureRmVM [... other parameters]}```
* Persist login information across sessions, new cmdlets:
- Enable-AzureRmContextAutosave - Enable login persistence across sessions.
- Disable-AzureRmContextAutosave - Disable login persistence across sessions.
* Manage context information, new cmdets
- Select-AzureRmContext - Select the active named context.
- Rename-AzureRmContext - Rename an exsiting context for easy reference.
- Remove-AzureRmContext - Remove an existing context.
- Remove-AzureRmAccount - Remove all credentials, subscriptions, and tenants associated with an account.
* Manage context information, cmdlet changes:
- Added Scope = (Process | CurrentUser) to all cmdlets that change credentials
* Persist login information across sessions, new cmdlets:
- Enable-AzureRmContextAutosave - Enable login persistence across sessions.
- Disable-AzureRmContextAutosave - Disable login persistence across sessions.
* Manage context information, new cmdets
- Select-AzureRmContext - Select the active named context.
- Rename-AzureRmContext - Rename an exsiting context for easy reference.
- Remove-AzureRmContext - Remove an existing context.
- Remove-AzureRmAccount - Remove all credentials, subscriptions, and tenants associated with an account.
* Manage context information, cmdlet changes:
- Added Scope = (Process | CurrentUser) to all cmdlets that change credentials
- Get-AzureRmContext - Added ListAvailable parameter to list all saved contexts

## Version 3.3.1

## Version 3.3.0
Expand Down Expand Up @@ -167,7 +170,7 @@
* *Obsolete*: Select-AzureRmProfile is renamed to Import-AzureRmContext, there is an alias to the old cmdlet name, the alias will be removed in the next release.
* The PSAzureContext and PSAzureProfile output types of profile cmdlets will be changed in the next release.
* The Save-AzureRmContext cmdlet will have no OutputType in the next release.
* Fix bug in cmdlet common code to use FIPS-compliant algorithm for data hashes: https://github.com/Azure/azure-powershell/issues/3651
* Fix bug in cmdlet common code to use FIPS-compliant algorithm for data hashes: https://github.com/Azure/azure-powershell/issues/3651

## Version 2.7.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,23 @@ public AzureRmProfile Login(
// (tenant is present and subscription is not provided)
if (!string.IsNullOrEmpty(tenantId))
{
Guid tempGuid = Guid.Empty;
if (!Guid.TryParse(tenantId, out tempGuid))
{
var tenant = ListAccountTenants(
account,
environment,
password,
promptBehavior,
promptAction)?.FirstOrDefault();
if (tenant == null)
{
throw new ArgumentNullException(string.Format("Could not find tenant id for provided tenant domain '{0}'.", tenantId));
Copy link
Member

Choose a reason for hiding this comment

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

Is there a way that we can add a test to exercise this exception?

Copy link
Member

Choose a reason for hiding this comment

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

even if this is a live only test, it would behoove us to have this to prevent regressions

}

tenantId = tenant.Id;
}

var token = AcquireAccessToken(
account,
environment,
Expand Down