-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into GitLab_Policy_Addition
- Loading branch information
Showing
4 changed files
with
160 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package formatter | ||
|
||
import ( | ||
"encoding/csv" | ||
"bytes" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"github.com/Legit-Labs/legitify/internal/analyzers" | ||
"github.com/Legit-Labs/legitify/internal/outputer/scheme" | ||
) | ||
|
||
type CsvFormatter struct { | ||
colorizer humanColorizer | ||
} | ||
|
||
func newCSVFormatter() OutputFormatter { | ||
return &CsvFormatter{ | ||
colorizer: humanColorizer{}, | ||
} | ||
} | ||
|
||
func (f *CsvFormatter) csvFailedPolicies(output *scheme.Flattened, csvwriter *csv.Writer) bool { | ||
failedPolicies := output.OnlyFailedViolations() | ||
headers := []string{"#", "Policy Name", "Namespace", "Severity", "Threat", "Violations", "Remediation Steps"} | ||
err := csvwriter.Write(headers) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for i, policyName := range failedPolicies.AsOrderedMap().Keys() { | ||
policyData := output.GetPolicyData(policyName) | ||
policyInfo := policyData.PolicyInfo | ||
rowNum := i+1 | ||
policyName := policyInfo.PolicyName | ||
Namespace := policyInfo.Namespace | ||
Severity := policyInfo.Severity | ||
Threat := strings.Join([]string(policyInfo.Threat), "\n") | ||
|
||
RemediationSteps := strings.Join([]string(policyInfo.RemediationSteps), "\n") | ||
var entityType, Link, violationString string | ||
var violationsSummary []string | ||
for _, violation := range policyData.Violations { | ||
entityType = (&violation).ViolationEntityType | ||
Link = violation.CanonicalLink | ||
violationString = entityType + " " + Link | ||
violationsSummary = append(violationsSummary, violationString) | ||
} | ||
violationsPolicy := strings.Join([]string(violationsSummary), "\n") | ||
|
||
row := []string{strconv.Itoa(rowNum), policyName, Namespace, Severity, Threat, violationsPolicy, RemediationSteps} | ||
err := csvwriter.Write(row) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
} | ||
err = csvwriter.Write([]string{"\n"}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
|
||
return true | ||
} | ||
|
||
|
||
func (f *CsvFormatter) formatSummary(output *scheme.Flattened, csvwriter *csv.Writer) bool { | ||
headers := []string{"#", "Namespace", "Policy", "Severity", "Passed", "Failed", "Skipped"} | ||
err := csvwriter.Write(headers) | ||
|
||
for i, policyName := range output.AsOrderedMap().Keys() { | ||
rowNum := i+1 | ||
data := output.GetPolicyData(policyName) | ||
policyInfo := data.PolicyInfo | ||
title := policyInfo.Title | ||
severity := policyInfo.Severity | ||
namespace := policyInfo.Namespace | ||
|
||
var passed, failed, skipped int | ||
for _, violation := range data.Violations { | ||
switch violation.Status { | ||
case analyzers.PolicyPassed: | ||
passed++ | ||
case analyzers.PolicyFailed: | ||
failed++ | ||
case analyzers.PolicySkipped: | ||
skipped++ | ||
} | ||
} | ||
|
||
|
||
row := []string{strconv.Itoa(rowNum), namespace, title, severity, strconv.Itoa(passed), strconv.Itoa(failed), strconv.Itoa(skipped)} | ||
err := csvwriter.Write(row) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return true | ||
} | ||
|
||
func (f *CsvFormatter) Format(output scheme.Scheme, failedOnly bool) ([]byte, error) { | ||
var csvBuffer bytes.Buffer | ||
csvWriter := csv.NewWriter(&csvBuffer) | ||
|
||
typedOutput, ok := output.(*scheme.Flattened) | ||
if !ok { | ||
return nil, UnsupportedScheme{output} | ||
} | ||
|
||
if !failedOnly { | ||
f.formatSummary(typedOutput, csvWriter) | ||
} | ||
f.csvFailedPolicies(typedOutput, csvWriter) | ||
csvWriter.Flush() | ||
|
||
// Check for errors during flushing | ||
if err := csvWriter.Error(); err != nil { | ||
fmt.Printf("Error: %v\n", err) | ||
panic(err) | ||
} | ||
|
||
csvData := csvBuffer.Bytes() | ||
|
||
return csvData, nil | ||
} | ||
|
||
func (f *CsvFormatter) IsSchemeSupported(schemeType string) bool { | ||
return schemeType == scheme.TypeFlattened | ||
} |
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,20 @@ | ||
package formatter_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Legit-Labs/legitify/internal/outputer/formatter" | ||
"github.com/Legit-Labs/legitify/internal/outputer/scheme/scheme_test" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFormatCsv(t *testing.T) { | ||
sample := scheme_test.SchemeSample() | ||
|
||
for _, f := range []bool{true, false} { | ||
bytes, err := formatter.Format(formatter.Csv, formatter.DefaultOutputIndent, sample, f) | ||
require.Nilf(t, err, "Error formatting csv: %v", err) | ||
require.NotNil(t, bytes, "Error formatting csv") | ||
require.NotEmpty(t, bytes, "Error formatting csv") | ||
} | ||
} |
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