client: move osv.DBIndex to package client osv.DBIndex is relevant to the client package, not the osv package. Change-Id: I9a1007fffaeb9bac42974e9f837943faf7dd6f00 Reviewed-on: https://go-review.googlesource.com/c/vuln/+/372995 Reviewed-by: Jonathan Amsterdam <jba@google.com> Trust: Julie Qiu <julie@golang.org>
diff --git a/client/cache.go b/client/cache.go index e02330f..5ad181d 100644 --- a/client/cache.go +++ b/client/cache.go
@@ -12,8 +12,8 @@ // Cache interface for vuln DB caching. type Cache interface { - ReadIndex(string) (osv.DBIndex, time.Time, error) - WriteIndex(string, osv.DBIndex, time.Time) error + ReadIndex(string) (DBIndex, time.Time, error) + WriteIndex(string, DBIndex, time.Time) error ReadEntries(string, string) ([]*osv.Entry, error) WriteEntries(string, string, []*osv.Entry) error }
diff --git a/client/client.go b/client/client.go index 7d994cf..6b3ef4b 100644 --- a/client/client.go +++ b/client/client.go
@@ -12,7 +12,7 @@ // was added. The index file is called indx.json, and has the // following format: // -// map[string]time.Time (osv.DBIndex) +// map[string]time.Time (DBIndex) // // Each vulnerable module is represented by an individual JSON file // which contains all of the vulnerabilities in that module. The path @@ -50,6 +50,10 @@ "golang.org/x/vuln/osv" ) +// DBIndex contains a mapping of vulnerable packages to the last time a new +// vulnerability was added to the database. +type DBIndex map[string]time.Time + // Client interface for fetching vulnerabilities based on module path or ID. type Client interface { // GetByModule returns the entries that affect the given module path. @@ -68,7 +72,7 @@ type source interface { Client - Index(context.Context) (osv.DBIndex, error) + Index(context.Context) (DBIndex, error) } type localSource struct { @@ -120,9 +124,9 @@ return ids, nil } -func (ls *localSource) Index(context.Context) (_ osv.DBIndex, err error) { +func (ls *localSource) Index(context.Context) (_ DBIndex, err error) { defer derrors.Wrap(&err, "Index()") - var index osv.DBIndex + var index DBIndex b, err := ioutil.ReadFile(filepath.Join(ls.dir, "index.json")) if err != nil { return nil, err @@ -140,10 +144,10 @@ dbName string } -func (hs *httpSource) Index(ctx context.Context) (_ osv.DBIndex, err error) { +func (hs *httpSource) Index(ctx context.Context) (_ DBIndex, err error) { defer derrors.Wrap(&err, "Index()") - var cachedIndex osv.DBIndex + var cachedIndex DBIndex var cachedIndexRetrieved *time.Time if hs.cache != nil { @@ -190,7 +194,7 @@ if err != nil { return nil, err } - var index osv.DBIndex + var index DBIndex if err = json.Unmarshal(b, &index); err != nil { return nil, err }
diff --git a/client/client_test.go b/client/client_test.go index 9b2ca26..cb45cf1 100644 --- a/client/client_test.go +++ b/client/client_test.go
@@ -46,20 +46,20 @@ // testCache for testing purposes type testCache struct { - indexMap map[string]osv.DBIndex + indexMap map[string]DBIndex indexStamp map[string]time.Time vulnMap map[string]map[string][]*osv.Entry } func freshTestCache() *testCache { return &testCache{ - indexMap: make(map[string]osv.DBIndex), + indexMap: make(map[string]DBIndex), indexStamp: make(map[string]time.Time), vulnMap: make(map[string]map[string][]*osv.Entry), } } -func (tc *testCache) ReadIndex(db string) (osv.DBIndex, time.Time, error) { +func (tc *testCache) ReadIndex(db string) (DBIndex, time.Time, error) { index, ok := tc.indexMap[db] if !ok { return nil, time.Time{}, nil @@ -71,7 +71,7 @@ return index, stamp, nil } -func (tc *testCache) WriteIndex(db string, index osv.DBIndex, stamp time.Time) error { +func (tc *testCache) WriteIndex(db string, index DBIndex, stamp time.Time) error { tc.indexMap[db] = index tc.indexStamp[db] = stamp return nil @@ -219,7 +219,7 @@ ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fetches[r.URL.Path]++ if r.URL.Path == "/index.json" { - j, _ := json.Marshal(osv.DBIndex{ + j, _ := json.Marshal(DBIndex{ "a": time.Now(), "b": time.Now(), }) @@ -256,7 +256,7 @@ // set timestamp so that cached index is stale, // i.e., more than two hours old. timeStamp := time.Now().Add(time.Hour * (-3)) - index := osv.DBIndex{"a": timeStamp} + index := DBIndex{"a": timeStamp} cache := freshTestCache() cache.WriteIndex(url.Hostname(), index, timeStamp)
diff --git a/osv/json.go b/osv/json.go index 6ca54d8..8964220 100644 --- a/osv/json.go +++ b/osv/json.go
@@ -18,13 +18,6 @@ "golang.org/x/mod/semver" ) -// DBIndex contains a mapping of vulnerable packages to the -// last time a new vulnerability was added to the database. -// TODO: this is probably not the correct place to put this -// type, since it's not really an OSV/CVF thing, but rather -// vulndb implementatiion detail. -type DBIndex map[string]time.Time - type AffectsRangeType string const (
diff --git a/srv/cmd/dbdiff/main.go b/srv/cmd/dbdiff/main.go index a832f26..21c19d4 100644 --- a/srv/cmd/dbdiff/main.go +++ b/srv/cmd/dbdiff/main.go
@@ -15,14 +15,15 @@ "strings" "github.com/google/go-cmp/cmp" + "golang.org/x/vuln/client" "golang.org/x/vuln/osv" "golang.org/x/vuln/srv/internal" "golang.org/x/vuln/srv/internal/derrors" ) -func loadDB(dbPath string) (_ osv.DBIndex, _ map[string][]osv.Entry, err error) { +func loadDB(dbPath string) (_ client.DBIndex, _ map[string][]osv.Entry, err error) { defer derrors.Wrap(&err, "loadDB(%q)", dbPath) - index := osv.DBIndex{} + index := client.DBIndex{} dbMap := map[string][]osv.Entry{} var loadDir func(string) error
diff --git a/srv/cmd/gendb/main.go b/srv/cmd/gendb/main.go index 9267be8..4d58c8a 100644 --- a/srv/cmd/gendb/main.go +++ b/srv/cmd/gendb/main.go
@@ -15,6 +15,7 @@ "path/filepath" "strings" + "golang.org/x/vuln/client" "golang.org/x/vuln/osv" "golang.org/x/vuln/srv/internal" "golang.org/x/vuln/srv/internal/database" @@ -76,7 +77,7 @@ entries = append(entries, entry) } - index := make(osv.DBIndex, len(jsonVulns)) + index := make(client.DBIndex, len(jsonVulns)) for path, vulns := range jsonVulns { outPath := filepath.Join(*jsonDir, path) content, err := json.Marshal(vulns)