-
Notifications
You must be signed in to change notification settings - Fork 0
/
severity.go
153 lines (129 loc) · 3.28 KB
/
severity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package osv_schema
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"github.com/golang-infrastructure/go-pointer"
"strconv"
)
// ------------------------------------------------ ---------------------------------------------------------------------
type SeveritySlice []*Severity
var _ sql.Scanner = &SeveritySlice{}
var _ driver.Valuer = &SeveritySlice{}
func (x SeveritySlice) GetCVSS3() *Severity {
for _, s := range x {
if s.Type == SeverityTypeCVSS3 {
return s
}
}
return nil
}
func (x SeveritySlice) GetCVSS2() *Severity {
for _, s := range x {
if s.Type == SeverityTypeCVSS2 {
return s
}
}
return nil
}
func (x *SeveritySlice) Scan(src any) error {
if src == nil {
return nil
}
bytes, ok := src.([]byte)
if !ok {
return wrapScanError(src, x)
}
if len(bytes) == 0 {
return nil
}
return json.Unmarshal(bytes, &x)
}
func (x SeveritySlice) Value() (driver.Value, error) {
if len(x) == 0 {
return nil, nil
}
marshal, err := json.Marshal(x)
if err != nil {
return nil, err
}
if len(marshal) == 0 {
return nil, nil
}
return string(marshal), nil
}
// ------------------------------------------------ ---------------------------------------------------------------------
type SeverityType string
const (
// SeverityTypeCVSS2 e.g."AV:L/AC:M/Au:N/C:N/I:P/A:C"
SeverityTypeCVSS2 SeverityType = "CVSS_V2"
// SeverityTypeCVSS3 CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:C/C:H/I:N/A:N
SeverityTypeCVSS3 SeverityType = "CVSS_V3"
)
// Severity
// Example:
//
// {
// "type": "CVSS_V3",
// "score": "CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:N/I:N/A:H"
// }
//
// Document: https://ossf.github.io/osv-schema/#severity-field
type Severity struct {
Type SeverityType `mapstructure:"type" json:"type" yaml:"type" db:"type" bson:"type" gorm:"column:type"`
Score string `mapstructure:"score" json:"score" yaml:"score" db:"score" bson:"score" gorm:"column:score"`
score *float64
err error
}
var _ sql.Scanner = &Severity{}
var _ driver.Valuer = &Severity{}
// ------------------------------------------------- --------------------------------------------------------------------
func (x *Severity) GetScore() float64 {
score, _ := x.GetScoreAsFloat()
return score
}
func (x *Severity) GetScoreAsPointer() *float64 {
score, err := x.GetScoreAsFloat()
if err != nil {
return nil
} else {
return pointer.ToPointer(score)
}
}
func (x *Severity) GetScoreAsFloat() (float64, error) {
if x.err != nil {
return 0, x.err
} else if x.score != nil {
return pointer.FromPointer(x.score), nil
}
if x.Score == "" {
x.err = fmt.Errorf("score can not be empty")
return 0, x.err
}
score, err := strconv.ParseFloat(x.Score, 64)
if err != nil {
x.err = err
return 0, err
}
x.score = pointer.ToPointer(score)
return score, nil
}
// ------------------------------------------------- --------------------------------------------------------------------
func (x *Severity) Value() (driver.Value, error) {
if x == nil {
return nil, nil
}
return json.Marshal(x)
}
func (x *Severity) Scan(src any) error {
if src == nil {
return nil
}
bytes, ok := src.([]byte)
if !ok {
return wrapScanError(src, x)
}
return json.Unmarshal(bytes, &x)
}
// ------------------------------------------------- --------------------------------------------------------------------