Skip to content

Commit ac7100f

Browse files
committed
refactor: misc changes to import
- skip statements that would contain neither commits nor affected artifact - invoke new backend endpoint - change order of fields in statements (notes brought towards the top)
1 parent 797ae1e commit ac7100f

File tree

5 files changed

+44
-19
lines changed

5 files changed

+44
-19
lines changed

kaybee/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.6.7
1+
0.6.8

kaybee/internal/model/statement.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ import (
1313
"path/filepath"
1414
"reflect"
1515
"regexp"
16+
"strings"
1617

1718
"github.com/google/uuid"
1819
"gopkg.in/yaml.v2"
19-
// "strings"
2020
)
2121

2222
// Statement represents a vulnerability statement
2323
type Statement struct {
2424
ID uuid.UUID `yaml:"-" json:"-"`
2525
VulnerabilityID string `yaml:"vulnerability_id" json:"vulnerability_id"`
26-
Aliases []Alias `yaml:"aliases" json:"aliases"`
27-
Fixes []Fix `yaml:"fixes" json:"-"`
28-
AffectedArtifacts []Artifact `yaml:"artifacts" json:"affected_artifacts"`
29-
Notes []Note `yaml:"notes" json:"notes"`
26+
Aliases []Alias `yaml:"-" json:"-"`
27+
Notes []Note `yaml:"notes,omitempty" json:"notes"`
28+
Fixes []Fix `yaml:"fixes,omitempty" json:"-"`
29+
AffectedArtifacts []Artifact `yaml:"artifacts,omitempty" json:"affected_artifacts"`
3030
Metadata Metadata `yaml:"-" json:"-"`
3131
}
3232

