Skip to content

Commit 9a872b4

Browse files
committed
Merge pull request Azure#1372 from hovsepm/dev
[#108127818] Fixed issues with token id that does not match incoming account id
2 parents 69ef62d + bac6f98 commit 9a872b4

File tree

6 files changed

+93
-5
lines changed

6 files changed

+93
-5
lines changed

src/ResourceManager/Common/Commands.ScenarioTests.ResourceManager.Common/Mocks/MockClientFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public void AddUserAgent(string productName)
189189

190190
/// <summary>
191191
/// This class exists to allow adding an additional reference to the httpClient to prevent the client
192-
/// from being disposed. Should not be used execpt in this mocked context.
192+
/// from being disposed. Should not be used except in this mocked context.
193193
/// </summary>
194194
class PassThroughDelegatingHandler : DelegatingHandler
195195
{

src/ResourceManager/Profile/Commands.Profile.Test/AzureRMProfileTests.cs

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
using System.Collections.Generic;
2222
using Microsoft.IdentityModel.Clients.ActiveDirectory;
2323
using Microsoft.WindowsAzure.Commands.ScenarioTest;
24+
using Microsoft.WindowsAzure.Commands.Common;
25+
using Moq;
2426

2527
namespace Microsoft.Azure.Commands.ResourceManager.Common.Test
2628
{
@@ -32,6 +34,7 @@ public class AzureRMProfileTests
3234
private static string DefaultSubscriptionName = "Contoso Subscription";
3335
private static string DefaultDomain = "contoso.com";
3436
private static Guid DefaultTenant = Guid.NewGuid();
37+
private static AzureContext Context;
3538

3639
private static RMProfileClient SetupTestEnvironment(List<string> tenants, params List<string>[] subscriptionLists)
3740
{
@@ -45,7 +48,7 @@ private static RMProfileClient SetupTestEnvironment(List<string> tenants, params
4548
}, true);
4649
mock.MoqClients = true;
4750
AzureSession.ClientFactory = mock;
48-
var context = new AzureContext(new AzureSubscription()
51+
Context = new AzureContext(new AzureSubscription()
4952
{
5053
Account = DefaultAccount,
5154
Environment = EnvironmentName.AzureCloud,
@@ -56,10 +59,64 @@ private static RMProfileClient SetupTestEnvironment(List<string> tenants, params
5659
AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud],
5760
new AzureTenant() { Domain = DefaultDomain, Id = DefaultTenant });
5861
var profile = new AzureRMProfile();
59-
profile.Context = context;
62+
profile.Context = Context;
6063
return new RMProfileClient(profile);
6164
}
6265

66+
[Fact]
67+
[Trait(Category.AcceptanceType, Category.CheckIn)]
68+
public void TokenIdAndAccountIdMismatch()
69+
{
70+
var tenants = new List<string> { Guid.NewGuid().ToString(), DefaultTenant.ToString() };
71+
var secondsubscriptionInTheFirstTenant = Guid.NewGuid().ToString();
72+
var firstList = new List<string> { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant };
73+
var secondList = new List<string> { Guid.NewGuid().ToString() };
74+
var thirdList = new List<string> { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant };
75+
var fourthList = new List<string> { DefaultSubscription.ToString(), secondsubscriptionInTheFirstTenant };
76+
var client = SetupTestEnvironment(tenants, firstList, secondList, thirdList, fourthList);
77+
78+
var tokens = new Queue<MockAccessToken>();
79+
tokens.Enqueue(new MockAccessToken
80+
{
81+
UserId = "[email protected]",
82+
LoginType = LoginType.OrgId,
83+
AccessToken = "bbb"
84+
});
85+
tokens.Enqueue(new MockAccessToken
86+
{
87+
UserId = "[email protected]",
88+
LoginType = LoginType.OrgId,
89+
AccessToken = "bbb",
90+
TenantId = tenants.First()
91+
});
92+
tokens.Enqueue(new MockAccessToken
93+
{
94+
UserId = "[email protected]",
95+
LoginType = LoginType.OrgId,
96+
AccessToken = "bbb",
97+
TenantId = tenants.Last()
98+
});
99+
100+
((MockTokenAuthenticationFactory)AzureSession.AuthenticationFactory).TokenProvider = (account, environment, tenant) =>
101+
{
102+
var token = tokens.Dequeue();
103+
account.Id = token.UserId;
104+
return token;
105+
};
106+
107+
var azureRmProfile = client.Login(
108+
Context.Account,
109+
Context.Environment,
110+
null,
111+
secondsubscriptionInTheFirstTenant,
112+
null,
113+
null);
114+
115+
var tenantsInAccount = azureRmProfile.Context.Account.GetPropertyAsArray( AzureAccount.Property.Tenants);
116+
Assert.Equal(1, tenantsInAccount.Length);
117+
Assert.Equal(tenants.First(), tenantsInAccount[0]);
118+
}
119+
63120
[Fact]
64121
[Trait(Category.AcceptanceType, Category.CheckIn)]
65122
public void MultipleTenantsAndSubscriptionsSucceed()

src/ResourceManager/Profile/Commands.Profile.Test/ProfileController.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ private void SetupManagementClients()
120120
helper.SetupManagementClients(SubscriptionClient);
121121
}
122122

123-
124123
private SubscriptionClient GetSubscriptionClient()
125124
{
126125
return TestBase.GetServiceClient<SubscriptionClient>(this.csmTestFactory);

src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public AzureRMProfile Login(
7777
{
7878
var tenants = ListAccountTenants(account, environment, password, promptBehavior).Select(s => s.Id.ToString()).ToArray();
7979
account.SetProperty(AzureAccount.Property.Tenants, null);
80+
string accountId = null;
8081

8182
for (int i = 0; i < tenants.Count(); i++)
8283
{
@@ -90,7 +91,26 @@ public AzureRMProfile Login(
9091
try
9192
{
9293
token = AcquireAccessToken(account, environment, tenant, password, ShowDialog.Auto);
93-
account.SetOrAppendProperty(AzureAccount.Property.Tenants, tenant);
94+
95+
if (accountId == null)
96+
{
97+
accountId = account.Id;
98+
account.SetOrAppendProperty(AzureAccount.Property.Tenants, tenant);
99+
}
100+
else if (accountId.Equals(account.Id, StringComparison.OrdinalIgnoreCase))
101+
{
102+
account.SetOrAppendProperty(AzureAccount.Property.Tenants, tenant);
103+
}
104+
else
105+
{ // if account ID is different from the first tenant account id we need to ignore current tenant
106+
WriteWarningMessage(string.Format(
107+
Microsoft.Azure.Commands.Profile.Properties.Resources.AccountIdMismatch,
108+
account.Id,
109+
tenant,
110+
accountId));
111+
account.Id = accountId;
112+
token = null;
113+
}
94114
}
95115
catch
96116
{

src/ResourceManager/Profile/Commands.Profile/Properties/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/Profile/Commands.Profile/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@
120120
<data name="AccessTokenRequiresAccount" xml:space="preserve">
121121
<value>AccountId must be provided to use an AccessToken credential.</value>
122122
</data>
123+
<data name="AccountIdMismatch" xml:space="preserve">
124+
<value>Account ID '{0}' for tenant '{1}' does not match home Account ID '{2}'</value>
125+
</data>
123126
<data name="AccountIdRequired" xml:space="preserve">
124127
<value>Access token credentials must provide the AccountId parameter.</value>
125128
</data>

0 commit comments

Comments
 (0)