Skip to content

Commit 9df3fa5

Browse files
NoriZCwyunchi-ms
andauthored
[Az.Accounts] Supported tenant domain as input while logging in. (Azure#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]>
1 parent 6cbf87c commit 9df3fa5

File tree

5 files changed

+52
-14
lines changed

5 files changed

+52
-14
lines changed

src/Accounts/Accounts.Test/AzureRMProfileTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,36 @@ public void SpecifyTenantAndSubscriptionIdSucceed()
149149
Assert.Equal("2021-01-01", client.SubscriptionAndTenantClient.ApiVersion);
150150
}
151151

152+
[Fact]
153+
[Trait(Category.AcceptanceType, Category.CheckIn)]
154+
public void SpecifyTenantDomainAndSubscriptionIdSucceed()
155+
{
156+
var tenants = new List<string> { DefaultTenant.ToString() };
157+
var firstList = new List<string> { DefaultSubscription.ToString(), Guid.NewGuid().ToString() };
158+
var secondList = new List<string> { Guid.NewGuid().ToString() };
159+
var client = SetupTestEnvironment(tenants, firstList, secondList);
160+
161+
((MockTokenAuthenticationFactory)AzureSession.Instance.AuthenticationFactory).TokenProvider = (account, environment, tenant) =>
162+
new MockAccessToken
163+
{
164+
UserId = "[email protected]",
165+
LoginType = LoginType.OrgId,
166+
AccessToken = "bbb",
167+
TenantId = DefaultTenant.ToString()
168+
};
169+
170+
var azureRmProfile = client.Login(
171+
Context.Account,
172+
Context.Environment,
173+
MockSubscriptionClientFactory.GetTenantDomainFromId(DefaultTenant.ToString()),
174+
DefaultSubscription.ToString(),
175+
null,
176+
null,
177+
false,
178+
null);
179+
Assert.Equal("2021-01-01", client.SubscriptionAndTenantClient.ApiVersion);
180+
}
181+
152182
[Fact]
153183
[Trait(Category.AcceptanceType, Category.CheckIn)]
154184
public void SubscriptionIdNotExist()

src/Accounts/Accounts.Test/Mocks/MockSubscriptionClientFactory.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public MockSubscriptionClientFactory()
5252
{
5353
}
5454

