Skip to content

Commit

Permalink
[Az.Accounts] Supported tenant domain as input while logging in. (Azu…
Browse files Browse the repository at this point in the history
…re#19492)

* Support tenant domain as input while logging in.

* Update ChangeLog.

* Update src/Accounts/Accounts/Models/RMProfileClient.cs

Co-authored-by: Yunchi Wang <[email protected]>

Co-authored-by: Yunchi Wang <[email protected]>
  • Loading branch information
NoriZC and wyunchi-ms authored Sep 16, 2022
1 parent 6cbf87c commit 9df3fa5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
30 changes: 30 additions & 0 deletions src/Accounts/Accounts.Test/AzureRMProfileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,36 @@ public void SpecifyTenantAndSubscriptionIdSucceed()
Assert.Equal("2021-01-01", client.SubscriptionAndTenantClient.ApiVersion);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SpecifyTenantDomainAndSubscriptionIdSucceed()
{
var tenants = new List<string> { DefaultTenant.ToString() };
var firstList = new List<string> { DefaultSubscription.ToString(), Guid.NewGuid().ToString() };
var secondList = new List<string> { Guid.NewGuid().ToString() };
var client = SetupTestEnvironment(tenants, firstList, secondList);

((MockTokenAuthenticationFactory)AzureSession.Instance.AuthenticationFactory).TokenProvider = (account, environment, tenant) =>
new MockAccessToken
{
UserId = "[email protected]",
LoginType = LoginType.OrgId,
AccessToken = "bbb",
TenantId = DefaultTenant.ToString()
};

var azureRmProfile = client.Login(
Context.Account,
Context.Environment,
MockSubscriptionClientFactory.GetTenantDomainFromId(DefaultTenant.ToString()),
DefaultSubscription.ToString(),
null,
null,
false,
null);
Assert.Equal("2021-01-01", client.SubscriptionAndTenantClient.ApiVersion);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SubscriptionIdNotExist()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public MockSubscriptionClientFactory()
{
}

public static string GetTenantDomainFromId(string id)
{
return id.Substring(3)+".com";
}

public static string GetSubscriptionNameFromId(string id)
{
if(id == "a11a11aa-aaaa-aaaa-aaaa-aaaa1111aaaa" || id == "aaaa11aa-aaaa-aaaa-aaaa-aaaa1111aaaa")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public SubscriptionClient GetSubscriptionClientVerLatest()
{
return ListTenantQueueDequeueVerLatest();
}
var tenants = _tenants.Select((k) => new TenantIdDescription(id: k, tenantId: k));
var tenants = _tenants.Select((k) => new TenantIdDescription(id: k, tenantId: k, domains: new List<string>{GetTenantDomainFromId(k)}));
var mockPage = new MockPage<TenantIdDescription>(tenants.ToList());
AzureOperationResponse<IPage<TenantIdDescription>> r = new AzureOperationResponse<IPage<TenantIdDescription>>
Expand Down
1 change: 1 addition & 0 deletions src/Accounts/Accounts/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
-->

## Upcoming Release
* Supported tenant domain as input while using `Connect-AzAccount` with parameter `Tenant`. [#19471]

## Version 2.10.1
* Deduplicated subscriptions belonging to multiple tenants while using `Get-AzSubscription` with parameter `SubscriptionName`. [#19427]
Expand Down
28 changes: 15 additions & 13 deletions src/Accounts/Accounts/Models/RMProfileClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public bool TryRemoveContext(IAzureContext context)
public AzureRmProfile Login(
IAzureAccount account,
IAzureEnvironment environment,
string tenantId,
string tenantIdOrName,
string subscriptionId,
string subscriptionName,
SecureString password,
Expand All @@ -138,13 +138,13 @@ public AzureRmProfile Login(
bool needDataPlanAuthFirst = !string.IsNullOrEmpty(authScope);
if(needDataPlanAuthFirst)
{
var token = AcquireAccessToken(account, environment, tenantId, password, promptBehavior, promptAction, authScope);
var token = AcquireAccessToken(account, environment, tenantIdOrName, password, promptBehavior, promptAction, authScope);
promptBehavior = ShowDialog.Never;
}

if (skipValidation)
{
if (string.IsNullOrEmpty(subscriptionId) || string.IsNullOrEmpty(tenantId))
if (string.IsNullOrEmpty(subscriptionId) || string.IsNullOrEmpty(tenantIdOrName))
{
throw new PSInvalidOperationException(Resources.SubscriptionOrTenantMissing);
}
Expand All @@ -154,29 +154,31 @@ public AzureRmProfile Login(
Id = subscriptionId
};

newSubscription.SetOrAppendProperty(AzureSubscription.Property.Tenants, tenantId);
newSubscription.SetOrAppendProperty(AzureSubscription.Property.Tenants, tenantIdOrName);
newSubscription.SetOrAppendProperty(AzureSubscription.Property.Account, account.Id);

newTenant = new AzureTenant
{
Id = tenantId
Id = tenantIdOrName
};
}
else
{
// (tenant and subscription are present) OR
// (tenant is present and subscription is not provided)
if (!string.IsNullOrEmpty(tenantId))
if (!string.IsNullOrEmpty(tenantIdOrName))
{
Guid tempGuid = Guid.Empty;
if (!Guid.TryParse(tenantId, out tempGuid))
if (!Guid.TryParse(tenantIdOrName, out tempGuid))
{
var tenants = ListAccountTenants(account, environment, password, promptBehavior, promptAction);
var homeTenants = tenants.FirstOrDefault(t => t.IsHome);
var tenant = homeTenants ?? tenants.FirstOrDefault();
var matchesName = tenants.Where(t => t.GetPropertyAsArray(AzureTenant.Property.Domains)
.Contains(tenantIdOrName, StringComparer.InvariantCultureIgnoreCase));
var homeTenants = matchesName.FirstOrDefault(t => t.IsHome);
var tenant = homeTenants ?? matchesName.FirstOrDefault();
if (tenant == null || tenant.Id == null)
{
string baseMessage = string.Format(ProfileMessages.TenantDomainNotFound, tenantId);
string baseMessage = string.Format(ProfileMessages.TenantDomainNotFound, tenantIdOrName);
var typeMessageMap = new Dictionary<string, string>
{
{ AzureAccount.AccountType.ServicePrincipal, string.Format(ProfileMessages.ServicePrincipalTenantDomainNotFound, account.Id) },
Expand All @@ -187,14 +189,14 @@ public AzureRmProfile Login(
throw new ArgumentNullException(string.Format("{0} {1}", baseMessage, typeMessage));
}

tenantId = tenant.Id;
tenantIdOrName = tenant.Id;
}


var token = AcquireAccessToken(
account,
environment,
tenantId,
tenantIdOrName,
password,
promptBehavior,
promptAction);
Expand Down Expand Up @@ -317,7 +319,7 @@ public AzureRmProfile Login(
if (shouldPopulateContextList && maxContextPopulation != 0)
{
var defaultContext = _profile.DefaultContext;
var subscriptions = maxContextPopulation > 0 ? ListSubscriptions(tenantId).Take(maxContextPopulation) : ListSubscriptions(tenantId);
var subscriptions = maxContextPopulation > 0 ? ListSubscriptions(tenantIdOrName).Take(maxContextPopulation) : ListSubscriptions(tenantIdOrName);

foreach (var subscription in subscriptions)
{
Expand Down

0 comments on commit 9df3fa5

Please sign in to comment.