Skip to content

Commit fd9d02b

Browse files
authored
Revert "Fix that RemoveUser doesn't work when create Public Client with broker" (Azure#27713)
1 parent cea7493 commit fd9d02b

13 files changed

+78
-90
lines changed

src/Accounts/Accounts/Account/DisconnectAzureRmAccount.cs

+12-11
Original file line numberDiff line numberDiff line change
@@ -124,20 +124,21 @@ public override void ExecuteCmdlet()
124124

125125
if (ShouldProcess(string.Format("Log out principal '{0}'", azureAccount.Id), "log out"))
126126
{
127+
if (GetContextModificationScope() == ContextModificationScope.CurrentUser)
128+
{
129+
AzureSession.Instance.AuthenticationFactory.RemoveUser(azureAccount, null);
130+
}
131+
127132
if (AzureRmProfileProvider.Instance.Profile != null)
128133
{
129134
ModifyContext((localProfile, profileClient) =>
130-
{
131-
var matchingContexts = localProfile.Contexts?.Values?.Where((c) => c != null && c.Account != null && string.Equals(c.Account.Id, azureAccount.Id, StringComparison.CurrentCultureIgnoreCase));
132-
foreach (var context in matchingContexts)
133-
{
134-
if (GetContextModificationScope() == ContextModificationScope.CurrentUser)
135-
{
136-
AzureSession.Instance.AuthenticationFactory.RemoveUser(azureAccount, context.Environment);
137-
}
138-
profileClient.TryRemoveContext(context);
139-
}
140-
});
135+
{
136+
var matchingContexts = localProfile.Contexts?.Values?.Where((c) => c != null && c.Account != null && string.Equals(c.Account.Id, azureAccount.Id, StringComparison.CurrentCultureIgnoreCase));
137+
foreach (var context in matchingContexts)
138+
{
139+
profileClient.TryRemoveContext(context);
140+
}
141+
});
141142
}
142143

143144
WriteObject(new PSAzureRmAccount(azureAccount));

src/Accounts/Accounts/ChangeLog.md

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
-->
2020

2121
## Upcoming Release
22-
* Fixed that removeUser didn't work when create Public Client with broker.
2322

2423
## Version 4.2.0
2524
* Updated warning message about MFA. For more details, see https://go.microsoft.com/fwlink/?linkid=2276314

src/Accounts/Accounts/Context/ClearAzureRmContext.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -65,29 +65,29 @@ void ClearContext(AzureRmProfile profile, RMProfileClient client)
6565
bool result = false;
6666
if (profile != null)
6767
{
68-
PowerShellTokenCacheProvider tokenCacheProvider = null;
69-
if (!AzureSession.Instance.TryGetComponent(PowerShellTokenCacheProvider.PowerShellTokenCacheProviderKey, out tokenCacheProvider))
70-
{
71-
WriteWarning(Resources.ClientFactoryNotRegisteredClear);
72-
}
73-
7468
var contexts = profile.Contexts.Values;
7569
foreach (var context in contexts)
7670
{
77-
tokenCacheProvider?.ClearCache(context.Environment.ActiveDirectoryAuthority);
7871
client.TryRemoveContext(context);
7972
}
8073

81-
if (tokenCacheProvider != null)
74+
PowerShellTokenCacheProvider tokenCacheProvider;
75+
if (!AzureSession.Instance.TryGetComponent(PowerShellTokenCacheProvider.PowerShellTokenCacheProviderKey, out tokenCacheProvider))
8276
{
83-
profile.TrySetDefaultContext(new AzureContext());
77+
WriteWarning(Resources.ClientFactoryNotRegisteredClear);
78+
}
79+
else
80+
{
81+
tokenCacheProvider.ClearCache();
82+
var defaultContext = new AzureContext();
83+
profile.TrySetDefaultContext(defaultContext);
8484
result = true;
8585
}
86-
8786
if (AzureSession.Instance.TryGetComponent(AzKeyStore.Name, out AzKeyStore keyStore))
8887
{
8988
keyStore?.Clear();
9089
}
90+
9191
}
9292

9393
AzureSession.Instance.RaiseContextClearedEvent();

src/Accounts/Accounts/Context/GetAzureRMContext.cs

-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ public override void ExecuteCmdlet()
8484
var defaultProfile = DefaultProfile as AzureRmProfile;
8585
if (defaultProfile != null && string.Equals(AzureSession.Instance?.ARMContextSaveMode, "CurrentUser"))
8686
{
87-
AzureSession.Instance.SetProperty(AzureSession.Property.Environment, DefaultContext.Environment.Name);
8887
defaultProfile.RefreshContextsFromCache(_cmdletContext);
8988
}
9089
}

