Skip to content

Commit

Permalink
🐛 fix policy scoring with exceptions (#1350)
Browse files Browse the repository at this point in the history
* 🐛 fix policy scoring with exceptions

Signed-off-by: Ivan Milchev <ivan@mondoo.com>

* add tests for ignored scores

Signed-off-by: Ivan Milchev <ivan@mondoo.com>

---------

Signed-off-by: Ivan Milchev <ivan@mondoo.com>
  • Loading branch information
imilchev authored Jul 2, 2024
1 parent 4ce6e08 commit a44781b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
30 changes: 25 additions & 5 deletions policy/score_calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,17 @@ func AddDataScore(calculator ScoreCalculator, totalDeps int, finishedDeps int) {

func (c *averageScoreCalculator) Add(score *Score, impact *explorer.Impact) {
switch score.Type {
case ScoreType_Skip:
case ScoreType_Skip, ScoreType_Disabled, ScoreType_OutOfScope:
return
case ScoreType_Unscored:
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal

case ScoreType_Result:
if impact != nil && (impact.Action == explorer.Action_IGNORE || impact.Action == explorer.Action_DEACTIVATE) {
return
}

c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.weight += score.Weight
Expand Down Expand Up @@ -226,13 +230,17 @@ func (c *weightedScoreCalculator) Init() {

func (c *weightedScoreCalculator) Add(score *Score, impact *explorer.Impact) {
switch score.Type {
case ScoreType_Skip:
case ScoreType_Skip, ScoreType_Disabled, ScoreType_OutOfScope:
return
case ScoreType_Unscored:
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal

case ScoreType_Result:
if impact != nil && (impact.Action == explorer.Action_IGNORE || impact.Action == explorer.Action_DEACTIVATE) {
return
}

c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.weight += score.Weight
Expand Down Expand Up @@ -332,13 +340,17 @@ func (c *worstScoreCalculator) Init() {

func (c *worstScoreCalculator) Add(score *Score, impact *explorer.Impact) {
switch score.Type {
case ScoreType_Skip:
case ScoreType_Skip, ScoreType_Disabled, ScoreType_OutOfScope:
return
case ScoreType_Unscored:
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal

case ScoreType_Result:
if impact != nil && (impact.Action == explorer.Action_IGNORE || impact.Action == explorer.Action_DEACTIVATE) {
return
}

c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.weight += score.Weight
Expand Down Expand Up @@ -452,13 +464,17 @@ func (c *bandedScoreCalculator) Init() {

func (c *bandedScoreCalculator) Add(score *Score, impact *explorer.Impact) {
switch score.Type {
case ScoreType_Skip:
case ScoreType_Skip, ScoreType_OutOfScope, ScoreType_Disabled:
return
case ScoreType_Unscored:
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal

case ScoreType_Result:
if impact != nil && (impact.Action == explorer.Action_IGNORE || impact.Action == explorer.Action_DEACTIVATE) {
return
}

c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.weight += score.Weight
Expand Down Expand Up @@ -623,13 +639,17 @@ func (c *decayedScoreCalculator) Init() {

func (c *decayedScoreCalculator) Add(score *Score, impact *explorer.Impact) {
switch score.Type {
case ScoreType_Skip:
case ScoreType_Skip, ScoreType_OutOfScope, ScoreType_Disabled:
return
case ScoreType_Unscored:
c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal

case ScoreType_Result:
if impact != nil && (impact.Action == explorer.Action_IGNORE || impact.Action == explorer.Action_DEACTIVATE) {
return
}

c.dataCompletion += score.DataCompletion * score.DataTotal
c.dataTotal += score.DataTotal
c.weight += score.Weight
Expand Down
30 changes: 30 additions & 0 deletions policy/score_calculator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ func TestAverageScores(t *testing.T) {
{Value: 0, ScoreCompletion: 0, DataCompletion: 80, DataTotal: 5, Weight: 1, Type: ScoreType_Result},
{Value: 20, ScoreCompletion: 20, DataCompletion: 50, DataTotal: 2, Weight: 2, Type: ScoreType_Result},
{Value: 100, ScoreCompletion: 100, DataCompletion: 33, DataTotal: 3, Weight: 3, Type: ScoreType_Result},
{Value: 30, ScoreCompletion: 100, DataCompletion: 33, DataTotal: 3, Weight: 3, Type: ScoreType_Disabled},
{Value: 30, ScoreCompletion: 100, DataCompletion: 33, DataTotal: 3, Weight: 3, Type: ScoreType_OutOfScope},
{Value: 30, ScoreCompletion: 100, DataCompletion: 33, DataTotal: 3, Weight: 3, Type: ScoreType_Result},
},
impacts: []*explorer.Impact{
nil, nil, nil, nil, nil, {Action: explorer.Action_IGNORE},
},
out: &Score{Value: 60, ScoreCompletion: 40, DataCompletion: 59, DataTotal: 10, Weight: 6, Type: ScoreType_Result},
},
Expand Down Expand Up @@ -110,6 +116,12 @@ func TestWeightedScores(t *testing.T) {
{Value: 0, ScoreCompletion: 0, DataCompletion: 80, DataTotal: 5, Weight: 1, Type: ScoreType_Result},
{Value: 20, ScoreCompletion: 20, DataCompletion: 50, DataTotal: 2, Weight: 2, Type: ScoreType_Result},
{Value: 100, ScoreCompletion: 100, DataCompletion: 33, DataTotal: 3, Weight: 3, Type: ScoreType_Result},
{Value: 30, ScoreCompletion: 100, DataCompletion: 33, DataTotal: 3, Weight: 3, Type: ScoreType_Disabled},
{Value: 30, ScoreCompletion: 100, DataCompletion: 33, DataTotal: 3, Weight: 3, Type: ScoreType_OutOfScope},
{Value: 30, ScoreCompletion: 100, DataCompletion: 33, DataTotal: 3, Weight: 3, Type: ScoreType_Result},
},
impacts: []*explorer.Impact{
nil, nil, nil, nil, nil, {Action: explorer.Action_IGNORE},
},
out: &Score{Value: 68, ScoreCompletion: 40, DataCompletion: 59, Weight: 6, Type: ScoreType_Result},
},
Expand Down Expand Up @@ -151,6 +163,12 @@ func TestWorstScores(t *testing.T) {
{Value: 0, ScoreCompletion: 0, DataCompletion: 80, DataTotal: 5, Weight: 1, Type: ScoreType_Result},
{Value: 20, ScoreCompletion: 20, DataCompletion: 50, DataTotal: 2, Weight: 2, Type: ScoreType_Result},
{Value: 100, ScoreCompletion: 100, DataCompletion: 33, DataTotal: 3, Weight: 3, Type: ScoreType_Result},
{Value: 30, ScoreCompletion: 100, DataCompletion: 33, DataTotal: 3, Weight: 3, Type: ScoreType_Disabled},
{Value: 30, ScoreCompletion: 100, DataCompletion: 33, DataTotal: 3, Weight: 3, Type: ScoreType_OutOfScope},
{Value: 30, ScoreCompletion: 100, DataCompletion: 33, DataTotal: 3, Weight: 3, Type: ScoreType_Result},
},
impacts: []*explorer.Impact{
nil, nil, nil, nil, nil, {Action: explorer.Action_IGNORE},
},
out: &Score{Value: 20, ScoreCompletion: 40, DataCompletion: 59, Weight: 6, Type: ScoreType_Result},
},
Expand Down Expand Up @@ -190,13 +208,19 @@ func TestBandedScores(t *testing.T) {
{Value: 100, ScoreCompletion: 100, DataCompletion: 100, DataTotal: 1, Weight: 1, Type: ScoreType_Result},
// 8 low checks (ok)
{Value: 100, ScoreCompletion: 100, DataCompletion: 33, DataTotal: 3, Weight: 8, Type: ScoreType_Result},
{Value: 30, ScoreCompletion: 100, DataCompletion: 33, DataTotal: 3, Weight: 3, Type: ScoreType_Disabled},
{Value: 30, ScoreCompletion: 100, DataCompletion: 33, DataTotal: 3, Weight: 3, Type: ScoreType_OutOfScope},
{Value: 30, ScoreCompletion: 100, DataCompletion: 33, DataTotal: 3, Weight: 3, Type: ScoreType_Result},
},
impacts: []*explorer.Impact{
// 2 critical checks
{Value: &explorer.ImpactValue{Value: 100}},
{Value: &explorer.ImpactValue{Value: 100}},
// 8 low checks
{Value: &explorer.ImpactValue{Value: 20}},
{Value: &explorer.ImpactValue{Value: 100}},
{Value: &explorer.ImpactValue{Value: 100}},
{Action: explorer.Action_IGNORE},
},
out: &Score{Value: 25, ScoreCompletion: 100, DataCompletion: 66, Weight: 10, Type: ScoreType_Result},
},
Expand Down Expand Up @@ -250,13 +274,19 @@ func TestDecayedScores(t *testing.T) {
{Value: 100, ScoreCompletion: 100, DataCompletion: 100, DataTotal: 1, Weight: 1, Type: ScoreType_Result},
// 8 low checks (ok)
{Value: 100, ScoreCompletion: 100, DataCompletion: 33, DataTotal: 3, Weight: 8, Type: ScoreType_Result},
{Value: 30, ScoreCompletion: 100, DataCompletion: 33, DataTotal: 3, Weight: 3, Type: ScoreType_Disabled},
{Value: 30, ScoreCompletion: 100, DataCompletion: 33, DataTotal: 3, Weight: 3, Type: ScoreType_OutOfScope},
{Value: 30, ScoreCompletion: 100, DataCompletion: 33, DataTotal: 3, Weight: 3, Type: ScoreType_Result},
},
impacts: []*explorer.Impact{
// 2 critical checks
{Value: &explorer.ImpactValue{Value: 100}},
{Value: &explorer.ImpactValue{Value: 100}},
// 8 low checks
{Value: &explorer.ImpactValue{Value: 20}},
{Value: &explorer.ImpactValue{Value: 100}},
{Value: &explorer.ImpactValue{Value: 100}},
{Action: explorer.Action_IGNORE},
},
out: &Score{Value: 61, ScoreCompletion: 100, DataCompletion: 66, Weight: 10, Type: ScoreType_Result},
},
Expand Down

0 comments on commit a44781b

Please sign in to comment.