Skip to content

Commit

Permalink
Display the remediation errors to the user in policy_status list
Browse files Browse the repository at this point in the history
Extends the CLI tool to display the remediation status in a new column
  • Loading branch information
jhrozek committed Oct 4, 2023
1 parent 818d23b commit d971bde
Showing 1 changed file with 53 additions and 15 deletions.
68 changes: 53 additions & 15 deletions cmd/cli/app/policy_status/table_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ import (
)

const (
successStatus = "success"
failureStatus = "failure"
errorStatus = "error"
skippedStatus = "skipped"
pendingStatus = "pending"
successStatus = "success"
failureStatus = "failure"
errorStatus = "error"
skippedStatus = "skipped"
pendingStatus = "pending"
notAvailableStatus = "not_available"
)

func initializePolicyStatusTable(cmd *cobra.Command) *tablewriter.Table {
Expand All @@ -53,22 +54,22 @@ func renderPolicyStatusTable(
row := []string{
ps.PolicyId,
ps.PolicyName,
getStatusText(ps.PolicyStatus),
getEvalStatusText(ps.PolicyStatus),
ps.LastUpdated.AsTime().Format(time.RFC3339),
}

table.Rich(row, []tablewriter.Colors{
{},
{},
getStatusColor(ps.PolicyStatus),
getEvalStatusColor(ps.PolicyStatus),
{},
})
}

func initializeRuleEvaluationStatusTable(cmd *cobra.Command) *tablewriter.Table {
table := tablewriter.NewWriter(cmd.OutOrStdout())
table.SetHeader([]string{
"Policy ID", "Rule ID", "Rule Name", "Entity", "Status", "Entity Info", "Guidance"})
"Policy ID", "Rule ID", "Rule Name", "Entity", "Status", "Remediation Status", "Entity Info", "Guidance"})
table.SetRowLine(true)
table.SetRowSeparator("-")
table.SetAutoMergeCellsByColumnIndex([]int{0})
Expand All @@ -87,7 +88,8 @@ func renderRuleEvaluationStatusTable(
reval.RuleId,
reval.RuleName,
reval.Entity,
getStatusText(reval.Status),
getEvalStatusText(reval.Status),
getRemediationStatusText(reval.RemediationStatus),
mapToYAMLOrEmpty(reval.EntityInfo),
guidanceOrEncouragement(reval.Status, reval.Guidance),
}
Expand All @@ -97,15 +99,16 @@ func renderRuleEvaluationStatusTable(
{},
{},
{},
getStatusColor(reval.Status),
getEvalStatusColor(reval.Status),
getRemediateStatusColor(reval.RemediationStatus),
{},
{},
})
}

// Gets a friendly status text with an emoji
func getStatusText(status string) string {
// statuses can be 'success', 'failure', 'error', 'skipped', 'pending'
func getEvalStatusText(status string) string {
// eval statuses can be 'success', 'failure', 'error', 'skipped', 'pending'
switch strings.ToLower(status) {
case successStatus:
return "✅ Success"
Expand All @@ -114,16 +117,35 @@ func getStatusText(status string) string {
case errorStatus:
return "❌ Error"
case skippedStatus:
return "⚠️ Skipped"
return " Skipped"
case pendingStatus:
return "⏳ Pending"
default:
return "⚠️ Unknown"
}
}

func getStatusColor(status string) tablewriter.Colors {
// statuses can be 'success', 'failure', 'error', 'skipped', 'pending'
// Gets a friendly status text with an emoji
func getRemediationStatusText(status string) string {
// remediation statuses can be 'success', 'failure', 'error', 'skipped', 'not supported'
switch strings.ToLower(status) {
case successStatus:
return "✅ Success"
case failureStatus:
return "❌ Failure"
case errorStatus:
return "❌ Error"
case skippedStatus:
return "" // visually empty as we didn't have to remediate
case notAvailableStatus:
return "🚫 Not Available"
default:
return "⚠️ Unknown"
}
}

func getEvalStatusColor(status string) tablewriter.Colors {
// eval statuses can be 'success', 'failure', 'error', 'skipped', 'pending'
switch strings.ToLower(status) {
case successStatus:
return tablewriter.Colors{tablewriter.FgGreenColor}
Expand All @@ -138,6 +160,22 @@ func getStatusColor(status string) tablewriter.Colors {
}
}

func getRemediateStatusColor(status string) tablewriter.Colors {
// remediation statuses can be 'success', 'failure', 'error', 'skipped', 'not supported'
switch strings.ToLower(status) {
case successStatus:
return tablewriter.Colors{tablewriter.FgGreenColor}
case failureStatus:
return tablewriter.Colors{tablewriter.FgRedColor}
case errorStatus:
return tablewriter.Colors{tablewriter.FgRedColor}
case notAvailableStatus:
return tablewriter.Colors{tablewriter.FgYellowColor}
default:
return tablewriter.Colors{}
}
}

func mapToYAMLOrEmpty(m map[string]string) string {
if m == nil {
return ""
Expand Down

0 comments on commit d971bde

Please sign in to comment.