-
Notifications
You must be signed in to change notification settings - Fork 320
feat: migrate to new go worker #5330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
The head ref may contain hidden characters: "\u{1F477}\u{1F645}\u{1F40D}"
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| FROM golang:1.26.2-alpine@sha256:f85330846cde1e57ca9ec309382da3b8e6ae3ab943d2739500e08c86393a21b1 AS build | ||
|
|
||
| WORKDIR /src | ||
|
|
||
| COPY ./go.mod /src/go.mod | ||
| COPY ./go.sum /src/go.sum | ||
| RUN go mod download && go mod verify | ||
|
|
||
|
|
||
| COPY ./ /src/ | ||
| RUN CGO_ENABLED=0 go build -o worker ./cmd/worker | ||
|
|
||
| FROM gcr.io/distroless/static-debian12@sha256:20bc6c0bc4d625a22a8fde3e55f6515709b32055ef8fb9cfbddaa06d1760f838 | ||
|
|
||
| COPY --from=build /src/worker / | ||
|
|
||
| ENTRYPOINT ["/worker"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // Package main implements the OSV worker for ingesting and enriching upstream vulns sent from the importer | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "flag" | ||
| "log/slog" | ||
| "net/http" | ||
| "os" | ||
| "os/signal" | ||
| "strconv" | ||
| "syscall" | ||
| "time" | ||
|
|
||
| "cloud.google.com/go/datastore" | ||
| "cloud.google.com/go/pubsub/v2" | ||
| "cloud.google.com/go/storage" | ||
| db "github.com/google/osv.dev/go/internal/database/datastore" | ||
| "github.com/google/osv.dev/go/internal/worker" | ||
| "github.com/google/osv.dev/go/internal/worker/pipeline/registry" | ||
| "github.com/google/osv.dev/go/logger" | ||
| "github.com/google/osv.dev/go/osv/clients" | ||
| "github.com/google/osv.dev/go/osv/ecosystem" | ||
| ) | ||
|
|
||
| func main() { | ||
| if err := run(); err != nil { | ||
| os.Exit(1) | ||
| } | ||
| } | ||
|
|
||
| func run() error { | ||
| logger.InitGlobalLogger() | ||
| defer logger.Close() | ||
| ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) | ||
| defer stop() | ||
|
|
||
| logger.DebugContext(ctx, "worker starting") | ||
|
|
||
| project := os.Getenv("GOOGLE_CLOUD_PROJECT") | ||
| if project == "" { | ||
| logger.ErrorContext(ctx, "GOOGLE_CLOUD_PROJECT environment variable is not set") | ||
| return errors.New("GOOGLE_CLOUD_PROJECT environment variable is not set") | ||
| } | ||
| gitterHost := os.Getenv("GITTER_HOST") | ||
| if gitterHost == "" { | ||
| logger.ErrorContext(ctx, "GITTER_HOST environment variable is not set") | ||
| return errors.New("GITTER_HOST environment variable is not set") | ||
| } | ||
|
|
||
| numWorkers := flag.Int("num-workers", 10, "Number of workers used to process tasks") | ||
|
|
||
| flag.Parse() | ||
|
|
||
| pubsubSubscription := envOrDefault("PUBSUB_SUBSCRIPTION", "tasks") | ||
| datastoreID := envOrDefault("DATASTORE_DATABASE_ID", "") // empty string is the (default) database | ||
| vulnBucket := envOrDefault("OSV_VULNERABILITIES_BUCKET", "osv-test-vulnerabilities") | ||
| failTasksTopic := envOrDefault("FAILED_TASKS_TOPIC", "failed-tasks") | ||
| notifyPyPI, _ := strconv.ParseBool(envOrDefault("NOTIFY_PYPI", "false")) // returns false on error | ||
|
|
||
| // Plug in all the connections to the engine | ||
| dsClient, err := datastore.NewClientWithDatabase(ctx, project, datastoreID) | ||
| if err != nil { | ||
| logger.ErrorContext(ctx, "Failed to create datastore client", slog.Any("error", err)) | ||
| return err | ||
| } | ||
| defer dsClient.Close() | ||
|
|
||
| gcsClient, err := storage.NewClient(ctx) | ||
| if err != nil { | ||
| logger.ErrorContext(ctx, "Failed to create storage client", slog.Any("error", err)) | ||
| return err | ||
| } | ||
| defer gcsClient.Close() | ||
|
|
||
| psClient, err := pubsub.NewClient(ctx, project) | ||
| if err != nil { | ||
| logger.ErrorContext(ctx, "Failed to create pubsub client", slog.Any("error", err)) | ||
| return err | ||
| } | ||
| defer psClient.Close() | ||
|
|
||
| stores := worker.Stores{ | ||
| SourceRepo: db.NewSourceRepositoryStore(dsClient), | ||
| Vulnerability: db.NewVulnerabilityStore(db.VulnStoreConfig{ | ||
| Client: dsClient, | ||
| GCS: clients.NewGCSClient(gcsClient, vulnBucket), | ||
| FailedWritePublisher: &clients.GCPPublisher{Publisher: psClient.Publisher(failTasksTopic)}, | ||
| }), | ||
| Relations: db.NewRelationsStore(dsClient), | ||
| ImportFindings: db.NewImportFindingsStore(dsClient), | ||
| } | ||
|
|
||
| engine := worker.Engine{ | ||
| Stores: stores, | ||
| Pipeline: registry.List, | ||
|
|
||
| GitterHost: gitterHost, | ||
| GitterClient: &http.Client{Timeout: 1 * time.Hour}, | ||
| EcosystemProvider: ecosystem.DefaultProvider, | ||
| } | ||
|
|
||
| if notifyPyPI { | ||
| engine.NotifyPyPI = true | ||
| engine.Stores.PyPIPublisher = &clients.GCPPublisher{Publisher: psClient.Publisher("pypi-bridge")} | ||
| } | ||
|
|
||
| // Set up and run the subscriber | ||
| sub := psClient.Subscriber(pubsubSubscription) | ||
| sub.ReceiveSettings.MaxOutstandingMessages = *numWorkers | ||
| sub.ReceiveSettings.MaxOutstandingBytes = -1 // no limit - we can give lots of memory to these machines | ||
| sub.ReceiveSettings.MaxExtension = 6 * time.Hour | ||
| sub.ReceiveSettings.MaxDurationPerAckExtension = 10 * time.Minute | ||
| subscriber := worker.Subscriber{ | ||
| Engine: engine, | ||
| PubSubSub: sub, | ||
| } | ||
|
|
||
| return subscriber.Run(ctx) | ||
| } | ||
|
|
||
| // envOrDefault retrieves the value of the environment variable named by the key. | ||
| // If the variable is not present in the environment, it returns the defaultValue. | ||
| func envOrDefault(key, defaultValue string) string { | ||
| if value, exists := os.LookupEnv(key); exists { | ||
| return value | ||
| } | ||
|
|
||
| return defaultValue | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just double checking running this locally works fine? no problems with musl because we are compiling on alpine because CGO is 0?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, running locally seems to work