Skip to content

Commit a02bcdb

Browse files
authored
Add ACR domain suffix (#221)
* add ACR domain suffix * fix * provide parameter to accepet list of regex filter * remove redundant constructor * correct variable name * move debugging filter to AzurePSCmdlet * Update AzurePSCmdlet.cs remove redundant line * Update AzurePSCmdlet.cs remove unnecessary decorator * Update AzurePSCmdlet.cs add comments * mark default matcher as static readonly * fix
1 parent b8b7e0f commit a02bcdb

File tree

6 files changed

+71
-21
lines changed

6 files changed

+71
-21
lines changed

src/Authentication.Abstractions/AzureEnvironment.cs

+12-2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ internal static IDictionary<string, AzureEnvironment> InitializeBuiltInEnvironme
100100
GraphEndpointResourceId = AzureEnvironmentConstants.AzureGraphEndpoint,
101101
DataLakeEndpointResourceId = AzureEnvironmentConstants.AzureDataLakeServiceEndpointResourceId,
102102
BatchEndpointResourceId = AzureEnvironmentConstants.BatchEndpointResourceId,
103+
ContainerRegistryEndpointSuffix = AzureEnvironmentConstants.AzureContainerRegistryEndpointSuffix,
103104
AdTenant = "Common"
104105
};
105106

@@ -125,6 +126,7 @@ internal static IDictionary<string, AzureEnvironment> InitializeBuiltInEnvironme
125126
DataLakeEndpointResourceId = null,
126127
GraphEndpointResourceId = AzureEnvironmentConstants.ChinaGraphEndpoint,
127128
BatchEndpointResourceId = AzureEnvironmentConstants.ChinaBatchEndpointResourceId,
129+
ContainerRegistryEndpointSuffix = AzureEnvironmentConstants.ChinaContainerRegistryEndpointSuffix,
128130
AdTenant = "Common"
129131
};
130132

@@ -150,6 +152,7 @@ internal static IDictionary<string, AzureEnvironment> InitializeBuiltInEnvironme
150152
DataLakeEndpointResourceId = null,
151153
GraphEndpointResourceId = AzureEnvironmentConstants.USGovernmentGraphEndpoint,
152154
BatchEndpointResourceId = AzureEnvironmentConstants.USGovernmentBatchEndpointResourceId,
155+
ContainerRegistryEndpointSuffix = AzureEnvironmentConstants.USGovernmentContainerRegistryEndpointSuffix,
153156
AdTenant = "Common"
154157
};
155158

@@ -298,7 +301,8 @@ private static AzureEnvironment MapArmToAzureEnvironment(ArmMetadata armMetadata
298301
DataLakeEndpointResourceId = armMetadata.ActiveDirectoryDataLake,
299302
GraphEndpointResourceId = armMetadata.Graph,
300303
BatchEndpointResourceId = armMetadata.Batch,
301-
AdTenant = armMetadata.Authentication.Tenant
304+
AdTenant = armMetadata.Authentication.Tenant,
305+
ContainerRegistryEndpointSuffix = armMetadata.Suffixes.AcrLoginServer
302306
};
303307

304308
// There are mismatches between metadata built in Azure PowerShell/CLI and from ARM endpoint.
@@ -536,6 +540,11 @@ public AzureEnvironment(IAzureEnvironment other)
536540
/// </summary>
537541
public string AdTenant { get; set; }
538542

