Skip to content

Commit 797bfc2

Browse files
msJinLeiCopilotNoriZC
authored
Collect Authentication Telemetry (#27560)
Co-authored-by: Copilot <[email protected]> Co-authored-by: NoriZC <[email protected]>
1 parent c674667 commit 797bfc2

File tree

50 files changed

+733
-116
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+733
-116
lines changed

src/Accounts/Accounts.Test/AccessTokenCmdletTest.cs

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

1515
using Microsoft.Azure.Commands.Common.Authentication;
1616
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
17+
using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Interfaces;
1718
using Microsoft.Azure.Commands.Common.Authentication.Models;
1819
using Microsoft.Azure.Commands.Profile;
1920
using Microsoft.Azure.Commands.Profile.Models;
@@ -28,6 +29,7 @@
2829
using Moq;
2930

3031
using System;
32+
using System.Collections.Generic;
3133
using System.Linq;
3234
using System.Security;
3335

@@ -49,7 +51,7 @@ public AccessTokenCmdletTests(ITestOutputHelper output)
4951
{
5052
TestExecutionHelpers.SetUpSessionAndProfile();
5153
XunitTracingInterceptor.AddToContext(new XunitTracingInterceptor(output));
52-
54+
AzureSession.Instance.RegisterComponent<AuthenticationTelemetry>(AuthenticationTelemetry.Name, () => new AuthenticationTelemetry());
5355
var defaultContext = new AzureContext(
5456
new AzureSubscription()
5557
{
@@ -97,8 +99,7 @@ public void TestGetAccessTokenAsPlainText()
9799
It.IsAny<SecureString>(),
98100
It.IsAny<string>(),
99101
It.IsAny<Action<string>>(),
100-
It.IsAny<IAzureTokenCache>(),
101-
It.IsAny<string>())).Returns(new MockAccessToken
102+
It.IsAny<IDictionary<string, object>>())).Returns(new MockAccessToken
102103
{
103104
UserId = expected.UserId,
104105
LoginType = LoginType.OrgId,
@@ -146,8 +147,7 @@ public void TestGetAccessTokenAsSecureString()
146147
It.IsAny<SecureString>(),
147148
It.IsAny<string>(),
148149
It.IsAny<Action<string>>(),
149-
It.IsAny<IAzureTokenCache>(),
150-
It.IsAny<string>())).Returns(new MockAccessToken
150+
It.IsAny<IDictionary<string, object>>())).Returns(new MockAccessToken
151151
{
152152
UserId = expected.UserId,
153153
LoginType = LoginType.OrgId,

src/Accounts/Accounts.Test/AutosaveTests.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
using Xunit.Abstractions;
2323
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
2424
using System;
25-
using System.Security;
2625
using Microsoft.Azure.Commands.Profile.Context;
2726
using Microsoft.Azure.Commands.ScenarioTest;
2827
using Microsoft.Azure.Commands.ResourceManager.Common;
@@ -57,7 +56,6 @@ private AzKeyStore SetMockedAzKeyStore()
5756

5857
void ResetState()
5958
{
60-
6159
TestExecutionHelpers.SetUpSessionAndProfile();
6260
ResourceManagerProfileProvider.InitializeResourceManagerProfile(true);
6361
// prevent token acquisition
@@ -69,6 +67,7 @@ void ResetState()
6967
PowerShellTokenCacheProvider tokenProvider = new InMemoryTokenCacheProvider();
7068
AzureSession.Instance.RegisterComponent(PowerShellTokenCacheProvider.PowerShellTokenCacheProviderKey, () => tokenProvider, true);
7169
AzureSession.Instance.RegisterComponent(AzKeyStore.Name, () => keyStore, true);
70+
AzureSession.Instance.RegisterComponent<AuthenticationTelemetry>(AuthenticationTelemetry.Name, () => new AuthenticationTelemetry());
7271
}
7372

7473
[Fact]

src/Accounts/Accounts.Test/AzureRMProfileTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
using System.Management.Automation;
4242
using System.Net.Http;
4343
using System.Runtime.InteropServices;
44-
using System.Runtime.Serialization.Formatters.Binary;
4544
using System.Text.Json;
4645
using System.Threading;
4746
using System.Threading.Tasks;
@@ -176,6 +175,7 @@ public AzureRMProfileTests(ITestOutputHelper output)
176175
{
177176
TestExecutionHelpers.SetUpSessionAndProfile();
178177
XunitTracingInterceptor.AddToContext(new XunitTracingInterceptor(output));
178+
AzureSession.Instance.RegisterComponent<AuthenticationTelemetry>(AuthenticationTelemetry.Name, () => new AuthenticationTelemetry());
179179
}
180180

181181
[Fact]

src/Accounts/Accounts.Test/AzureSessionTestInitializer.cs

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public static void Initialize()
3333
AzureSession.Instance.RegisterComponent(AuthenticatorBuilder.AuthenticatorBuilderKey, () => builder);
3434
AzureSession.Instance.RegisterComponent(nameof(AzureCredentialFactory), () => new AzureCredentialFactory());
3535
AzureSession.Instance.RegisterComponent(nameof(MsalAccessTokenAcquirerFactory), () => new MsalAccessTokenAcquirerFactory());
36+
AzureSession.Instance.RegisterComponent<AuthenticationTelemetry>(AuthenticationTelemetry.Name, () => new AuthenticationTelemetry());
3637
}
3738

3839
}

src/Accounts/Accounts.Test/ContextCmdletTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public ContextCmdletTests(ITestOutputHelper output)
6666
string profilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), Resources.AzureDirectoryName);
6767
azKeyStore = new AzKeyStore(profilePath, AzureSession.Instance.KeyStoreFile, true, storageMocker.Object);
6868
AzureSession.Instance.RegisterComponent(AzKeyStore.Name, () => azKeyStore, true);
69+
AzureSession.Instance.RegisterComponent<AuthenticationTelemetry>(AuthenticationTelemetry.Name, () => new AuthenticationTelemetry());
6970
}
7071

7172
[Fact]

src/Accounts/Accounts.Test/EnvironmentCmdletTests.cs

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public EnvironmentCmdletTests(ITestOutputHelper output)
5050
XunitTracingInterceptor.AddToContext(new XunitTracingInterceptor(output));
5151
dataStore = new MemoryDataStore();
5252
AzureSession.Instance.DataStore = dataStore;
53+
if (!AzureSession.Instance.TryGetComponent(nameof(AuthenticationTelemetry), out AuthenticationTelemetry authenticationTelemetry))
54+
{
55+
AzureSession.Instance.RegisterComponent<AuthenticationTelemetry>(nameof(AuthenticationTelemetry), () => new AuthenticationTelemetry());
56+
}
5357
}
5458

5559
private void Cleanup()

src/Accounts/Accounts.Test/ErrorResolutionTests.cs

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using Hyak.Common;
16+
17+
using Microsoft.Azure.Commands.Common.Authentication;
18+
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
1619
using Microsoft.Azure.Commands.Profile.Errors;
1720
using Microsoft.Azure.Commands.ScenarioTest;
1821
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
@@ -43,6 +46,7 @@ public TestHyakException(string message, CloudHttpRequestErrorInfo request, Clou
4346
public void DoesNotThrowWithNullError()
4447
{
4548
TestExecutionHelpers.SetUpSessionAndProfile();
49+
AzureSession.Instance.RegisterComponent<AuthenticationTelemetry>(AuthenticationTelemetry.Name, () => new AuthenticationTelemetry());
4650
var cmdlet = new ResolveError();
4751
var output = cmdlet.ExecuteCmdletInPipeline<AzureErrorRecord>("Resolve-Error");
4852
Assert.True(output == null || output.Count == 0);
@@ -168,6 +172,7 @@ public void HandlesNullValuesInArmExceptions()
168172
public void LastParameterFindsLastError()
169173
{
170174
TestExecutionHelpers.SetUpSessionAndProfile();
175+
AzureSession.Instance.RegisterComponent<AuthenticationTelemetry>(AuthenticationTelemetry.Name, () => new AuthenticationTelemetry());
171176
var mock = new MockCommandRuntime();
172177
var cmdlet = new ResolveError { CommandRuntime = mock };
173178
var message = "RuntimeErrorMessage";

src/Accounts/Accounts.Test/ProfileCmdletTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public ProfileCmdletTests(ITestOutputHelper output)
5555
AzureSession.Instance.DataStore = dataStore;
5656
commandRuntimeMock = new MockCommandRuntime();
5757
AzureSession.Instance.AuthenticationFactory = new MockTokenAuthenticationFactory();
58+
AzureSession.Instance.RegisterComponent<AuthenticationTelemetry>(AuthenticationTelemetry.Name, () => new AuthenticationTelemetry());
5859
keyStore = SetMockedAzKeyStore();
5960
}
6061

src/Accounts/Accounts.Test/SilentReAuthByTenantCmdletTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
using System.Threading.Tasks;
3737
using Xunit;
3838
using Xunit.Abstractions;
39-
using Microsoft.CodeAnalysis.CSharp.Syntax;
40-
using Microsoft.Azure.Commands.Common.Authentication.Factories;
4139

4240
namespace Microsoft.Azure.Commands.ResourceManager.Common.Test
4341
{
@@ -326,6 +324,8 @@ private void InitializeSession()
326324
DefaultContext = defaultContext
327325
};
328326
cmdlet.profileClient = new RMProfileClient(profile);
327+
328+
AzureSession.Instance.RegisterComponent<AuthenticationTelemetry>(AuthenticationTelemetry.Name, () => new AuthenticationTelemetry());
329329
}
330330

331331
~SilentReAuthByTenantCmdletTest()

src/Accounts/Accounts.Test/TenantCmdletMockTests.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
using Microsoft.Azure.Commands.ScenarioTest;
2121
using Microsoft.Azure.Commands.TestFx.Mocks;
2222
using Microsoft.Azure.ServiceManagement.Common.Models;
23-
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
2423
using Microsoft.WindowsAzure.Commands.ScenarioTest;
2524
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2625
using Moq;
@@ -55,7 +54,7 @@ public TenantCmdletMockTests(ITestOutputHelper output)
5554
{
5655
TestExecutionHelpers.SetUpSessionAndProfile();
5756
XunitTracingInterceptor.AddToContext(new XunitTracingInterceptor(output));
58-
57+
AzureSession.Instance.RegisterComponent<AuthenticationTelemetry>(AuthenticationTelemetry.Name, () => new AuthenticationTelemetry());
5958
AzureSession.Instance.AuthenticationFactory = new MockTokenAuthenticationFactory();
6059
((MockTokenAuthenticationFactory)AzureSession.Instance.AuthenticationFactory).TokenProvider = (account, environment, tenant) =>
6160
new MockAccessToken

src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs

+1
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,7 @@ public void OnImport()
860860
AzureSession.Instance.RegisterComponent(nameof(MsalAccessTokenAcquirerFactory), () => new MsalAccessTokenAcquirerFactory());
861861
AzureSession.Instance.RegisterComponent<ISshCredentialFactory>(nameof(ISshCredentialFactory), () => new SshCredentialFactory());
862862
AzureSession.Instance.RegisterComponent<IOutputSanitizer>(nameof(IOutputSanitizer), () => new OutputSanitizer());
863+
AzureSession.Instance.RegisterComponent<AuthenticationTelemetry>(AuthenticationTelemetry.Name, () => new AuthenticationTelemetry());
863864
#if DEBUG || TESTCOVERAGE
864865
AzureSession.Instance.RegisterComponent<ITestCoverage>(nameof(ITestCoverage), () => new TestCoverage());
865866
#endif

src/Accounts/Accounts/ChangeLog.md

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
-->
2020

2121
## Upcoming Release
22+
* Fixed token in auxiliary authentication header.
23+
* Collected authentication method for telemetry in end process of cmdlet.
2224

2325
## Version 4.1.0
2426
* Added AppConfiguration ResourceId and Suffix endpoints for Mooncake and USGov clouds to fix issue [#24219]

src/Accounts/Accounts/Common/AzureContextModificationCmdlet.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ protected virtual void ModifyContext(Action<AzureRmProfile, RMProfileClient> con
4747
{
4848
var client = new RMProfileClient(profile)
4949
{
50-
WarningLog = (s) => WriteWarning(s)
50+
WarningLog = (s) => WriteWarning(s),
51+
CmdletContext = _cmdletContext
5152
};
5253
contextAction(profile.ToProfile(), client);
5354
}

src/Accounts/Accounts/Context/GetAzureRMContext.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public override void ExecuteCmdlet()
8484
var defaultProfile = DefaultProfile as AzureRmProfile;
8585
if (defaultProfile != null && string.Equals(AzureSession.Instance?.ARMContextSaveMode, "CurrentUser"))
8686
{
87-
defaultProfile.RefreshContextsFromCache();
87+
defaultProfile.RefreshContextsFromCache(_cmdletContext);
8888
}
8989
}
9090
catch (Exception e)

src/Accounts/Accounts/Default/GetAzureRmDefault.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public override void ExecuteCmdlet()
4040
IAzureContext context = DefaultContext;
4141
IResourceManagementClient client = AzureSession.Instance.ClientFactory.CreateCustomArmClient<ResourceManagementClient>(
4242
context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager),
43-
AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials(context, AzureEnvironment.Endpoint.ResourceManager),
43+
AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials(context, AzureEnvironment.Endpoint.ResourceManager, _cmdletContext),
4444
AzureSession.Instance.ClientFactory.GetCustomHandlers());
4545
client.SubscriptionId = context.Subscription.Id;
4646

src/Accounts/Accounts/Default/SetAzureRMDefault.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public override void ExecuteCmdlet()
5353
IAzureContext context = DefaultContext;
5454
IResourceManagementClient resourceManagementclient = AzureSession.Instance.ClientFactory.CreateCustomArmClient<ResourceManagementClient>(
5555
context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager),
56-
AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials(context, AzureEnvironment.Endpoint.ResourceManager),
56+
AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials(context, AzureEnvironment.Endpoint.ResourceManager, _cmdletContext),
5757
AzureSession.Instance.ClientFactory.GetCustomHandlers());
5858
resourceManagementclient.SubscriptionId = context.Subscription.Id;
5959

src/Accounts/Accounts/Environment/GetAzureRMEnvironment.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ protected override bool RequireDefaultContext()
4040

4141
public override void ExecuteCmdlet()
4242
{
43-
var profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.GetProfile<AzureRmProfile>());
43+
var profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.GetProfile<AzureRmProfile>())
44+
{
45+
WarningLog = (s) => WriteWarning(s),
46+
CmdletContext = _cmdletContext
47+
};
4448
var result = profileClient.ListEnvironments(Name).Select(s => new PSAzureEnvironment(s)).ToList();
4549
WriteObject(result, enumerateCollection: true);
4650
}