src/Accounts/Accounts/Context/RemoveAzureRmContext.cs

+6-5
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using System;
16+
using System.Linq;
17+
using System.Management.Automation;
18+
1519
using Microsoft.Azure.Commands.Common.Authentication;
1620
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
1721
using Microsoft.Azure.Commands.Common.Authentication.Models;
1822
using Microsoft.Azure.Commands.Profile.Common;
1923
using Microsoft.Azure.Commands.Profile.Models.Core;
2024
using Microsoft.Azure.Commands.Profile.Properties;
21-
22-
using System;
23-
using System.Linq;
24-
using System.Management.Automation;
25+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2526

2627
namespace Microsoft.Azure.Commands.Profile.Context
2728
{
@@ -90,7 +91,7 @@ public override void ExecuteCmdlet()
9091
}
9192
else
9293
{
93-
if (!tokenCacheProvider.TryRemoveAccount(removedContext.Account.Id, removedContext.Environment.ActiveDirectoryAuthority))
94+
if (!tokenCacheProvider.TryRemoveAccount(removedContext.Account.Id))
9495
{
9596
WriteWarning(string.Format(Resources.NoContextsRemain, removedContext.Account.Id));
9697
}

src/Accounts/Authentication.ResourceManager/AzureRmProfile.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -819,12 +819,9 @@ public void RefreshContextsFromCache(ICmdletContext cmdletContext)
819819
out PowerShellTokenCacheProvider tokenCacheProvider);
820820

821821
string authority = null;
822-
//If the function is called from "public virtual IAzureContext DefaultContext", authroity is empty and then ListAccounts will return empty.
823-
//But as "ShouldRefreshContextsFromCache" is always false, the only call path is from GetAzureRMContext for now.
824822
if (TryGetEnvironment(AzureSession.Instance.GetProperty(AzureSession.Property.Environment), out IAzureEnvironment sessionEnvironment))
825823
{
826-
authority = new Uri(new Uri(sessionEnvironment.ActiveDirectoryAuthority), "organizations").AbsoluteUri;
827-
824+
authority = $"{sessionEnvironment.ActiveDirectoryAuthority}organizations";
828825
}
829826
var accounts = tokenCacheProvider.ListAccounts(authority);
830827
if (!accounts.Any())

src/Accounts/Authentication/Authentication/TokenCache/InMemoryTokenCacheProvider.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
using Azure.Identity;
1616

17-
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
1817
using Microsoft.Identity.Client;
1918

2019
namespace Microsoft.Azure.Commands.Common.Authentication
@@ -47,7 +46,7 @@ public override void FlushTokenData()
4746
}
4847
}
4948

50-
public override void ClearCache(string authority)
49+
public override void ClearCache()
5150
{
5251
InMemoryTokenCacheOptions = new InMemoryTokenCacheOptions();
5352
}

src/Accounts/Authentication/Authentication/TokenCache/PowerShellTokenCacheProvider.cs

+22-9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using System;
16+
using System.Collections.Generic;
17+
using System.Linq;
18+
1519
using Azure.Identity;
1620

