-
Notifications
You must be signed in to change notification settings - Fork 439
/
Copy pathdao_node_run_vulnerability.go
270 lines (240 loc) · 9.62 KB
/
dao_node_run_vulnerability.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package workflow
import (
"context"
"database/sql"
"fmt"
"github.com/go-gorp/gorp"
"github.com/rockbears/log"
"github.com/ovh/cds/engine/api/application"
"github.com/ovh/cds/engine/api/database/gorpmapping"
"github.com/ovh/cds/engine/api/metrics"
"github.com/ovh/cds/engine/api/repositoriesmanager"
"github.com/ovh/cds/engine/cache"
"github.com/ovh/cds/engine/gorpmapper"
"github.com/ovh/cds/sdk"
)
// SaveVulnerabilityReport calculate vulnerability trend and save report.
func SaveVulnerabilityReport(ctx context.Context, db gorpmapper.SqlExecutorWithTx, cache cache.Store, proj sdk.Project, nr *sdk.WorkflowNodeRun, workerReport sdk.VulnerabilityWorkerReport) error {
var defaultBranch string
// Get default branch
if nr.VCSServer != "" {
// Get vcs info to known if we are on the default branch or not
client, err := repositoriesmanager.AuthorizedClient(ctx, db, cache, proj.Key, nr.VCSServer)
if err != nil {
return sdk.NewErrorWithStack(err, sdk.WrapError(sdk.ErrNoReposManagerClientAuth, "cannot get repo client %s", nr.VCSServer))
}
b, err := repositoriesmanager.DefaultBranch(ctx, client, nr.VCSRepository)
if err != nil {
return sdk.WrapError(err, "unable to get default branch")
}
defaultBranch = b.DisplayID
}
// Get node run report if exists
nodeRunReport, err := loadVulnerabilityReport(db, nr.ID)
if err != nil && !sdk.ErrorIs(err, sdk.ErrNotFound) {
return sdk.WrapError(err, "unable to load vulnerability report")
}
if nodeRunReport == nil {
nodeRunReport = &sdk.WorkflowNodeRunVulnerabilityReport{
WorkflowID: nr.WorkflowID,
ApplicationID: nr.ApplicationID,
Branch: nr.VCSBranch,
Num: nr.Number,
WorkflowNodeRunID: nr.ID,
WorkflowRunID: nr.WorkflowRunID,
Report: sdk.WorkflowNodeRunVulnerability{
Vulnerabilities: workerReport.Vulnerabilities,
},
}
// Get summary from previous run
previousRunReport, err := loadPreviousRunVulnerabilityReport(db, nr)
if err != nil && !sdk.ErrorIs(err, sdk.ErrNotFound) {
return sdk.WrapError(err, "unable to get previous vulnerability report")
}
nodeRunReport.Report.PreviousRunSummary = previousRunReport
// Get summary from default branch
if defaultBranch != "" && defaultBranch != nr.VCSBranch {
defaultBranchReport, err := loadLatestRunVulnerabilityReport(db, nr, defaultBranch)
if err != nil && !sdk.ErrorIs(err, sdk.ErrNotFound) {
return sdk.WrapError(err, "unable to get default branch vulnerability report")
}
nodeRunReport.Report.DefaultBranchSummary = defaultBranchReport
}
}
// Append new vulnerabilities to report
newVulnerabilities := make([]sdk.Vulnerability, 0, len(workerReport.Vulnerabilities))
for _, v := range workerReport.Vulnerabilities {
v.Type = workerReport.Type
newVulnerabilities = append(newVulnerabilities, v)
}
// Remove existing vulnerabilities for same type in node report
filteredVulnerabilities := make([]sdk.Vulnerability, 0, len(nodeRunReport.Report.Vulnerabilities)+len(newVulnerabilities))
removedVulnerabilities := make([]sdk.Vulnerability, 0, len(nodeRunReport.Report.Vulnerabilities))
for _, v := range nodeRunReport.Report.Vulnerabilities {
if v.Type != workerReport.Type {
filteredVulnerabilities = append(filteredVulnerabilities, v)
} else {
removedVulnerabilities = append(removedVulnerabilities, v)
}
}
nodeRunReport.Report.Vulnerabilities = append(filteredVulnerabilities, newVulnerabilities...)
// Init or update summary counter
if nodeRunReport.Report.Summary == nil {
nodeRunReport.Report.Summary = make(map[string]int64)
}
for i := range removedVulnerabilities {
if _, ok := nodeRunReport.Report.Summary[removedVulnerabilities[i].Severity]; ok {
nodeRunReport.Report.Summary[removedVulnerabilities[i].Severity]--
}
}
for i := range newVulnerabilities {
if _, ok := nodeRunReport.Report.Summary[newVulnerabilities[i].Severity]; !ok {
nodeRunReport.Report.Summary[newVulnerabilities[i].Severity] = 0
}
nodeRunReport.Report.Summary[newVulnerabilities[i].Severity]++
}
// Load existing vulnerabilities for application to check if some are ignored
appVulnerabilities, err := application.LoadVulnerabilities(db, nr.ApplicationID)
if err != nil {
return sdk.WrapError(err, "unable to load vulnerabilities for application with id %d", nr.ApplicationID)
}
key := func(v sdk.Vulnerability) string {
return fmt.Sprintf("%s-%s-%s-%s", v.Type, v.Component, v.Version, v.CVE)
}
// Create a map of all ignored vulnerabilities
mIgnored := make(map[string]struct{})
for i := range appVulnerabilities {
if appVulnerabilities[i].Ignored {
mIgnored[key(appVulnerabilities[i])] = struct{}{}
}
}
// For all report's vulnerabilities set ignored flag if true on application
for _, v := range nodeRunReport.Report.Vulnerabilities {
if _, ok := mIgnored[key(v)]; ok {
v.Ignored = true
}
}
if nodeRunReport.ID == 0 {
if err := InsertVulnerabilityReport(db, nodeRunReport); err != nil {
return sdk.WrapError(err, "unable to save vulnerability report")
}
} else {
if err := UpdateVulnerabilityReport(db, nodeRunReport); err != nil {
return sdk.WrapError(err, "unable to save vulnerability report")
}
}
// If we are on default branch, save report on application
if defaultBranch != "" && defaultBranch == nr.VCSBranch {
if err := application.DeleteVulnerabilitiesByApplicationIDAndType(db, nr.ApplicationID, workerReport.Type); err != nil {
return err
}
if err := application.InsertVulnerabilities(db, newVulnerabilities, nr.ApplicationID); err != nil {
return err
}
// push metrics
vulnsDBSummary, errS := application.LoadVulnerabilitiesSummary(db, nr.ApplicationID)
if errS != nil {
log.Error(ctx, "SaveVulnerabilityReport> Unable to get summary to create metrics: %s", err)
}
if vulnsDBSummary != nil && errS == nil {
metrics.PushVulnerabilities(proj.Key, nr.ApplicationID, nr.WorkflowID, nr.Number, vulnsDBSummary)
}
}
return nil
}
func loadPreviousRunVulnerabilityReport(db gorp.SqlExecutor, nr *sdk.WorkflowNodeRun) (map[string]int64, error) {
var dbReport dbNodeRunVulenrabilitiesReport
query := `
SELECT * FROM workflow_node_run_vulnerability
WHERE application_id = $1 AND workflow_id = $2 AND branch = $3 AND workflow_number < $4
ORDER BY workflow_number DESC
LIMIT 1
`
if err := db.SelectOne(&dbReport, query, nr.ApplicationID, nr.WorkflowID, nr.VCSBranch, nr.Number); err != nil {
if err == sql.ErrNoRows {
return nil, sdk.WithStack(sdk.ErrNotFound)
}
return nil, sdk.WrapError(err, "unable to load previous report")
}
return dbReport.Report.Summary, nil
}
func loadLatestRunVulnerabilityReport(db gorp.SqlExecutor, nr *sdk.WorkflowNodeRun, branch string) (map[string]int64, error) {
var dbReport dbNodeRunVulenrabilitiesReport
query := `
SELECT * FROM workflow_node_run_vulnerability
WHERE application_id = $1 AND workflow_id = $2 AND branch = $3
ORDER BY workflow_number DESC, workflow_node_run_id DESC
LIMIT 1
`
if err := db.SelectOne(&dbReport, query, nr.ApplicationID, nr.WorkflowID, branch); err != nil {
if err == sql.ErrNoRows {
return nil, sdk.WithStack(sdk.ErrNotFound)
}
return nil, sdk.WrapError(err, "Unable to load latest report")
}
return dbReport.Report.Summary, nil
}
// InsertVulnerabilityReport in database.
func InsertVulnerabilityReport(db gorp.SqlExecutor, report *sdk.WorkflowNodeRunVulnerabilityReport) error {
dbReport := dbNodeRunVulenrabilitiesReport(*report)
if err := db.Insert(&dbReport); err != nil {
return sdk.WrapError(err, "unable to insert report")
}
*report = sdk.WorkflowNodeRunVulnerabilityReport(dbReport)
return nil
}
// UpdateVulnerabilityReport in database.
func UpdateVulnerabilityReport(db gorp.SqlExecutor, report *sdk.WorkflowNodeRunVulnerabilityReport) error {
dbReport := dbNodeRunVulenrabilitiesReport(*report)
if _, err := db.Update(&dbReport); err != nil {
return sdk.WrapError(err, "upable to update report with id %d", report.ID)
}
*report = sdk.WorkflowNodeRunVulnerabilityReport(dbReport)
return nil
}
// PostGet is a db hook
func (d *dbNodeRunVulenrabilitiesReport) PostGet(db gorp.SqlExecutor) error {
var reportS sql.NullString
query := "SELECT report from workflow_node_run_vulnerability WHERE id = $1"
if err := db.QueryRow(query, d.ID).Scan(&reportS); err != nil {
return sdk.WrapError(err, "Unable to report")
}
var report sdk.WorkflowNodeRunVulnerability
if err := gorpmapping.JSONNullString(reportS, &report); err != nil {
return sdk.WrapError(err, "Unable to unmarshal report")
}
d.Report = report
return nil
}
// PostInsert is a db hook
func (d *dbNodeRunVulenrabilitiesReport) PostInsert(db gorp.SqlExecutor) error {
report, err := gorpmapping.JSONToNullString(d.Report)
if err != nil {
return sdk.WrapError(err, "unable to marshal report")
}
query := "UPDATE workflow_node_run_vulnerability set report=$1 WHERE id=$2"
if _, err := db.Exec(query, report, d.ID); err != nil {
return sdk.WrapError(err, "unable to insert report")
}
return nil
}
func (d *dbNodeRunVulenrabilitiesReport) PostUpdate(db gorp.SqlExecutor) error {
return d.PostInsert(db)
}
func loadVulnerabilityReport(db gorp.SqlExecutor, nodeRunID int64) (*sdk.WorkflowNodeRunVulnerabilityReport, error) {
var dbReport dbNodeRunVulenrabilitiesReport
query := `
SELECT * FROM workflow_node_run_vulnerability
WHERE workflow_node_run_id = $1
ORDER BY id DESC
LIMIT 1
`
if err := db.SelectOne(&dbReport, query, nodeRunID); err != nil {
if err == sql.ErrNoRows {
return nil, sdk.WithStack(sdk.ErrNotFound)
}
return nil, sdk.WrapError(err, "unable to load vulnerability report for node run %d", nodeRunID)
}
report := sdk.WorkflowNodeRunVulnerabilityReport(dbReport)
return &report, nil
}