src/Accounts/Accounts/Models/RMProfileClient.cs

+12-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// ----------------------------------------------------------------------------------
1414
using Microsoft.Azure.Commands.Common.Authentication;
1515
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
16+
using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Interfaces;
17+
using Microsoft.Azure.Commands.Common.Authentication.Factories;
1618
using Microsoft.Azure.Commands.Common.Authentication.Models;
1719
using Microsoft.Azure.Commands.Common.Authentication.ResourceManager;
1820
using Microsoft.Azure.Commands.Profile.Models;
@@ -42,6 +44,8 @@ public class RMProfileClient
4244
public Action<string> InteractiveInformationLog;
4345
internal Func<string, string> PromptAndReadLine;
4446

47+
public ICmdletContext CmdletContext;
48+
4549
private List<AzureTenant> _queriedTenants = new List<AzureTenant>();
4650

4751
private IAzureContext DefaultContext
@@ -683,15 +687,21 @@ private IAccessToken AcquireAccessToken(
683687
return new SimpleAccessToken(account, tenantId);
684688
}
685689

690+
var optionalParameters = new Dictionary<string, object>()
691+
{
692+
{AuthenticationFactory.TokenCacheParameterName, _cache},
693+
{AuthenticationFactory.ResourceIdParameterName, resourceId },
694+
{AuthenticationFactory.CmdletContextParameterName, CmdletContext }
695+
};
696+
686697
return AzureSession.Instance.AuthenticationFactory.Authenticate(
687698
account,
688699
environment,
689700
tenantId,
690701
password,
691702
promptBehavior,
692703
promptAction,
693-
_cache,
694-
resourceId);
704+
optionalParameters);
695705
}
696706