1721
using Hyak.Common;
@@ -20,15 +24,13 @@
2024
using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Extensions;
2125
using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Interfaces;
2226
using Microsoft.Azure.Commands.Common.Authentication.Utilities;
27+
using Microsoft.Azure.Commands.Shared.Config;
2328
using Microsoft.Azure.Internal.Subscriptions;
2429
using Microsoft.Azure.Internal.Subscriptions.Models;
30+
using Microsoft.Azure.PowerShell.Common.Config;
2531
using Microsoft.Identity.Client;
2632
using Microsoft.Identity.Client.Broker;
2733

28-
using System;
29-
using System.Collections.Generic;
30-
using System.Linq;
31-
3234
namespace Microsoft.Azure.Commands.Common.Authentication
3335
{
3436
public abstract class PowerShellTokenCacheProvider
@@ -53,14 +55,14 @@ public virtual void FlushTokenData()
5355
_tokenCacheDataToFlush = null;
5456
}
5557

56-
public virtual void ClearCache(string authority = null)
58+
public virtual void ClearCache()
5759
{
5860
}
5961

60-
public bool TryRemoveAccount(string accountId, string authority = null)
62+
public bool TryRemoveAccount(string accountId)
6163
{
6264
TracingAdapter.Information(string.Format("[AuthenticationClientFactory] Calling GetAccountsAsync"));
63-
var client = CreatePublicClient(authority);
65+
var client = CreatePublicClient();
6466
var account = client.GetAccountsAsync()
6567
.ConfigureAwait(false).GetAwaiter().GetResult()
6668
.FirstOrDefault(a => string.Equals(a.Username, accountId, StringComparison.OrdinalIgnoreCase));
@@ -87,7 +89,7 @@ public IEnumerable<IAccount> ListAccounts(string authority = null)
8789
{
8890
TracingAdapter.Information(string.Format("[PowerShellTokenCacheProvider] Calling GetAccountsAsync on {0}", authority ?? "AzureCloud"));
8991

90-
return CreatePublicClient(authority)
92+
return CreatePublicClient(authority: authority)
9193
.GetAccountsAsync()
9294
.ConfigureAwait(false).GetAwaiter().GetResult();
9395
}
@@ -193,7 +195,18 @@ public virtual IPublicClientApplication CreatePublicClient(string authority, str
193195
/// </summary>
194196
public virtual IPublicClientApplication CreatePublicClient(string authority = null)
195197
{
196-
return CreatePublicClient(authority, organizationTenant);
198+
var builder = PublicClientApplicationBuilder.Create(Constants.PowerShellClientId);
199+
if (AzConfigReader.IsWamEnabled(authority))
200+
{
201+
builder = builder.WithBroker(new BrokerOptions(BrokerOptions.OperatingSystems.Windows));
202+
}
203+
if (!string.IsNullOrEmpty(authority))
204+
{
205+
builder.WithAuthority(authority);
206+
}
207+
var client = builder.Build();
208+
RegisterCache(client);
209+
return client;
197210
}
198211

199212
public abstract TokenCachePersistenceOptions GetTokenCachePersistenceOptions();

src/Accounts/Authentication/Authentication/TokenCache/SharedTokenCacheProvider.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using System;
16+
1517
using Azure.Identity;
1618

17-
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
1819
using Microsoft.Identity.Client;
1920
using Microsoft.Identity.Client.Extensions.Msal;
2021

21-
using System;
22-
2322
namespace Microsoft.Azure.Commands.Common.Authentication
2423
{
2524
public class SharedTokenCacheProvider : PowerShellTokenCacheProvider
@@ -104,9 +103,9 @@ protected override void RegisterCache(IPublicClientApplication client)
104103
}
105104
}
106105

107-
public override void ClearCache(string authority)
106+
public override void ClearCache()
108107
{
109-
var client = CreatePublicClient(authority);
108+
var client = CreatePublicClient();
110109
var accounts = client.GetAccountsAsync().GetAwaiter().GetResult();
111110
foreach (var account in accounts)
112111
{

src/Accounts/Authentication/Factories/AuthenticationFactory.cs

+5-15
Original file line numberDiff line numberDiff line change
@@ -487,16 +487,6 @@ public ServiceClientCredentials GetServiceClientCredentials(string accessToken,
487487
/// <param name="account"></param>
488488
/// <param name="tokenCache">This parameter is no longer used. However to keep the API unchanged it's not removed.</param>
489489
public void RemoveUser(IAzureAccount account, IAzureTokenCache tokenCache)
490-
{
491-
RemoveUser(account, environment: null);
492-
}
493-
494-
/// <summary>
495-
/// Remove any stored credentials for the given user and the Azure environment used.
496-
/// </summary>
497-
/// <param name="account">The account to remove credentials for</param>
498-
/// <param name="environment">The environment which account belongs to</param>
499-
public void RemoveUser(IAzureAccount account, IAzureEnvironment environment)
500490
{
501491
if (account != null && !string.IsNullOrEmpty(account.Id) && !string.IsNullOrWhiteSpace(account.Type))
502492
{
@@ -523,10 +513,10 @@ public void RemoveUser(IAzureAccount account, IAzureEnvironment environment)
523513
// make best effort to remove credentials
524514
}
525515

526-
RemoveFromTokenCache(account, environment.ActiveDirectoryAuthority);
516+
RemoveFromTokenCache(account);
527517
break;
528518
case AzureAccount.AccountType.User:
529-
RemoveFromTokenCache(account, environment.ActiveDirectoryAuthority);
519+
RemoveFromTokenCache(account);
530520
break;
531521
}
532522
}
@@ -568,20 +558,20 @@ private string GetEndpointToken(IAzureAccount account, string targetEndpoint)
568558
return account.GetProperty(tokenKey);
569559
}
570560

571-
private void RemoveFromTokenCache(IAzureAccount account, string authority = null)
561+
private void RemoveFromTokenCache(IAzureAccount account)
572562
{
573563
PowerShellTokenCacheProvider tokenCacheProvider;
574564
if (!AzureSession.Instance.TryGetComponent(PowerShellTokenCacheProvider.PowerShellTokenCacheProviderKey, out tokenCacheProvider))
575565
{
576566
throw new NullReferenceException(Resources.AuthenticationClientFactoryNotRegistered);
577567
}
578568

579-
var publicClient = tokenCacheProvider.CreatePublicClient(authority);
569+
var publicClient = tokenCacheProvider.CreatePublicClient();
580570
var accounts = publicClient.GetAccountsAsync()
581571
.ConfigureAwait(false).GetAwaiter().GetResult();
582572
var tokenAccounts = accounts.Where(a => MatchCacheItem(account, a));
583573
foreach (var tokenAccount in tokenAccounts)
584-
{
574+
{
585575
publicClient.RemoveAsync(tokenAccount)
586576
.ConfigureAwait(false).GetAwaiter().GetResult();
587577
}

tools/Common.Netcore.Dependencies.targets

+17-17
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
<ItemGroup>
44
<PackageReference Include="Microsoft.Rest.ClientRuntime" Version="2.3.24"/>
55
<PackageReference Include="Microsoft.Rest.ClientRuntime.Azure" Version="3.3.19"/>
6-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Aks" Version="1.3.107-preview"/>
7-
<PackageReference Include="Microsoft.Azure.PowerShell.Authentication.Abstractions" Version="1.3.107-preview"/>
8-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Authorization" Version="1.3.107-preview"/>
9-
<PackageReference Include="Microsoft.Azure.PowerShell.Common" Version="1.3.107-preview"/>
10-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Compute" Version="1.3.107-preview"/>
11-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Graph.Rbac" Version="1.3.107-preview"/>
12-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.KeyVault" Version="1.3.107-preview"/>
13-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Monitor" Version="1.3.107-preview"/>
14-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Network" Version="1.3.107-preview"/>
15-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.PolicyInsights" Version="1.3.107-preview"/>
16-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.ResourceManager" Version="1.3.107-preview"/>
17-
<PackageReference Include="Microsoft.Azure.PowerShell.Storage" Version="1.3.107-preview"/>
18-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Storage.Management" Version="1.3.107-preview"/>
19-
<PackageReference Include="Microsoft.Azure.PowerShell.Strategies" Version="1.3.107-preview"/>
20-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Websites" Version="1.3.107-preview"/>
21-
<PackageReference Include="Microsoft.Azure.PowerShell.Common.Share" Version="1.3.107-preview"/>
6+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Aks" Version="1.3.106-preview"/>
7+
<PackageReference Include="Microsoft.Azure.PowerShell.Authentication.Abstractions" Version="1.3.106-preview"/>
8+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Authorization" Version="1.3.106-preview"/>
9+
<PackageReference Include="Microsoft.Azure.PowerShell.Common" Version="1.3.106-preview"/>
10+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Compute" Version="1.3.106-preview"/>
11+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Graph.Rbac" Version="1.3.106-preview"/>
12+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.KeyVault" Version="1.3.106-preview"/>
13+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Monitor" Version="1.3.106-preview"/>
14+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Network" Version="1.3.106-preview"/>
15+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.PolicyInsights" Version="1.3.106-preview"/>
16+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.ResourceManager" Version="1.3.106-preview"/>
17+
<PackageReference Include="Microsoft.Azure.PowerShell.Storage" Version="1.3.106-preview"/>
18+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Storage.Management" Version="1.3.106-preview"/>
19+
<PackageReference Include="Microsoft.Azure.PowerShell.Strategies" Version="1.3.106-preview"/>
20+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Websites" Version="1.3.106-preview"/>
21+
<PackageReference Include="Microsoft.Azure.PowerShell.Common.Share" Version="1.3.106-preview"/>
2222
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
2323
</ItemGroup>
2424
<ItemGroup>
@@ -37,7 +37,7 @@
3737
<PackageReference Include="PowerShellStandard.Library" Version="5.1.0" PrivateAssets="All" />
3838
</ItemGroup>
3939
<PropertyGroup>
40-
<StorageToolsPath>$(NugetPackageRoot)\microsoft.azure.powershell.storage\1.3.107-preview\tools\</StorageToolsPath>
40+
<StorageToolsPath>$(NugetPackageRoot)\microsoft.azure.powershell.storage\1.3.106-preview\tools\</StorageToolsPath>
4141
</PropertyGroup>
4242
<ItemGroup Condition="'$(OmitJsonPackage)' != 'true'">
4343
<PackageReference Include="Newtonsoft.Json" Version="13.0.2"/>

tools/TestFx/Mocks/MockCertificateAuthenticationFactory.cs

-5
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,5 @@ public IAccessToken Authenticate(IAzureAccount account, IAzureEnvironment enviro
124124
{
125125
throw new NotImplementedException();
126126
}
127-
128-
public void RemoveUser(IAzureAccount account, IAzureEnvironment environment)
129-
{
130-
throw new NotImplementedException();
131-
}
132127
}
133128
}

tools/TestFx/Mocks/MockTokenAuthenticationFactory.cs

-5
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,5 @@ public ServiceClientCredentials GetServiceClientCredentials(IAzureContext contex
148148
{
149149
return GetServiceClientCredentials(context, targetEndpoint, AzureCmdletContext.CmdletNone);
150150
}
151-
152-
public void RemoveUser(IAzureAccount account, IAzureEnvironment environment)
153-
{
154-
throw new NotImplementedException();
155-
}
156151
}
157152
}

0 commit comments

Comments
 (0)