Skip to content

Commit

Permalink
fix: Json result adheres to filters for cmd get-report(aws,gcp,azure)
Browse files Browse the repository at this point in the history
Signed-off-by: Darren Murray <[email protected]>
  • Loading branch information
dmurray-lacework committed Apr 12, 2021
1 parent 44b8e58 commit e84217e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 15 deletions.
8 changes: 2 additions & 6 deletions cli/cmd/compliance.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,8 @@ func complianceReportSummaryTable(summaries []api.ComplianceSummary) [][]string
}
}

func complianceReportRecommendationsTable(recommendations []api.ComplianceRecommendation) ([][]string, string) {
func complianceReportRecommendationsTable(recommendations []api.ComplianceRecommendation) [][]string {
out := [][]string{}
var filteredOutput string
if complianceFiltersEnabled() {
recommendations, filteredOutput = filterRecommendations(recommendations)
}
for _, recommend := range recommendations {
out = append(out, []string{
recommend.RecID,
Expand All @@ -211,7 +207,7 @@ func complianceReportRecommendationsTable(recommendations []api.ComplianceRecomm
return severityOrder(out[i][3]) < severityOrder(out[j][3])
})

return out, filteredOutput
return out
}

func buildComplianceReportTable(detailsTable, summaryTable, recommendationsTable [][]string, filteredOutput string) string {
Expand Down
12 changes: 9 additions & 3 deletions cli/cmd/compliance_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,18 @@ To run an ad-hoc compliance assessment of an AWS account:
return errors.New("there is no data found in the report")
}

report := response.Data[0]
filteredOutput := ""

if complianceFiltersEnabled() {
report.Recommendations, filteredOutput = filterRecommendations(report.Recommendations)
}

if cli.JSONOutput() {
return cli.OutputJSON(response.Data[0])
return cli.OutputJSON(report)
}

report := response.Data[0]
recommendations, filteredOutput := complianceReportRecommendationsTable(report.Recommendations)
recommendations := complianceReportRecommendationsTable(report.Recommendations)
cli.OutputHuman("\n")
cli.OutputHuman(
buildComplianceReportTable(
Expand Down
12 changes: 9 additions & 3 deletions cli/cmd/compliance_azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,18 @@ To run an ad-hoc compliance assessment use the command:
return errors.New("there is no data found in the report")
}

report := response.Data[0]
filteredOutput := ""

if complianceFiltersEnabled() {
report.Recommendations, filteredOutput = filterRecommendations(report.Recommendations)
}

if cli.JSONOutput() {
return cli.OutputJSON(response.Data[0])
return cli.OutputJSON(report)
}

report := response.Data[0]
recommendations, filteredOutput := complianceReportRecommendationsTable(report.Recommendations)
recommendations := complianceReportRecommendationsTable(report.Recommendations)
cli.OutputHuman("\n")
cli.OutputHuman(
buildComplianceReportTable(
Expand Down
12 changes: 9 additions & 3 deletions cli/cmd/compliance_gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,19 @@ To run an ad-hoc compliance assessment use the command:
return errors.New("there is no data found in the report")
}

report := response.Data[0]
filteredOutput := ""

if complianceFiltersEnabled() {
report.Recommendations, filteredOutput = filterRecommendations(report.Recommendations)
}

if cli.JSONOutput() {
return cli.OutputJSON(response.Data[0])
return cli.OutputJSON(report)
}

report := response.Data[0]
recommendations := complianceReportRecommendationsTable(report.Recommendations)
cli.OutputHuman("\n")
recommendations, filteredOutput := complianceReportRecommendationsTable(report.Recommendations)
cli.OutputHuman(
buildComplianceReportTable(
complianceGcpReportDetailsTable(&report),
Expand Down
11 changes: 11 additions & 0 deletions integration/compliance_aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,14 @@ func TestComplianceAwsGetReportDetails(t *testing.T) {
assert.Contains(t, out.String(), "ASSESSED",
"STDOUT table headers changed, please check")
}

func TestComplianceAwsGetReportFiltersWithJsonOutput(t *testing.T) {
account := os.Getenv("LW_INT_TEST_AWS_ACC")
out, err, exitcode := LaceworkCLIWithTOMLConfig("compliance", "aws", "get-report", account, "--severity", "critical", "--json")
severities := []string{"\"severity\": 2","\"severity\": 3","\"severity\": 4", "\"severity\": 5"}
assert.Empty(t, err.String(), "STDERR should be empty")
assert.Equal(t, 0, exitcode, "EXITCODE is not the expected one")
// When critical severity filter is set, other severities should not be returned in json result
assert.NotContains(t, severities, out.String(),
"Json output does not adhere to severity filter")
}

0 comments on commit e84217e

Please sign in to comment.