697707
private bool TryGetTenantSubscription(IAccessToken accessToken,

src/Accounts/Accounts/Rest/InvokeAzRestMethodCommand.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ private IAzureRestClient InitializeServiceClient()
432432

433433
if (ByPath.Equals(this.ParameterSetName) || ByParameters.Equals(this.ParameterSetName))
434434
{
435-
serviceClient = AzureSession.Instance.ClientFactory.CreateArmClient<AzureRestClient>(context, AzureEnvironment.Endpoint.ResourceManager);
435+
serviceClient = AzureSession.Instance.ClientFactory.CreateArmClient<AzureRestClient>(context, AzureEnvironment.Endpoint.ResourceManager, _cmdletContext);
436436
}
437437
else if (ByURI.Equals(this.ParameterSetName))
438438
{
@@ -454,11 +454,11 @@ private IAzureRestClient InitializeServiceClient()
454454
ServiceClientCredentials creds = null;
455455
if (AzureSession.Instance.AuthenticationFactory is Commands.Common.Authentication.Factories.AuthenticationFactory factory)
456456
{
457-
creds = factory.GetServiceClientCredentials(context, targetResourceIdKey, targetResourceId);
457+
creds = factory.GetServiceClientCredentials(context, targetResourceIdKey, targetResourceId, _cmdletContext);
458458
}
459459
else
460460
{
461-
creds = AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials(context, targetResourceId);
461+
creds = AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials(context, targetResourceId, _cmdletContext);
462462
}
463463
Uri baseUri = new Uri($"{Uri.Scheme}://{Uri.Authority}");
464464
serviceClient = AzureSession.Instance.ClientFactory.CreateCustomArmClient<AzureRestClient>(baseUri, creds);

src/Accounts/Accounts/Subscription/GetAzureRMSubscription.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ protected override void BeginProcessing()
5959
throw new InvalidOperationException(Resources.ContextCannotBeNull);
6060
}
6161

62-
_client = new RMProfileClient(profile);
62+
_client = new RMProfileClient(profile)
63+
{
64+
WarningLog = (s) => WriteWarning(s),
65+
CmdletContext = _cmdletContext
66+
};
6367
_client.WarningLog = (s) => WriteWarning(s);
6468
}
6569

src/Accounts/Accounts/Tenant/GetAzureRMTenant.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ public class GetAzureRMTenantCommand : AzureRMCmdlet
4040

4141
public override void ExecuteCmdlet()
4242
{
43-
var profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.GetProfile<AzureRmProfile>());
43+
var profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.GetProfile<AzureRmProfile>())
44+
{
45+
WarningLog = (s) => WriteWarning(s),
46+
CmdletContext = _cmdletContext
47+
};
4448
profileClient.WarningLog = (message) => _tasks.Enqueue(new Task(() => this.WriteWarning(message)));
4549

4650
var tenants = profileClient.ListTenants(TenantId).Select((t) => new PSAzureTenant(t));

src/Accounts/Accounts/Token/GetAzureRmAccessToken.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414

1515
using Microsoft.Azure.Commands.Common.Authentication;
1616
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
17+
using Microsoft.Azure.Commands.Common.Authentication.Factories;
1718
using Microsoft.Azure.Commands.Profile.Models;
1819
using Microsoft.Azure.Commands.ResourceManager.Common;
1920
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
2021
using Microsoft.Azure.PowerShell.Authenticators;
2122
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2223

2324
using System;
25+
using System.Collections.Generic;
2426
using System.Linq;
2527
using System.Management.Automation;
2628
using System.Text.Json;
@@ -102,15 +104,20 @@ public override void ExecuteCmdlet()
102104
TenantId = context.Tenant?.Id;
103105
}
104106

107+
var optionalParameters = new Dictionary<string, object>()
108+
{
109+
{AuthenticationFactory.ResourceIdParameterName, resourceUrlOrId },
110+
{AuthenticationFactory.CmdletContextParameterName, _cmdletContext }
111+
};
112+
105113
IAccessToken accessToken = AzureSession.Instance.AuthenticationFactory.Authenticate(
106114
context.Account,
107115
context.Environment,
108116
TenantId,
109117
null,
110118
ShowDialog.Never,
111119
null,
112-
null,
113-
resourceUrlOrId);
120+
optionalParameters);
114121

115122
var result = new PSAccessToken()
116123
{

0 commit comments

Comments
 (0)