-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enabling kusto command #2296
Merged
Merged
Enabling kusto command #2296
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Linq; | ||
|
||
using Kusto.Data; | ||
using Kusto.Data.Common; | ||
using Kusto.Data.Net.Client; | ||
|
||
using Microsoft.CodeAnalysis.Sarif.Driver; | ||
|
||
using Newtonsoft.Json; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Multitool | ||
{ | ||
public class KustoCommand : CommandBase | ||
{ | ||
private ICslQueryProvider _kustoClient; | ||
private KustoOptions _options; | ||
|
||
public int Run(KustoOptions options) | ||
{ | ||
try | ||
{ | ||
if (!options.Validate()) | ||
{ | ||
return FAILURE; | ||
} | ||
|
||
_options = options; | ||
|
||
InitializeKustoClient(); | ||
|
||
(List<Result>, List<ReportingDescriptor>) sarifResults = RetrieveResultsFromKusto(); | ||
var sarifLog = new SarifLog | ||
{ | ||
Runs = new[] | ||
{ | ||
new Run | ||
{ | ||
Tool = new Tool | ||
{ | ||
Driver = new ToolComponent | ||
{ | ||
Name = "SARIF Kusto", | ||
Rules = sarifResults.Item2, | ||
} | ||
}, | ||
Results = sarifResults.Item1, | ||
} | ||
} | ||
}; | ||
|
||
WriteSarifFile(FileSystem, sarifLog, options.OutputFilePath, options.Minify); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(ex); | ||
return FAILURE; | ||
} | ||
finally | ||
{ | ||
_kustoClient?.Dispose(); | ||
} | ||
return SUCCESS; | ||
} | ||
|
||
private void InitializeKustoClient() | ||
{ | ||
KustoConnectionStringBuilder connection = new KustoConnectionStringBuilder(_options.HostAddress) | ||
.WithAadApplicationKeyAuthentication( | ||
Environment.GetEnvironmentVariable("AppClientId"), | ||
Environment.GetEnvironmentVariable("AppSecret"), | ||
Environment.GetEnvironmentVariable("AuthorityId")); | ||
|
||
_kustoClient = KustoClientFactory.CreateCslQueryProvider(connection); | ||
} | ||
|
||
private (List<Result>, List<ReportingDescriptor>) RetrieveResultsFromKusto() | ||
{ | ||
Dictionary<string, int> dataReaderIndex = new Dictionary<string, int>(); | ||
var results = new List<Result>(); | ||
var rules = new Dictionary<string, ReportingDescriptor>(); | ||
|
||
using IDataReader dataReader = _kustoClient.ExecuteQuery( | ||
_options.Database, | ||
_options.Query, | ||
new ClientRequestProperties { ClientRequestId = Guid.NewGuid().ToString() }); | ||
while (dataReader.Read()) | ||
{ | ||
try | ||
{ | ||
string itemPathUri = dataReader.GetString(GetIndex(dataReader, dataReaderIndex, "ItemPathUri")); | ||
string organizationName = dataReader.GetString(GetIndex(dataReader, dataReaderIndex, "OrganizationName")); | ||
string projectName = dataReader.GetString(GetIndex(dataReader, dataReaderIndex, "ProjectName")); | ||
string repositoryName = dataReader.GetString(GetIndex(dataReader, dataReaderIndex, "RepositoryName")); | ||
string regionSnippet = dataReader.GetString(GetIndex(dataReader, dataReaderIndex, "RegionSnippet")); | ||
string validationFingerprint = dataReader.GetString(GetIndex(dataReader, dataReaderIndex, "ValidationFingerprint")); | ||
string globalFingerprint = dataReader.GetString(GetIndex(dataReader, dataReaderIndex, "GlobalFingerprint")); | ||
string ruleId = dataReader.GetString(GetIndex(dataReader, dataReaderIndex, "RuleId")); | ||
string ruleName = dataReader.GetString(GetIndex(dataReader, dataReaderIndex, "RuleName")); | ||
int regionStartLine = dataReader.GetInt32(GetIndex(dataReader, dataReaderIndex, "RegionStartLine")); | ||
int regionEndLine = dataReader.GetInt32(GetIndex(dataReader, dataReaderIndex, "RegionEndLine")); | ||
int regionStartColumn = dataReader.GetInt32(GetIndex(dataReader, dataReaderIndex, "RegionStartColumn")); | ||
int regionEndColumn = dataReader.GetInt32(GetIndex(dataReader, dataReaderIndex, "RegionEndColumn")); | ||
int regionCharOffset = dataReader.GetInt32(GetIndex(dataReader, dataReaderIndex, "RegionCharOffset")); | ||
int regionCharLength = dataReader.GetInt32(GetIndex(dataReader, dataReaderIndex, "RegionCharLength")); | ||
string resultKind = dataReader.GetString(GetIndex(dataReader, dataReaderIndex, "ResultKind")); | ||
string level = dataReader.GetString(GetIndex(dataReader, dataReaderIndex, "Level")); | ||
string resultMessageText = dataReader.GetString(GetIndex(dataReader, dataReaderIndex, "ResultMessageText")); | ||
string result = dataReader.GetString(GetIndex(dataReader, dataReaderIndex, "Result")); | ||
|
||
Result resultObj = JsonConvert.DeserializeObject<Result>(result); | ||
// Removing this, because the ruleIndex might not be in the correct place. | ||
resultObj.RuleIndex = -1; | ||
resultObj.Level = (FailureLevel)Enum.Parse(typeof(FailureLevel), level); | ||
resultObj.Kind = (ResultKind)Enum.Parse(typeof(ResultKind), resultKind); | ||
resultObj.Locations.Add(new Location | ||
{ | ||
PhysicalLocation = new PhysicalLocation | ||
{ | ||
Region = new Region | ||
{ | ||
CharLength = regionCharLength, | ||
CharOffset = regionCharOffset, | ||
StartColumn = regionStartColumn, | ||
StartLine = regionStartLine, | ||
EndColumn = regionEndColumn, | ||
EndLine = regionEndLine, | ||
Snippet = new ArtifactContent | ||
{ | ||
Text = regionSnippet | ||
} | ||
}, | ||
ArtifactLocation = new ArtifactLocation | ||
{ | ||
Uri = new Uri(itemPathUri) | ||
} | ||
}, | ||
}); | ||
resultObj.SetProperty("organizationName", organizationName); | ||
resultObj.SetProperty("projectName", projectName); | ||
resultObj.SetProperty("repositoryName", repositoryName); | ||
resultObj.Fingerprints.Add("ValidationFingerprint", validationFingerprint); | ||
resultObj.Fingerprints.Add("GlobalFingerprint", globalFingerprint); | ||
|
||
if (!rules.ContainsKey(ruleId)) | ||
{ | ||
rules.Add(ruleId, new ReportingDescriptor | ||
{ | ||
Id = ruleId, | ||
Name = ruleName, | ||
}); | ||
} | ||
|
||
results.Add(resultObj); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(ex); | ||
} | ||
} | ||
|
||
return (results, rules.Values.ToList()); | ||
} | ||
|
||
private static int GetIndex(IDataReader dataReader, Dictionary<string, int> dataReaderIndex, string key) | ||
{ | ||
if (!dataReaderIndex.TryGetValue(key, out int index)) | ||
{ | ||
dataReaderIndex[key] = index = dataReader.GetOrdinal(key); | ||
} | ||
|
||
return index; | ||
} | ||
} | ||
} |
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,55 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
|
||
using CommandLine; | ||
|
||
using Microsoft.CodeAnalysis.Sarif.Driver; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Multitool | ||
{ | ||
[Verb("kusto", HelpText = "TODO.")] | ||
public class KustoOptions : CommonOptionsBase | ||
{ | ||
[Value( | ||
0, | ||
HelpText = "Output path for exported SARIF", | ||
Required = true)] | ||
public string OutputFilePath { get; set; } | ||
|
||
[Option( | ||
"host-address", | ||
HelpText = "TODO", | ||
Required = true)] | ||
public string HostAddress { get; set; } | ||
|
||
[Option( | ||
"database", | ||
HelpText = "TODO", | ||
Required = true)] | ||
public string Database { get; set; } | ||
|
||
[Option( | ||
"query", | ||
HelpText = "TODO", | ||
Required = true)] | ||
public string Query { get; set; } | ||
|
||
public bool Validate() | ||
{ | ||
string appClientId = Environment.GetEnvironmentVariable("AppClientId"); | ||
string appSecret = Environment.GetEnvironmentVariable("AppSecret"); | ||
string authorityId = Environment.GetEnvironmentVariable("AuthorityId"); | ||
|
||
if (string.IsNullOrEmpty(appClientId) || | ||
string.IsNullOrEmpty(appSecret) || | ||
string.IsNullOrEmpty(authorityId)) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be 'spam' only. You can note in a property somewhere that this is spam SARIF rehydrated by the kusto command.