forked from facebookincubator/nvdtools
/
export.go
249 lines (209 loc) · 5.24 KB
/
export.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
// Copyright (c) Facebook, Inc. and its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package vulndb
import (
"context"
"database/sql"
"encoding/csv"
"io"
"strconv"
"time"
"github.com/pkg/errors"
"github.com/facebookincubator/flog"
"github.com/Daviid-P/nvdtools/vulndb/debug"
"github.com/Daviid-P/nvdtools/vulndb/sqlutil"
)
// DataExporter is a helper for exporting vulnerability records from the db.
type DataExporter struct {
DB *sql.DB
FilterProviders []string
FilterCVEs []string
}
func (exp DataExporter) selectLatestVersion() *sqlutil.SelectStmt {
q := sqlutil.Select(
"latest.version",
).From().
SelectGroup("latest", latestVendorVersion())
if len(exp.FilterProviders) > 0 {
q = q.Where(
sqlutil.Cond().In("provider", exp.FilterProviders),
)
}
return q
}
// fields must be vendor.field_name or vendor_data.field_name
func (exp DataExporter) selectVendorData(fields ...string) *sqlutil.SelectStmt {
q := sqlutil.Select(
fields...,
).From(
"vendor_data",
).Literal(
"LEFT JOIN vendor ON vendor.version = vendor_data.version",
).Literal(
"LEFT JOIN custom_data ON custom_data.cve_id = vendor_data.cve_id",
)
cond := sqlutil.Cond().
InSelect("vendor.version", exp.selectLatestVersion()).
And().
IsNull("custom_data.cve_id")
if len(exp.FilterCVEs) > 0 {
cond = cond.And().In("vendor_data.cve_id", exp.FilterCVEs)
}
q = q.Where(cond)
return q
}
func (exp DataExporter) selectOverrides(fields ...string) *sqlutil.SelectStmt {
q := sqlutil.Select(
fields...,
).From(
"custom_data",
)
var cond *sqlutil.QueryConditionSet
if len(exp.FilterProviders) > 0 {
cond = sqlutil.Cond().In("provider", exp.FilterProviders)
}
if len(exp.FilterCVEs) > 0 {
if cond == nil {
cond = sqlutil.Cond()
} else {
cond = cond.And()
}
cond = cond.In("cve_id", exp.FilterCVEs)
}
if cond != nil {
q = q.Where(cond)
}
return q
}
// CSV exports data to w.
func (exp DataExporter) CSV(ctx context.Context, w io.Writer, header bool) error {
q := sqlutil.Select(
"d.owner",
"d.provider",
"d.cve_id",
"d.published",
"d.modified",
"d.base_score",
"d.summary",
).From().SelectGroup(
"d",
exp.selectVendorData(
"vendor.owner",
"vendor.provider",
"vendor_data.cve_id",
"vendor_data.published",
"vendor_data.modified",
"vendor_data.base_score",
"vendor_data.summary",
).Literal("UNION ALL").
Select(exp.selectOverrides(
"custom_data.owner",
"custom_data.provider",
"custom_data.cve_id",
"custom_data.published",
"custom_data.modified",
"custom_data.base_score",
"custom_data.summary",
)),
)
query, args := q.String(), q.QueryArgs()
if debug.V(1) {
flog.Infof("running: %q / %#v", query, args)
}
rows, err := exp.DB.QueryContext(ctx, query, args...)
if err != nil {
return errors.Wrap(err, "cannot query export data")
}
defer rows.Close()
record := struct {
Owner string `sql:"owner"`
Provider string `sql:"provider"`
CVE string `sql:"cve_id"`
Published time.Time `sql:"published"`
Modified time.Time `sql:"modified"`
BaseScore float64 `sql:"base_score"`
Summary string `sql:"summary"`
}{}
cw := csv.NewWriter(w)
defer cw.Flush()
if header {
fields := sqlutil.NewRecordType(record).Fields()
cw.Write(fields)
}
for rows.Next() {
v := record
err = rows.Scan(sqlutil.NewRecordType(&v).Values()...)
if err != nil {
return errors.Wrap(err, "cannot scan export data")
}
cw.Write([]string{
v.Owner,
v.Provider,
v.CVE,
v.Published.Format(TimeLayout),
v.Modified.Format(TimeLayout),
strconv.FormatFloat(v.BaseScore, 'f', 3, 64),
v.Summary,
})
}
return nil
}
// JSON exports NVD CVE JSON to w.
func (exp DataExporter) JSON(ctx context.Context, w io.Writer, indent string) error {
q := sqlutil.Select(
"d.cve_id",
"d.cve_json",
).From().SelectGroup(
"d",
exp.selectVendorData(
"vendor_data.cve_id AS cve_id",
"vendor_data.cve_json AS cve_json",
).
Literal("UNION ALL").
Select(exp.selectOverrides(
"custom_data.cve_id",
"custom_data.cve_json",
)),
)
query, args := q.String(), q.QueryArgs()
if debug.V(1) {
flog.Infof("running: %q / %#v", query, args)
}
rows, err := exp.DB.QueryContext(ctx, query, args...)
if err != nil {
return errors.Wrap(err, "cannot query export data")
}
defer rows.Close()
record := struct {
CVE string
JSON []byte
}{}
f := &cveFile{}
for rows.Next() {
v := record
err = rows.Scan(sqlutil.NewRecordType(&v).Values()...)
if err != nil {
return errors.Wrap(err, "cannot scan export data")
}
err = f.Add(v.CVE, v.JSON)
if err != nil {
return err
}
}
if indent == "" {
return f.EncodeJSON(w)
}
const prefix = ""
return f.EncodeIndentedJSON(w, prefix, indent)
}