diff --git a/src/Authentication.Abstractions/AuthenticationTelemetry.cs b/src/Authentication.Abstractions/AuthenticationTelemetry.cs index 75172387f2..4778eeb278 100644 --- a/src/Authentication.Abstractions/AuthenticationTelemetry.cs +++ b/src/Authentication.Abstractions/AuthenticationTelemetry.cs @@ -1,48 +1,19 @@ -// ---------------------------------------------------------------------------------- -// -// 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.Generic; +using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Interfaces; namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions { - /// - /// A model class for a list of authenction telemetry records. - /// - public class AuthenticationTelemetry + public class AuthenticationTelemetry: IAzureTelemetry { - /// - /// The first record of authentication telemetry data, usually describes the main method of this authentication process. - /// - public IAuthTelemetryRecord Head { get; } = null; + //public delegate string RequestIdAccquirer(); - /// - /// The left part of authentication telemetry records. - /// - public IList Tail { get; } = new List(); + //public static RequestIdAccquirer GetRequestId; - public AuthenticationTelemetry(IEnumerable records) - { - var enumerator = records.GetEnumerator(); - if (enumerator.MoveNext()) - { - Head = enumerator.Current; - } + //private static string requestId; - while (enumerator.MoveNext()) - { - Tail.Add(enumerator.Current); - } + public AuthenticationTelemetryData GetTelemetryRecord(ICmdletContext cmdletContext) + { + var records = PopTelemetryRecord(cmdletContext); + return records == null ? null : new AuthenticationTelemetryData(records); } } } diff --git a/src/Authentication.Abstractions/AuthenticationTelemetryData.cs b/src/Authentication.Abstractions/AuthenticationTelemetryData.cs new file mode 100644 index 0000000000..fd256055e4 --- /dev/null +++ b/src/Authentication.Abstractions/AuthenticationTelemetryData.cs @@ -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.Generic; + +namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions +{ + /// + /// A model class for a list of authenction telemetry records. + /// + public class AuthenticationTelemetryData + { + /// + /// The first record of authentication telemetry data, usually describes the main method of this authentication process. + /// + public IAuthTelemetryRecord Head { get; } = null; + + /// + /// The left part of authentication telemetry records. + /// + public IList Tail { get; } = new List(); + + public AuthenticationTelemetryData(IEnumerable records) + { + var enumerator = records.GetEnumerator(); + if (enumerator.MoveNext()) + { + Head = enumerator.Current; + } + + while (enumerator.MoveNext()) + { + Tail.Add(enumerator.Current); + } + } + } +} diff --git a/src/Authentication.Abstractions/AzureCmdletContext.cs b/src/Authentication.Abstractions/AzureCmdletContext.cs new file mode 100644 index 0000000000..fe70917ee9 --- /dev/null +++ b/src/Authentication.Abstractions/AzureCmdletContext.cs @@ -0,0 +1,30 @@ +using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Interfaces; + +using System; +using System.Collections.Generic; +using System.Text; + +namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions +{ + public class AzureCmdletContext : ICmdletContext + { + private string cmdletId; + + public const ICmdletContext CmdletNone = null; + + public AzureCmdletContext(string id) + { + cmdletId = id; + } + public string CmdletId + { + get => cmdletId; + set => cmdletId = value; + } + + public bool IsValid + { + get => string.IsNullOrEmpty(cmdletId); + } + } +} diff --git a/src/Authentication.Abstractions/Interfaces/IAuthenticationFactory.cs b/src/Authentication.Abstractions/Interfaces/IAuthenticationFactory.cs index 377fc7cef8..dfe39f7d62 100644 --- a/src/Authentication.Abstractions/Interfaces/IAuthenticationFactory.cs +++ b/src/Authentication.Abstractions/Interfaces/IAuthenticationFactory.cs @@ -15,6 +15,7 @@ using Microsoft.Rest; using System; using System.Security; +using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Interfaces; namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions { @@ -43,6 +44,7 @@ IAccessToken Authenticate( string promptBehavior, Action promptAction, IAzureTokenCache tokenCache, + ICmdletContext cmdletContext, string resourceId = AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId); /// @@ -63,22 +65,25 @@ IAccessToken Authenticate( SecureString password, string promptBehavior, Action promptAction, + ICmdletContext cmdletContext, string resourceId = AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId); /// /// Get AutoRest credentials for the given context /// /// The target azure context + /// The caller cmdlet context /// AutoRest client credentials targeting the given context - ServiceClientCredentials GetServiceClientCredentials(IAzureContext context); + ServiceClientCredentials GetServiceClientCredentials(IAzureContext context, ICmdletContext cmdletContext); /// /// Get AutoRest credebntials using the given context and named endpoint /// /// The context to use for authentication + /// The caller cmdlet context /// The named endpoint the AutoRest client will target /// AutoRest client crentials targeting the given context and endpoint - ServiceClientCredentials GetServiceClientCredentials(IAzureContext context, string targetEndpoint); + ServiceClientCredentials GetServiceClientCredentials(IAzureContext context, string targetEndpoint, ICmdletContext cmdletContext); /// /// Get service client credentials with initial token and delegate for renewing @@ -94,10 +99,5 @@ IAccessToken Authenticate( /// The account to remove credentials for /// The TokenCache to remove credentials from void RemoveUser(IAzureAccount account, IAzureTokenCache tokenCache); - - /// - /// Get the information to be recorded in Telemetry - /// - AuthenticationTelemetry GetDataForTelemetry(string requestId); } } diff --git a/src/Authentication.Abstractions/Interfaces/IAzureTelemetry.cs b/src/Authentication.Abstractions/Interfaces/IAzureTelemetry.cs new file mode 100644 index 0000000000..0cf63cf8b3 --- /dev/null +++ b/src/Authentication.Abstractions/Interfaces/IAzureTelemetry.cs @@ -0,0 +1,34 @@ +using System.Collections.Concurrent; +using System.Collections.Generic; + +namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions.Interfaces +{ + public abstract class IAzureTelemetry + { + private ConcurrentDictionary> telemetryDataAccquirer = new ConcurrentDictionary>(); + + public bool PushTelemetryRecord(ICmdletContext cmdletContext, T record) + { + if (cmdletContext != null && cmdletContext.IsValid && record != null) + { + if (!telemetryDataAccquirer.ContainsKey(cmdletContext.CmdletId)) + { + telemetryDataAccquirer[cmdletContext.CmdletId] = new List(); + } + telemetryDataAccquirer[cmdletContext.CmdletId].Add(record); + return true; + } + return false; + } + + public IList PopTelemetryRecord(ICmdletContext cmdletContext) + { + if (cmdletContext != null && cmdletContext.IsValid && telemetryDataAccquirer.ContainsKey(cmdletContext.CmdletId)) + { + telemetryDataAccquirer.TryRemove(cmdletContext.CmdletId, out IList records); + return records; + } + return null; + } + } +} diff --git a/src/Authentication.Abstractions/Interfaces/IClientFactory.cs b/src/Authentication.Abstractions/Interfaces/IClientFactory.cs index dd6eb334fd..c1c24fa6d9 100644 --- a/src/Authentication.Abstractions/Interfaces/IClientFactory.cs +++ b/src/Authentication.Abstractions/Interfaces/IClientFactory.cs @@ -13,6 +13,7 @@ // ---------------------------------------------------------------------------------- using Microsoft.Azure.Commands.Common.Authentication.Abstractions; +using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Interfaces; using Microsoft.Azure.Commands.Common.Authentication.Models; using Microsoft.Rest; using System; @@ -34,9 +35,10 @@ public interface IClientFactory: IHyakClientFactory /// The client type to create /// The azure context to target /// The named endpoint the client shoulld target + /// Cmdlet Context /// A client properly authenticated in the given context, properly configured for use with Azure PowerShell, /// targeting the given named endpoint in the targeted environment - TClient CreateArmClient(IAzureContext context, string endpoint) where TClient : ServiceClient; + TClient CreateArmClient(IAzureContext context, string endpoint, ICmdletContext cmdletContext) where TClient : ServiceClient; /// /// Create a properly configured AutoRest client using custom client parameters diff --git a/src/Authentication.Abstractions/Interfaces/ICmdletContext.cs b/src/Authentication.Abstractions/Interfaces/ICmdletContext.cs new file mode 100644 index 0000000000..600afbf4fb --- /dev/null +++ b/src/Authentication.Abstractions/Interfaces/ICmdletContext.cs @@ -0,0 +1,22 @@ +// ---------------------------------------------------------------------------------- +// +// 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.Interfaces +{ + public interface ICmdletContext + { + string CmdletId { get; } + + bool IsValid { get; } + } +} diff --git a/src/Common/AzurePSCmdlet.cs b/src/Common/AzurePSCmdlet.cs index 25ff917042..4394883131 100644 --- a/src/Common/AzurePSCmdlet.cs +++ b/src/Common/AzurePSCmdlet.cs @@ -14,7 +14,7 @@ using Microsoft.Azure.Commands.Common.Authentication; using Microsoft.Azure.Commands.Common.Authentication.Abstractions; -using Microsoft.Azure.Commands.Common.Authentication.Utilities; +using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Interfaces; using Microsoft.Azure.PowerShell.Common.Config; using Microsoft.Azure.PowerShell.Common.Share.Survey; using Microsoft.Azure.PowerShell.Common.UpgradeNotification; @@ -94,6 +94,7 @@ protected AzurePSDataCollectionProfile _dataCollectionProfile protected static string _sessionId = Guid.NewGuid().ToString(); protected const string _fileTimeStampSuffixFormat = "yyyy-MM-dd-THH-mm-ss-fff"; protected string _clientRequestId = Guid.NewGuid().ToString(); + protected ICmdletContext _cmdletContext; protected static DateTimeOffset? _previousEndTime = null; protected MetricHelper _metricHelper; protected AzurePSQoSEvent _qosEvent; @@ -377,16 +378,12 @@ protected override void BeginProcessing() InitDebuggingFilter(); SetupDebuggingTraces(); SetupHttpClientPipeline(); + _cmdletContext = new AzureCmdletContext(_clientRequestId); base.BeginProcessing(); //Now see if the cmdlet has any Breaking change attributes on it and process them if it does //This will print any breaking change attribute messages that are applied to the cmdlet WriteBreakingChangeOrPreviewMessage(); - - if (AzureSession.Instance.TryGetComponent(nameof(ThreadCmdldetMap), out ThreadCmdldetMap threadCmdletMap)) - { - threadCmdletMap.PushCmdletId(this._clientRequestId); - } } private void WriteBreakingChangeOrPreviewMessage() @@ -849,7 +846,12 @@ protected void LogQosEvent() _qosEvent.ParameterSetName = this.ParameterSetName; _qosEvent.FinishQosEvent(); - _qosEvent.AuthTelemetry = AzureSession.Instance.AuthenticationFactory.GetDataForTelemetry(_qosEvent.ClientRequestId); + + if (!AzureSession.Instance.TryGetComponent(nameof(AuthenticationTelemetry), out AuthenticationTelemetry authenticationTelemetry)) + { + throw new NullReferenceException("AuthenticationTelemetry not registered"); + } + _qosEvent.AuthTelemetry = authenticationTelemetry.GetTelemetryRecord(_cmdletContext); if (!IsUsageMetricEnabled && (!IsErrorMetricEnabled || _qosEvent.IsSuccess)) { diff --git a/src/Common/MetricHelper.cs b/src/Common/MetricHelper.cs index 6e99e16528..6b2deec069 100644 --- a/src/Common/MetricHelper.cs +++ b/src/Common/MetricHelper.cs @@ -292,7 +292,7 @@ public void SetPSHost(PSHost host) { } - private static void PopulateAuthenticationPropertiesFromQos(AuthenticationTelemetry telemetry, IDictionary eventProperties) + private static void PopulateAuthenticationPropertiesFromQos(AuthenticationTelemetryData telemetry, IDictionary eventProperties) { var record = telemetry.Head; eventProperties[$"{AuthTelemetryRecord.AuthTelemetryPropertyHeadPrefix}-{nameof(record.TokenCredentialName).ToLower()}"] = record.TokenCredentialName; @@ -693,7 +693,7 @@ public class AzurePSQoSEvent public SanitizerTelemetry SanitizerInfo { get; set; } - public AuthenticationTelemetry AuthTelemetry { get; set; } + public AuthenticationTelemetryData AuthTelemetry { get; set; } public AzurePSQoSEvent() { diff --git a/src/Common/ThreadCmdldetMap.cs b/src/Common/ThreadCmdldetMap.cs deleted file mode 100644 index 50f658bc12..0000000000 --- a/src/Common/ThreadCmdldetMap.cs +++ /dev/null @@ -1,66 +0,0 @@ -// ---------------------------------------------------------------------------------- -// -// 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.IO; -using System.Threading; - -namespace Microsoft.Azure.Commands.Common.Authentication.Utilities -{ - public class ThreadCmdldetMap - { - private ConcurrentDictionary map = new ConcurrentDictionary(); - - private static string logFile = Path.Combine(AzureSession.Instance.ProfileDirectory, "threadCmdletMap.log"); - - private void WriteLog(string log) - { - if (!File.Exists(logFile)) - { - using (StreamWriter sw = File.CreateText(logFile)) - { - sw.WriteLine(log); - } - } - using (StreamWriter sw = File.AppendText(logFile)) - { - sw.WriteLine(log); - } - } - - public void PushCmdletId(string cmdletId) - { -#if DEBUG - WriteLog($"[PushCmdletId] ThreadId={Thread.CurrentThread.ManagedThreadId}, CmdletId={cmdletId}"); -#endif - map[Thread.CurrentThread.ManagedThreadId] = cmdletId; - } - - public string PopCmdletId() - { - var id = Thread.CurrentThread.ManagedThreadId; - if (map.ContainsKey(id)) - { - map.TryRemove(id, out string cmdletId); -#if DEBUG - WriteLog($"[PopCmdletId] ThreadId={id}, CmdletId={cmdletId}"); -#endif - return cmdletId; - } -#if DEBUG - WriteLog($"[PopCmdletId] ThreadId={id}, CmdletId=NotFound"); -#endif - return string.Empty; - } - } -} diff --git a/src/Graph.Rbac/Version1_6.20190326/ActiveDirectory/ActiveDirectoryClient.cs b/src/Graph.Rbac/Version1_6.20190326/ActiveDirectory/ActiveDirectoryClient.cs index 3852fe4910..13302d790d 100644 --- a/src/Graph.Rbac/Version1_6.20190326/ActiveDirectory/ActiveDirectoryClient.cs +++ b/src/Graph.Rbac/Version1_6.20190326/ActiveDirectory/ActiveDirectoryClient.cs @@ -14,6 +14,7 @@ using Microsoft.Azure.Commands.Common.Authentication; using Microsoft.Azure.Commands.Common.Authentication.Abstractions; +using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Interfaces; using Microsoft.Azure.Commands.Common.Authentication.Models; using Microsoft.Azure.Commands.ResourceManager.Common.Paging; using Microsoft.Azure.Graph.RBAC.Version1_6_20190326; @@ -32,6 +33,8 @@ namespace Microsoft.Azure.Graph.RBAC.Version1_6_20190326.ActiveDirectory { public class ActiveDirectoryClient { + public ICmdletContext CmdletContext { get; set; } + public GraphRbacManagementClient GraphClient { get; private set; } /// @@ -41,7 +44,7 @@ public class ActiveDirectoryClient public ActiveDirectoryClient(IAzureContext context) { GraphClient = AzureSession.Instance.ClientFactory.CreateArmClient( - context, AzureEnvironment.Endpoint.Graph); + context, AzureEnvironment.Endpoint.Graph, CmdletContext); GraphClient.TenantID = context.Tenant.Id.ToString(); } diff --git a/src/Graph.Rbac/Version1_6/ActiveDirectory/ActiveDirectoryClient.cs b/src/Graph.Rbac/Version1_6/ActiveDirectory/ActiveDirectoryClient.cs index 50cc0c402c..eec3692952 100644 --- a/src/Graph.Rbac/Version1_6/ActiveDirectory/ActiveDirectoryClient.cs +++ b/src/Graph.Rbac/Version1_6/ActiveDirectory/ActiveDirectoryClient.cs @@ -14,6 +14,7 @@ using Microsoft.Azure.Commands.Common.Authentication; using Microsoft.Azure.Commands.Common.Authentication.Abstractions; +using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Interfaces; using Microsoft.Azure.Commands.Common.Authentication.Models; using Microsoft.Azure.Commands.ResourceManager.Common.Paging; using Microsoft.Azure.Graph.RBAC; @@ -32,6 +33,8 @@ namespace Microsoft.Azure.Graph.RBAC.Version1_6.ActiveDirectory { public class ActiveDirectoryClient { + public ICmdletContext CmdletContext { get; set; } + public GraphRbacManagementClient GraphClient { get; private set; } /// @@ -41,7 +44,7 @@ public class ActiveDirectoryClient public ActiveDirectoryClient(IAzureContext context) { GraphClient = AzureSession.Instance.ClientFactory.CreateArmClient( - context, AzureEnvironment.Endpoint.Graph); + context, AzureEnvironment.Endpoint.Graph, CmdletContext); GraphClient.TenantID = context.Tenant.Id.ToString(); } diff --git a/src/Network/Common/NetworkClient.cs b/src/Network/Common/NetworkClient.cs index b9e597d4a1..ce32765875 100644 --- a/src/Network/Common/NetworkClient.cs +++ b/src/Network/Common/NetworkClient.cs @@ -14,6 +14,7 @@ using Microsoft.Azure.Commands.Common.Authentication; using Microsoft.Azure.Commands.Common.Authentication.Abstractions; +using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Interfaces; using Microsoft.Azure.Commands.Common.Authentication.Models; using Microsoft.Azure.Management.Internal.Network.Version2017_03_01; using Microsoft.Azure.Management.Internal.Network.Version2017_03_01.Models; @@ -42,8 +43,7 @@ public partial class NetworkClient public Action WarningLogger { get; set; } - public NetworkClient(IAzureContext context) - : this(AzureSession.Instance.ClientFactory.CreateArmClient(context, AzureEnvironment.Endpoint.ResourceManager)) + public NetworkClient(IAzureContext context): this(AzureSession.Instance.ClientFactory.CreateArmClient(context, AzureEnvironment.Endpoint.ResourceManager, AzureCmdletContext.CmdletNone)) { } diff --git a/src/ResourceManager.Test/AzureRMCmdletAuthenticationUnitTests.cs b/src/ResourceManager.Test/AzureRMCmdletAuthenticationUnitTests.cs index 24d777af04..363f01088f 100644 --- a/src/ResourceManager.Test/AzureRMCmdletAuthenticationUnitTests.cs +++ b/src/ResourceManager.Test/AzureRMCmdletAuthenticationUnitTests.cs @@ -15,6 +15,7 @@ using Microsoft.Azure.Commands.Common.Authentication; using Microsoft.Azure.Commands.Common.Authentication.Abstractions; using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Core; +using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Interfaces; using Microsoft.Azure.Commands.ResourceManager.Common; using Moq; @@ -49,8 +50,9 @@ private static IAzureSession CreateAuthenticationFactory() It.IsAny(), It.IsAny(), It.IsAny>(), - It.IsAny())).Returns, string> - ((a, e, t, w, b, p, r) => new MockAccessToken(azureAccount.Id, t, azureAccount.Type, t)); + It.IsAny(), + It.IsAny())).Returns, ICmdletContext, string> + ((a, e, t, w, b, p, c, r) => new MockAccessToken(azureAccount.Id, t, azureAccount.Type, t)); session.SetupProperty(m => m.AuthenticationFactory, authFactory.Object); return session.Object; } diff --git a/src/ResourceManager/Version2016_09_01/ArgumentCompleters/LocationCompleter.cs b/src/ResourceManager/Version2016_09_01/ArgumentCompleters/LocationCompleter.cs index 3cdbb1db78..098128ec1b 100644 --- a/src/ResourceManager/Version2016_09_01/ArgumentCompleters/LocationCompleter.cs +++ b/src/ResourceManager/Version2016_09_01/ArgumentCompleters/LocationCompleter.cs @@ -25,6 +25,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters using System.Linq; using System.Management.Automation; using System.Collections.Concurrent; + using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Interfaces; /// @@ -49,7 +50,7 @@ protected static IDictionary> ResourceTypeLocationDi { try { - IResourceManagementClient client = AzureSession.Instance.ClientFactory.CreateArmClient(context, AzureEnvironment.Endpoint.ResourceManager); + IResourceManagementClient client = AzureSession.Instance.ClientFactory.CreateArmClient(context, AzureEnvironment.Endpoint.ResourceManager, AzureCmdletContext.CmdletNone); var allProviders = client.Providers.ListAsync(); if (_timeout == -1) { diff --git a/src/ResourceManager/Version2016_09_01/ArgumentCompleters/ResourceGroupCompleter.cs b/src/ResourceManager/Version2016_09_01/ArgumentCompleters/ResourceGroupCompleter.cs index 5e4997468c..98c1725d65 100644 --- a/src/ResourceManager/Version2016_09_01/ArgumentCompleters/ResourceGroupCompleter.cs +++ b/src/ResourceManager/Version2016_09_01/ArgumentCompleters/ResourceGroupCompleter.cs @@ -48,7 +48,7 @@ protected static IList ResourceGroupNames var tempResourceGroupList = new List(); try { - var client = AzureSession.Instance.ClientFactory.CreateArmClient(context, AzureEnvironment.Endpoint.ResourceManager); + var client = AzureSession.Instance.ClientFactory.CreateArmClient(context, AzureEnvironment.Endpoint.ResourceManager, AzureCmdletContext.CmdletNone); // Retrieve only the first page of ResourceGroups to display to the user var resourceGroups = client.ResourceGroups.ListAsync(); diff --git a/src/ResourceManager/Version2016_09_01/ArgumentCompleters/ResourceIdCompleter.cs b/src/ResourceManager/Version2016_09_01/ArgumentCompleters/ResourceIdCompleter.cs index 64d85d4145..790f38ab53 100644 --- a/src/ResourceManager/Version2016_09_01/ArgumentCompleters/ResourceIdCompleter.cs +++ b/src/ResourceManager/Version2016_09_01/ArgumentCompleters/ResourceIdCompleter.cs @@ -59,7 +59,7 @@ public static IEnumerable GetResourceIds(string resourceType) return Cache[contextHash].ResourceInfoList; } - var client = AzureSession.Instance.ClientFactory.CreateArmClient(context, AzureEnvironment.Endpoint.ResourceManager); + var client = AzureSession.Instance.ClientFactory.CreateArmClient(context, AzureEnvironment.Endpoint.ResourceManager, AzureCmdletContext.CmdletNone); var odata = new ODataQuery(r => r.ResourceType == resourceType); diff --git a/src/ResourceManager/Version2016_09_01/ArgumentCompleters/ResourceNameCompleter.cs b/src/ResourceManager/Version2016_09_01/ArgumentCompleters/ResourceNameCompleter.cs index 28c7eecb0b..4c07f19a81 100644 --- a/src/ResourceManager/Version2016_09_01/ArgumentCompleters/ResourceNameCompleter.cs +++ b/src/ResourceManager/Version2016_09_01/ArgumentCompleters/ResourceNameCompleter.cs @@ -121,7 +121,7 @@ public static string CreateFilter( public static List GetResourceIdsFromClient(string resourceType, string resourceGroupName) { IAzureContext context = AzureRmProfileProvider.Instance?.Profile?.DefaultContext; - IResourceManagementClient client = AzureSession.Instance.ClientFactory.CreateArmClient(context, AzureEnvironment.Endpoint.ResourceManager); + IResourceManagementClient client = AzureSession.Instance.ClientFactory.CreateArmClient(context, AzureEnvironment.Endpoint.ResourceManager, AzureCmdletContext.CmdletNone); var odataQuery = new ODataQuery(r => r.ResourceType == resourceType); var allProviders = string.IsNullOrWhiteSpace(resourceGroupName) diff --git a/src/ResourceManager/Version2016_09_01/ArgumentCompleters/ResourceTypeCompleter.cs b/src/ResourceManager/Version2016_09_01/ArgumentCompleters/ResourceTypeCompleter.cs index 74ec1f59c1..137202c340 100644 --- a/src/ResourceManager/Version2016_09_01/ArgumentCompleters/ResourceTypeCompleter.cs +++ b/src/ResourceManager/Version2016_09_01/ArgumentCompleters/ResourceTypeCompleter.cs @@ -48,7 +48,7 @@ protected static IList ResourceTypes var tempResourceTypeList = new List(); try { - var client = AzureSession.Instance.ClientFactory.CreateArmClient(context, AzureEnvironment.Endpoint.ResourceManager); + var client = AzureSession.Instance.ClientFactory.CreateArmClient(context, AzureEnvironment.Endpoint.ResourceManager, AzureCmdletContext.CmdletNone); var resourceTypes = new List(); var task = client.Providers.ListAsync(); if (task.Wait(TimeSpan.FromSeconds(_timeout))) diff --git a/src/ResourceManager/Version2016_09_01/ArgumentCompleters/ScopeCompleter.cs b/src/ResourceManager/Version2016_09_01/ArgumentCompleters/ScopeCompleter.cs index 624964fae2..5e1838ce01 100644 --- a/src/ResourceManager/Version2016_09_01/ArgumentCompleters/ScopeCompleter.cs +++ b/src/ResourceManager/Version2016_09_01/ArgumentCompleters/ScopeCompleter.cs @@ -45,7 +45,7 @@ protected static IList Scopes var tempScopeList = new List(); try { - var client = AzureSession.Instance.ClientFactory.CreateArmClient(context, AzureEnvironment.Endpoint.ResourceManager); + var client = AzureSession.Instance.ClientFactory.CreateArmClient(context, AzureEnvironment.Endpoint.ResourceManager, AzureCmdletContext.CmdletNone); // Retrieve only the first page of ResourceGroups to use for scopes var resourceGroups = client.ResourceGroups.ListAsync(); diff --git a/src/ResourceManager/Version2016_09_01/AzureRMCmdlet.cs b/src/ResourceManager/Version2016_09_01/AzureRMCmdlet.cs index 52e42b2209..34c3634afc 100644 --- a/src/ResourceManager/Version2016_09_01/AzureRMCmdlet.cs +++ b/src/ResourceManager/Version2016_09_01/AzureRMCmdlet.cs @@ -220,7 +220,7 @@ private List CheckAccessToSubscriptions(IEnumerable //this does not mean that the user does not have access to the subs, it just menas that the local context did not have them //We gotta now call into the subscription RP and see if the user really does not have access to these subscriptions - var result = Utilities.SubscriptionAndTenantHelper.GetTenantsForSubscriptions(subscriptionsNotInDefaultProfile.ToList(), DefaultContext); + var result = Utilities.SubscriptionAndTenantHelper.GetTenantsForSubscriptions(subscriptionsNotInDefaultProfile.ToList(), DefaultContext, _cmdletContext); if (result.Count < subscriptionsNotInDefaultProfile.Count()) { @@ -245,7 +245,8 @@ private IAccessToken GetTokenForTenant(string tenantId) { return Utilities.SubscriptionAndTenantHelper.AcquireAccessToken(DefaultContext.Account, DefaultContext.Environment, - tenantId); + tenantId, + _cmdletContext); } protected override string DataCollectionWarning @@ -449,7 +450,7 @@ protected override void BeginProcessing() { var client = new ResourceManagementClient( context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager), - AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials(context, AzureEnvironment.Endpoint.ResourceManager)); + AzureSession.Instance.AuthenticationFactory.GetServiceClientCredentials(context, AzureEnvironment.Endpoint.ResourceManager, _cmdletContext)); client.SubscriptionId = context.Subscription.Id; return client; }, diff --git a/src/ResourceManager/Version2016_09_01/Tags/TagsClient.cs b/src/ResourceManager/Version2016_09_01/Tags/TagsClient.cs index 388dece521..7c2d87afe9 100644 --- a/src/ResourceManager/Version2016_09_01/Tags/TagsClient.cs +++ b/src/ResourceManager/Version2016_09_01/Tags/TagsClient.cs @@ -20,6 +20,7 @@ using Microsoft.Azure.Management.Internal.Resources; using Microsoft.WindowsAzure.Commands.Utilities.Common; using Microsoft.Azure.Commands.Common.Authentication.Abstractions; +using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Interfaces; namespace Microsoft.Azure.Commands.ResourceManager.Common.Tags { @@ -37,10 +38,8 @@ public class TagsClient /// Creates new tags client instance. /// /// The Azure context instance - public TagsClient(IAzureContext context) - : this(AzureSession.Instance.ClientFactory.CreateArmClient(context, AzureEnvironment.Endpoint.ResourceManager)) + public TagsClient(IAzureContext context): this(AzureSession.Instance.ClientFactory.CreateArmClient(context, AzureEnvironment.Endpoint.ResourceManager, AzureCmdletContext.CmdletNone)) { - } /// diff --git a/src/ResourceManager/Version2016_09_01/Utilities/SubscriptionAndTenantHelper.cs b/src/ResourceManager/Version2016_09_01/Utilities/SubscriptionAndTenantHelper.cs index cb1fa4e85c..0ca01533fb 100644 --- a/src/ResourceManager/Version2016_09_01/Utilities/SubscriptionAndTenantHelper.cs +++ b/src/ResourceManager/Version2016_09_01/Utilities/SubscriptionAndTenantHelper.cs @@ -14,6 +14,7 @@ using Microsoft.Azure.Commands.Common.Authentication; using Microsoft.Azure.Commands.Common.Authentication.Abstractions; +using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Interfaces; using Microsoft.Azure.Internal.Subscriptions; using Microsoft.Azure.Internal.Subscriptions.Models; using Microsoft.Rest; @@ -26,7 +27,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Common.Utilities { class SubscriptionAndTenantHelper { - internal static IAccessToken AcquireAccessToken(IAzureAccount account, IAzureEnvironment environment, string tenantId) + internal static IAccessToken AcquireAccessToken(IAzureAccount account, IAzureEnvironment environment, string tenantId, ICmdletContext cmdletContext) { return AzureSession.Instance.AuthenticationFactory.Authenticate( account, @@ -34,10 +35,11 @@ internal static IAccessToken AcquireAccessToken(IAzureAccount account, IAzureEnv tenantId, null, ShowDialog.Never, - null); + null, + cmdletContext); } - internal static Dictionary GetTenantsForSubscriptions(List subscriptionIds, IAzureContext defaultContext) + internal static Dictionary GetTenantsForSubscriptions(List subscriptionIds, IAzureContext defaultContext, ICmdletContext cmdletContext) { Dictionary result = new Dictionary(); @@ -45,7 +47,7 @@ internal static Dictionary GetTenantsForSubscriptions { //First get all the tenants, then get subscriptions in each tenant till we exhaust the subscriotions sent in //Or we exhaust the tenants - List tenants = ListAccountTenants(defaultContext); + List tenants = ListAccountTenants(defaultContext, cmdletContext); HashSet subscriptionIdSet = new HashSet(subscriptionIds); @@ -57,7 +59,7 @@ internal static Dictionary GetTenantsForSubscriptions } var tId = tenant.GetId().ToString(); - var subscriptions = ListAllSubscriptionsForTenant(defaultContext, tId); + var subscriptions = ListAllSubscriptionsForTenant(defaultContext, tId, cmdletContext); subscriptions?.ForEach((s) => { @@ -75,7 +77,8 @@ internal static Dictionary GetTenantsForSubscriptions } private static List ListAccountTenants( - IAzureContext defaultContext) + IAzureContext defaultContext, + ICmdletContext cmdletContext) { List result = new List(); var commonTenant = GetCommonTenant(defaultContext.Account); @@ -83,7 +86,8 @@ private static List ListAccountTenants( var commonTenantToken = AcquireAccessToken( defaultContext.Account, defaultContext.Environment, - commonTenant); + commonTenant, + cmdletContext); SubscriptionClient subscriptionClient = null; try @@ -118,14 +122,15 @@ private static List ListAccountTenants( private static IEnumerable ListAllSubscriptionsForTenant( IAzureContext defaultContext, - string tenantId) + string tenantId, + ICmdletContext cmdletContext) { IAzureAccount account = defaultContext.Account; IAzureEnvironment environment = defaultContext.Environment; IAccessToken accessToken = null; try { - accessToken = AcquireAccessToken(account, environment, tenantId); + accessToken = AcquireAccessToken(account, environment, tenantId, cmdletContext); } catch (Exception e) {