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

✨ hard breakpoints for blastradius #1229

Merged
merged 1 commit into from
Apr 5, 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
26 changes: 18 additions & 8 deletions policy/score_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,36 @@ const (

// BlastRadiusConfig for custom blast radius indicators
type BlastRadiusConfig struct {
SmallPct float32
MediumPct float32
CategoryThreshold float32
// Percentage of infrastructure in total weight that is considered small.
// eg. 5%. If something affects 3/100 assets, it is 3% and thus small.
SmallPct float32
// Percentage of infrastructure in total weight that is considered medium.
// eg. 20%. If something affects 10/100 assets, it is 10% and thus medium.
MediumPct float32
// Minimum number of assets for something to be considered medium.
// eg. 10. If something affects 2/4 assets, it is 50%, but still small.
MediumMinCnt float32
// Minimum number of assets for something to be considered large.
// eg. 25. If something affects 20/40 assets, it is 50%, but still medium.
LargeMinCnt float32
}

// DefaultBlastRadiusConfig
var DefaultBlastRadiusConfig = BlastRadiusConfig{
SmallPct: 0.05,
MediumPct: 0.20,
CategoryThreshold: 20,
SmallPct: 0.05,
MediumPct: 0.20,
MediumMinCnt: 10,
LargeMinCnt: 25,
}

// BlastRadius retrieves the blast radius indicator and assets in this category.
// It requires a weight as input
func (b *BlastRadiusConfig) Indicator(totalWeight float32, weight float32) BlastRadiusIndicator {
rel := weight / totalWeight
if rel < b.SmallPct {
if rel < b.SmallPct || weight < b.MediumMinCnt {
return BlastRadius_Small
}
if rel < b.MediumPct {
if rel < b.MediumPct || weight < b.LargeMinCnt {
return BlastRadius_Medium
}
return BlastRadius_Large
Expand Down
34 changes: 34 additions & 0 deletions policy/score_stats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1

package policy

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestBlastRadius(t *testing.T) {
conf := DefaultBlastRadiusConfig
tests := []struct {
n float32
max float32
indicator string
}{
{1, 100, "s"},
{10, 100, "m"},
{30, 100, "l"},
{4, 5, "s"},
{10, 20, "m"},
{50, 100, "l"},
}

for i := range tests {
test := tests[i]
t.Run(fmt.Sprintf("%.2f / %.2f => %s", test.n, test.max, test.indicator), func(t *testing.T) {
assert.Equal(t, test.indicator, string(conf.Indicator(test.max, test.n)))
})
}
}
Loading