55+
public static string GetTenantDomainFromId(string id)
56+
{
57+
return id.Substring(3)+".com";
58+
}
59+
5560
public static string GetSubscriptionNameFromId(string id)
5661
{
5762
if(id == "a11a11aa-aaaa-aaaa-aaaa-aaaa1111aaaa" || id == "aaaa11aa-aaaa-aaaa-aaaa-aaaa1111aaaa")

src/Accounts/Accounts.Test/Mocks/MockSubscriptionClientFactoryVersion2019.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public SubscriptionClient GetSubscriptionClientVerLatest()
4848
{
4949
return ListTenantQueueDequeueVerLatest();
5050
}
51-
var tenants = _tenants.Select((k) => new TenantIdDescription(id: k, tenantId: k));
51+
var tenants = _tenants.Select((k) => new TenantIdDescription(id: k, tenantId: k, domains: new List<string>{GetTenantDomainFromId(k)}));
5252
var mockPage = new MockPage<TenantIdDescription>(tenants.ToList());
5353

5454
AzureOperationResponse<IPage<TenantIdDescription>> r = new AzureOperationResponse<IPage<TenantIdDescription>>

src/Accounts/Accounts/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
-->
2020

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

2324
## Version 2.10.1
2425
* Deduplicated subscriptions belonging to multiple tenants while using `Get-AzSubscription` with parameter `SubscriptionName`. [#19427]

src/Accounts/Accounts/Models/RMProfileClient.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public bool TryRemoveContext(IAzureContext context)
113113
public AzureRmProfile Login(
114114
IAzureAccount account,
115115
IAzureEnvironment environment,
116-
string tenantId,
116+
string tenantIdOrName,
117117
string subscriptionId,
118118
string subscriptionName,
119119
SecureString password,
@@ -138,13 +138,13 @@ public AzureRmProfile Login(
138138
bool needDataPlanAuthFirst = !string.IsNullOrEmpty(authScope);
139139
if(needDataPlanAuthFirst)
140140
{
141-
var token = AcquireAccessToken(account, environment, tenantId, password, promptBehavior, promptAction, authScope);
141+
var token = AcquireAccessToken(account, environment, tenantIdOrName, password, promptBehavior, promptAction, authScope);
142142
promptBehavior = ShowDialog.Never;
143143
}
144144

145145
if (skipValidation)
146146
{
147-
if (string.IsNullOrEmpty(subscriptionId) || string.IsNullOrEmpty(tenantId))
147+
if (string.IsNullOrEmpty(subscriptionId) || string.IsNullOrEmpty(tenantIdOrName))
148148
{
149149
throw new PSInvalidOperationException(Resources.SubscriptionOrTenantMissing);
150150
}
@@ -154,29 +154,31 @@ public AzureRmProfile Login(
154154
Id = subscriptionId
155155
};
156156

157-
newSubscription.SetOrAppendProperty(AzureSubscription.Property.Tenants, tenantId);
157+
newSubscription.SetOrAppendProperty(AzureSubscription.Property.Tenants, tenantIdOrName);
158158
newSubscription.SetOrAppendProperty(AzureSubscription.Property.Account, account.Id);
159159

160160
newTenant = new AzureTenant
161161
{
162-
Id = tenantId
162+
Id = tenantIdOrName
163163
};
164164
}
165165
else
166166
{
167167
// (tenant and subscription are present) OR
168168
// (tenant is present and subscription is not provided)
169-
if (!string.IsNullOrEmpty(tenantId))
169+
if (!string.IsNullOrEmpty(tenantIdOrName))
170170
{
171171
Guid tempGuid = Guid.Empty;
172-
if (!Guid.TryParse(tenantId, out tempGuid))
172+
if (!Guid.TryParse(tenantIdOrName, out tempGuid))
173173
{
174174
var tenants = ListAccountTenants(account, environment, password, promptBehavior, promptAction);
175-
var homeTenants = tenants.FirstOrDefault(t => t.IsHome);
176-
var tenant = homeTenants ?? tenants.FirstOrDefault();
175+
var matchesName = tenants.Where(t => t.GetPropertyAsArray(AzureTenant.Property.Domains)
176+
.Contains(tenantIdOrName, StringComparer.InvariantCultureIgnoreCase));
177+
var homeTenants = matchesName.FirstOrDefault(t => t.IsHome);
178+
var tenant = homeTenants ?? matchesName.FirstOrDefault();
177179
if (tenant == null || tenant.Id == null)
178180
{
179-
string baseMessage = string.Format(ProfileMessages.TenantDomainNotFound, tenantId);
181+
string baseMessage = string.Format(ProfileMessages.TenantDomainNotFound, tenantIdOrName);
180182
var typeMessageMap = new Dictionary<string, string>
181183
{
182184
{ AzureAccount.AccountType.ServicePrincipal, string.Format(ProfileMessages.ServicePrincipalTenantDomainNotFound, account.Id) },
@@ -187,14 +189,14 @@ public AzureRmProfile Login(
187189
throw new ArgumentNullException(string.Format("{0} {1}", baseMessage, typeMessage));
188190
}
189191

190-
tenantId = tenant.Id;
192+
tenantIdOrName = tenant.Id;
191193
}
192194

193195

194196
var token = AcquireAccessToken(
195197
account,
196198
environment,
197-
tenantId,
199+
tenantIdOrName,
198200
password,
199201
promptBehavior,
200202
promptAction);
@@ -317,7 +319,7 @@ public AzureRmProfile Login(
317319
if (shouldPopulateContextList && maxContextPopulation != 0)
318320
{
319321
var defaultContext = _profile.DefaultContext;
320-
var subscriptions = maxContextPopulation > 0 ? ListSubscriptions(tenantId).Take(maxContextPopulation) : ListSubscriptions(tenantId);
322+
var subscriptions = maxContextPopulation > 0 ? ListSubscriptions(tenantIdOrName).Take(maxContextPopulation) : ListSubscriptions(tenantIdOrName);
321323

322324
foreach (var subscription in subscriptions)
323325
{

0 commit comments

Comments
 (0)