Skip to content

Commit

Permalink
Add Interface to Send Auth Info to Telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
msJinLei committed Jun 17, 2024
1 parent 7be70ef commit eeb4c86
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/Authentication.Abstractions/AuthenticationInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System.Collections.Concurrent;
using System;
using System.Collections.Generic;

namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions
{
public class AuthenticationInfo : IAuthenticationInfo
{
/// <summary>
/// Class name of the TokenCredential, stands for the authentication method
/// </summary>
public string TokenCredentialName { get; set; }

/// <summary>
/// Authority Uri to do the authentiation
/// </summary>
public string AuthorityUri { get; set; }

/// <summary>
/// Authentication process succeed or not.
/// </summary>
public bool AuthenticationSuccess { get; set; } = false;

/// <summary>
/// Additional properties for AuthenticationInfo
/// </summary>
public IDictionary<string, string> ExtendedProperties { get; } = new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase);

/// <summary>
/// Key to show whether token cache is enabled or not.
/// </summary>
public const string TokenCacheEnabled = "TokenCacheEnabled";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,10 @@ IAccessToken Authenticate(
/// <param name="account">The account to remove credentials for</param>
/// <param name="tokenCache">The TokenCache to remove credentials from</param>
void RemoveUser(IAzureAccount account, IAzureTokenCache tokenCache);

/// <summary>
/// Get the information to be recorded in Telemetry
/// </summary>
IAuthenticationInfo GetDataForTelemetry();
}
}
34 changes: 34 additions & 0 deletions src/Authentication.Abstractions/Interfaces/IAuthenticationInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions
{
public interface IAuthenticationInfo : IExtensibleModel
{
/// <summary>
/// Class name of the TokenCredential, stands for the authentication method
/// </summary>
string TokenCredentialName { get; set; }

/// <summary>
/// Authority Uri to do the authentiation
/// </summary>
string AuthorityUri { get; set; }

/// <summary>
/// Authentication process succeed or not.
/// </summary>
bool AuthenticationSuccess { get; set; }
}
}
2 changes: 2 additions & 0 deletions src/Common/AzurePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,8 @@ protected override void EndProcessing()
WriteWarningMessageForVersionUpgrade();
WriteSecretsWarningMessage();

_qosEvent.AuthInfo = AzureSession.Instance.AuthenticationFactory.GetDataForTelemetry();

if (MetricHelper.IsCalledByUser()
&& SurveyHelper.GetInstance().ShouldPromptAzSurvey()
&& (AzureSession.Instance.TryGetComponent<IConfigManager>(nameof(IConfigManager), out var configManager)
Expand Down
15 changes: 15 additions & 0 deletions src/Common/MetricHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,18 @@ public void SetPSHost(PSHost host)
{
}

private static void PopulateAuthenticationInfoFromQos(IAuthenticationInfo info, IDictionary<string, string> eventProperties)
{
eventProperties[$"auth-info-{nameof(info.TokenCredentialName).ToLower()}"] = info.TokenCredentialName;
eventProperties[$"auth-info-{nameof(info.AuthorityUri).ToLower()}"] = info.AuthorityUri;
eventProperties[$"auth-info-{nameof(info.TokenCacheEnabled).ToLower()}"] = info.TokenCacheEnabled.ToString();

foreach (var property in info.ExtendedProperties)
{
eventProperties[$"auth-info-{property.Key.ToLower()}"] = property.Value;
}
}

private void PopulatePropertiesFromQos(AzurePSQoSEvent qos, IDictionary<string, string> eventProperties, bool populateException = false)
{
if (qos == null)
Expand Down Expand Up @@ -452,6 +464,7 @@ private void PopulatePropertiesFromQos(AzurePSQoSEvent qos, IDictionary<string,
}

PopulateSanitizerPropertiesFromQos(qos, eventProperties);
PopulateAuthenticationInfoFromQos(qos.AuthInfo, eventProperties);

if (qos.InputFromPipeline != null)
{
Expand Down Expand Up @@ -661,6 +674,8 @@ public class AzurePSQoSEvent

public SanitizerTelemetry SanitizerInfo { get; set; }

public IAuthenticationInfo AuthInfo { get; set; }

public AzurePSQoSEvent()
{
StartTime = DateTimeOffset.Now;
Expand Down

0 comments on commit eeb4c86

Please sign in to comment.