|
| 1 | +// Package main runs the exporter, exporting the whole OSV database to the GCS bucket. |
| 2 | +// See the README.md for more details. |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "flag" |
| 9 | + "log/slog" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "strings" |
| 13 | + "sync" |
| 14 | + |
| 15 | + "cloud.google.com/go/storage" |
| 16 | + "github.com/google/osv.dev/go/logger" |
| 17 | + "github.com/ossf/osv-schema/bindings/go/osvschema" |
| 18 | + "google.golang.org/api/iterator" |
| 19 | +) |
| 20 | + |
| 21 | +const gcsProtoPrefix = "all/pb/" |
| 22 | + |
| 23 | +// main is the entry point for the exporter. It initializes the GCS clients, |
| 24 | +// sets up the worker pipeline, and starts the GCS object iteration. |
| 25 | +func main() { |
| 26 | + logger.InitGlobalLogger() |
| 27 | + |
| 28 | + outBucketName := flag.String("bucket", "osv-test-vulnerabilities", "Output bucket or directory name. If -local is true, this is a local path; otherwise, it's a GCS bucket name.") |
| 29 | + vulnBucketName := flag.String("osv_vulns_bucket", os.Getenv("OSV_VULNERABILITIES_BUCKET"), "GCS bucket to read vulnerability protobufs from. Can also be set with the OSV_VULNERABILITIES_BUCKET environment variable.") |
| 30 | + uploadToGCS := flag.Bool("uploadToGCS", false, "If false, writes the output to a local directory specified by -bucket instead of a GCS bucket.") |
| 31 | + numWorkers := flag.Int("num_workers", 200, "The total number of concurrent workers to use for downloading from GCS and writing the output.") |
| 32 | + |
| 33 | + flag.Parse() |
| 34 | + |
| 35 | + logger.Info("exporter starting", |
| 36 | + slog.String("bucket", *outBucketName), |
| 37 | + slog.String("osv_vulns_bucket", *vulnBucketName), |
| 38 | + slog.Bool("uploadToGCS", *uploadToGCS), |
| 39 | + slog.Int("num_workers", *numWorkers)) |
| 40 | + |
| 41 | + if *vulnBucketName == "" { |
| 42 | + logger.Fatal("OSV_VULNERABILITIES_BUCKET must be set") |
| 43 | + } |
| 44 | + |
| 45 | + ctx, cancel := context.WithCancel(context.Background()) |
| 46 | + defer cancel() |
| 47 | + |
| 48 | + cl, err := storage.NewClient(ctx) |
| 49 | + if err != nil { |
| 50 | + logger.Fatal("failed to create storage client", slog.Any("err", err)) |
| 51 | + } |
| 52 | + |
| 53 | + vulnBucket := cl.Bucket(*vulnBucketName) |
| 54 | + var outBucket *storage.BucketHandle |
| 55 | + var outPrefix string |
| 56 | + if *uploadToGCS { |
| 57 | + outBucket = cl.Bucket(*outBucketName) |
| 58 | + } else { |
| 59 | + outPrefix = *outBucketName |
| 60 | + } |
| 61 | + |
| 62 | + // The exporter uses a pipeline of channels and worker pools. The data flow is as follows: |
| 63 | + // 1. The main goroutine lists GCS objects and sends them to `gcsObjToDownloaderCh`. |
| 64 | + // 2. A pool of `downloader` workers receive GCS objects, downloads and unmarshals them into |
| 65 | + // OSV vulnerabilities, and send them to `downloaderToRouterCh`. |
| 66 | + // 3. The `ecosystemRouter` receives vulnerabilities and dispatches them. It creates a new |
| 67 | + // `ecosystemWorker` for each new ecosystem, and sends all vulnerabilities to a single |
| 68 | + // `allEcosystemWorker`. |
| 69 | + // 4. The `ecosystemWorker`s and the `allEcosystemWorker` process the vulnerabilities and |
| 70 | + // generate the final files, sending the data to be written to `routerToWriteCh`. |
| 71 | + // 5. A pool of `writer` workers receive the file data and write it to the output. |
| 72 | + gcsObjToDownloaderCh := make(chan *storage.ObjectHandle) |
| 73 | + downloaderToRouterCh := make(chan *osvschema.Vulnerability) |
| 74 | + routerToWriteCh := make(chan writeMsg) |
| 75 | + |
| 76 | + var downloaderWg sync.WaitGroup |
| 77 | + for range *numWorkers / 2 { |
| 78 | + downloaderWg.Add(1) |
| 79 | + go downloader(ctx, gcsObjToDownloaderCh, downloaderToRouterCh, &downloaderWg) |
| 80 | + } |
| 81 | + |
| 82 | + var writerWg sync.WaitGroup |
| 83 | + for range *numWorkers / 2 { |
| 84 | + writerWg.Add(1) |
| 85 | + go writer(ctx, cancel, routerToWriteCh, outBucket, outPrefix, &writerWg) |
| 86 | + } |
| 87 | + var routerWg sync.WaitGroup |
| 88 | + routerWg.Add(1) |
| 89 | + go ecosystemRouter(ctx, downloaderToRouterCh, routerToWriteCh, &routerWg) |
| 90 | + |
| 91 | + it := vulnBucket.Objects(ctx, &storage.Query{Prefix: gcsProtoPrefix}) |
| 92 | + prevPrefix := "" |
| 93 | +MainLoop: |
| 94 | + for { |
| 95 | + attrs, err := it.Next() |
| 96 | + if errors.Is(err, iterator.Done) { |
| 97 | + break |
| 98 | + } |
| 99 | + if err != nil { |
| 100 | + logger.Fatal("failed to list objects", slog.Any("err", err)) |
| 101 | + } |
| 102 | + // Only log when we see a new ID prefix (i.e. roughly once per data source) |
| 103 | + prefix := filepath.Base(attrs.Name) |
| 104 | + prefix, _, _ = strings.Cut(prefix, "-") |
| 105 | + if prefix != prevPrefix { |
| 106 | + logger.Info("iterating vulnerabilities", slog.String("now_at", attrs.Name)) |
| 107 | + prevPrefix = prefix |
| 108 | + } |
| 109 | + select { |
| 110 | + case gcsObjToDownloaderCh <- vulnBucket.Object(attrs.Name): |
| 111 | + case <-ctx.Done(): |
| 112 | + break MainLoop |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + close(gcsObjToDownloaderCh) |
| 117 | + downloaderWg.Wait() |
| 118 | + close(downloaderToRouterCh) |
| 119 | + routerWg.Wait() |
| 120 | + close(routerToWriteCh) |
| 121 | + writerWg.Wait() |
| 122 | + |
| 123 | + if ctx.Err() != nil { |
| 124 | + logger.Fatal("exporter cancelled") |
| 125 | + } |
| 126 | + logger.Info("export completed successfully") |
| 127 | +} |
| 128 | + |
| 129 | +// ecosystemRouter receives vulnerabilities from inCh and fans them out to the |
| 130 | +// appropriate ecosystemWorker. It creates workers on-demand for each new |
| 131 | +// ecosystem encountered. It also sends every vulnerability to the allEcosystemWorker. |
| 132 | +func ecosystemRouter(ctx context.Context, inCh <-chan *osvschema.Vulnerability, outCh chan<- writeMsg, wg *sync.WaitGroup) { |
| 133 | + defer wg.Done() |
| 134 | + logger.Info("ecosystem router starting") |
| 135 | + workers := make(map[string]*ecosystemWorker) |
| 136 | + var workersWg sync.WaitGroup |
| 137 | + vulnCounter := 0 |
| 138 | + |
| 139 | + allEcosystemWorker := newAllEcosystemWorker(ctx, outCh, &workersWg) |
| 140 | + |
| 141 | + for vuln := range inCh { |
| 142 | + vulnCounter++ |
| 143 | + ecosystems := make(map[string]struct{}) |
| 144 | + for _, aff := range vuln.GetAffected() { |
| 145 | + eco := aff.GetPackage().GetEcosystem() |
| 146 | + eco, _, _ = strings.Cut(eco, ":") |
| 147 | + if eco != "" { |
| 148 | + ecosystems[eco] = struct{}{} |
| 149 | + } |
| 150 | + for _, ref := range aff.GetRanges() { |
| 151 | + if ref.GetType() == osvschema.Range_GIT { |
| 152 | + ecosystems["GIT"] = struct{}{} |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + if len(ecosystems) == 0 { |
| 157 | + ecosystems["[EMPTY]"] = struct{}{} |
| 158 | + } |
| 159 | + ecoNames := make([]string, 0, len(ecosystems)) |
| 160 | + for eco := range ecosystems { |
| 161 | + ecoNames = append(ecoNames, eco) |
| 162 | + worker, ok := workers[eco] |
| 163 | + if !ok { |
| 164 | + worker = newEcosystemWorker(ctx, eco, outCh, &workersWg) |
| 165 | + workers[eco] = worker |
| 166 | + } |
| 167 | + worker.inCh <- vuln |
| 168 | + } |
| 169 | + allEcosystemWorker.inCh <- vulnAndEcos{Vulnerability: vuln, ecosystems: ecoNames} |
| 170 | + } |
| 171 | + |
| 172 | + for _, worker := range workers { |
| 173 | + worker.Finish() |
| 174 | + } |
| 175 | + allEcosystemWorker.Finish() |
| 176 | + workersWg.Wait() |
| 177 | + logger.Info("ecosystem router finished, all vulnerabilities dispatched", slog.Int("total_vulnerabilities", vulnCounter)) |
| 178 | +} |
0 commit comments