From 3666ece96829bfd0ea9a2ed02b2972e57bc6018f Mon Sep 17 00:00:00 2001 From: Christoph Hartmann Date: Sat, 28 Dec 2024 18:57:03 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=E2=AD=90=EF=B8=8F=20human=20readable=20imp?= =?UTF-8?q?act=20value?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- explorer/impact.go | 31 ++++++++++++++++++++++++++----- explorer/impact_test.go | 19 +++++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/explorer/impact.go b/explorer/impact.go index 1e160a69df..f2ee9e1e03 100644 --- a/explorer/impact.go +++ b/explorer/impact.go @@ -11,6 +11,15 @@ import ( "gopkg.in/yaml.v3" ) +// Impact represents severity rating scale when impact is provided as human-readable string value +var impactMapping = map[string]int32{ + "none": 0, + "low": 10, + "medium": 40, + "high": 70, + "critical": 100, +} + func (v *Impact) HumanReadable() string { if v.Value == nil { return "unknown" @@ -22,10 +31,10 @@ func (v *Impact) HumanReadable() string { return "high" case v.Value.Value >= 40: return "medium" - case v.Value.Value > 0: + case v.Value.Value >= 10: return "low" default: - return "info" + return "none" } } @@ -65,10 +74,12 @@ func (v *Impact) Checksum() uint64 { return uint64(res) } +// UnmarshalJSON implements the json.Unmarshaler interface for impact value. It supports human-readable string, int and +// complex struct. func (v *Impact) UnmarshalJSON(data []byte) error { - var res int32 - if err := json.Unmarshal(data, &res); err == nil { - v.Value = &ImpactValue{Value: res} + var intRes int32 + if err := json.Unmarshal(data, &intRes); err == nil { + v.Value = &ImpactValue{Value: intRes} if v.Value.Value < 0 || v.Value.Value > 100 { return errors.New("impact must be between 0 and 100") @@ -76,6 +87,16 @@ func (v *Impact) UnmarshalJSON(data []byte) error { return nil } + var stringRes string + if err := json.Unmarshal(data, &stringRes); err == nil { + val, ok := impactMapping[stringRes] + if !ok { + return errors.New("impact must use critical, high, medium, low or none") + } + v.Value = &ImpactValue{Value: val} + return nil + } + type tmp Impact return json.Unmarshal(data, (*tmp)(v)) } diff --git a/explorer/impact_test.go b/explorer/impact_test.go index 5f11f908de..febf35bc58 100644 --- a/explorer/impact_test.go +++ b/explorer/impact_test.go @@ -34,6 +34,20 @@ func TestImpactParsing(t *testing.T) { Value: &ImpactValue{Value: 40}, }, }, + { + "critical rating", + `"critical"`, + &Impact{ + Value: &ImpactValue{Value: 100}, + }, + }, + { + "low rating", + `"low"`, + &Impact{ + Value: &ImpactValue{Value: 10}, + }, + }, } for i := range tests { @@ -71,6 +85,11 @@ func TestImpactParsing(t *testing.T) { `{"value": 101, "weight": 90}`, "impact must be between 0 and 100", }, + { + "invalid string value", + `"mycustomcritical"`, + "impact must use critical, high, medium, low or none", + }, } for i := range errTests { From 14a2e513381ee719df20ddfb6c6fc08179255daa Mon Sep 17 00:00:00 2001 From: Christoph Hartmann Date: Sun, 29 Dec 2024 13:53:21 +0100 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=A7=B9=20adjust=20impact=20values=20t?= =?UTF-8?q?hat=20are=20mapped=20to=20ratings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- explorer/impact.go | 8 ++++---- explorer/impact_test.go | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/explorer/impact.go b/explorer/impact.go index f2ee9e1e03..cb2b2efb5d 100644 --- a/explorer/impact.go +++ b/explorer/impact.go @@ -14,10 +14,10 @@ import ( // Impact represents severity rating scale when impact is provided as human-readable string value var impactMapping = map[string]int32{ "none": 0, - "low": 10, - "medium": 40, - "high": 70, - "critical": 100, + "low": 20, + "medium": 55, + "high": 80, + "critical": 95, } func (v *Impact) HumanReadable() string { diff --git a/explorer/impact_test.go b/explorer/impact_test.go index febf35bc58..212efacd87 100644 --- a/explorer/impact_test.go +++ b/explorer/impact_test.go @@ -38,14 +38,14 @@ func TestImpactParsing(t *testing.T) { "critical rating", `"critical"`, &Impact{ - Value: &ImpactValue{Value: 100}, + Value: &ImpactValue{Value: 95}, }, }, { "low rating", `"low"`, &Impact{ - Value: &ImpactValue{Value: 10}, + Value: &ImpactValue{Value: 20}, }, }, }