Skip to content

Commit

Permalink
Added SARIF support #878 (#944)
Browse files Browse the repository at this point in the history
* Added SARIF support #878

* Doc updates

* Clean up

* Bump tests

* Added minor code suggestions

Co-authored-by: ArmaanMcleod <[email protected]>
  • Loading branch information
BernieWhite and ArmaanMcleod authored Jan 31, 2022
1 parent d6d90a1 commit 94f5bc2
Show file tree
Hide file tree
Showing 25 changed files with 587 additions and 113 deletions.
3 changes: 3 additions & 0 deletions docs/CHANGELOG-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ What's changed since pre-release v2.0.0-B2201146:
- `IsDateTime`
- `IsInteger`
- `IsNumeric`
- Added support for formatting results as SARIF. [#878](https://github.com/microsoft/PSRule/issues/878)
- Set `Output.Format` to `Sarif` to output results in the SARIF format.
- See [about_PSRule_Options] for details.

## v2.0.0-B2201146 (pre-release)

Expand Down
3 changes: 2 additions & 1 deletion docs/commands/PSRule/en-US/Assert-PSRule.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,15 @@ This is the default.
- Markdown - Output is serialized as Markdown.
- NUnit3 - Output is serialized as NUnit3 (XML).
- Csv - Output is serialized as a comma separated values (CSV).
- Sarif - Output is serialized as SARIF.

The `Wide` format is not applicable to `Assert-PSRule`.

```yaml
Type: OutputFormat
Parameter Sets: (All)
Aliases: o
Accepted values: None, Yaml, Json, Markdown, NUnit3, Csv
Accepted values: None, Yaml, Json, Markdown, NUnit3, Csv, Sarif
Required: False
Position: Named
Expand Down
3 changes: 2 additions & 1 deletion docs/commands/PSRule/en-US/Invoke-PSRule.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,12 +454,13 @@ This is the default.
- NUnit3 - Output is serialized as NUnit3 (XML).
- Csv - Output is serialized as a comma separated values (CSV).
- Wide - Output is presented using the wide table format, which includes reason and wraps columns.
- Sarif - Output is serialized as SARIF.

```yaml
Type: OutputFormat
Parameter Sets: (All)
Aliases: o
Accepted values: None, Yaml, Json, Markdown, NUnit3, Csv, Wide
Accepted values: None, Yaml, Json, Markdown, NUnit3, Csv, Wide, Sarif
Required: False
Position: Named
Expand Down
2 changes: 1 addition & 1 deletion docs/commands/PSRule/en-US/New-PSRuleOption.md
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ See about_PSRule_Options for more information.
Type: OutputFormat
Parameter Sets: (All)
Aliases:
Accepted values: None, Yaml, Json, Markdown, NUnit3, Csv, Wide
Accepted values: None, Yaml, Json, Markdown, NUnit3, Csv, Wide, Sarif
Required: False
Position: Named
Expand Down
2 changes: 1 addition & 1 deletion docs/commands/PSRule/en-US/Set-PSRuleOption.md
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ The `Output.Format` option configures the format that results will be presented
Type: OutputFormat
Parameter Sets: (All)
Aliases:
Accepted values: None, Yaml, Json, Markdown, NUnit3, Csv, Wide
Accepted values: None, Yaml, Json, Markdown, NUnit3, Csv, Wide, Sarif
Required: False
Position: Named
Expand Down
4 changes: 3 additions & 1 deletion docs/concepts/PSRule/en-US/about_PSRule_Options.md
Original file line number Diff line number Diff line change
Expand Up @@ -1887,8 +1887,10 @@ RuleName, TargetName, TargetType, Outcome, OutcomeReason, Synopsis, Recommendati
- The following columns are included for `Summary` output:
RuleName, Pass, Fail, Outcome, Synopsis, Recommendation
- Wide - Output is presented using the wide table format, which includes reason and wraps columns.
- Sarif - Output is serialized as SARIF.

The Wide format is ignored by `Assert-PSRule`. `Get-PSRule` only accepts `None`, `Wide`, `Yaml` and `Json`.
The Wide format is ignored by `Assert-PSRule`.
`Get-PSRule` only accepts `None`, `Wide`, `Yaml` and `Json`.
Usage of other formats are treated as `None`.

The `Get-PSRuleBaseline` cmdlet only accepts `None` or `Yaml`.
Expand Down
3 changes: 2 additions & 1 deletion schemas/PSRule-options.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@
"Markdown",
"NUnit3",
"Csv",
"Wide"
"Wide",
"Sarif"
],
"default": "None"
},
Expand Down
1 change: 1 addition & 0 deletions src/PSRule.Badges/PSRule.Badges.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<RepositoryUrl>https://github.com/Microsoft/PSRule</RepositoryUrl>
<PackageLicenseExpression>https://github.com/Microsoft/PSRule/blob/main/LICENSE</PackageLicenseExpression>
<Version>0.0.1</Version>
<Company>Microsoft Corporation</Company>
<Copyright>Copyright (c) Microsoft Corporation. Licensed under the MIT License.</Copyright>
<Description>Contains resources for PSRule to work with badges.

