|
| 1 | +package helper |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "os/exec" |
| 7 | + "runtime" |
| 8 | + "slices" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/google/osv-scanner/pkg/reporter" |
| 13 | + "github.com/urfave/cli/v2" |
| 14 | +) |
| 15 | + |
| 16 | +// flags that require network access and values to disable them. |
| 17 | +var OfflineFlags = map[string]string{ |
| 18 | + "skip-git": "true", |
| 19 | + "experimental-offline-vulnerabilities": "true", |
| 20 | + "experimental-no-resolve": "true", |
| 21 | + "experimental-licenses-summary": "false", |
| 22 | + // "experimental-licenses": "", // StringSliceFlag has to be manually cleared. |
| 23 | +} |
| 24 | + |
| 25 | +var GlobalScanFlags = []cli.Flag{ |
| 26 | + &cli.StringFlag{ |
| 27 | + Name: "config", |
| 28 | + Usage: "set/override config file", |
| 29 | + TakesFile: true, |
| 30 | + }, |
| 31 | + &cli.StringFlag{ |
| 32 | + Name: "format", |
| 33 | + Aliases: []string{"f"}, |
| 34 | + Usage: "sets the output format; value can be: " + strings.Join(reporter.Format(), ", "), |
| 35 | + Value: "table", |
| 36 | + Action: func(_ *cli.Context, s string) error { |
| 37 | + if slices.Contains(reporter.Format(), s) { |
| 38 | + return nil |
| 39 | + } |
| 40 | + |
| 41 | + return fmt.Errorf("unsupported output format \"%s\" - must be one of: %s", s, strings.Join(reporter.Format(), ", ")) |
| 42 | + }, |
| 43 | + }, |
| 44 | + &cli.BoolFlag{ |
| 45 | + Name: "serve", |
| 46 | + Usage: "output as HTML result and serve it locally", |
| 47 | + }, |
| 48 | + &cli.StringFlag{ |
| 49 | + Name: "output", |
| 50 | + Usage: "saves the result to the given file path", |
| 51 | + TakesFile: true, |
| 52 | + }, |
| 53 | + &cli.StringFlag{ |
| 54 | + Name: "verbosity", |
| 55 | + Usage: "specify the level of information that should be provided during runtime; value can be: " + strings.Join(reporter.VerbosityLevels(), ", "), |
| 56 | + Value: "info", |
| 57 | + }, |
| 58 | + &cli.BoolFlag{ |
| 59 | + Name: "experimental-offline", |
| 60 | + Usage: "run in offline mode, disabling any features requiring network access", |
| 61 | + Action: func(ctx *cli.Context, b bool) error { |
| 62 | + if !b { |
| 63 | + return nil |
| 64 | + } |
| 65 | + // Disable the features requiring network access. |
| 66 | + for flag, value := range OfflineFlags { |
| 67 | + // TODO(michaelkedar): do something if the flag was already explicitly set. |
| 68 | + if err := ctx.Set(flag, value); err != nil { |
| 69 | + panic(fmt.Sprintf("failed setting offline flag %s to %s: %v", flag, value, err)) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return nil |
| 74 | + }, |
| 75 | + }, |
| 76 | + &cli.BoolFlag{ |
| 77 | + Name: "experimental-offline-vulnerabilities", |
| 78 | + Usage: "checks for vulnerabilities using local databases that are already cached", |
| 79 | + }, |
| 80 | + &cli.BoolFlag{ |
| 81 | + Name: "experimental-download-offline-databases", |
| 82 | + Usage: "downloads vulnerability databases for offline comparison", |
| 83 | + }, |
| 84 | + &cli.BoolFlag{ |
| 85 | + Name: "experimental-no-resolve", |
| 86 | + Usage: "disable transitive dependency resolution of manifest files", |
| 87 | + }, |
| 88 | + &cli.StringFlag{ |
| 89 | + Name: "experimental-local-db-path", |
| 90 | + Usage: "sets the path that local databases should be stored", |
| 91 | + Hidden: true, |
| 92 | + }, |
| 93 | + &cli.BoolFlag{ |
| 94 | + Name: "experimental-all-packages", |
| 95 | + Usage: "when json output is selected, prints all packages", |
| 96 | + }, |
| 97 | + &cli.BoolFlag{ |
| 98 | + Name: "experimental-licenses-summary", |
| 99 | + Usage: "report a license summary, implying the --experimental-all-packages flag", |
| 100 | + }, |
| 101 | + &cli.StringSliceFlag{ |
| 102 | + Name: "experimental-licenses", |
| 103 | + Usage: "report on licenses based on an allowlist", |
| 104 | + }, |
| 105 | +} |
| 106 | + |
| 107 | +// openHTML opens the outputted HTML file. |
| 108 | +func OpenHTML(r reporter.Reporter, outputPath string) { |
| 109 | + // Open the outputted HTML file in the default browser. |
| 110 | + r.Infof("Opening %s...\n", outputPath) |
| 111 | + var err error |
| 112 | + switch runtime.GOOS { |
| 113 | + case "linux": |
| 114 | + err = exec.Command("xdg-open", outputPath).Start() |
| 115 | + case "windows": |
| 116 | + err = exec.Command("start", "", outputPath).Start() |
| 117 | + case "darwin": // macOS |
| 118 | + err = exec.Command("open", outputPath).Start() |
| 119 | + default: |
| 120 | + r.Infof("Unsupported OS.\n") |
| 121 | + } |
| 122 | + |
| 123 | + if err != nil { |
| 124 | + r.Errorf("Failed to open: %s.\n Please manually open the outputted HTML file: %s\n", err, outputPath) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// ServeHTML serves the single HTML file for remote accessing. |
| 129 | +// The program will keep running to serve the HTML report on localhost |
| 130 | +// until the user manually terminates it (e.g. using Ctrl+C). |
| 131 | +func ServeHTML(r reporter.Reporter, outputPath string) { |
| 132 | + servePort := "8000" |
| 133 | + localhostURL := fmt.Sprintf("http://localhost:%s/", servePort) |
| 134 | + r.Infof("Serving HTML report at %s.\nIf you are accessing remotely, use the following SSH command:\n`ssh -L local_port:destination_server_ip:%s ssh_server_hostname`\n", localhostURL, servePort) |
| 135 | + server := &http.Server{ |
| 136 | + Addr: ":" + servePort, |
| 137 | + Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 138 | + http.ServeFile(w, r, outputPath) |
| 139 | + }), |
| 140 | + ReadHeaderTimeout: 3 * time.Second, |
| 141 | + } |
| 142 | + if err := server.ListenAndServe(); err != nil { |
| 143 | + r.Errorf("Failed to start server: %v\n", err) |
| 144 | + } |
| 145 | +} |
0 commit comments