Skip to content

Commit 3455efa

Browse files
rolandshoemakerFiloSottile
authored andcommitted
all: use consistent index type across packages
All reports still need to have their published fields set. Change-Id: I64feda32742bb5f85e310211f8da270e4346ad6b Reviewed-on: https://team-review.git.corp.google.com/c/golang/vulndb/+/1036000 Reviewed-by: Roland Shoemaker <bracewell@google.com>
1 parent b88680f commit 3455efa

File tree

6 files changed

+45
-56
lines changed

6 files changed

+45
-56
lines changed

client/cache.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
// $GOPATH/pkg/mod/cache/download/vulndb/{db hostname}/indexes/index.json
2626
// {
2727
// Retrieved time.Time
28-
// Index map[string]time.Time
28+
// Index osv.DBIndex
2929
// }
3030
//
3131
// Each package also has a JSON file which contains the array of vulnerability
@@ -35,8 +35,8 @@ import (
3535
// []*osv.Entry
3636

3737
type Cache interface {
38-
ReadIndex(string) (map[string]time.Time, time.Time, error)
39-
WriteIndex(string, map[string]time.Time, time.Time) error
38+
ReadIndex(string) (osv.DBIndex, time.Time, error)
39+
WriteIndex(string, osv.DBIndex, time.Time) error
4040
ReadEntries(string, string) ([]*osv.Entry, error)
4141
WriteEntries(string, string, []*osv.Entry) error
4242
}
@@ -54,10 +54,10 @@ var cacheRoot = filepath.Join(build.Default.GOPATH, "/pkg/mod/cache/download/vul
5454

5555
type cachedIndex struct {
5656
Retrieved time.Time
57-
Index map[string]time.Time
57+
Index osv.DBIndex
5858
}
5959

60-
func (c *fsCache) ReadIndex(dbName string) (map[string]time.Time, time.Time, error) {
60+
func (c *fsCache) ReadIndex(dbName string) (osv.DBIndex, time.Time, error) {
6161
b, err := os.ReadFile(filepath.Join(cacheRoot, dbName, "index.json"))
6262
if err != nil {
6363
if os.IsNotExist(err) {
@@ -72,7 +72,7 @@ func (c *fsCache) ReadIndex(dbName string) (map[string]time.Time, time.Time, err
7272
return index.Index, index.Retrieved, nil
7373
}
7474

75-
func (c *fsCache) WriteIndex(dbName string, index map[string]time.Time, retrieved time.Time) error {
75+
func (c *fsCache) WriteIndex(dbName string, index osv.DBIndex, retrieved time.Time) error {
7676
path := filepath.Join(cacheRoot, dbName)
7777
if err := os.MkdirAll(path, 0777); err != nil {
7878
return err

client/cache_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestCache(t *testing.T) {
3838
}
3939

4040
now := time.Now()
41-
expectedIdx := map[string]time.Time{
41+
expectedIdx := osv.DBIndex{
4242
"a.vuln.example.com": time.Time{}.Add(time.Hour),
4343
"b.vuln.example.com": time.Time{}.Add(time.Hour * 2),
4444
"c.vuln.example.com": time.Time{}.Add(time.Hour * 3),

client/client.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type dbIndex struct{}
1919

2020
type source interface {
2121
Get([]string) ([]*osv.Entry, error)
22-
Index() (map[string]time.Time, error)
22+
Index() (osv.DBIndex, error)
2323
}
2424

2525
type localSource struct {
@@ -44,8 +44,8 @@ func (ls *localSource) Get(packages []string) ([]*osv.Entry, error) {
4444
return entries, nil
4545
}
4646

47-
func (ls *localSource) Index() (map[string]time.Time, error) {
48-
var index map[string]time.Time
47+
func (ls *localSource) Index() (osv.DBIndex, error) {
48+
var index osv.DBIndex
4949
b, err := os.ReadFile(filepath.Join(ls.dir, "index.json"))
5050
if err != nil {
5151
return nil, err
@@ -63,8 +63,8 @@ type httpSource struct {
6363
dbName string
6464
}
6565

66-
func (hs *httpSource) Index() (map[string]time.Time, error) {
67-
var cachedIndex map[string]time.Time
66+
func (hs *httpSource) Index() (osv.DBIndex, error) {
67+
var cachedIndex osv.DBIndex
6868
var cachedIndexRetrieved *time.Time
6969

7070
if hs.cache != nil {
@@ -104,7 +104,7 @@ func (hs *httpSource) Index() (map[string]time.Time, error) {
104104
if err != nil {
105105
return nil, err
106106
}
107-
var index map[string]time.Time
107+
var index osv.DBIndex
108108
if err = json.Unmarshal(b, &index); err != nil {
109109
return nil, err
110110
}

cmd/gendb/main.go

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,12 @@ import (
99
"path/filepath"
1010
"reflect"
1111
"strings"
12-
"time"
1312

1413
"github.com/BurntSushi/toml"
1514
"golang.org/x/vulndb/osv"
1615
"golang.org/x/vulndb/report"
1716
)
1817

19-
type IndexEntry struct {
20-
LastModified time.Time
21-
LastNewFinding time.Time
22-
}
23-
2418
func fail(why string) {
2519
fmt.Fprintln(os.Stderr, why)
2620
os.Exit(1)
@@ -77,46 +71,26 @@ func main() {
7771
}
7872
}
7973

80-
index := map[string]*IndexEntry{}
81-
if content, err := ioutil.ReadFile(filepath.Join(*jsonDir, "index.json")); err == nil {
82-
err = json.Unmarshal(content, &index)
83-
if err != nil {
84-
fail(fmt.Sprintf("failed to parse index: %s", err))
85-
}
86-
} else if err != nil && !os.IsNotExist(err) {
87-
fail(fmt.Sprintf("failed to read index %q: %s", filepath.Join(*jsonDir, "index.json"), err))
88-
}
89-
90-
// TODO(bracewell): I'm pretty sure the freshness stuff is basically
91-
// completely broken at the moment.
92-
now := time.Now()
93-
for path, v := range jsonVulns {
74+
index := make(osv.DBIndex, len(jsonVulns))
75+
for path, vulns := range jsonVulns {
9476
outPath := filepath.Join(*jsonDir, path)
95-
content, err := json.Marshal(v)
77+
content, err := json.Marshal(vulns)
9678
if err != nil {
9779
fail(fmt.Sprintf("failed to marshal json: %s", err))
9880
}
99-
// fmt.Println("making", filepath.Dir(outPath))
10081
err = os.MkdirAll(filepath.Dir(outPath), 0700)
10182
if err != nil {
10283
fail(fmt.Sprintf("failed to create directory %q: %s", filepath.Dir(outPath), err))
10384
}
104-
// if there is already an index entry, only update the file
105-
// if the set of vulns differ from what is already on disk
106-
if _, ok := index[path]; ok && matchesCurrent(outPath, v) {
107-
// fmt.Println("skipping", outPath)
108-
continue
109-
}
110-
// fmt.Println("writing", outPath, string(content))
11185
err = ioutil.WriteFile(outPath+".json", content, 0644)
11286
if err != nil {
11387
fail(fmt.Sprintf("failed to write %q: %s", outPath+".json", err))
11488
}
115-
if index[path] == nil {
116-
index[path] = &IndexEntry{}
89+
for _, v := range vulns {
90+
if v.LastModified.After(index[path]) {
91+
index[path] = v.LastModified
92+
}
11793
}
118-
index[path].LastModified = now
119-
// also need to set the LastNewFinding, somewhat more complicated...
12094
}
12195

12296
indexJSON, err := json.Marshal(index)

osv/json.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ import (
77
"golang.org/x/vulndb/report"
88
)
99

10+
// DBIndex contains a mapping of vulnerable packages to the
11+
// last time a new vulnerability was added to the database.
12+
// TODO: this is probably not the correct place to put this
13+
// type, since it's not really an OSV/CVF thing, but rather
14+
// vulndb implementatiion detail.
15+
type DBIndex map[string]time.Time
16+
1017
type Severity int
1118

1219
const (
@@ -166,7 +173,11 @@ func Generate(id string, url string, r report.Report) []Entry {
166173
// It would be better if this was just a recursive thing probably
167174
for _, additional := range r.AdditionalPackages {
168175
entryCopy := entry
169-
entryCopy.Package.Name = additional.Package
176+
additionalImportPath := additional.Module
177+
if additional.Package != "" {
178+
additionalImportPath = additional.Package
179+
}
180+
entryCopy.Package.Name = additionalImportPath
170181
entryCopy.EcosystemSpecific.Symbols = additional.Symbols
171182
entryCopy.Affects = generateAffects(additional.Versions)
172183

report/report.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package report
22

3+
import "time"
4+
35
type VersionRange struct {
46
Introduced string
57
Fixed string
@@ -26,15 +28,17 @@ type Report struct {
2628
Symbols []string
2729
Versions []VersionRange
2830
} `toml:"additional_packages"`
29-
Versions []VersionRange
30-
Description string
31-
Severity string
32-
CVE string
33-
Credit string
34-
Symbols []string
35-
OS []string
36-
Arch []string
37-
Links struct {
31+
Versions []VersionRange
32+
Description string
33+
Published time.Time
34+
LastModified time.Time `toml:"last_modified"`
35+
Severity string
36+
CVE string
37+
Credit string
38+
Symbols []string
39+
OS []string
40+
Arch []string
41+
Links struct {
3842
PR string
3943
Commit string
4044
Context []string

0 commit comments

Comments
 (0)