-
Notifications
You must be signed in to change notification settings - Fork 50
/
finding.go
106 lines (94 loc) · 2.47 KB
/
finding.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
package types
import (
"fmt"
"strings"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// Findings is an array of finding
type Findings []Finding
func NewFinding(pid, fid, title, detail, hash string, operator sdk.AccAddress, createTime time.Time, level SeverityLevel) (Finding, error) {
return Finding{
ProgramId: pid,
FindingId: fid,
Title: title,
FindingHash: hash,
SubmitterAddress: operator.String(),
SeverityLevel: level,
Status: FindingStatusSubmitted,
Detail: detail,
CreateTime: createTime,
}, nil
}
func (m Finding) ValidateBasic() error {
if len(m.ProgramId) == 0 {
return ErrProgramID
}
if len(m.FindingId) == 0 {
return ErrFindingID
}
if _, err := sdk.AccAddressFromBech32(m.SubmitterAddress); err != nil {
return err
}
if !ValidFindingStatus(m.Status) {
return ErrFindingStatusInvalid
}
if !ValidFindingSeverityLevel(m.SeverityLevel) {
return ErrFindingSeverityLevelInvalid
}
return nil
}
// ValidFindingStatus returns true if the finding status is valid and false
// otherwise.
func ValidFindingStatus(status FindingStatus) bool {
if status == FindingStatusSubmitted ||
status == FindingStatusActive ||
status == FindingStatusConfirmed ||
status == FindingStatusPaid ||
status == FindingStatusClosed {
return true
}
return false
}
// ValidFindingSeverityLevel returns true if the finding level is valid and false
// otherwise.
func ValidFindingSeverityLevel(level SeverityLevel) bool {
if level == Unspecified ||
level == Critical ||
level == High ||
level == Medium ||
level == Low ||
level == Informational {
return true
}
return false
}
// SeverityLevelFromString returns a SeverityLevel from a string. It returns an error
// if the string is invalid.
func SeverityLevelFromString(str string) (SeverityLevel, error) {
option, ok := SeverityLevel_value[str]
if !ok {
return Unspecified, fmt.Errorf("'%s' is not a valid SeverityLevel option", str)
}
return SeverityLevel(option), nil
}
// NormalizeSeverityLevel - normalize user specified severity level
func NormalizeSeverityLevel(level string) string {
upperLevel := strings.ToUpper(level)
switch upperLevel {
case "UNSPECIFIED":
return Unspecified.String()
case "CRITICAL":
return Critical.String()
case "HIGH":
return High.String()
case "MEDIUM":
return Medium.String()
case "LOW":
return Low.String()
case "INFORMATIONAL":
return Informational.String()
default:
return level
}
}