@@ -76,7 +76,7 @@ type Artifact struct {
7676
// A Note represents a description that accompanies a statement; it can have a
7777
// set of links and a free-text comment. Neither are mandatory.
7878
type Note struct {
79-
Links []string `json:"links"`
79+
Links []string `yaml:"links" json:"links"`
8080
Text string `json:"text"`
8181
hash string
8282
}
@@ -178,6 +178,13 @@ func (s *Statement) ToFile(path string) error {
178178
os.MkdirAll(targetDir, 0750)
179179
}
180180

181+
// strip slashes from the end of repository URLs
182+
for i := range s.Fixes {
183+
for j := range s.Fixes[i].Commits {
184+
s.Fixes[i].Commits[j].RepositoryURL = strings.TrimRight(s.Fixes[i].Commits[j].RepositoryURL, "/")
185+
}
186+
}
187+
181188
dest := filepath.Join(targetDir, "statement.yaml")
182189
// fmt.Print("\nSaving statement to file", dest)
183190
data, _ := yaml.Marshal(s)

kaybee/internal/tasks/data/default_config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
apiVersion: "v1"
22

3+
# List of sources from which statements are pulled
4+
# Ranks: smaller value means higher rank (higher priority)
35
sources:
46
- repo: https://github.com/sap/project-kb
57
branch: vulnerability-data

kaybee/internal/tasks/import.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (t *ImportTask) Execute() (success bool) {
9494

9595
fmt.Printf("Importing vulnerability data from %s (using %d workers)\n", t.backend, t.concurrency)
9696

97-
importers, err := NewImporterPool(t.backend, t.concurrency, t.limit, nil)
97+
importers, err := NewImporterPool(t.backend, t.concurrency, t.limit, nil, t.verbose)
9898
if err != nil {
9999
log.Fatalln("Could not create importers pool")
100100
}
@@ -125,11 +125,12 @@ type Importer struct {
125125
Client *http.Client
126126
Filter map[string][]*regexp.Regexp
127127
ProgressBar *progressbar.ProgressBar
128+
Verbose bool
128129
}
129130

130131
// NewImporterPool instantiates a pool of Exporters, each taking care of fetching vulnerability
131132
// data for a subset of the overall set of vulnerabilities stored in the Steady backend.
132-
func NewImporterPool(backend string, concurrent int, limit int, filter map[string][]*regexp.Regexp) (*ImporterPool, error) {
133+
func NewImporterPool(backend string, concurrent int, limit int, filter map[string][]*regexp.Regexp, verbose bool) (*ImporterPool, error) {
133134

134135
pool := &ImporterPool{}
135136
bugs, err := fetchVulnerabilityIDs(backend)
@@ -171,6 +172,7 @@ func NewImporterPool(backend string, concurrent int, limit int, filter map[strin
171172
Filter: filter,
172173
Statements: make(map[string]model.Statement),
173174
ProgressBar: bar,
175+
Verbose: verbose,
174176
})
175177
}
176178

@@ -220,7 +222,8 @@ func (f *Importer) Run() error {
220222

221223
// Fetch affected artifacts data
222224
var affectedLibs []SteadyAffectedLib
223-
resp2, err := f.Client.Get(f.Backend + BugsEndpoint + "/" + b.VulnerabilityID + "/affectedLibIds?onlyWellKnown=true")
225+
resp2, err := f.Client.Get(f.Backend + BugsEndpoint + "/" + b.VulnerabilityID + "/affectedLibIds?onlyWellKnown=true&resolved=true")
226+
// resp2, err := f.Client.Get(f.Backend + BugsEndpoint + "/" + b.VulnerabilityID + "/affectedLibIds?onlyWellKnown=true")
224227
if err != nil {
225228
return err
226229
}
@@ -233,17 +236,32 @@ func (f *Importer) Run() error {
233236
return err
234237
}
235238
f.ProgressBar.Add(1)
239+
// fmt.Println("Fetching " + b.VulnerabilityID)
236240
s := b.ToStatement()
237241
for _, al := range affectedLibs {
238-
// if al.Source == "MANUAL" || al.Source == "AST_EQUALITY" {
239242
if al.Source == "MANUAL" {
240-
s.AffectedArtifacts = append(s.AffectedArtifacts, al.toAffectedArtifact())
243+
aa := al.toAffectedArtifact()
244+
aa.Reason = "Reviewed manually"
245+
s.AffectedArtifacts = append(s.AffectedArtifacts, aa)
246+
} else if al.Source == "AST_EQUALITY" {
247+
aa := al.toAffectedArtifact()
248+
aa.Reason = "Assessed with Eclipse Steady (AST_EQUALITY)"
249+
s.AffectedArtifacts = append(s.AffectedArtifacts, aa)
241250
}
242251
}
243252
// fmt.Printf("%+v", affectedLibs)
253+
254+
// Skip statements that would not contain neither commits nor affected artifacts
255+
if len(s.Fixes) == 0 && len(s.AffectedArtifacts) == 0 {
256+
if f.Verbose {
257+
fmt.Printf("\nStatement for %s would not contain fixes nor affected artifacts, skipping.\n", s.VulnerabilityID)
258+
}
259+
continue
260+
}
244261
if !model.Matches(s, f.Filter) {
245262
f.Statements[b.VulnerabilityID] = *s
246263
}
264+
247265
}
248266
return nil
249267
}

kaybee/internal/tasks/merge.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,15 @@ func (t *MergeTask) Execute() (success bool) {
9595
}
9696
}
9797

98+
fmt.Printf("\n")
9899
if t.verbose {
99-
fmt.Printf("\nReconciling statements...\n")
100+
fmt.Printf("Reconciling statements...\n")
100101
}
101102

102103
// TODO adjust terminology: reduce, merge, reconcile....
103104
mergedStatements, mergeLog, err := t.policy.Reduce(statementsToMerge)
104105
if err != nil {
105-
fmt.Printf("\nCould not merge: %v", err)
106+
fmt.Printf("Could not merge: %v", err)
106107
}
107108

108109
// fmt.Printf("Merged:\n%v", mergedStatements)
@@ -115,11 +116,8 @@ func (t *MergeTask) Execute() (success bool) {
115116
}
116117
st[0].ToFile(".kaybee/merged/")
117118
}
118-
if t.verbose {
119-
fmt.Printf("The merge operation on %d statements from %d sources resulted in %d statements.\n", inputStatementCount, len(t.sources), len(mergedStatements))
120-
} else {
121-
fmt.Printf("Merged %d sources (%d statements): yielded %d statements.\n", len(t.sources), inputStatementCount, len(mergedStatements))
122-
}
119+
120+
fmt.Printf("Merged %d sources (%d statements): yielded %d statements.\n", len(t.sources), inputStatementCount, len(mergedStatements))
123121

124122
os.MkdirAll(".kaybee/merged/", os.ModePerm)
125123
mergeLog.Dump(".kaybee/merged/")

0 commit comments

Comments
 (0)