543+
/// <summary>
544+
/// The domain name suffix for Azure Container Registry
545+
/// </summary>
546+
public string ContainerRegistryEndpointSuffix { get; set; }
547+
539548
/// <summary>
540549
/// The set of Azure Version Profiles supported in this environment
541550
/// </summary>
@@ -590,7 +599,8 @@ public bool Equals(AzureEnvironment other)
590599
&& string.Equals(this.BatchEndpointResourceId?.TrimEnd('/'), other.BatchEndpointResourceId?.TrimEnd('/'), StringComparison.OrdinalIgnoreCase)
591600
&& string.Equals(this.AzureDataLakeAnalyticsCatalogAndJobEndpointSuffix, other.AzureDataLakeAnalyticsCatalogAndJobEndpointSuffix, StringComparison.OrdinalIgnoreCase)
592601
&& string.Equals(this.AzureDataLakeStoreFileSystemEndpointSuffix, other.AzureDataLakeStoreFileSystemEndpointSuffix, StringComparison.OrdinalIgnoreCase)
593-
&& string.Equals(this.AdTenant?.TrimEnd('/'), other.AdTenant?.TrimEnd('/'), StringComparison.OrdinalIgnoreCase);
602+
&& string.Equals(this.AdTenant?.TrimEnd('/'), other.AdTenant?.TrimEnd('/'), StringComparison.OrdinalIgnoreCase)
603+
&& string.Equals(this.ContainerRegistryEndpointSuffix?.TrimEnd('/'), other.ContainerRegistryEndpointSuffix?.TrimEnd('/'), StringComparison.OrdinalIgnoreCase);
594604
}
595605

596606

src/Authentication.Abstractions/AzureEnvironmentConstants.cs

+7
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ public static class AzureEnvironmentConstants
188188
public const string AzureSynapseAnalyticsEndpointSuffix = "dev.azuresynapse.net";
189189
public const string ChinaSynapseAnalyticsEndpointSuffix = "dev.azuresynapse.azure.cn";
190190

191+
/// <summary>
192+
/// The domain name suffix for Azure Container Registry
193+
/// </summary>
194+
public const string AzureContainerRegistryEndpointSuffix = "azurecr.io";
195+
public const string ChinaContainerRegistryEndpointSuffix = "azurecr.cn";
196+
public const string USGovernmentContainerRegistryEndpointSuffix = "azurecr.us";
197+
191198
/// <summary>
192199
/// The token audience for authorizing Synapse Service requests
193200
/// </summary>

src/Authentication.Abstractions/Interfaces/IAzureEnvironment.cs

+5
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,10 @@ public interface IAzureEnvironment : IExtensibleModel
131131
/// The set of version profile s(service capabilities) supported
132132
/// </summary>
133133
IList<string> VersionProfiles { get; }
134+
135+
/// <summary>
136+
/// The domain name suffix for Azure Container Registry
137+
/// </summary>
138+
string ContainerRegistryEndpointSuffix { get; set; }
134139
}
135140
}

src/Common/AzurePSCmdlet.cs

+18-1
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2121
using System;
2222
using System.Collections.Concurrent;
23+
using System.Collections.Generic;
2324
using System.Diagnostics;
2425
using System.Globalization;
2526
using System.IO;
2627
using System.Linq;
2728
using System.Management.Automation;
2829
using System.Net.Http.Headers;
2930
using System.Text;
31+
using System.Text.RegularExpressions;
3032

