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

Add severity to rule in ListEvaluationHistory RPC. #4014

Merged
merged 1 commit into from
Jul 29, 2024
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
1 change: 1 addition & 0 deletions database/query/eval_history.sql
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ SELECT s.id::uuid AS evaluation_id,
-- rule type, name, and profile
rt.name AS rule_type,
ri.name AS rule_name,
rt.severity_value as rule_severity,
p.name AS profile_name,
-- evaluation status and details
s.status AS evaluation_status,
Expand Down
1 change: 1 addition & 0 deletions docs/docs/ref/proto.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 24 additions & 4 deletions internal/controlplane/handlers_evalstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ func fromEvaluationHistoryRows(
return nil, err
}

ruleSeverity, err := dbSeverityToSeverity(row.RuleSeverity)
if err != nil {
return nil, err
}

var alert *minderv1.EvaluationHistoryAlert
if row.AlertStatus.Valid {
alert = &minderv1.EvaluationHistoryAlert{
Expand All @@ -174,6 +179,7 @@ func fromEvaluationHistoryRows(
Rule: &minderv1.EvaluationHistoryRule{
Name: row.RuleName,
RuleType: row.RuleType,
Severity: ruleSeverity,
Profile: row.ProfileName,
},
Status: &minderv1.EvaluationHistoryStatus{
Expand Down Expand Up @@ -466,9 +472,11 @@ func buildRuleEvaluationStatusFromDBEvaluation(
guidance = eval.RuleTypeGuidance
}

sev := &minderv1.Severity{}
sev.EnsureDefault()
if err := sev.Value.FromString(string(eval.RuleTypeSeverityValue)); err != nil {
var sev *minderv1.Severity
var err error

sev, err = dbSeverityToSeverity(eval.RuleTypeSeverityValue)
if err != nil {
zerolog.Ctx(ctx).
Err(err).
Str("value", string(eval.RuleTypeSeverityValue)).
Expand All @@ -477,7 +485,6 @@ func buildRuleEvaluationStatusFromDBEvaluation(

entityInfo := map[string]string{}
remediationURL := ""
var err error
if eval.Entity == db.EntitiesRepository {
entityInfo["provider"] = eval.Provider
entityInfo["repo_owner"] = eval.RepoOwner
Expand Down Expand Up @@ -603,6 +610,19 @@ func dbEntityToEntity(dbEnt db.Entities) minderv1.Entity {
}
}

func dbSeverityToSeverity(dbSev db.Severity) (*minderv1.Severity, error) {
severity := &minderv1.Severity{}
severity.EnsureDefault()
if err := severity.Value.FromString(string(dbSev)); err != nil {
// This is not an elegant pattern, but we have places
// in the code where the error was simply logged and
// default value for severity was used.
return severity, err
}

return severity, nil
}

func getEntityName(
dbEnt db.Entities,
row db.ListEvaluationHistoryRow,
Expand Down
8 changes: 8 additions & 0 deletions internal/controlplane/handlers_evalstatus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ func TestFromEvaluationHistoryRows(t *testing.T) {
ProjectID: uuid.NullUUID{},
RuleType: "rule_type",
RuleName: "rule_name",
RuleSeverity: "unknown",
ProfileName: "profile_name",
},
},
Expand All @@ -388,6 +389,7 @@ func TestFromEvaluationHistoryRows(t *testing.T) {
ProjectID: uuid.NullUUID{},
RuleType: "rule_type",
RuleName: "rule_name",
RuleSeverity: "unknown",
ProfileName: "profile_name",
},
{
Expand All @@ -400,6 +402,7 @@ func TestFromEvaluationHistoryRows(t *testing.T) {
ProjectID: uuid.NullUUID{},
RuleType: "rule_type",
RuleName: "rule_name",
RuleSeverity: "unknown",
ProfileName: "profile_name",
},
},
Expand All @@ -417,6 +420,7 @@ func TestFromEvaluationHistoryRows(t *testing.T) {
ProjectID: uuid.NullUUID{},
RuleType: "rule_type",
RuleName: "rule_name",
RuleSeverity: "unknown",
ProfileName: "profile_name",
AlertStatus: nullAlertStatusOK(),
AlertDetails: nullStr("alert details"),
Expand All @@ -436,6 +440,7 @@ func TestFromEvaluationHistoryRows(t *testing.T) {
ProjectID: uuid.NullUUID{},
RuleType: "rule_type",
RuleName: "rule_name",
RuleSeverity: "unknown",
ProfileName: "profile_name",
RemediationStatus: nullRemediationStatusTypesSuccess(),
RemediationDetails: nullStr("remediation details"),
Expand Down Expand Up @@ -466,6 +471,9 @@ func TestFromEvaluationHistoryRows(t *testing.T) {
require.Equal(t, dbEntityToEntity(row.EntityType), item.Entity.Type)
require.Equal(t, row.RuleType, item.Rule.RuleType)
require.Equal(t, row.RuleName, item.Rule.Name)
sev, err := dbSeverityToSeverity(row.RuleSeverity)
require.NoError(t, err)
require.Equal(t, sev, item.Rule.Severity)
require.Equal(t, row.ProfileName, item.Rule.Profile)

require.Equal(t, row.AlertStatus.Valid, item.Alert != nil)
Expand Down
3 changes: 3 additions & 0 deletions internal/db/eval_history.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pkg/api/openapi/minder/v1/minder.swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading