-
Notifications
You must be signed in to change notification settings - Fork 571
/
vulndb.go
227 lines (185 loc) · 5.14 KB
/
vulndb.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
package threatintel
import (
"context"
"encoding/json"
"fmt"
"path"
"path/filepath"
"sort"
"time"
"github.com/deepfence/ThreatMapper/deepfence_utils/directory"
"github.com/deepfence/ThreatMapper/deepfence_utils/log"
"github.com/deepfence/ThreatMapper/deepfence_utils/telemetry"
"github.com/minio/minio-go/v7"
)
const (
Version3 = "3"
Version5 = "5"
)
var (
ListingJSON = "listing.json"
VulnerabilityDBStore = "vulnerability"
ListingPath = path.Join(VulnerabilityDBStore, ListingJSON)
)
type VulnerabilityDBListing struct {
Available map[string][]Database `json:"available"`
}
type Database struct {
Built time.Time `json:"built"`
Version int `json:"version"`
URL string `json:"url"`
Checksum string `json:"checksum"`
}
func NewVulnerabilityDBListing() *VulnerabilityDBListing {
return &VulnerabilityDBListing{
Available: map[string][]Database{
Version3: make([]Database, 0),
Version5: make([]Database, 0),
},
}
}
func LoadListing(d []byte) (*VulnerabilityDBListing, error) {
var v VulnerabilityDBListing
if err := json.Unmarshal(d, &v); err != nil {
return nil, err
}
return &v, nil
}
func (v *VulnerabilityDBListing) Bytes() ([]byte, error) {
return json.Marshal(v)
}
func (v *VulnerabilityDBListing) Set(dbs []Database, version string) {
v.Available[version] = dbs
}
func (v *VulnerabilityDBListing) Append(db Database, version string) {
exists := false
index := 0
for i, d := range v.Available[version] {
if d.URL == db.URL {
exists = true
index = i
}
}
if !exists {
v.Available[version] = append(v.Available[version], db)
} else {
v.Available[version][index] = db
}
}
func (v *VulnerabilityDBListing) Sort(version string) {
if len(v.Available[version]) <= 1 {
return
}
dbs := v.Available[version]
sort.Slice(dbs, func(i, j int) bool {
return dbs[i].Built.Before(dbs[j].Built)
})
v.Available[version] = dbs
}
func (v *VulnerabilityDBListing) Latest(version string) *Database {
// sort, get last element
v.Sort(version)
dbs, ok := v.Available[version]
if !ok {
return nil
}
if len(dbs) >= 1 {
return &dbs[len(dbs)-1]
}
return nil
}
func (v *VulnerabilityDBListing) LatestN(version string, num int) (latest []Database, oldest []Database) {
// sort
v.Sort(version)
dbs, ok := v.Available[version]
if !ok {
return latest, oldest
}
if len(dbs) <= num {
latest = dbs
} else {
latest = dbs[len(dbs)-num:]
}
if len(dbs) > num {
oldest = dbs[:len(dbs)-num]
}
return latest, oldest
}
func VulnDBUpdateListing(ctx context.Context, newFile, newFileCheckSum string, buildTime time.Time) error {
log.Info().Msg("update vulnerability database listing")
mc, err := directory.FileServerClient(directory.WithDatabaseContext(ctx))
if err != nil {
log.Error().Msg(err.Error())
return err
}
// if err ignore, listing file is missing
data, err := mc.DownloadFileContexts(ctx, ListingPath, minio.GetObjectOptions{})
if err != nil {
log.Error().Err(err).Msg("failed to load listing file might be missing")
}
listing, err := LoadListing(data)
if err != nil {
log.Warn().Msg("failed to load listing file create new listing")
listing = NewVulnerabilityDBListing()
}
// for aws s3
fileURL := fmt.Sprintf("%s://%s.s3.%s.amazonaws.com/%s",
directory.FileServerProtocol, directory.FileServerDatabaseBucket, directory.FileServerRegion, newFile)
if directory.FileServerHost != "s3.amazonaws.com" {
fileURL = fmt.Sprintf("%s://%s:%s/%s",
directory.FileServerProtocol, directory.FileServerHost, directory.FileServerPort,
path.Join(string(directory.DatabaseDirKey), newFile))
}
listing.Append(
Database{
Built: buildTime,
Version: 5,
URL: fileURL,
Checksum: newFileCheckSum,
},
Version5,
)
latest, oldest := listing.LatestN(Version5, 3)
// keep only latest 3 databases
listing.Set(latest, Version5)
// delete old database
for _, d := range oldest {
fname := path.Join(VulnerabilityDBStore, filepath.Base(d.URL))
log.Info().Msgf("remove old vuln db file %s", fname)
if err := mc.DeleteFile(ctx, fname, true, minio.RemoveObjectOptions{ForceDelete: true}); err != nil {
log.Error().Err(err).Msg("failed to remove old ")
}
}
lb, err := listing.Bytes()
if err != nil {
log.Error().Msgf(err.Error())
return err
}
_, err = mc.UploadFile(ctx, ListingPath, lb, true,
minio.PutObjectOptions{ContentType: "application/json"})
if err != nil {
log.Error().Msgf(err.Error())
return err
}
log.Info().Msgf("vulnerability db listing updated with file %s checksum %s",
newFile, newFileCheckSum)
return nil
}
func DownloadVulnerabilityDB(ctx context.Context, info Entry) error {
log.Info().Msg("download latest vulnerability database")
ctx, span := telemetry.NewSpan(ctx, "threatintel", "download-vulnerability-db")
defer span.End()
data, err := downloadFile(ctx, info.URL)
if err != nil {
log.Error().Msgf(err.Error())
return err
}
path, checksum, err := UploadToMinio(ctx, data.Bytes(), VulnerabilityDBStore,
fmt.Sprintf("vuln-db-%d.tar.gz", info.Built.Unix()))
if err != nil {
log.Error().Msg(err.Error())
return err
}
// update listing.json file
return VulnDBUpdateListing(ctx, path, checksum, info.Built)
}