forked from golang/vulndb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
299 lines (276 loc) · 8.46 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Command worker runs the vuln worker server.
// It can also be used to perform actions from the command line
// by providing a sub-command.
package main
import (
"bufio"
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"net/http"
"os"
"strings"
"text/tabwriter"
"time"
"golang.org/x/exp/event"
"golang.org/x/vulndb/internal/cvelistrepo"
"golang.org/x/vulndb/internal/ghsa"
"golang.org/x/vulndb/internal/gitrepo"
"golang.org/x/vulndb/internal/issues"
"golang.org/x/vulndb/internal/report"
"golang.org/x/vulndb/internal/worker"
"golang.org/x/vulndb/internal/worker/log"
"golang.org/x/vulndb/internal/worker/store"
)
var (
// Flags only for the command-line tool.
localRepoPath = flag.String("local-cve-repo", "", "path to local repo, instead of cloning remote")
force = flag.Bool("force", false, "force an update or scan to happen")
limit = flag.Int("limit", 0,
"limit on number of things to list or issues to create (0 means unlimited)")
githubTokenFile = flag.String("ghtokenfile", "",
"path to file containing GitHub access token (for creating issues)")
knownModuleFile = flag.String("known-module-file", "", "file with list of all known modules")
)
// Config for both the server and the command-line tool.
var cfg worker.Config
func init() {
flag.StringVar(&cfg.Project, "project", os.Getenv("GOOGLE_CLOUD_PROJECT"), "project ID (required)")
flag.StringVar(&cfg.Namespace, "namespace", os.Getenv("VULN_WORKER_NAMESPACE"), "Firestore namespace (required)")
flag.BoolVar(&cfg.UseErrorReporting, "report-errors", os.Getenv("VULN_WORKER_REPORT_ERRORS") == "true",
"use the error reporting API")
flag.StringVar(&cfg.IssueRepo, "issue-repo", os.Getenv("VULN_WORKER_ISSUE_REPO"), "repo to create issues in")
}
const pkgsiteURL = "https://pkg.go.dev"
func main() {
flag.Usage = func() {
out := flag.CommandLine.Output()
fmt.Fprintln(out, "usage:")
fmt.Fprintln(out, "worker FLAGS")
fmt.Fprintln(out, " run as a server, listening at the PORT env var")
fmt.Fprintln(out, "worker FLAGS SUBCOMMAND ...")
fmt.Fprintln(out, " run as a command-line tool, executing SUBCOMMAND")
fmt.Fprintln(out, " subcommands:")
fmt.Fprintln(out, " update COMMIT: perform an update operation")
fmt.Fprintln(out, " list-updates: display info about update operations")
fmt.Fprintln(out, " list-cves TRIAGE_STATE: display info about CVE records")
fmt.Fprintln(out, " create-issues: create issues for CVEs that need them")
fmt.Fprintln(out, " show ID1 ID2 ...: display CVE records")
fmt.Fprintln(out, "flags:")
flag.PrintDefaults()
}
flag.Parse()
if *githubTokenFile != "" {
data, err := os.ReadFile(*githubTokenFile)
if err != nil {
die("%v", err)
}
cfg.GitHubAccessToken = strings.TrimSpace(string(data))
} else {
cfg.GitHubAccessToken = os.Getenv("VULN_GITHUB_ACCESS_TOKEN")
}
if err := cfg.Validate(); err != nil {
dieWithUsage("%v", err)
}
ctx := event.WithExporter(context.Background(),
event.NewExporter(log.NewLineHandler(os.Stderr), nil))
if img := os.Getenv("DOCKER_IMAGE"); img != "" {
log.Infof(ctx, "running in docker image %s", img)
}
log.Infof(ctx, "config: project=%s, namespace=%s, issueRepo=%s", cfg.Project, cfg.Namespace, cfg.IssueRepo)
var err error
cfg.Store, err = store.NewFireStore(ctx, cfg.Project, cfg.Namespace, "")
if err != nil {
die("firestore: %v", err)
}
if flag.NArg() > 0 {
err = runCommandLine(ctx)
} else {
err = runServer(ctx)
}
if err != nil {
dieWithUsage("%v", err)
}
}
func runServer(ctx context.Context) error {
if os.Getenv("PORT") == "" {
return errors.New("need PORT")
}
if _, err := worker.NewServer(ctx, cfg); err != nil {
return err
}
addr := ":" + os.Getenv("PORT")
log.Infof(ctx, "Listening on addr %s", addr)
return fmt.Errorf("listening: %v", http.ListenAndServe(addr, nil))
}
const timeFormat = "2006/01/02 15:04:05"
func runCommandLine(ctx context.Context) error {
switch flag.Arg(0) {
case "list-updates":
return listUpdatesCommand(ctx)
case "list-cves":
return listCVEsCommand(ctx, flag.Arg(1))
case "update":
if flag.NArg() != 2 {
return errors.New("usage: update COMMIT")
}
return updateCommand(ctx, flag.Arg(1))
case "create-issues":
return createIssuesCommand(ctx)
case "show":
return showCommand(ctx, flag.Args()[1:])
default:
return fmt.Errorf("unknown command: %q", flag.Arg(1))
}
}
func listUpdatesCommand(ctx context.Context) error {
recs, err := cfg.Store.ListCommitUpdateRecords(ctx, 0)
if err != nil {
return err
}
tw := tabwriter.NewWriter(os.Stdout, 1, 8, 2, ' ', 0)
fmt.Fprintf(tw, "Start\tEnd\tCommit\tID\tCVEs Processed\n")
for i, r := range recs {
if *limit > 0 && i >= *limit {
break
}
endTime := "unfinished"
if !r.EndedAt.IsZero() {
endTime = r.EndedAt.In(time.Local).Format(timeFormat)
}
fmt.Fprintf(tw, "%s\t%s\t%s\t%s\t%d/%d (added %d, modified %d)\n",
r.StartedAt.In(time.Local).Format(timeFormat),
endTime,
r.CommitHash,
r.ID,
r.NumProcessed, r.NumTotal, r.NumAdded, r.NumModified)
}
return tw.Flush()
}
func listCVEsCommand(ctx context.Context, triageState string) error {
ts := store.TriageState(triageState)
if err := ts.Validate(); err != nil {
return err
}
crs, err := cfg.Store.ListCVERecordsWithTriageState(ctx, ts)
if err != nil {
return err
}
tw := tabwriter.NewWriter(os.Stdout, 1, 8, 2, ' ', 0)
fmt.Fprintf(tw, "ID\tCVEState\tCommit\tReason\tModule\tIssue\tIssue Created\n")
for i, r := range crs {
if *limit > 0 && i >= *limit {
break
}
fmt.Fprintf(tw, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
r.ID, r.CVEState, r.CommitHash, r.TriageStateReason, r.Module, r.IssueReference, worker.FormatTime(r.IssueCreatedAt))
}
return tw.Flush()
}
func updateCommand(ctx context.Context, commitHash string) error {
repoPath := cvelistrepo.URL
if *localRepoPath != "" {
repoPath = *localRepoPath
}
if *knownModuleFile != "" {
if err := populateKnownModules(*knownModuleFile); err != nil {
return err
}
}
err := worker.UpdateCVEsAtCommit(ctx, repoPath, commitHash, cfg.Store, pkgsiteURL, *force)
if cerr := new(worker.CheckUpdateError); errors.As(err, &cerr) {
return fmt.Errorf("%w; use -force to override", cerr)
}
if err != nil {
return err
}
if cfg.GitHubAccessToken == "" {
fmt.Printf("Missing GitHub access token; not updating GH security advisories.\n")
return nil
}
ghsaClient := ghsa.NewClient(ctx, cfg.GitHubAccessToken)
listSAs := func(ctx context.Context, since time.Time) ([]*ghsa.SecurityAdvisory, error) {
return ghsaClient.List(ctx, since)
}
_, err = worker.UpdateGHSAs(ctx, listSAs, cfg.Store)
return err
}
func populateKnownModules(filename string) error {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
var mods []string
scan := bufio.NewScanner(f)
for scan.Scan() {
line := strings.TrimSpace(scan.Text())
if line == "" || line[0] == '#' {
continue
}
mods = append(mods, line)
}
if err := scan.Err(); err != nil {
return err
}
worker.SetKnownModules(mods)
fmt.Printf("set %d known modules\n", len(mods))
return nil
}
func createIssuesCommand(ctx context.Context) error {
if cfg.IssueRepo == "" {
return errors.New("need -issue-repo")
}
if cfg.GitHubAccessToken == "" {
return errors.New("need -ghtokenfile")
}
owner, repoName, err := gitrepo.ParseGitHubRepo(cfg.IssueRepo)
if err != nil {
return err
}
client := issues.NewClient(ctx, &issues.Config{Owner: owner, Repo: repoName, Token: cfg.GitHubAccessToken})
repo, err := gitrepo.Clone(ctx, "https://github.com/golang/vulndb")
if err != nil {
return err
}
_, allReports, err := report.All(repo)
if err != nil {
return err
}
return worker.CreateIssues(ctx, cfg.Store, client, allReports, *limit)
}
func showCommand(ctx context.Context, ids []string) error {
for _, id := range ids {
r, err := cfg.Store.GetCVERecord(ctx, id)
if err != nil {
return err
}
if r == nil {
fmt.Printf("%s not found\n", id)
} else {
// Display as JSON because it's an easy way to get nice formatting.
j, err := json.MarshalIndent(r, "", "\t")
if err != nil {
return err
}
fmt.Printf("%s\n", j)
}
}
return nil
}
func die(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format, args...)
fmt.Fprintln(os.Stderr)
os.Exit(1)
}
func dieWithUsage(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format, args...)
fmt.Fprintln(os.Stderr)
flag.Usage()
os.Exit(1)
}