-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
527 lines (438 loc) · 12.3 KB
/
client.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
package go_opencve
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
)
type CVE struct {
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
CveID string `json:"cve_id"`
Title string `json:"title,omitempty"`
Description string `json:"description"`
Metrics Metrics `json:"metrics"`
Weaknesses []string `json:"weaknesses"`
Vendors []string `json:"vendors"`
}
type Metrics struct {
Kev ProviderData `json:"kev"`
Ssvc ProviderData `json:"ssvc"`
CvssV2_0 ProviderData `json:"cvssV2_0"`
CvssV3_0 ProviderData `json:"cvssV3_0"`
CvssV3_1 ProviderData `json:"cvssV3_1"`
CvssV4_0 ProviderData `json:"cvssV4_0"`
ThreatSeverity ProviderData `json:"threat_severity"`
}
type ProviderData struct {
Data interface{} `json:"data"`
Provider *string `json:"provider"`
}
type Organization struct {
ID string `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Name string `json:"name"`
}
type OrganizationListResponse struct {
Count int `json:"count"`
Next *string `json:"next"`
Previous *string `json:"previous"`
Results []Organization `json:"results"`
}
type ProjectListResponse struct {
Count int `json:"count"`
Next *string `json:"next"`
Previous *string `json:"previous"`
Results []Project `json:"results"`
}
type Project struct {
ID string `json:"id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Name string `json:"name"`
Description string `json:"description"`
Subscriptions struct {
Vendors []string `json:"vendors"`
Products interface{} `json:"products"`
} `json:"subscriptions"`
}
type Client struct {
httpClient *http.Client
baseURL *url.URL
username string
password string
}
func NewClient(baseURL, username, password string) (*Client, error) {
parsedBaseURL, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
return &Client{
httpClient: http.DefaultClient,
baseURL: parsedBaseURL,
username: username,
password: password,
}, nil
}
func (c *Client) newRequest(ctx context.Context, method, path string, body interface{}) (*http.Request, error) {
u, err := c.baseURL.Parse(path)
if err != nil {
return nil, err
}
var buf bytes.Buffer
if body != nil {
err := json.NewEncoder(&buf).Encode(body)
if err != nil {
return nil, err
}
}
req, err := http.NewRequestWithContext(ctx, method, u.String(), &buf)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/json")
auth := c.username + ":" + c.password
encodedAuth := base64.StdEncoding.EncodeToString([]byte(auth))
req.Header.Set("Authorization", "Basic "+encodedAuth)
return req, nil
}
func (c *Client) do(req *http.Request, v interface{}) (*http.Response, error) {
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return resp, err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return resp, fmt.Errorf("request failed with status code %d: %s", resp.StatusCode, bodyBytes)
}
if v != nil {
err := json.Unmarshal(bodyBytes, v)
if err != nil {
return resp, fmt.Errorf("failed to unmarshal JSON: %w, response body: %s", err, bodyBytes)
}
}
return resp, nil
}
type CVEListResponse struct {
Count int `json:"count"`
Next *string `json:"next"`
Previous *string `json:"previous"`
Results []CVE `json:"results"`
}
func (c *Client) ListCVEs(ctx context.Context, params map[string]string) (*CVEListResponse, error) {
queryParams := url.Values{}
for k, v := range params {
queryParams.Add(k, v)
}
path := "/api/cve" + "?" + queryParams.Encode()
req, err := c.newRequest(ctx, "GET", path, nil)
if err != nil {
return nil, err
}
var response CVEListResponse
_, err = c.do(req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
func (c *Client) GetCVE(ctx context.Context, cveID string) (*CVE, error) {
path := fmt.Sprintf("/api/cve/%s", cveID)
req, err := c.newRequest(ctx, "GET", path, nil)
if err != nil {
return nil, err
}
var cve CVE
_, err = c.do(req, &cve)
if err != nil {
return nil, err
}
return &cve, nil
}
func (c *Client) ListOrganizations(ctx context.Context, params map[string]string) (*OrganizationListResponse, error) {
queryParams := url.Values{}
for k, v := range params {
queryParams.Add(k, v)
}
path := "/api/organizations" + "?" + queryParams.Encode()
req, err := c.newRequest(ctx, "GET", path, nil)
if err != nil {
return nil, err
}
var response OrganizationListResponse
_, err = c.do(req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
func (c *Client) GetOrganization(ctx context.Context, orgName string) (*Organization, error) {
path := fmt.Sprintf("/api/organizations/%s", orgName)
req, err := c.newRequest(ctx, "GET", path, nil)
if err != nil {
return nil, err
}
var org Organization
_, err = c.do(req, &org)
if err != nil {
return nil, err
}
return &org, nil
}
func (c *Client) ListProjects(ctx context.Context, orgName string, params map[string]string) (*ProjectListResponse, error) {
queryParams := url.Values{}
for k, v := range params {
queryParams.Add(k, v)
}
path := fmt.Sprintf("/api/organizations/%s/projects?%s", orgName, queryParams.Encode())
req, err := c.newRequest(ctx, "GET", path, nil)
if err != nil {
return nil, err
}
var response ProjectListResponse
_, err = c.do(req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
func (c *Client) GetProject(ctx context.Context, orgName, projectName string) (*Project, error) {
path := fmt.Sprintf("/api/organizations/%s/projects/%s", orgName, projectName)
req, err := c.newRequest(ctx, "GET", path, nil)
if err != nil {
return nil, err
}
var project Project
_, err = c.do(req, &project)
if err != nil {
return nil, err
}
return &project, nil
}
type ProjectCVEListResponse struct {
Count int `json:"count"`
Next *string `json:"next"`
Previous *string `json:"previous"`
Results []CVE `json:"results"`
}
func (c *Client) ListProjectCVEs(ctx context.Context, orgName, projectName string, params map[string]string) (*ProjectCVEListResponse, error) {
queryParams := url.Values{}
for k, v := range params {
queryParams.Add(k, v)
}
path := fmt.Sprintf("/api/organizations/%s/projects/%s/cve?%s", orgName, projectName, queryParams.Encode())
req, err := c.newRequest(ctx, "GET", path, nil)
if err != nil {
return nil, err
}
var response ProjectCVEListResponse
_, err = c.do(req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
type Product struct {
ID string `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Name string `json:"name"`
}
type ProductListResponse struct {
Count int `json:"count"`
Next *string `json:"next"`
Previous *string `json:"previous"`
Results []Product `json:"results"`
}
type ProductCVEListResponse struct {
Count int `json:"count"`
Next *string `json:"next"`
Previous *string `json:"previous"`
Results []CVE `json:"results"`
}
type Vendor struct {
ID string `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Name string `json:"name"`
}
type VendorListResponse struct {
Count int `json:"count"`
Next *string `json:"next"`
Previous *string `json:"previous"`
Results []Vendor `json:"results"`
}
type VendorCVEListResponse struct {
Count int `json:"count"`
Next *string `json:"next"`
Previous *string `json:"previous"`
Results []CVE `json:"results"`
}
type Weakness struct {
CWEID string `json:"cwe_id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type WeaknessListResponse struct {
Count int `json:"count"`
Next *string `json:"next"`
Previous *string `json:"previous"`
Results []Weakness `json:"results"`
}
type WeaknessCVEListResponse struct {
Count int `json:"count"`
Next *string `json:"next"`
Previous *string `json:"previous"`
Results []CVE `json:"results"`
}
func (c *Client) ListProducts(ctx context.Context, vendorName string, params map[string]string) (*ProductListResponse, error) {
queryParams := url.Values{}
for k, v := range params {
queryParams.Add(k, v)
}
path := fmt.Sprintf("/api/vendors/%s/products?%s", vendorName, queryParams.Encode())
req, err := c.newRequest(ctx, "GET", path, nil)
if err != nil {
return nil, err
}
var response ProductListResponse
_, err = c.do(req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
func (c *Client) GetProduct(ctx context.Context, vendorName, productName string) (*Product, error) {
path := fmt.Sprintf("/api/vendors/%s/products/%s", vendorName, productName)
req, err := c.newRequest(ctx, "GET", path, nil)
if err != nil {
return nil, err
}
var product Product
_, err = c.do(req, &product)
if err != nil {
return nil, err
}
return &product, nil
}
func (c *Client) ListProductCVEs(ctx context.Context, vendorName, productName string, params map[string]string) (*ProductCVEListResponse, error) {
queryParams := url.Values{}
for k, v := range params {
queryParams.Add(k, v)
}
path := fmt.Sprintf("/api/vendors/%s/products/%s/cve?%s", vendorName, productName, queryParams.Encode())
req, err := c.newRequest(ctx, "GET", path, nil)
if err != nil {
return nil, err
}
var response ProductCVEListResponse
_, err = c.do(req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
func (c *Client) ListVendors(ctx context.Context, params map[string]string) (*VendorListResponse, error) {
queryParams := url.Values{}
for k, v := range params {
queryParams.Add(k, v)
}
path := "/api/vendors" + "?" + queryParams.Encode()
req, err := c.newRequest(ctx, "GET", path, nil)
if err != nil {
return nil, err
}
var response VendorListResponse
_, err = c.do(req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
func (c *Client) GetVendor(ctx context.Context, vendorName string) (*Vendor, error) {
path := fmt.Sprintf("/api/vendors/%s", vendorName)
req, err := c.newRequest(ctx, "GET", path, nil)
if err != nil {
return nil, err
}
var vendor Vendor
_, err = c.do(req, &vendor)
if err != nil {
return nil, err
}
return &vendor, nil
}
func (c *Client) ListVendorCVEs(ctx context.Context, vendorName string, params map[string]string) (*VendorCVEListResponse, error) {
queryParams := url.Values{}
for k, v := range params {
queryParams.Add(k, v)
}
path := fmt.Sprintf("/api/vendors/%s/cve?%s", vendorName, queryParams.Encode())
req, err := c.newRequest(ctx, "GET", path, nil)
if err != nil {
return nil, err
}
var response VendorCVEListResponse
_, err = c.do(req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
func (c *Client) ListWeaknesses(ctx context.Context, params map[string]string) (*WeaknessListResponse, error) {
queryParams := url.Values{}
for k, v := range params {
queryParams.Add(k, v)
}
path := "/api/weaknesses" + "?" + queryParams.Encode()
req, err := c.newRequest(ctx, "GET", path, nil)
if err != nil {
return nil, err
}
var response WeaknessListResponse
_, err = c.do(req, &response)
if err != nil {
return nil, err
}
return &response, nil
}
func (c *Client) GetWeakness(ctx context.Context, weaknessID string) (*Weakness, error) {
path := fmt.Sprintf("/api/weaknesses/%s", weaknessID)
req, err := c.newRequest(ctx, "GET", path, nil)
if err != nil {
return nil, err
}
var weakness Weakness
_, err = c.do(req, &weakness)
if err != nil {
return nil, err
}
return &weakness, nil
}
func (c *Client) ListWeaknessCVEs(ctx context.Context, weaknessID string, params map[string]string) (*WeaknessCVEListResponse, error) {
queryParams := url.Values{}
for k, v := range params {
queryParams.Add(k, v)
}
path := fmt.Sprintf("/api/weaknesses/%s/cve?%s", weaknessID, queryParams.Encode())
req, err := c.newRequest(ctx, "GET", path, nil)
if err != nil {
return nil, err
}
var response WeaknessCVEListResponse
_, err = c.do(req, &response)
if err != nil {
return nil, err
}
return &response, nil
}