forked from bearded-web/bearded
/
entity.go
165 lines (147 loc) · 4.14 KB
/
entity.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
154
155
156
157
158
159
160
161
162
163
164
165
package issue
import (
"net/http"
"time"
"github.com/bearded-web/bearded/models/issue"
)
type StatusEntity struct {
Confirmed *bool `json:"confirmed,omitempty"`
False *bool `json:"false,omitempty"`
Muted *bool `json:"muted,omitempty"`
Resolved *bool `json:"resolved,omitempty"`
}
type HeaderMyEntity struct {
Key string `json:"key"`
Values []string `json:"values,omitempty"`
}
type CommentEntity struct {
Text string `json:"text" description:"raw markdown text"`
}
func TransformHeader(he []*HeaderMyEntity) http.Header {
h := http.Header{}
for _, heElem := range he {
for _, val := range heElem.Values {
h.Add(heElem.Key, val)
}
}
return h
}
type HttpMyEntity struct {
Status string `json:"status"`
// The problem with header is that map isn't supported in swagger api
// So we transform map[string][]string to []struct{key:string,values:[]string}
Header []*HeaderMyEntity `json:"header"`
Body *issue.HttpBody `json:"body,omitempty"`
}
func (he *HttpMyEntity) Transform() *issue.HttpEntity {
if he == nil {
return nil
}
dst := &issue.HttpEntity{
Status: he.Status,
Body: he.Body,
Header: TransformHeader(he.Header),
}
return dst
}
type HttpTransactionEntity struct {
Id int `json:"id,omitempty"`
Url string `json:"url,omitempty"`
Params []string `json:"params,omitempty"`
Method string `json:"method"`
Request *HttpMyEntity `json:"request,omitempty"`
Response *HttpMyEntity `json:"response,omitempty"`
}
type VectorEntity struct {
Url string `json:"url,omitempty" description:"where this issue is happened"`
HttpTransactions []*HttpTransactionEntity `json:"httpTransactions,omitempty" bson:"httpTransactions"`
}
func (v *VectorEntity) Transform() *issue.Vector {
if v == nil {
return nil
}
vec := &issue.Vector{
Url: v.Url,
}
for _, tr := range v.HttpTransactions {
httpTr := &issue.HttpTransaction{
Id: tr.Id,
Url: tr.Url,
Params: tr.Params,
Method: tr.Method,
Request: tr.Request.Transform(),
Response: tr.Response.Transform(),
}
vec.HttpTransactions = append(vec.HttpTransactions, httpTr)
}
return vec
}
type IssueEntity struct {
Summary *string `json:"summary,omitempty" creating:"nonzero,min=3,max=120"`
VulnType *int `json:"vulnType,omitempty" bson:"vulnType" description:"vulnerability type from vulndb"`
Severity *issue.Severity `json:"severity,omitempty" description:"one of [high medium low info]"`
References []*issue.Reference `json:"references,omitempty" bson:"references" description:"information about vulnerability"`
Desc *string `json:"desc,omitempty"`
Vector *VectorEntity `json:"vector,omitempty"`
}
type TargetIssueEntity struct {
Target string `json:"target,omitempty" creating:"nonzero,bsonId"`
StatusEntity `json:",inline"`
IssueEntity `json:",inline"`
}
func isValidSeverity(sev issue.Severity) bool {
if sev == issue.SeverityHigh ||
sev == issue.SeverityMedium ||
sev == issue.SeverityLow ||
sev == issue.SeverityInfo {
return true
}
return false
}
// Update all fields for dst with entity data if they present
// Return true if target should rebuild summary for issues
func updateTargetIssue(raw *TargetIssueEntity, dst *issue.TargetIssue) bool {
rebuildSummary := false
if raw.Summary != nil {
dst.Summary = *raw.Summary
}
if raw.Desc != nil {
dst.Desc = *raw.Desc
}
if raw.References != nil {
dst.References = raw.References
}
if raw.VulnType != nil {
dst.VulnType = *raw.VulnType
}
if raw.Vector != nil {
dst.Vector = raw.Vector.Transform()
}
if raw.Confirmed != nil {
dst.Confirmed = *raw.Confirmed
}
if raw.False != nil {
rebuildSummary = true
dst.False = *raw.False
}
if raw.Resolved != nil {
rebuildSummary = true
dst.Resolved = *raw.Resolved
if *raw.Resolved {
dst.ResolvedAt = time.Now()
} else {
dst.ResolvedAt = time.Time{}
}
}
if raw.Muted != nil {
rebuildSummary = true
dst.Muted = *raw.Muted
}
if raw.Severity != nil {
if isValidSeverity(*raw.Severity) {
rebuildSummary = true
dst.Severity = *raw.Severity
}
}
return rebuildSummary
}