-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert.go
168 lines (148 loc) · 4.32 KB
/
alert.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
package db
import (
"fmt"
"github.com/inconshreveable/log15"
"github.com/jinzhu/gorm"
"github.com/tomoyamachi/gocarts/models"
"gopkg.in/cheggaaa/pb.v1"
"time"
)
// Fecth alerts by CVE-ID
func (r *RDBDriver) GetAlertsByCveId(cveId string) (allAlerts []models.Alert, err error) {
// Fetch target id's reference data
refs := []models.Cve{}
if err = r.conn.Where("cve_id = ?", cveId).Find(&refs).Error; err != nil {
return nil, err
}
return returnRelatedAlertsByCVEs(r, refs)
}
// Fetch all alerts by team
func (r *RDBDriver) GetTargetTeamAlerts(team string) (allAlerts []models.Alert, err error) {
if err = r.conn.Where("team = ?", team).Find(&allAlerts).Error; err != nil {
return nil, err
}
return allAlerts, nil
}
func (r *RDBDriver) GetAllAlertsCveIdKeyByTeam(team string) (alertsMap map[string][]models.Alert, err error) {
refs := []models.Cve{}
// alert_id's first char is depend on TEAM. check util.CreateID
searchID := models.TEAM_PREFIX_ID[team] * models.TEAM_ID_DIGIT
if err = r.conn.Where("alert_id between ? and ?", searchID, searchID+models.TEAM_ID_DIGIT).Find(&refs).Error; err != nil {
return nil, err
}
if len(refs) == 0 {
return alertsMap, nil
}
// aggregate alertID by CVE-ID
cveAlertMap := map[string][]uint{}
for _, cve := range refs {
alertIDs, ok := cveAlertMap[cve.CveID]
if ok {
cveAlertMap[cve.CveID] = append(alertIDs, cve.AlertID)
} else {
cveAlertMap[cve.CveID] = []uint{cve.AlertID}
}
}
// get unique cve id
alertsMap = map[string][]models.Alert{}
for cveID, alertIDs := range cveAlertMap {
alerts := []models.Alert{}
if err = r.conn.Where("alert_id in (?)", alertIDs).Find(&alerts).Error; err != nil {
return nil, err
}
alertsMap[cveID] = alerts
}
return alertsMap, nil
}
// Fetch alerts by published date
func (r *RDBDriver) GetAfterTimeAlerts(after time.Time) (allAlerts []models.Alert, err error) {
all := []models.Alert{}
if err = r.conn.Where("publish_date >= ?", after.Format("2006-01-02")).Find(&all).Error; err != nil {
return nil, err
}
if allAlerts, err = appendRelatedCves(r, all); err != nil {
return nil, err
}
return allAlerts, nil
}
func (r *RDBDriver) InsertAlert(alerts []models.Alert) (err error) {
bar := pb.StartNew(len(alerts))
log15.Info(fmt.Sprintf("Insert %d alerts", len(alerts)))
for _, alert := range alerts {
if err := r.deleteAndInsertJpcert(r.conn, alert); err != nil {
return fmt.Errorf("Failed to insert. alert: %s, err: %s",
alert.AlertID, err)
}
bar.Increment()
}
bar.Finish()
return nil
}
func (r *RDBDriver) deleteAndInsertJpcert(conn *gorm.DB, alert models.Alert) (err error) {
tx := conn.Begin()
defer func() {
if err != nil {
tx.Rollback()
return
}
tx.Commit()
}()
// Delete old records if found
old := models.Alert{}
result := tx.Where("alert_id = ?", alert.AlertID).First(&old)
if !result.RecordNotFound() {
// Delete old records
var errs gorm.Errors
errs = errs.Add(
tx.Where("alert_id = ?", alert.AlertID).Delete(models.Cve{}).Error,
)
errs = errs.Add(tx.Unscoped().Delete(&old).Error)
// Delete nil in errs
var validErrs []error
for _, err := range errs {
if err != nil {
validErrs = append(validErrs, err)
}
}
errs = validErrs
if len(errs.GetErrors()) > 0 {
return fmt.Errorf("Failed to delete old records. id: %s, err: %s",
alert.AlertID, errs.Error())
}
}
if err = tx.Create(&alert).Error; err != nil {
return err
}
return nil
}
func appendRelatedCves(r *RDBDriver, alerts []models.Alert) (allAlerts []models.Alert, err error) {
for _, a := range alerts {
cves := []models.Cve{}
//r.conn.Model(&a).Related(&a.Cves) TODO : it didn't work.
if err = r.conn.Where("alert_id = ?", a.AlertID).Find(&cves).Error; err != nil {
return nil, err
}
a.Cves = cves
allAlerts = append(allAlerts, a)
}
return allAlerts, nil
}
func returnRelatedAlertsByCVEs(r *RDBDriver, refs []models.Cve) (allAlerts []models.Alert, err error) {
if len(refs) == 0 {
return allAlerts, nil
}
// aggregate alert ids
alertIds := []uint{}
for _, ref := range refs {
alertIds = append(alertIds, ref.AlertID)
}
// get alerts from alert id
all := []models.Alert{}
if err = r.conn.Where("alert_id in (?)", alertIds).Find(&all).Error; err != nil {
return nil, err
}
if allAlerts, err = appendRelatedCves(r, all); err != nil {
return nil, err
}
return allAlerts, nil
}