3133
namespace Microsoft.WindowsAzure.Commands.Utilities.Common
3234
{
@@ -46,6 +48,9 @@ public abstract class AzurePSCmdlet : PSCmdlet, IDisposable
4648
private object lockObject = new object();
4749
private AzurePSDataCollectionProfile _cachedProfile = null;
4850

51+
private IList<Regex> _matchers = new List<Regex>();
52+
private static readonly Regex _defaultMatcher = new Regex("(\\s*\"refresh_token\"\\s*:\\s*)\"[^\"]+\"");
53+
4954
protected AzurePSDataCollectionProfile _dataCollectionProfile
5055
{
5156
get
@@ -305,10 +310,21 @@ protected virtual void LogCmdletEndInvocationInfo()
305310
WriteDebugWithTimestamp(message);
306311
}
307312

313+
protected void AddDebuggingFilter(Regex matcher)
314+
{
315+
_matchers.Add(matcher);
316+
}
317+
318+
//Override this method in cmdlet if customized regedx filters needed for debugging message
319+
protected virtual void InitDebuggingFilter()
320+
{
321+
AddDebuggingFilter(_defaultMatcher);
322+
}
323+
308324
protected virtual void SetupDebuggingTraces()
309325
{
310326
_httpTracingInterceptor = _httpTracingInterceptor ?? new
311-
RecordingTracingInterceptor(DebugMessages);
327+
RecordingTracingInterceptor(DebugMessages, _matchers);
312328
_adalListener = _adalListener ?? new DebugStreamTraceListener(DebugMessages);
313329
RecordingTracingInterceptor.AddToContext(_httpTracingInterceptor);
314330
DebugStreamTraceListener.AddAdalTracing(_adalListener);
@@ -361,6 +377,7 @@ protected override void BeginProcessing()
361377

362378
InitializeQosEvent();
363379
LogCmdletStartInvocationInfo();
380+
InitDebuggingFilter();
364381
SetupDebuggingTraces();
365382
SetupHttpClientPipeline();
366383
base.BeginProcessing();

src/Common/RecordingTracingInterceptor.cs

+8-3
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,23 @@
1919
using System.Collections.Concurrent;
2020
using System.Collections.Generic;
2121
using System.Net.Http;
22+
using System.Text.RegularExpressions;
2223

2324
namespace Microsoft.Azure.ServiceManagement.Common.Models
2425
{
2526
public class RecordingTracingInterceptor : Hyak.Common.ICloudTracingInterceptor
2627
{
27-
public RecordingTracingInterceptor(ConcurrentQueue<string> queue)
28+
29+
public RecordingTracingInterceptor(ConcurrentQueue<string> queue, IList<Regex> matchers = null)
2830
{
2931
MessageQueue = queue;
32+
Matchers = matchers;
3033
}
3134

3235
public ConcurrentQueue<string> MessageQueue { get; private set; }
3336

37+
private IList<Regex> Matchers { get; set; }
38+
3439
private void Write(string message, params object[] arguments)
3540
{
3641
if (arguments == null || arguments.Length == 0)
@@ -60,12 +65,12 @@ public void Enter(string invocationId, object instance, string method, IDictiona
6065

6166
public void SendRequest(string invocationId, HttpRequestMessage request)
6267
{
63-
Write(GeneralUtilities.GetLog(request));
68+
Write(GeneralUtilities.GetLog(request, Matchers));
6469
}
6570

6671
public void ReceiveResponse(string invocationId, HttpResponseMessage response)
6772
{
68-
Write(GeneralUtilities.GetLog(response));
73+
Write(GeneralUtilities.GetLog(response, Matchers));
6974
}
7075

7176
public void Error(string invocationId, Exception ex)

src/Common/Utilities/GeneralUtilities.cs

+21-15
Original file line numberDiff line numberDiff line change
@@ -252,60 +252,65 @@ public static string EnsureTrailingSlash(string url)
252252
return address.Uri.AbsoluteUri;
253253
}
254254

255-
public static string GetHttpResponseLog(string statusCode, IDictionary<string, IEnumerable<string>> headers, string body)
255+
public static string GetHttpResponseLog(string statusCode, IDictionary<string, IEnumerable<string>> headers, string body, IList<Regex> matchers = null)
256256
{
257257
StringBuilder httpResponseLog = new StringBuilder();
258258
httpResponseLog.AppendLine($"============================ HTTP RESPONSE ============================{Environment.NewLine}");
259259
httpResponseLog.AppendLine($"Status Code:{Environment.NewLine}{statusCode}{Environment.NewLine}");
260260
httpResponseLog.AppendLine($"Headers:{ Environment.NewLine}{ MessageHeadersToString(headers)}");
261-
httpResponseLog.AppendLine($"Body:{Environment.NewLine}{TransformBody(body)}{Environment.NewLine}");
261+
httpResponseLog.AppendLine($"Body:{Environment.NewLine}{TransformBody(body, matchers)}{Environment.NewLine}");
262262
return httpResponseLog.ToString();
263263
}
264264

265-
public static string GetHttpResponseLog(string statusCode, HttpHeaders headers, string body)
265+
public static string GetHttpResponseLog(string statusCode, HttpHeaders headers, string body, IList<Regex> matchers = null)
266266
{
267-
return GetHttpResponseLog(statusCode, ConvertHttpHeadersToWebHeaderCollection(headers), body);
267+
return GetHttpResponseLog(statusCode, ConvertHttpHeadersToWebHeaderCollection(headers), body, matchers);
268268
}
269269

270-
public static string TransformBody(string inBody)
270+
public static string TransformBody(string inBody, IList<Regex> matchers = null)
271271
{
272-
Regex matcher = new Regex("(\\s*\"access_token\"\\s*:\\s*)\"[^\"]+\"");
273-
return matcher.Replace(inBody, "$1\"<redacted>\"");
272+
foreach (Regex match in matchers)
273+
{
274+
inBody = match.Replace(inBody, "$1\"<redacted>\"");
275+
}
276+
return inBody;
274277
}
275278

276279
public static string GetHttpRequestLog(
277280
string method,
278281
string requestUri,
279282
IDictionary<string, IEnumerable<string>> headers,
280-
string body)
283+
string body,
284+
IList<Regex> matchers = null)
281285
{
282286
StringBuilder httpRequestLog = new StringBuilder();
283287
httpRequestLog.AppendLine(string.Format("============================ HTTP REQUEST ============================{0}", Environment.NewLine));
284288
httpRequestLog.AppendLine(string.Format("HTTP Method:{0}{1}{0}", Environment.NewLine, method));
285289
httpRequestLog.AppendLine(string.Format("Absolute Uri:{0}{1}{0}", Environment.NewLine, requestUri));
286290
httpRequestLog.AppendLine(string.Format("Headers:{0}{1}", Environment.NewLine, MessageHeadersToString(headers)));
287-
httpRequestLog.AppendLine(string.Format("Body:{0}{1}{0}", Environment.NewLine, TransformBody(body)));
291+
httpRequestLog.AppendLine(string.Format("Body:{0}{1}{0}", Environment.NewLine, TransformBody(body, matchers)));
288292

289293
return httpRequestLog.ToString();
290294
}
291295

292-
public static string GetHttpRequestLog(string method, string requestUri, HttpHeaders headers, string body)
296+
public static string GetHttpRequestLog(string method, string requestUri, HttpHeaders headers, string body, IList<Regex> matchers = null)
293297
{
294-
return GetHttpRequestLog(method, requestUri, ConvertHttpHeadersToWebHeaderCollection(headers), body);
298+
return GetHttpRequestLog(method, requestUri, ConvertHttpHeadersToWebHeaderCollection(headers), body, matchers);
295299
}
296300

297-
public static string GetLog(HttpResponseMessage response)
301+
public static string GetLog(HttpResponseMessage response, IList<Regex> matchers = null)
298302
{
299303
string body = response.Content == null ? string.Empty
300304
: FormatString(response.Content.ReadAsStringAsync().Result);
301305

302306
return GetHttpResponseLog(
303307
response.StatusCode.ToString(),
304308
response.Headers,
305-
body);
309+
body,
310+
matchers);
306311
}
307312

308-
public static string GetLog(HttpRequestMessage request)
313+
public static string GetLog(HttpRequestMessage request, IList<Regex> matchers = null)
309314
{
310315
string body = request.Content == null ? string.Empty
311316
: FormatString(request.Content.ReadAsStringAsync().Result);
@@ -314,7 +319,8 @@ public static string GetLog(HttpRequestMessage request)
314319
request.Method.ToString(),
315320
request.RequestUri.ToString(),
316321
(HttpHeaders)request.Headers,
317-
body);
322+
body,
323+
matchers);
318324
}
319325

320326
public static string FormatString(string content)

0 commit comments

Comments
 (0)