forked from aquasecurity/trivy-db
/
amazon.go
188 lines (162 loc) · 4.79 KB
/
amazon.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
package amazon
import (
"encoding/json"
"fmt"
"io"
"log"
"path/filepath"
"strings"
bolt "go.etcd.io/bbolt"
"golang.org/x/xerrors"
"github.com/alt-cloud/trivy-db/pkg/db"
"github.com/alt-cloud/trivy-db/pkg/types"
"github.com/alt-cloud/trivy-db/pkg/utils"
ustrings "github.com/alt-cloud/trivy-db/pkg/utils/strings"
"github.com/alt-cloud/trivy-db/pkg/vulnsrc/vulnerability"
)
const (
amazonDir = "amazon"
platformFormat = "amazon linux %s"
)
var (
targetVersions = []string{"1", "2", "2022", "2023"}
source = types.DataSource{
ID: vulnerability.Amazon,
Name: "Amazon Linux Security Center",
URL: "https://alas.aws.amazon.com/",
}
)
type VulnSrc struct {
dbc db.Operation
advisories map[string][]ALAS
}
// ALAS has detailed data of ALAS
type ALAS struct {
ID string `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Severity string `json:"severity,omitempty"`
Description string `json:"description,omitempty"`
Packages []Package `json:"packages,omitempty"`
References []Reference `json:"references,omitempty"`
CveIDs []string `json:"cveids,omitempty"`
}
// Package has affected package information
type Package struct {
Name string `json:"name,omitempty"`
Epoch string `json:"epoch,omitempty"`
Version string `json:"version,omitempty"`
Release string `json:"release,omitempty"`
Arch string `json:"arch,omitempty"`
}
// Reference has reference information
type Reference struct {
Href string `json:"href,omitempty"`
}
func NewVulnSrc() VulnSrc {
return VulnSrc{
dbc: db.Config{},
advisories: map[string][]ALAS{},
}
}
func (vs VulnSrc) Name() types.SourceID {
return source.ID
}
func (vs VulnSrc) Update(dir string) error {
rootDir := filepath.Join(dir, "vuln-list", amazonDir)
err := utils.FileWalk(rootDir, vs.walkFunc)
if err != nil {
return xerrors.Errorf("error in Amazon walk: %w", err)
}
if err = vs.save(); err != nil {
return xerrors.Errorf("error in Amazon save: %w", err)
}
return nil
}
func (vs *VulnSrc) walkFunc(r io.Reader, path string) error {
paths := strings.Split(path, string(filepath.Separator))
if len(paths) < 2 {
return nil
}
version := paths[len(paths)-2]
if !ustrings.InSlice(version, targetVersions) {
log.Printf("unsupported Amazon version: %s\n", version)
return nil
}
var alas ALAS
if err := json.NewDecoder(r).Decode(&alas); err != nil {
return xerrors.Errorf("failed to decode Amazon JSON: %w", err)
}
vs.advisories[version] = append(vs.advisories[version], alas)
return nil
}
func (vs VulnSrc) save() error {
log.Println("Saving Amazon DB")
err := vs.dbc.BatchUpdate(func(tx *bolt.Tx) error {
return vs.commit(tx)
})
if err != nil {
return xerrors.Errorf("error in batch update: %w", err)
}
return nil
}
func (vs VulnSrc) commit(tx *bolt.Tx) error {
for majorVersion, alasList := range vs.advisories {
platformName := fmt.Sprintf(platformFormat, majorVersion)
if err := vs.dbc.PutDataSource(tx, platformName, source); err != nil {
return xerrors.Errorf("failed to put data source: %w", err)
}
for _, alas := range alasList {
for _, cveID := range alas.CveIDs {
for _, pkg := range alas.Packages {
advisory := types.Advisory{
FixedVersion: utils.ConstructVersion(pkg.Epoch, pkg.Version, pkg.Release),
}
if err := vs.dbc.PutAdvisoryDetail(tx, cveID, pkg.Name, []string{platformName}, advisory); err != nil {
return xerrors.Errorf("failed to save Amazon advisory: %w", err)
}
var references []string
for _, ref := range alas.References {
references = append(references, ref.Href)
}
vuln := types.VulnerabilityDetail{
Severity: severityFromPriority(alas.Severity),
References: references,
Description: alas.Description,
Title: "",
}
if err := vs.dbc.PutVulnerabilityDetail(tx, cveID, source.ID, vuln); err != nil {
return xerrors.Errorf("failed to save Amazon vulnerability detail: %w", err)
}
// for optimization
if err := vs.dbc.PutVulnerabilityID(tx, cveID); err != nil {
return xerrors.Errorf("failed to save the vulnerability ID: %w", err)
}
}
}
}
}
return nil
}
// Get returns a security advisory
func (vs VulnSrc) Get(version string, pkgName string) ([]types.Advisory, error) {
bucket := fmt.Sprintf(platformFormat, version)
advisories, err := vs.dbc.GetAdvisories(bucket, pkgName)
if err != nil {
return nil, xerrors.Errorf("failed to get Amazon advisories: %w", err)
}
return advisories, nil
}
func severityFromPriority(priority string) types.Severity {
switch priority {
case "low":
return types.SeverityLow
case "medium":
return types.SeverityMedium
case "important":
return types.SeverityHigh
case "critical":
return types.SeverityCritical
default:
return types.SeverityUnknown
}
}