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

fix(cli): Json result adheres to filters for cmd get-report (aws,gcp,azure) #379

Merged
merged 2 commits into from
Apr 13, 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
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")
}
12 changes: 12 additions & 0 deletions integration/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,15 @@ func TestEventCommandOpenError(t *testing.T) {
assert.Equal(t, 1, exitcode,
"EXITCODE is not the expected one")
}

func TestEventCommandListSeverityWithJsonFlag(t *testing.T) {
out, err, exitcode := LaceworkCLIWithTOMLConfig("event", "list", "--severity", "high", "--json")
severities := []string{"\"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")
assert.NotContains(t, severities, out.String(),
"Json output does not adhere to severity filter")
}