-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate Secret Scanning from
Microsoft.Security.Utilities
(#8140)
* Integrate Microsoft.Security.Utilities into the test-proxy, preventing pushes that contain detected secrets.
- Loading branch information
Showing
8 changed files
with
235 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
tools/test-proxy/Azure.Sdk.Tools.TestProxy/Common/SecretScanner.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure.Sdk.Tools.TestProxy.Console; | ||
using Microsoft.Build.Tasks; | ||
using Microsoft.Security.Utilities; | ||
|
||
namespace Azure.Sdk.Tools.TestProxy.Common | ||
{ | ||
public class SecretScanner | ||
{ | ||
public SecretMasker SecretMasker = new SecretMasker( | ||
WellKnownRegexPatterns.HighConfidenceMicrosoftSecurityModels.Concat(WellKnownRegexPatterns.LowConfidencePotentialSecurityKeys), | ||
generateCorrelatingIds: true); | ||
|
||
private IConsoleWrapper Console; | ||
|
||
public SecretScanner(IConsoleWrapper consoleWrapper) | ||
{ | ||
Console = consoleWrapper; | ||
} | ||
|
||
public List<Tuple<string, Detection>> DiscoverSecrets(string assetRepoRoot, IEnumerable<string> relativePaths) | ||
{ | ||
var detectedSecrets = new ConcurrentBag<Tuple<string, Detection>>(); | ||
var total = relativePaths.Count(); | ||
var seen = 0; | ||
Console.WriteLine(string.Empty); | ||
|
||
var options = new ParallelOptions | ||
{ | ||
MaxDegreeOfParallelism = Environment.ProcessorCount | ||
}; | ||
|
||
Parallel.ForEach(relativePaths, options, (filePath) => | ||
{ | ||
var content = File.ReadAllText(Path.Combine(assetRepoRoot, filePath)); | ||
var fileDetections = DetectSecrets(content); | ||
|
||
if (fileDetections != null && fileDetections.Count > 0) | ||
{ | ||
foreach (Detection detection in fileDetections) | ||
{ | ||
detectedSecrets.Add(Tuple.Create(filePath, detection)); | ||
} | ||
} | ||
|
||
Interlocked.Increment(ref seen); | ||
|
||
Console.Write($"\r\u001b[2KScanned {seen}/{total}."); | ||
}); | ||
|
||
Console.WriteLine(string.Empty); | ||
|
||
return detectedSecrets.ToList(); | ||
} | ||
|
||
private async Task<string> ReadFile(string filePath) | ||
{ | ||
using (StreamReader reader = new StreamReader(filePath)) | ||
{ | ||
return await reader.ReadToEndAsync(); | ||
} | ||
} | ||
|
||
private ICollection<Detection> DetectSecrets(string stringContent) | ||
{ | ||
return SecretMasker.DetectSecrets(stringContent); | ||
} | ||
|
||
} | ||
} |
3 changes: 1 addition & 2 deletions
3
tools/test-proxy/Azure.Sdk.Tools.TestProxy/Console/ConsoleWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
| ||
using System; | ||
|
||
|
||
namespace Azure.Sdk.Tools.TestProxy.Console | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
tools/test-proxy/Azure.Sdk.Tools.TestProxy/Console/IConsoleWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters