Skip to content

Commit 5ece248

Browse files
authored
Integrate secrets detection library with telemetry (#26399)
1 parent b057e93 commit 5ece248

File tree

9 files changed

+54
-37
lines changed

9 files changed

+54
-37
lines changed

src/Accounts/Accounts/CommonModule/AzModule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,10 @@ public Dictionary<string, string> GetTelemetryInfo(string telemetryId)
385385
Dictionary<string, string> telemetryInfo = null;
386386
if (_telemetry.TryGetValue(telemetryId, out var qos))
387387
{
388-
if (qos?.SanitizerInfo?.DetectedProperties?.Count > 0)
388+
if (qos?.SanitizerInfo?.DetectedProperties.IsEmpty == false)
389389
{
390390
var showSecretsWarning = qos.SanitizerInfo.ShowSecretsWarning && qos.SanitizerInfo.SecretsDetected;
391-
var sanitizedProperties = string.Join(", ", qos.SanitizerInfo.DetectedProperties);
391+
var sanitizedProperties = string.Join(", ", qos.SanitizerInfo.DetectedProperties.PropertyNames);
392392
var invocationName = qos.InvocationName;
393393
telemetryInfo = new Dictionary<string, string>
394394
{

src/Accounts/Authentication/Sanitizer/Providers/SanitizerCollectionProvider.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,16 @@ public override void SanitizeValue(object sanitizingObject, Stack<object> saniti
3939
var collItemType = collItem.GetType();
4040
if (collItemType == typeof(string))
4141
{
42-
if (Service.TrySanitizeData(collItem as string, out string sanitizedData))
42+
if (Service.TrySanitizeData(collItem as string, out var detections, out _))
4343
{
4444
telemetry.SecretsDetected = true;
4545
var propertyPath = ResolvePropertyPath(property);
4646
if (!string.IsNullOrEmpty(propertyPath))
4747
{
48-
telemetry.DetectedProperties.Add(ResolvePropertyPath(property));
48+
foreach (var detection in detections)
49+
{
50+
telemetry.DetectedProperties.AddPropertyInfo(propertyPath, detection.Moniker);
51+
}
4952
}
5053
}
5154
}

src/Accounts/Authentication/Sanitizer/Providers/SanitizerDictionaryProvider.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,17 @@ public override void SanitizeValue(object sanitizingObject, Stack<object> saniti
4040
var dicItemValueType = dictItemValue.GetType();
4141
if (dicItemValueType == typeof(string))
4242
{
43-
if (Service.TrySanitizeData(dictItemValue as string, out string sanitizedData))
43+
if (Service.TrySanitizeData(dictItemValue as string, out var detections, out _))
4444
{
4545
// Sanitize dictionary item value
4646
telemetry.SecretsDetected = true;
4747
var propertyPath = ResolvePropertyPath(property);
4848
if (!string.IsNullOrEmpty(propertyPath))
4949
{
50-
telemetry.DetectedProperties.Add(propertyPath);
50+
foreach (var detection in detections)
51+
{
52+
telemetry.DetectedProperties.AddPropertyInfo(propertyPath, detection.Moniker);
53+
}
5154
}
5255
}
5356
}

src/Accounts/Authentication/Sanitizer/Providers/SanitizerJsonArrayProvider.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,16 @@ public override void SanitizeValue(object sanitizingObject, Stack<object> saniti
3737
switch (jItem.Type)
3838
{
3939
case JTokenType.String:
40-
if (Service.TrySanitizeData(jItem.Value<string>(), out string sanitizedData))
40+
if (Service.TrySanitizeData(jItem.Value<string>(), out var detections, out _))
4141
{
4242
telemetry.SecretsDetected = true;
4343
var propertyPath = ResolvePropertyPath(property);
4444
if (!string.IsNullOrEmpty(propertyPath))
4545
{
46-
telemetry.DetectedProperties.Add(propertyPath);
46+
foreach (var detection in detections)
47+
{
48+
telemetry.DetectedProperties.AddPropertyInfo(propertyPath, detection.Moniker);
49+
}
4750
}
4851
}
4952
break;

src/Accounts/Authentication/Sanitizer/Providers/SanitizerJsonObjectProvider.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,16 @@ public override void SanitizeValue(object sanitizingObject, Stack<object> saniti
3737
switch (propValue.Type)
3838
{
3939
case JTokenType.String:
40-
if (Service.TrySanitizeData(propValue.Value<string>(), out string sanitizedData))
40+
if (Service.TrySanitizeData(propValue.Value<string>(), out var detections, out _))
4141
{
4242
telemetry.SecretsDetected = true;
4343
var propertyPath = ResolvePropertyPath(property);
4444
if (!string.IsNullOrEmpty(propertyPath))
4545
{
46-
telemetry.DetectedProperties.Add(propertyPath);
46+
foreach (var detection in detections)
47+
{
48+
telemetry.DetectedProperties.AddPropertyInfo(propertyPath, detection.Moniker);
49+
}
4750
}
4851
}
4952
break;

src/Accounts/Authentication/Sanitizer/Providers/SanitizerStringProvider.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using System.Collections.Generic;
1615
using Microsoft.Azure.Commands.Common.Authentication.Sanitizer.Services;
1716
using Microsoft.WindowsAzure.Commands.Common.Sanitizer;
17+
using System.Collections.Generic;
1818

1919
namespace Microsoft.Azure.Commands.Common.Authentication.Sanitizer.Providers
2020
{
@@ -29,13 +29,16 @@ public override void SanitizeValue(object sanitizingObject, Stack<object> saniti
2929
var propertyValue = property?.GetValue(sanitizingObject) ?? sanitizingObject;
3030
if (propertyValue is string data)
3131
{
32-
if (Service.TrySanitizeData(data, out string sanitizedData))
32+
if (Service.TrySanitizeData(data, out var detections, out _))
3333
{
3434
telemetry.SecretsDetected = true;
3535
var propertyPath = ResolvePropertyPath(property);
3636
if (!string.IsNullOrEmpty(propertyPath))
3737
{
38-
telemetry.DetectedProperties.Add(propertyPath);
38+
foreach (var detection in detections)
39+
{
40+
telemetry.DetectedProperties.AddPropertyInfo(propertyPath, detection.Moniker);
41+
}
3942
}
4043
}
4144
}

src/Accounts/Authentication/Sanitizer/Services/DefaultSanitizerService.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,20 @@ internal class DefaultSanitizerService : ISanitizerService
4545
{ "Microsoft.Azure.Storage.File.CloudFileDirectory", new[] { "Parent" } },
4646
};
4747

48-
private readonly SecretMasker _secretMasker = new SecretMasker(WellKnownRegexPatterns.HighConfidenceMicrosoftSecurityModels, generateCorrelatingIds: true);
48+
private readonly SecretMasker _secretMasker = new SecretMasker(WellKnownRegexPatterns.HighConfidenceMicrosoftSecurityModels);
4949

50-
public bool TrySanitizeData(string data, out string sanitizedData)
50+
public bool TrySanitizeData(string data, out IEnumerable<Detection> detections, out string sanitizedData)
5151
{
5252
sanitizedData = string.Empty;
5353

54-
if (!string.IsNullOrWhiteSpace(data))
54+
if (string.IsNullOrWhiteSpace(data))
5555
{
56-
var detections = _secretMasker.DetectSecrets(data);
57-
return detections.Any();
56+
detections = Enumerable.Empty<Detection>();
57+
return false;
5858
}
5959

60-
return false;
60+
detections = _secretMasker.DetectSecrets(data);
61+
return detections.Any();
6162
}
6263
}
6364
}

src/Accounts/Authentication/Sanitizer/Services/ISanitizerService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Microsoft.Security.Utilities;
1516
using System.Collections.Generic;
1617

1718
namespace Microsoft.Azure.Commands.Common.Authentication.Sanitizer.Services
@@ -20,6 +21,6 @@ public interface ISanitizerService
2021
{
2122
IReadOnlyDictionary<string, IEnumerable<string>> IgnoredProperties { get; }
2223

23-
bool TrySanitizeData(string data, out string sanitizedData);
24+
bool TrySanitizeData(string data, out IEnumerable<Detection> detections, out string sanitizedData);
2425
}
2526
}

tools/Common.Netcore.Dependencies.targets

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
<ItemGroup>
44
<PackageReference Include="Microsoft.Rest.ClientRuntime" Version="2.3.24"/>
55
<PackageReference Include="Microsoft.Rest.ClientRuntime.Azure" Version="3.3.19"/>
6-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Aks" Version="1.3.101-preview"/>
7-
<PackageReference Include="Microsoft.Azure.PowerShell.Authentication.Abstractions" Version="1.3.101-preview"/>
8-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Authorization" Version="1.3.101-preview"/>
9-
<PackageReference Include="Microsoft.Azure.PowerShell.Common" Version="1.3.101-preview"/>
10-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Compute" Version="1.3.101-preview"/>
11-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Graph.Rbac" Version="1.3.101-preview"/>
12-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.KeyVault" Version="1.3.101-preview"/>
13-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Monitor" Version="1.3.101-preview"/>
14-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Network" Version="1.3.101-preview"/>
15-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.PolicyInsights" Version="1.3.101-preview"/>
16-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.ResourceManager" Version="1.3.101-preview"/>
17-
<PackageReference Include="Microsoft.Azure.PowerShell.Storage" Version="1.3.101-preview"/>
18-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Storage.Management" Version="1.3.101-preview"/>
19-
<PackageReference Include="Microsoft.Azure.PowerShell.Strategies" Version="1.3.101-preview"/>
20-
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Websites" Version="1.3.101-preview"/>
21-
<PackageReference Include="Microsoft.Azure.PowerShell.Common.Share" Version="1.3.101-preview"/>
6+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Aks" Version="1.3.102-preview"/>
7+
<PackageReference Include="Microsoft.Azure.PowerShell.Authentication.Abstractions" Version="1.3.102-preview"/>
8+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Authorization" Version="1.3.102-preview"/>
9+
<PackageReference Include="Microsoft.Azure.PowerShell.Common" Version="1.3.102-preview"/>
10+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Compute" Version="1.3.102-preview"/>
11+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Graph.Rbac" Version="1.3.102-preview"/>
12+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.KeyVault" Version="1.3.102-preview"/>
13+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Monitor" Version="1.3.102-preview"/>
14+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Network" Version="1.3.102-preview"/>
15+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.PolicyInsights" Version="1.3.102-preview"/>
16+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.ResourceManager" Version="1.3.102-preview"/>
17+
<PackageReference Include="Microsoft.Azure.PowerShell.Storage" Version="1.3.102-preview"/>
18+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Storage.Management" Version="1.3.102-preview"/>
19+
<PackageReference Include="Microsoft.Azure.PowerShell.Strategies" Version="1.3.102-preview"/>
20+
<PackageReference Include="Microsoft.Azure.PowerShell.Clients.Websites" Version="1.3.102-preview"/>
21+
<PackageReference Include="Microsoft.Azure.PowerShell.Common.Share" Version="1.3.102-preview"/>
2222
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
2323
</ItemGroup>
2424
<ItemGroup>
@@ -37,7 +37,7 @@
3737
<PackageReference Include="PowerShellStandard.Library" Version="5.1.0" PrivateAssets="All" />
3838
</ItemGroup>
3939
<PropertyGroup>
40-
<StorageToolsPath>$(NugetPackageRoot)\microsoft.azure.powershell.storage\1.3.101-preview\tools\</StorageToolsPath>
40+
<StorageToolsPath>$(NugetPackageRoot)\microsoft.azure.powershell.storage\1.3.102-preview\tools\</StorageToolsPath>
4141
</PropertyGroup>
4242
<ItemGroup Condition="'$(OmitJsonPackage)' != 'true'">
4343
<PackageReference Include="Newtonsoft.Json" Version="13.0.2"/>

0 commit comments

Comments
 (0)