@@ -80,55 +80,56 @@ func main() {
80
80
* githubToken = os .Getenv ("VULN_GITHUB_ACCESS_TOKEN" )
81
81
}
82
82
83
- cmd := flag .Arg (0 )
84
-
85
- // Create-excluded has no args, so it is separated form the other commands.
86
- if cmd == "create-excluded" {
87
- _ , cfg , err := setupCreate (ctx , nil )
88
- if err != nil {
89
- log .Fatal (err )
83
+ var (
84
+ args []string
85
+ cmd = flag .Arg (0 )
86
+ )
87
+ if cmd != "create-excluded" {
88
+ if flag .NArg () < 2 {
89
+ flag .Usage ()
90
+ log .Fatal ("not enough arguments" )
90
91
}
91
- if err = createExcluded (ctx , cfg ); err != nil {
92
- log .Fatal (err )
93
- }
94
- return
92
+ args = flag .Args ()[1 :]
95
93
}
96
94
97
- if flag .NArg () < 2 {
98
- flag .Usage ()
99
- log .Fatal ("not enough arguments" )
100
- }
101
-
102
- args := flag .Args ()[1 :]
103
-
104
- // Create operates on github issue IDs instead of filenames, so it is
105
- // separated from the other commands.
106
- if cmd == "create" {
95
+ // setupCreate clones the CVEList repo and can be very slow,
96
+ // so commands that require this functionality are separated from other
97
+ // commands.
98
+ if cmd == "create-excluded" || cmd == "create" {
107
99
githubIDs , cfg , err := setupCreate (ctx , args )
108
100
if err != nil {
109
101
log .Fatal (err )
110
102
}
111
- for _ , githubID := range githubIDs {
112
- if err := create (ctx , githubID , cfg ); err != nil {
113
- fmt .Printf ("skipped: %s\n " , err )
103
+ switch cmd {
104
+ case "create-excluded" :
105
+ if err = createExcluded (ctx , cfg ); err != nil {
106
+ log .Fatal (err )
107
+ }
108
+ case "create" :
109
+ // Unlike commands below, create operates on github issue IDs
110
+ // instead of filenames.
111
+ for _ , githubID := range githubIDs {
112
+ if err := create (ctx , githubID , cfg ); err != nil {
113
+ fmt .Printf ("skipped: %s\n " , err )
114
+ }
114
115
}
115
116
}
116
- return
117
117
}
118
118
119
+ ghsaClient := ghsa .NewClient (ctx , * githubToken )
119
120
var cmdFunc func (string ) error
120
121
switch cmd {
121
122
case "lint" :
122
123
cmdFunc = lint
123
124
case "commit" :
124
- cmdFunc = func (name string ) error { return commit (ctx , name , * githubToken ) }
125
+ cmdFunc = func (name string ) error { return commit (ctx , name , ghsaClient ) }
125
126
case "cve" :
126
127
cmdFunc = func (name string ) error { return cveCmd (ctx , name ) }
127
128
//TODO: (https://github.com/golang/go/issues/56356): Deprecate this command once CVE JSON 5.0 publishing is available
128
129
case "cve4" :
129
130
cmdFunc = func (name string ) error { return cve4Cmd (ctx , name , * indent ) }
130
131
case "fix" :
131
- cmdFunc = func (name string ) error { return fix (ctx , name , * githubToken ) }
132
+ cmdFunc = func (name string ) error { return fix (ctx , name , ghsaClient ) }
132
133
case "osv" :
133
134
cmdFunc = osvCmd
134
135
case "set-dates" :
@@ -233,8 +234,8 @@ func parseArgsToGithubIDs(args []string, existingByIssue map[int]*report.Report)
233
234
}
234
235
235
236
type createCfg struct {
236
- ghToken string
237
237
repo * git.Repository
238
+ ghsaClient * ghsa.Client
238
239
issuesClient * issues.Client
239
240
existingByFile map [string ]* report.Report
240
241
existingByIssue map [int ]* report.Report
@@ -270,9 +271,9 @@ func setupCreate(ctx context.Context, args []string) ([]int, *createCfg, error)
270
271
return nil , nil , err
271
272
}
272
273
return githubIDs , & createCfg {
273
- ghToken : * githubToken ,
274
274
repo : repo ,
275
275
issuesClient : issues .NewClient (& issues.Config {Owner : owner , Repo : repoName , Token : * githubToken }),
276
+ ghsaClient : ghsa .NewClient (ctx , * githubToken ),
276
277
existingByFile : existingByFile ,
277
278
existingByIssue : existingByIssue ,
278
279
allowClosed : * closedOk ,
@@ -287,7 +288,7 @@ func createReport(ctx context.Context, cfg *createCfg, iss *issues.Issue) (r *re
287
288
}
288
289
if len (parsed .ghsas ) == 0 && len (parsed .cves ) > 0 {
289
290
for _ , cve := range parsed .cves {
290
- sas , err := ghsa . ListForCVE (ctx , cfg . ghToken , cve )
291
+ sas , err := cfg . ghsaClient . ListForCVE (ctx , cve )
291
292
if err != nil {
292
293
return nil , err
293
294
}
@@ -427,7 +428,7 @@ func newReport(ctx context.Context, cfg *createCfg, parsed *parsedIssue) (*repor
427
428
var r * report.Report
428
429
switch {
429
430
case len (parsed .ghsas ) > 0 :
430
- ghsa , err := ghsa . FetchGHSA (ctx , cfg . ghToken , parsed .ghsas [0 ])
431
+ ghsa , err := cfg . ghsaClient . FetchGHSA (ctx , parsed .ghsas [0 ])
431
432
if err != nil {
432
433
return nil , err
433
434
}
@@ -597,7 +598,7 @@ func lint(filename string) (err error) {
597
598
return nil
598
599
}
599
600
600
- func fix (ctx context.Context , filename string , accessToken string ) (err error ) {
601
+ func fix (ctx context.Context , filename string , ghsaClient * ghsa. Client ) (err error ) {
601
602
defer derrors .Wrap (& err , "fix(%q)" , filename )
602
603
r , err := report .Read (filename )
603
604
if err != nil {
@@ -611,7 +612,7 @@ func fix(ctx context.Context, filename string, accessToken string) (err error) {
611
612
return err
612
613
}
613
614
}
614
- if err := fixGHSAs (ctx , r , accessToken ); err != nil {
615
+ if err := fixGHSAs (ctx , r , ghsaClient ); err != nil {
615
616
return err
616
617
}
617
618
// Write unconditionally in order to format.
@@ -842,12 +843,12 @@ func irun(name string, arg ...string) error {
842
843
return cmd .Run ()
843
844
}
844
845
845
- func commit (ctx context.Context , filename , accessToken string ) (err error ) {
846
+ func commit (ctx context.Context , filename string , ghsaClient * ghsa. Client ) (err error ) {
846
847
defer derrors .Wrap (& err , "commit(%q)" , filename )
847
848
848
849
// Ignore errors. If anything is really wrong with the report, we'll
849
850
// detect it on re-linting below.
850
- _ = fix (ctx , filename , accessToken )
851
+ _ = fix (ctx , filename , ghsaClient )
851
852
852
853
r , err := report .ReadAndLint (filename )
853
854
if err != nil {
@@ -1027,39 +1028,15 @@ func setDates(filename string, dates map[string]gitrepo.Dates) (err error) {
1027
1028
return r .Write (filename )
1028
1029
}
1029
1030
1030
- // loadGHSAsByCVE returns a map from CVE ID to GHSA IDs.
1031
- // It does this by using the GitHub API to list all Go security
1032
- // advisories.
1033
- func loadGHSAsByCVE (ctx context.Context , accessToken string ) (_ map [string ][]string , err error ) {
1034
- defer derrors .Wrap (& err , "loadGHSAsByCVE" )
1035
-
1036
- sas , err := ghsa .List (ctx , accessToken , time.Time {})
1037
- if err != nil {
1038
- return nil , err
1039
- }
1040
- m := map [string ][]string {}
1041
- for _ , sa := range sas {
1042
- for _ , id := range sa .Identifiers {
1043
- if id .Type == "CVE" {
1044
- m [id .Value ] = append (m [id .Value ], sa .ID )
1045
- }
1046
- }
1047
- }
1048
- return m , nil
1049
- }
1050
-
1051
1031
// fixGHSAs replaces r.GHSAs with a sorted list of GitHub Security
1052
1032
// Advisory IDs that correspond to the CVEs.
1053
- func fixGHSAs (ctx context.Context , r * report.Report , accessToken string ) error {
1054
- if accessToken == "" {
1055
- return nil
1056
- }
1033
+ func fixGHSAs (ctx context.Context , r * report.Report , ghsaClient * ghsa.Client ) error {
1057
1034
if len (r .GHSAs ) > 0 && ! * alwaysFixGHSA {
1058
1035
return nil
1059
1036
}
1060
1037
m := map [string ]struct {}{}
1061
1038
for _ , cid := range r .CVEs {
1062
- sas , err := ghsa .ListForCVE (ctx , accessToken , cid )
1039
+ sas , err := ghsaClient .ListForCVE (ctx , cid )
1063
1040
if err != nil {
1064
1041
return err
1065
1042
}
0 commit comments