Expand Down
5 changes: 5 additions & 0 deletions src/PSRule.Benchmark/PSRule.Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<PlatformTarget>AnyCPU</PlatformTarget>
<ProjectGuid>{3ec0912f-bfc7-4b53-a1a1-0ba993c6282e}</ProjectGuid>
<Authors>Bernie White</Authors>
<RepositoryUrl>https://github.com/Microsoft/PSRule</RepositoryUrl>
<PackageLicenseExpression>https://github.com/Microsoft/PSRule/blob/main/LICENSE</PackageLicenseExpression>
<Company>Microsoft Corporation</Company>
<Copyright>Copyright (c) Microsoft Corporation. Licensed under the MIT License.</Copyright>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand Down
1 change: 1 addition & 0 deletions src/PSRule.BuildTool/PSRule.BuildTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<RepositoryUrl>https://github.com/Microsoft/PSRule</RepositoryUrl>
<PackageLicenseExpression>https://github.com/Microsoft/PSRule/blob/main/LICENSE</PackageLicenseExpression>
<Version>0.0.1</Version>
<Company>Microsoft Corporation</Company>
<Copyright>Copyright (c) Microsoft Corporation. Licensed under the MIT License.</Copyright>
<Description>Tools for building PSRule.

Expand Down
30 changes: 30 additions & 0 deletions src/PSRule/Common/OutputOptionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Text;
using PSRule.Configuration;

namespace PSRule
{
internal static class OutputOptionExtensions
{
/// <summary>
/// Get the character encoding for the specified output encoding.
/// </summary>
public static Encoding GetEncoding(this OutputOption option)
{
var defaultEncoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
return option == null || !option.Encoding.HasValue
? defaultEncoding
: option.Encoding switch
{
OutputEncoding.UTF8 => Encoding.UTF8,
OutputEncoding.UTF7 => Encoding.UTF7,
OutputEncoding.Unicode => Encoding.Unicode,
OutputEncoding.UTF32 => Encoding.UTF32,
OutputEncoding.ASCII => Encoding.ASCII,
_ => defaultEncoding,
};
}
}
}
6 changes: 4 additions & 2 deletions src/PSRule/Configuration/OutputFormat.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Newtonsoft.Json;
Expand All @@ -24,6 +24,8 @@ public enum OutputFormat

Wide = 5,

Markdown = 6
Markdown = 6,

Sarif = 7
}
}
9 changes: 7 additions & 2 deletions src/PSRule/Data/TargetSourceInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
Expand Down Expand Up @@ -85,12 +85,17 @@ public override string ToString()
public string ToString(string defaultType, bool useRelativePath)
{
var type = Type ?? defaultType;
var file = useRelativePath ? ExpressionHelpers.NormalizePath(PSRuleOption.GetWorkingPath(), File) : File;
var file = GetPath(useRelativePath);
return string.IsNullOrEmpty(type)
? string.Concat(file, COLON, Line, COLON, Position)
: string.Concat(type, COLONSPACE, file, COLON, Line, COLON, Position);
}

internal string GetPath(bool useRelativePath)
{
return useRelativePath ? ExpressionHelpers.NormalizePath(PSRuleOption.GetWorkingPath(), File) : File;
}

