Skip to content

Commit

Permalink
Improve the interface
Browse files Browse the repository at this point in the history
  • Loading branch information
msJinLei committed Aug 20, 2024
1 parent 8468613 commit 4574c39
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 15 deletions.
6 changes: 0 additions & 6 deletions src/Authentication.Abstractions/AuthenticationTelemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions
{
public class AuthenticationTelemetry: IAzureTelemetry <AuthTelemetryRecord>
{
//public delegate string RequestIdAccquirer();

//public static RequestIdAccquirer GetRequestId;

//private static string requestId;

public AuthenticationTelemetryData GetTelemetryRecord(ICmdletContext cmdletContext)
{
var records = PopTelemetryRecord(cmdletContext);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// ----------------------------------------------------------------------------------
//
// 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 Microsoft.Azure.Commands.Common.Authentication.Abstractions.Interfaces;

using System.Collections.Generic;

namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions.Extensions
{
public static class CmdletContextExtension
{
public static IDictionary<string, object> ToExtensibleParameters(this ICmdletContext cmdletContext)
{
if (cmdletContext != null)
{
return new Dictionary<string, object> { { nameof(ICmdletContext), cmdletContext } };
}
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System;
using System.Security;
using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Interfaces;
using System.Collections.Generic;

namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions
{
Expand All @@ -24,6 +25,27 @@ namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions
/// </summary>
public interface IAuthenticationFactory : IHyakAuthenticationFactory
{

/// <summary>
/// Returns IAccessToken if authentication succeeds or throws an exception if authentication fails.
/// </summary>
/// <param name="account">The azure account object</param>
/// <param name="environment">The azure environment object</param>
/// <param name="tenant">The AD tenant in most cases should be 'common'</param>
/// <param name="password">The AD account password</param>
/// <param name="promptBehavior">The prompt behavior</param>
/// <param name="promptAction">The prompt action used in DeviceFlow authentication</param>
/// <param name="optionalParameters">The optional parameters, may include TokenCache, ResourceId and CmdletContext</param>
/// <returns></returns>
IAccessToken Authenticate(
IAzureAccount account,
IAzureEnvironment environment,
string tenant,
SecureString password,
string promptBehavior,
Action<string> promptAction,
IDictionary<string, object> optionalParameters);

/// <summary>
/// Returns IAccessToken if authentication succeeds or throws an exception if authentication fails.
/// </summary>
Expand All @@ -44,7 +66,6 @@ IAccessToken Authenticate(
string promptBehavior,
Action<string> promptAction,
IAzureTokenCache tokenCache,
ICmdletContext cmdletContext,
string resourceId = AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId);

/// <summary>
Expand All @@ -65,9 +86,15 @@ IAccessToken Authenticate(
SecureString password,
string promptBehavior,
Action<string> promptAction,
ICmdletContext cmdletContext,
string resourceId = AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId);

/// <summary>
/// Get AutoRest credentials for the given context
/// </summary>
/// <param name="context">The target azure context</param>
/// <returns>AutoRest client credentials targeting the given context</returns>
ServiceClientCredentials GetServiceClientCredentials(IAzureContext context);

/// <summary>
/// Get AutoRest credentials for the given context
/// </summary>
Expand All @@ -80,9 +107,17 @@ IAccessToken Authenticate(
/// Get AutoRest credebntials using the given context and named endpoint
/// </summary>
/// <param name="context">The context to use for authentication</param>
/// <param name="cmdletContext">The caller cmdlet context</param>
/// <param name="targetEndpoint">The named endpoint the AutoRest client will target</param>
/// <returns>AutoRest client crentials targeting the given context and endpoint</returns>
ServiceClientCredentials GetServiceClientCredentials(IAzureContext context, string targetEndpoint);

/// <summary>
/// Get AutoRest credebntials using the given context and named endpoint
/// </summary>
/// <param name="context">The context to use for authentication</param>
/// <param name="targetEndpoint">The named endpoint the AutoRest client will target</param>
/// <param name="cmdletContext">The caller cmdlet context</param>
/// <returns>AutoRest client crentials targeting the given context and endpoint</returns>
ServiceClientCredentials GetServiceClientCredentials(IAzureContext context, string targetEndpoint, ICmdletContext cmdletContext);

/// <summary>
Expand Down
39 changes: 34 additions & 5 deletions src/Authentication.Abstractions/Interfaces/IAzureTelemetry.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,61 @@
using System.Collections.Concurrent;
using Newtonsoft.Json.Linq;
using System.Threading;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions.Interfaces
{
public abstract class IAzureTelemetry <T>
{
private ConcurrentDictionary<string, IList<T>> telemetryDataAccquirer = new ConcurrentDictionary<string, IList<T>>();


protected int historyKeyCount = 0;
public int KeysAllCount { get => historyKeyCount; }

protected int currentKeyCount = 0;
public int KeysCurrentCount { get => currentKeyCount; }

protected int nullCmdletContextCount = 0;
public int EmptyCmdletContextCount { get => nullCmdletContextCount; }

protected int keyNotFoundCount = 0;
public int KeyNotFoundCount { get => keyNotFoundCount; }

public bool PushTelemetryRecord(ICmdletContext cmdletContext, T record)
{
if (cmdletContext != null && cmdletContext.IsValid && record != null)
{
if (!telemetryDataAccquirer.ContainsKey(cmdletContext.CmdletId))
{
telemetryDataAccquirer[cmdletContext.CmdletId] = new List<T>();
Interlocked.Increment(ref historyKeyCount);
Interlocked.Increment(ref currentKeyCount);
}
telemetryDataAccquirer[cmdletContext.CmdletId].Add(record);
return true;
}
Interlocked.Increment(ref nullCmdletContextCount);
return false;
}

public IList<T> PopTelemetryRecord(ICmdletContext cmdletContext)
{
if (cmdletContext != null && cmdletContext.IsValid && telemetryDataAccquirer.ContainsKey(cmdletContext.CmdletId))
if (cmdletContext != null && cmdletContext.IsValid)
{
if (telemetryDataAccquirer.ContainsKey(cmdletContext.CmdletId))
{
telemetryDataAccquirer.TryRemove(cmdletContext.CmdletId, out IList<T> records);
Interlocked.Decrement(ref currentKeyCount);
return records;
}
else
{
Interlocked.Increment(ref keyNotFoundCount);
}
}
else
{
telemetryDataAccquirer.TryRemove(cmdletContext.CmdletId, out IList<T> records);
return records;
Interlocked.Increment(ref nullCmdletContextCount);
}
return null;
}
Expand Down
1 change: 1 addition & 0 deletions src/Common/AzurePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,7 @@ protected void LogQosEvent()
throw new NullReferenceException("AuthenticationTelemetry not registered");
}
_qosEvent.AuthTelemetry = authenticationTelemetry.GetTelemetryRecord(_cmdletContext);
WriteDebugWithTimestamp($"TotalKeyCount={authenticationTelemetry.KeysAllCount}; CurrentKeyCount={authenticationTelemetry.KeysCurrentCount}; EmptyCmdletContextCount={authenticationTelemetry.EmptyCmdletContextCount}; KeysNotFoundCount={authenticationTelemetry.KeyNotFoundCount}");

if (!IsUsageMetricEnabled && (!IsErrorMetricEnabled || _qosEvent.IsSuccess))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Extensions;
using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Interfaces;
using Microsoft.Azure.Internal.Subscriptions;
using Microsoft.Azure.Internal.Subscriptions.Models;
Expand All @@ -36,7 +37,7 @@ internal static IAccessToken AcquireAccessToken(IAzureAccount account, IAzureEnv
null,
ShowDialog.Never,
null,
cmdletContext);
cmdletContext.ToExtensibleParameters());
}

internal static Dictionary<string, AzureSubscription> GetTenantsForSubscriptions(List<string> subscriptionIds, IAzureContext defaultContext, ICmdletContext cmdletContext)
Expand Down

0 comments on commit 4574c39

Please sign in to comment.