Skip to content

Commit 0817681

Browse files
committed
internal/frontend, internal/vulns: isolate references to x/vuln repo
No-op refactor to move all code that depends on x/vuln to the internal/vuln (renamed from internal/vulns) package. This will allow us to more easily remove the dependency, as a part of the migration to the v1 database schema. For golang/go#58928 Change-Id: Ic8ac2377832d8e4a2a6afbb42729a7e10553665c Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/474255 Reviewed-by: Julie Qiu <[email protected]> Reviewed-by: Tatiana Bradley <[email protected]> Run-TryBot: Tatiana Bradley <[email protected]> TryBot-Result: kokoro <[email protected]>
1 parent a0d43ae commit 0817681

File tree

15 files changed

+176
-119
lines changed

15 files changed

+176
-119
lines changed

cmd/frontend/main.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"golang.org/x/pkgsite/internal/proxy"
2929
"golang.org/x/pkgsite/internal/queue"
3030
"golang.org/x/pkgsite/internal/source"
31-
vulnc "golang.org/x/vuln/client"
31+
"golang.org/x/pkgsite/internal/vuln"
3232
)
3333

3434
var (
@@ -105,11 +105,9 @@ func main() {
105105
}
106106

107107
rc := cmdconfig.ReportingClient(ctx, cfg)
108-
vc, err := vulnc.NewClient([]string{cfg.VulnDB}, vulnc.Options{
109-
HTTPCache: newVulndbCache(),
110-
})
108+
vc, err := vuln.NewClient(cfg.VulnDB)
111109
if err != nil {
112-
log.Fatalf(ctx, "vulndbc.NewClient: %v", err)
110+
log.Fatalf(ctx, "vuln.NewClient: %v", err)
113111
}
114112
staticSource := template.TrustedSourceFromFlag(flag.Lookup("static").Value)
115113
server, err := frontend.NewServer(frontend.ServerConfig{

internal/frontend/search.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ import (
2727
"golang.org/x/pkgsite/internal/postgres"
2828
"golang.org/x/pkgsite/internal/stdlib"
2929
"golang.org/x/pkgsite/internal/version"
30-
"golang.org/x/pkgsite/internal/vulns"
30+
"golang.org/x/pkgsite/internal/vuln"
3131
"golang.org/x/text/message"
32-
vulnc "golang.org/x/vuln/client"
3332
)
3433

3534
// serveSearch applies database data to the search template. Handles endpoint
@@ -59,7 +58,7 @@ type searchAction struct {
5958
page interface{ setBasePage(basePage) }
6059
}
6160

62-
func determineSearchAction(r *http.Request, ds internal.DataSource, vulnClient vulnc.Client) (*searchAction, error) {
61+
func determineSearchAction(r *http.Request, ds internal.DataSource, vulnClient *vuln.Client) (*searchAction, error) {
6362
if r.Method != http.MethodGet && r.Method != http.MethodHead {
6463
return nil, &serverError{status: http.StatusMethodNotAllowed}
6564
}
@@ -130,9 +129,9 @@ func determineSearchAction(r *http.Request, ds internal.DataSource, vulnClient v
130129
if len(filters) > 0 {
131130
symbol = filters[0]
132131
}
133-
var getVulnEntries vulns.VulnEntriesFunc
132+
var getVulnEntries vuln.VulnEntriesFunc
134133
if vulnClient != nil {
135-
getVulnEntries = vulnClient.GetByModule
134+
getVulnEntries = vulnClient.ByModule
136135
}
137136
page, err := fetchSearchPage(ctx, db, cq, symbol, pageParams, mode == searchModeSymbol, getVulnEntries)
138137
if err != nil {
@@ -226,7 +225,7 @@ type SearchResult struct {
226225
SymbolGOOS string
227226
SymbolGOARCH string
228227
SymbolLink string
229-
Vulns []vulns.Vuln
228+
Vulns []vuln.Vuln
230229
}
231230

232231
type subResult struct {
@@ -237,7 +236,7 @@ type subResult struct {
237236
// fetchSearchPage fetches data matching the search query from the database and
238237
// returns a SearchPage.
239238
func fetchSearchPage(ctx context.Context, db *postgres.DB, cq, symbol string,
240-
pageParams paginationParams, searchSymbols bool, getVulnEntries vulns.VulnEntriesFunc) (*SearchPage, error) {
239+
pageParams paginationParams, searchSymbols bool, getVulnEntries vuln.VulnEntriesFunc) (*SearchPage, error) {
241240
maxResultCount := maxSearchOffset + pageParams.limit
242241

243242
// Pageless search: always start from the beginning.
@@ -371,7 +370,7 @@ func searchRequestRedirectPath(ctx context.Context, ds internal.DataSource, quer
371370
return fmt.Sprintf("/%s", requestedPath)
372371
}
373372

374-
func searchVulnModule(ctx context.Context, mode, cq string, client vulnc.Client) (_ *searchAction, err error) {
373+
func searchVulnModule(ctx context.Context, mode, cq string, client *vuln.Client) (_ *searchAction, err error) {
375374
if mode != searchModeVuln {
376375
return nil, nil
377376
}
@@ -401,13 +400,13 @@ EntryLoop:
401400
}, nil
402401
}
403402

404-
func searchVulnAlias(ctx context.Context, mode, cq string, vulnClient vulnc.Client) (_ *searchAction, err error) {
403+
func searchVulnAlias(ctx context.Context, mode, cq string, vulnClient *vuln.Client) (_ *searchAction, err error) {
405404
defer derrors.Wrap(&err, "searchVulnAlias(%q, %q)", mode, cq)
406405

407406
if mode != searchModeVuln || !isVulnAlias(cq) {
408407
return nil, nil
409408
}
410-
aliasEntries, err := vulnClient.GetByAlias(ctx, cq)
409+
aliasEntries, err := vulnClient.ByAlias(ctx, cq)
411410
if err != nil {
412411
return nil, err
413412
}
@@ -429,7 +428,7 @@ func searchVulnAlias(ctx context.Context, mode, cq string, vulnClient vulnc.Clie
429428
}
430429
}
431430

432-
// Regexps that match aliases for Go vulns.
431+
// Regexps that match aliases for Go vuln.
433432
var (
434433
cveRegexp = regexp.MustCompile("^CVE-[0-9]{4}-[0-9]+$")
435434
ghsaRegexp = regexp.MustCompile("^GHSA-.{4}-.{4}-.{4}$")
@@ -608,7 +607,7 @@ func elapsedTime(date time.Time) string {
608607

609608
// addVulns adds vulnerability information to search results by consulting the
610609
// vulnerability database.
611-
func addVulns(ctx context.Context, rs []*SearchResult, getVulnEntries vulns.VulnEntriesFunc) {
610+
func addVulns(ctx context.Context, rs []*SearchResult, getVulnEntries vuln.VulnEntriesFunc) {
612611
// Get all vulns concurrently.
613612
var wg sync.WaitGroup
614613
// TODO(golang/go#48223): throttle concurrency?
@@ -617,7 +616,7 @@ func addVulns(ctx context.Context, rs []*SearchResult, getVulnEntries vulns.Vuln
617616
wg.Add(1)
618617
go func() {
619618
defer wg.Done()
620-
r.Vulns = vulns.VulnsForPackage(ctx, r.ModulePath, r.Version, r.PackagePath, getVulnEntries)
619+
r.Vulns = vuln.VulnsForPackage(ctx, r.ModulePath, r.Version, r.PackagePath, getVulnEntries)
621620
}()
622621
}
623622
wg.Wait()

internal/frontend/search_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"golang.org/x/pkgsite/internal/licenses"
2323
"golang.org/x/pkgsite/internal/postgres"
2424
"golang.org/x/pkgsite/internal/testing/sample"
25-
"golang.org/x/pkgsite/internal/vulns"
25+
"golang.org/x/pkgsite/internal/vuln"
2626
"golang.org/x/text/language"
2727
"golang.org/x/text/message"
2828
"golang.org/x/vuln/osv"
@@ -39,7 +39,7 @@ func TestDetermineSearchAction(t *testing.T) {
3939
for _, v := range modules {
4040
postgres.MustInsertModule(ctx, t, testDB, v)
4141
}
42-
vc := newVulndbTestClient(testEntries)
42+
vc := vuln.NewTestClient(testEntries)
4343
for _, test := range []struct {
4444
name string
4545
method string
@@ -385,7 +385,7 @@ func TestFetchSearchPage(t *testing.T) {
385385
DisplayVersion: moduleFoo.Version,
386386
Licenses: []string{"MIT"},
387387
CommitTime: elapsedTime(moduleFoo.CommitTime),
388-
Vulns: []vulns.Vuln{{ID: "test", Details: "vuln"}},
388+
Vulns: []vuln.Vuln{{ID: "test", Details: "vuln"}},
389389
},
390390
},
391391
},
@@ -552,7 +552,7 @@ func TestSearchRequestRedirectPath(t *testing.T) {
552552
}
553553

554554
func TestSearchVulnAlias(t *testing.T) {
555-
vc := newVulndbTestClient(testEntries)
555+
vc := vuln.NewTestClient(testEntries)
556556
for _, test := range []struct {
557557
name string
558558
mode string
@@ -624,7 +624,7 @@ func TestSearchVulnAlias(t *testing.T) {
624624
}
625625

626626
func TestSearchVulnModulePath(t *testing.T) {
627-
vc := newVulndbTestClient(testEntries)
627+
vc := vuln.NewTestClient(testEntries)
628628
for _, test := range []struct {
629629
name string
630630
mode string

internal/frontend/server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ import (
3737
"golang.org/x/pkgsite/internal/queue"
3838
"golang.org/x/pkgsite/internal/static"
3939
"golang.org/x/pkgsite/internal/version"
40+
"golang.org/x/pkgsite/internal/vuln"
4041
"golang.org/x/text/cases"
4142
"golang.org/x/text/language"
42-
vulnc "golang.org/x/vuln/client"
4343
)
4444

4545
// Server can be installed to serve the go discovery frontend.
@@ -59,7 +59,7 @@ type Server struct {
5959
serveStats bool
6060
reportingClient *errorreporting.Client
6161
fileMux *http.ServeMux
62-
vulnClient vulnc.Client
62+
vulnClient *vuln.Client
6363
versionID string
6464
instanceID string
6565

@@ -81,7 +81,7 @@ type ServerConfig struct {
8181
DevMode bool
8282
StaticPath string // used only for dynamic loading in dev mode
8383
ReportingClient *errorreporting.Client
84-
VulndbClient vulnc.Client
84+
VulndbClient *vuln.Client
8585
}
8686

8787
// NewServer creates a new Server for the given database and template directory.

internal/frontend/tabs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
"golang.org/x/pkgsite/internal"
1313
"golang.org/x/pkgsite/internal/derrors"
14-
"golang.org/x/pkgsite/internal/vulns"
14+
"golang.org/x/pkgsite/internal/vuln"
1515
)
1616

1717
// TabSettings defines tab-specific metadata.
@@ -78,7 +78,7 @@ func init() {
7878
// handler.
7979
func fetchDetailsForUnit(ctx context.Context, r *http.Request, tab string, ds internal.DataSource, um *internal.UnitMeta,
8080
requestedVersion string, bc internal.BuildContext,
81-
getVulnEntries vulns.VulnEntriesFunc) (_ any, err error) {
81+
getVulnEntries vuln.VulnEntriesFunc) (_ any, err error) {
8282
defer derrors.Wrap(&err, "fetchDetailsForUnit(r, %q, ds, um=%q,%q,%q)", tab, um.Path, um.ModulePath, um.Version)
8383
switch tab {
8484
case tabMain:

internal/frontend/unit.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"golang.org/x/pkgsite/internal/middleware"
2222
"golang.org/x/pkgsite/internal/stdlib"
2323
"golang.org/x/pkgsite/internal/version"
24-
"golang.org/x/pkgsite/internal/vulns"
24+
"golang.org/x/pkgsite/internal/vuln"
2525
)
2626

2727
// UnitPage contains data needed to render the unit template.
@@ -92,7 +92,7 @@ type UnitPage struct {
9292
Details any
9393

9494
// Vulns holds vulnerability information.
95-
Vulns []vulns.Vuln
95+
Vulns []vuln.Vuln
9696

9797
// DepsDevURL holds the full URL to this module version on deps.dev.
9898
DepsDevURL string
@@ -135,9 +135,9 @@ func (s *Server) serveUnitPage(ctx context.Context, w http.ResponseWriter, r *ht
135135
// It's also okay to provide just one (e.g. GOOS=windows), which will select
136136
// the first doc with that value, ignoring the other one.
137137
bc := internal.BuildContext{GOOS: r.FormValue("GOOS"), GOARCH: r.FormValue("GOARCH")}
138-
var getVulnEntries vulns.VulnEntriesFunc
138+
var getVulnEntries vuln.VulnEntriesFunc
139139
if s.vulnClient != nil {
140-
getVulnEntries = s.vulnClient.GetByModule
140+
getVulnEntries = s.vulnClient.ByModule
141141
}
142142
d, err := fetchDetailsForUnit(ctx, r, tab, ds, um, info.requestedVersion, bc, getVulnEntries)
143143
if err != nil {
@@ -241,7 +241,7 @@ func (s *Server) serveUnitPage(ctx context.Context, w http.ResponseWriter, r *ht
241241

242242
// Get vulnerability information.
243243
if s.vulnClient != nil {
244-
page.Vulns = vulns.VulnsForPackage(ctx, um.ModulePath, um.Version, um.Path, s.vulnClient.GetByModule)
244+
page.Vulns = vuln.VulnsForPackage(ctx, um.ModulePath, um.Version, um.Path, s.vulnClient.ByModule)
245245
}
246246
s.servePage(ctx, w, tabSettings.TemplateName, page)
247247
return nil

internal/frontend/versions.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"golang.org/x/pkgsite/internal/postgres"
1919
"golang.org/x/pkgsite/internal/stdlib"
2020
"golang.org/x/pkgsite/internal/version"
21-
"golang.org/x/pkgsite/internal/vulns"
21+
"golang.org/x/pkgsite/internal/vuln"
2222
)
2323

2424
// VersionsDetails contains the hierarchy of version summary information used
@@ -82,10 +82,10 @@ type VersionSummary struct {
8282
RetractionRationale string
8383
IsMinor bool
8484
Symbols [][]*Symbol
85-
Vulns []vulns.Vuln
85+
Vulns []vuln.Vuln
8686
}
8787

88-
func fetchVersionsDetails(ctx context.Context, ds internal.DataSource, um *internal.UnitMeta, getVulnEntries vulns.VulnEntriesFunc) (*VersionsDetails, error) {
88+
func fetchVersionsDetails(ctx context.Context, ds internal.DataSource, um *internal.UnitMeta, getVulnEntries vuln.VulnEntriesFunc) (*VersionsDetails, error) {
8989
db, ok := ds.(*postgres.DB)
9090
if !ok {
9191
// The proxydatasource does not support the imported by page.
@@ -146,7 +146,7 @@ func buildVersionDetails(ctx context.Context, currentModulePath, packagePath str
146146
modInfos []*internal.ModuleInfo,
147147
sh *internal.SymbolHistory,
148148
linkify func(v *internal.ModuleInfo) string,
149-
getVulnEntries vulns.VulnEntriesFunc,
149+
getVulnEntries vuln.VulnEntriesFunc,
150150
) *VersionsDetails {
151151
// lists organizes versions by VersionListKey.
152152
lists := make(map[VersionListKey]*VersionList)
@@ -201,7 +201,7 @@ func buildVersionDetails(ctx context.Context, currentModulePath, packagePath str
201201
if mi.ModulePath == stdlib.ModulePath {
202202
pkg = packagePath
203203
}
204-
vs.Vulns = vulns.VulnsForPackage(ctx, mi.ModulePath, mi.Version, pkg, getVulnEntries)
204+
vs.Vulns = vuln.VulnsForPackage(ctx, mi.ModulePath, mi.Version, pkg, getVulnEntries)
205205
vl := lists[key]
206206
if vl == nil {
207207
seenLists = append(seenLists, key)

internal/frontend/versions_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"golang.org/x/pkgsite/internal/stdlib"
1515
"golang.org/x/pkgsite/internal/testing/sample"
1616
"golang.org/x/pkgsite/internal/version"
17-
"golang.org/x/pkgsite/internal/vulns"
17+
"golang.org/x/pkgsite/internal/vuln"
1818
"golang.org/x/vuln/osv"
1919
)
2020

@@ -150,7 +150,7 @@ func TestFetchPackageVersionsDetails(t *testing.T) {
150150
ThisModule: []*VersionList{
151151
func() *VersionList {
152152
vl := makeList(v1Path, modulePath1, "v1", []string{"v1.3.0", "v1.2.3", "v1.2.1"}, false)
153-
vl.Versions[2].Vulns = []vulns.Vuln{{
153+
vl.Versions[2].Vulns = []vuln.Vuln{{
154154
Details: vulnEntry.Details,
155155
}}
156156
return vl

internal/frontend/vulns.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ import (
1212

1313
"golang.org/x/pkgsite/internal"
1414
"golang.org/x/pkgsite/internal/derrors"
15-
"golang.org/x/pkgsite/internal/vulns"
15+
"golang.org/x/pkgsite/internal/vuln"
1616
"golang.org/x/sync/errgroup"
17-
vulnc "golang.org/x/vuln/client"
1817
"golang.org/x/vuln/osv"
1918
)
2019

@@ -34,7 +33,7 @@ type VulnListPage struct {
3433
type VulnPage struct {
3534
basePage
3635
Entry OSVEntry
37-
AffectedPackages []*vulns.AffectedPackage
36+
AffectedPackages []*vuln.AffectedPackage
3837
AliasLinks []link
3938
AdvisoryLinks []link
4039
}
@@ -105,8 +104,8 @@ func (s *Server) serveVuln(w http.ResponseWriter, r *http.Request, _ internal.Da
105104
return nil
106105
}
107106

108-
func newVulnPage(ctx context.Context, client vulnc.Client, id string) (*VulnPage, error) {
109-
entry, err := client.GetByID(ctx, id)
107+
func newVulnPage(ctx context.Context, client *vuln.Client, id string) (*VulnPage, error) {
108+
entry, err := client.ByID(ctx, id)
110109
if err != nil {
111110
return nil, derrors.VulnDBError
112111
}
@@ -115,13 +114,13 @@ func newVulnPage(ctx context.Context, client vulnc.Client, id string) (*VulnPage
115114
}
116115
return &VulnPage{
117116
Entry: OSVEntry{entry},
118-
AffectedPackages: vulns.AffectedPackages(entry),
117+
AffectedPackages: vuln.AffectedPackages(entry),
119118
AliasLinks: aliasLinks(entry),
120119
AdvisoryLinks: advisoryLinks(entry),
121120
}, nil
122121
}
123122

124-
func newVulnListPage(ctx context.Context, client vulnc.Client) (*VulnListPage, error) {
123+
func newVulnListPage(ctx context.Context, client *vuln.Client) (*VulnListPage, error) {
125124
entries, err := vulnList(ctx, client)
126125
if err != nil {
127126
return nil, err
@@ -131,10 +130,10 @@ func newVulnListPage(ctx context.Context, client vulnc.Client) (*VulnListPage, e
131130
return &VulnListPage{Entries: entries}, nil
132131
}
133132

134-
func vulnList(ctx context.Context, client vulnc.Client) ([]OSVEntry, error) {
133+
func vulnList(ctx context.Context, client *vuln.Client) ([]OSVEntry, error) {
135134
const concurrency = 4
136135

137-
ids, err := client.ListIDs(ctx)
136+
ids, err := client.IDs(ctx)
138137
if err != nil {
139138
return nil, derrors.VulnDBError
140139
}
@@ -148,7 +147,7 @@ func vulnList(ctx context.Context, client vulnc.Client) ([]OSVEntry, error) {
148147
sem <- struct{}{}
149148
g.Go(func() error {
150149
defer func() { <-sem }()
151-
e, err := client.GetByID(ctx, id)
150+
e, err := client.ByID(ctx, id)
152151
if err != nil {
153152
return err
154153
}

0 commit comments

Comments
 (0)