public static TargetSourceInfo Create(object o)
{
return o is PSObject pso ? Create(pso) : null;
Expand Down
5 changes: 4 additions & 1 deletion src/PSRule/PSRule.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
Expand All @@ -13,8 +13,10 @@
<Authors>Bernie White</Authors>
<AssemblyTitle>PSRule</AssemblyTitle>
<RepositoryUrl>https://github.com/Microsoft/PSRule</RepositoryUrl>
<PackageProjectUrl>https://aka.ms/ps-rule</PackageProjectUrl>
<PackageLicenseExpression>https://github.com/Microsoft/PSRule/blob/main/LICENSE</PackageLicenseExpression>
<Version>0.0.1</Version>
<Company>Microsoft Corporation</Company>
<Copyright>Copyright (c) Microsoft Corporation. Licensed under the MIT License.</Copyright>
<Description>Validate infrastructure as code (IaC) and objects using PowerShell rules.

Expand Down Expand Up @@ -45,6 +47,7 @@ This project uses GitHub Issues to track bugs and feature requests. See GitHub p
</PackageReference>
<PackageReference Include="PowerShellStandard.Library" Version="5.1.0" PrivateAssets="All" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="Sarif.Sdk" Version="2.4.12" />
<PackageReference Include="YamlDotNet" Version="11.2.1" />
</ItemGroup>

Expand Down
10 changes: 5 additions & 5 deletions src/PSRule/PSRule.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function Invoke-PSRule {
[String]$OutputPath,

[Parameter(Mandatory = $False)]
[ValidateSet('None', 'Yaml', 'Json', 'Markdown', 'NUnit3', 'Csv', 'Wide')]
[ValidateSet('None', 'Yaml', 'Json', 'Markdown', 'NUnit3', 'Csv', 'Wide', 'Sarif')]
[Alias('o')]
[PSRule.Configuration.OutputFormat]$OutputFormat = [PSRule.Configuration.OutputFormat]::None,

Expand Down Expand Up @@ -499,7 +499,7 @@ function Assert-PSRule {
[String]$OutputPath,

[Parameter(Mandatory = $False)]
[ValidateSet('None', 'Yaml', 'Json', 'Markdown', 'NUnit3', 'Csv')]
[ValidateSet('None', 'Yaml', 'Json', 'Markdown', 'NUnit3', 'Csv', 'Sarif')]
[Alias('o')]
[PSRule.Configuration.OutputFormat]$OutputFormat,

Expand Down Expand Up @@ -1258,7 +1258,7 @@ function New-PSRuleOption {

# Sets the Output.Format option
[Parameter(Mandatory = $False)]
[ValidateSet('None', 'Yaml', 'Json', 'Markdown', 'NUnit3', 'Csv', 'Wide')]
[ValidateSet('None', 'Yaml', 'Json', 'Markdown', 'NUnit3', 'Csv', 'Wide', 'Sarif')]
[PSRule.Configuration.OutputFormat]$OutputFormat = 'None',

# Sets the Output.Outcome option
Expand Down Expand Up @@ -1520,7 +1520,7 @@ function Set-PSRuleOption {

# Sets the Output.Format option
[Parameter(Mandatory = $False)]
[ValidateSet('None', 'Yaml', 'Json', 'Markdown', 'NUnit3', 'Csv', 'Wide')]
[ValidateSet('None', 'Yaml', 'Json', 'Markdown', 'NUnit3', 'Csv', 'Wide', 'Sarif')]
[PSRule.Configuration.OutputFormat]$OutputFormat = 'None',

# Sets the Output.Outcome option
Expand Down Expand Up @@ -2222,7 +2222,7 @@ function SetOptions {

# Sets the Output.Format option
[Parameter(Mandatory = $False)]
[ValidateSet('None', 'Yaml', 'Json', 'Markdown', 'NUnit3', 'Csv', 'Wide')]
[ValidateSet('None', 'Yaml', 'Json', 'Markdown', 'NUnit3', 'Csv', 'Wide', 'Sarif')]
[PSRule.Configuration.OutputFormat]$OutputFormat = 'None',

# Sets the Output.Outcome option
Expand Down
Loading

0 comments on commit 94f5bc2

Please sign in to comment.