-
Notifications
You must be signed in to change notification settings - Fork 16
/
cpe.go
121 lines (109 loc) · 3.21 KB
/
cpe.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
package nvd
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
)
// VendorsProducts parse CPEs and returns slice of Vendors containing Products
func (cve *CVEItem) VendorsProducts() []Vendor {
// Get all Configuration.Nodes -> CPEMatch -> CPE23URI
var cpeURIs []string
for _, node := range cve.Configurations.Nodes {
for _, cpe := range node.CPEMatch {
cpeURIs = append(cpeURIs, cpe.CPE23URI)
}
}
return generateVendorsProducts(cpeURIs)
}
// generateVendorProducts takes a slice of CPE strings and returns a slice of Vendors containing Products
func generateVendorsProducts(cpeURIs []string) []Vendor {
// Build a staging map of vendors to urishorts,
// where a map appends urishorts to unique vendors
// 1. Loop each CPE23URI
// 2. Split CPE23URI into vendor and urishort
// 3. Append urishort to its vendor key
tmp := make(map[string][]string) // temporary staging map
for _, uri := range cpeURIs {
vendor, urishort := splitCPE(uri)
// If vendor key doesn't exist, initialize new slice wit urishort
tmpURIs, exists := tmp[vendor]
if !exists {
tmp[vendor] = []string{urishort}
continue
}
// Append urishort if not already exists in vendor key
seen := false
for _, tmpURI := range tmpURIs {
if tmpURI == urishort {
seen = true
break
}
}
if !seen {
tmp[vendor] = append(tmp[vendor], urishort)
}
}
// Convert staging map to Vendors slice with each Vendor containing Products slice
var vendors []Vendor
for tmpVendor, tmpURIs := range tmp {
// Build Products slice
var products []Product
for _, u := range tmpURIs {
products = append(products, urishortToProduct(u))
}
// Build Vendor
vendors = append(vendors, Vendor{
// Name: strings.Title(strings.Join(strings.Split(tmpVendor, "-"), " ")),
// TODO splitmulti on vendor name with -_ separators
Name: strings.Title(tmpVendor),
Products: products,
})
}
return vendors
}
func urishortToProduct(urishort string) Product {
splitMulti := func(s string, seps string) []string {
splitter := func(r rune) bool {
return strings.ContainsRune(seps, r)
}
return strings.FieldsFunc(s, splitter)
}
productStr := strings.Split(urishort, ":")[1]
return Product{
Name: strings.Title(strings.Join(splitMulti(productStr, "-_"), " ")),
URIShort: urishort,
}
}
func splitCPE(cpe23URI string) (vendor, urishort string) {
split := strings.Split(cpe23URI, ":")
vendor = split[3]
urishort = strings.Join([]string{split[3], split[4]}, ":")
return
}
// FetchCPEMatches downloads cpematch feed from NVD (if not exist),
// and returns slice of cpe23Uri's
// Currently set to private method as test data only
func (c *Client) fetchCPEMatches() (CPEMatchFeed, error) {
p := path.Join(c.feedDir, "cpematch.json")
// TODO check if file exists
if _, err := os.Stat(p); os.IsNotExist(err) {
err := c.downloadFeed(nvdCPEMatchFeed, p)
if err != nil {
return CPEMatchFeed{}, err
}
}
raw, err := ioutil.ReadFile(p)
if err != nil {
return CPEMatchFeed{}, fmt.Errorf("error reading local feed file %s: %v", p, err)
}
var cpes CPEMatchFeed
err = json.Unmarshal(raw, &cpes)
if err != nil {
return CPEMatchFeed{}, errors.New("error unmarshaling cpe match feed")
}
return cpes, nil
}