Skip to content
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 3 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/Projects.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $Frameworks = @{}
$Frameworks.NetFx = @("net472")

# Frameworks for which we build libraries.
$Frameworks.Library = @("netstandard2.0") + $Frameworks.NetFx
$Frameworks.Library = @("netstandard2.0") + @("netstandard2.1") + $Frameworks.NetFx

# Frameworks for which we build applications.
$Frameworks.Application = @("netcoreapp3.1") + $Frameworks.NetFx
Expand Down
180 changes: 180 additions & 0 deletions src/Sarif.Multitool.Library/KustoCommand.cs
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 = "spam",
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;
}
}
}
55 changes: 55 additions & 0 deletions src/Sarif.Multitool.Library/KustoOptions.cs
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 = "Export a SARIF file from a kusto query.")]
public class KustoOptions : CommonOptionsBase
{
[Value(
0,
HelpText = "Output path for exported SARIF",
Required = true)]
public string OutputFilePath { get; set; }

[Option(
"host-address",
HelpText = "The host address from where we will fetch the data.",
Required = true)]
public string HostAddress { get; set; }

[Option(
"database",
HelpText = "The database that we will connect.",
Required = true)]
public string Database { get; set; }

[Option(
"query",
HelpText = "The query that will be used to generate the SARIF.",
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;
}
}
}
12 changes: 6 additions & 6 deletions src/Sarif.Multitool.Library/Sarif.Multitool.Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), build.props))\build.props" />

<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<TargetFrameworks>netstandard2.1</TargetFrameworks>
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);net472</TargetFrameworks>
<RootNamespace>Microsoft.CodeAnalysis.Sarif.Multitool</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Microsoft.Azure.Kusto.Data" Version="9.0.8" />
<PackageReference Include="Microsoft.Json.Pointer" Version="1.1.2" />
<PackageReference Include="System.Threading.Channels" Version="5.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Threading.Channels" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -49,7 +50,7 @@
</ItemGroup>

<ItemGroup>
<None Include="default.configuration.xml" Pack="true" PackagePath="lib/netstandard2.0;lib/net472" CopyToOutputDirectory="PreserveNewest" />
<None Include="default.configuration.xml" Pack="true" PackagePath="lib/netstandard2.1;lib/net472" CopyToOutputDirectory="PreserveNewest" />
<None Include="..\..\policies\*" Pack="true" PackagePath="policies" />
<None Include="**\*.cs" Pack="true" PackagePath="src" />
<None Include="..\ReleaseHistory.md" Pack="true" PackagePath="/" />
Expand All @@ -61,5 +62,4 @@
<ProjectReference Include="..\Sarif.WorkItems\Sarif.WorkItems.csproj" />
<ProjectReference Include="..\Sarif\Sarif.csproj" />
</ItemGroup>

</Project>
</Project>
2 changes: 2 additions & 0 deletions src/Sarif.Multitool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static int Main(string[] args)
ExportValidationConfigurationOptions,
ExportValidationRulesMetadataOptions,
FileWorkItemsOptions,
KustoOptions,
ResultMatchingOptions,
MergeOptions,
PageOptions,
Expand Down Expand Up @@ -63,6 +64,7 @@ public static int Main(string[] args)
(ExportValidationConfigurationOptions options) => new ExportValidationConfigurationCommand().Run(options),
(ExportValidationRulesMetadataOptions options) => new ExportValidationRulesMetadataCommand().Run(options),
(FileWorkItemsOptions fileWorkItemsOptions) => new FileWorkItemsCommand().Run(fileWorkItemsOptions),
(KustoOptions options) => new KustoCommand().Run(options),
(ResultMatchingOptions baselineOptions) => new ResultMatchingCommand().Run(baselineOptions),
(MergeOptions mergeOptions) => new MergeCommand().Run(mergeOptions),
(PageOptions pageOptions) => new PageCommand().Run(pageOptions),
Expand Down