-
Notifications
You must be signed in to change notification settings - Fork 276
feat: Add related computation to cron
#4704
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{1F46A}"
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "log/slog" | ||
| "slices" | ||
| "time" | ||
|
|
||
| "cloud.google.com/go/datastore" | ||
| "github.com/google/osv.dev/go/logger" | ||
| "github.com/google/osv.dev/go/osv/models" | ||
| "google.golang.org/api/iterator" | ||
| ) | ||
|
|
||
| // computeRelated computes all related groups for the given vulns. | ||
| // `groups` is a map of vuln IDs to their related IDs. | ||
| // `withdrawnVulns` is a map of withdrawn vulns. | ||
| // Returns a map of vuln IDs to their related IDs, with the inverse relation added. | ||
| // `groups` is modified in place. | ||
| func computeRelated(groups map[string][]string, withdrawnVulns map[string]struct{}) map[string][]string { | ||
| // Add the inverse relation of the groups to the map | ||
| for id, group := range groups { | ||
| if _, ok := withdrawnVulns[id]; ok { | ||
| // We want to prevent withdrawn vulns IDs from being added to related groups, | ||
| // if the withdrawn vuln itself references other non-withdrawn vulns. | ||
| // For example: | ||
| // - If A (withdrawn) relates to B (valid), B should NOT list A. | ||
| // - If A (valid) relates to B (withdrawn), B SHOULD list A. | ||
| continue | ||
| } | ||
| for _, related := range group { | ||
| if slices.Contains(groups[related], id) { | ||
| continue | ||
| } | ||
| groups[related] = append(groups[related], id) | ||
| slices.Sort(groups[related]) | ||
| } | ||
| } | ||
|
|
||
| return groups | ||
| } | ||
|
|
||
| func updateRelated(ctx context.Context, cl *datastore.Client, id string, relatedIDs []string, ch chan<- Update) error { | ||
| if len(relatedIDs) == 0 { | ||
| logger.Info("Deleting related group due to no related vulns", slog.String("id", id)) | ||
| if err := cl.Delete(ctx, datastore.NameKey("RelatedGroup", id, nil)); err != nil { | ||
| return err | ||
| } | ||
| ch <- Update{ | ||
| ID: id, | ||
| Timestamp: time.Now().UTC(), | ||
| Field: updateFieldRelated, | ||
| Value: nil, | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| group := models.RelatedGroup{ | ||
| RelatedIDs: relatedIDs, | ||
| Modified: time.Now().UTC(), | ||
| } | ||
| if _, err := cl.Put(ctx, datastore.NameKey("RelatedGroup", id, nil), &group); err != nil { | ||
| return err | ||
| } | ||
| ch <- Update{ | ||
| ID: id, | ||
| Timestamp: group.Modified, | ||
| Field: updateFieldRelated, | ||
| Value: relatedIDs, | ||
| } | ||
|
|
||
another-rex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return nil | ||
| } | ||
|
|
||
| func ComputeRelatedGroups(ctx context.Context, cl *datastore.Client, ch chan<- Update) error { | ||
| // Query for all vulns that have related. | ||
| // It's easier to recompute all groups than to try and figure out which ones | ||
| // need to be recomputed. | ||
| logger.Info("Retrieving vulns for related computation...") | ||
| q := datastore.NewQuery("Vulnerability").FilterField("related_raw", ">", "") | ||
another-rex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| rawRelated := make(map[string][]string) | ||
| withdrawnVulns := make(map[string]struct{}) | ||
| it := cl.Run(ctx, q) | ||
| for { | ||
| var v models.Vulnerability | ||
| _, err := it.Next(&v) | ||
| if errors.Is(err, iterator.Done) { | ||
| break | ||
| } | ||
| if err != nil { | ||
| return fmt.Errorf("failed to iterate vulnerabilities: %w", err) | ||
| } | ||
| if v.IsWithdrawn { | ||
| withdrawnVulns[v.Key.Name] = struct{}{} | ||
| } | ||
| related := slices.Clone(v.RelatedRaw) | ||
| slices.Sort(related) | ||
| related = slices.Compact(related) | ||
| rawRelated[v.Key.Name] = related | ||
| } | ||
| logger.Info("Retrieved vulns with related ids", slog.Int("count", len(rawRelated))) | ||
|
|
||
| logger.Info("Retrieving related groups...") | ||
| q = datastore.NewQuery("RelatedGroup") | ||
| it = cl.Run(ctx, q) | ||
| relatedGroups := make(map[string]models.RelatedGroup) | ||
| for { | ||
| var group models.RelatedGroup | ||
| _, err := it.Next(&group) | ||
| if errors.Is(err, iterator.Done) { | ||
| break | ||
| } | ||
| if err != nil { | ||
| return fmt.Errorf("failed to iterate related groups: %w", err) | ||
| } | ||
| relatedGroups[group.Key.Name] = group | ||
| } | ||
| logger.Info("Related groups successfully retrieved", slog.Int("count", len(relatedGroups))) | ||
|
|
||
| related := computeRelated(rawRelated, withdrawnVulns) | ||
|
|
||
| for id, relatedIDs := range related { | ||
| g, ok := relatedGroups[id] | ||
| delete(relatedGroups, id) | ||
| if !ok || !slices.Equal(g.RelatedIDs, relatedIDs) { | ||
| if err := updateRelated(ctx, cl, id, relatedIDs, ch); err != nil { | ||
| return fmt.Errorf("failed to update related group: %w", err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // The remaining groups in relatedGroups are the ones that are no longer | ||
| // present in the vulns, so we delete them. | ||
| for id := range relatedGroups { | ||
another-rex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err := updateRelated(ctx, cl, id, nil, ch); err != nil { | ||
| return fmt.Errorf("failed to delete related group: %w", err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
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,123 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "slices" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "cloud.google.com/go/datastore" | ||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/google/osv.dev/go/osv/models" | ||
| "github.com/google/osv.dev/go/testutils" | ||
| ) | ||
|
|
||
| func TestComputeRelated(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| groups map[string][]string | ||
| want map[string][]string | ||
| }{ | ||
| { | ||
| name: "Unrelated groups", | ||
| groups: map[string][]string{"A": {"B"}, "C": {"D"}}, | ||
| want: map[string][]string{"A": {"B"}, "B": {"A"}, "C": {"D"}, "D": {"C"}}, | ||
| }, | ||
| { | ||
| name: "Related groups", | ||
| groups: map[string][]string{"A": {"B", "C"}, "B": {"A"}}, | ||
| want: map[string][]string{"A": {"B", "C"}, "B": {"A"}, "C": {"A"}}, | ||
| }, | ||
| { | ||
| name: "Already computed", | ||
| groups: map[string][]string{"A": {"B"}, "B": {"A"}}, | ||
| want: map[string][]string{"A": {"B"}, "B": {"A"}}, | ||
| }, | ||
| { | ||
| name: "Circular", | ||
| groups: map[string][]string{"A": {"B"}, "B": {"C"}, "C": {"A"}}, | ||
| want: map[string][]string{"A": {"B", "C"}, "B": {"A", "C"}, "C": {"A", "B"}}, | ||
| }, | ||
| { | ||
| name: "Empty", | ||
| groups: map[string][]string{}, | ||
| want: map[string][]string{}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got := computeRelated(tt.groups, map[string]struct{}{}) | ||
| if diff := cmp.Diff(tt.want, got); diff != "" { | ||
| t.Errorf("computeRelated() mismatch (-want +got):\n%s", diff) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestComputeRelatedGroups(t *testing.T) { | ||
| ctx := context.Background() | ||
| dsClient := testutils.MustNewDatastoreClientForTesting(t) | ||
|
|
||
| // Setup Datastore | ||
| vulns := []*models.Vulnerability{ | ||
| { | ||
| Key: datastore.NameKey("Vulnerability", "A", nil), | ||
| RelatedRaw: []string{"B"}, | ||
| Modified: time.Now().UTC(), | ||
| }, | ||
| { | ||
| Key: datastore.NameKey("Vulnerability", "B", nil), | ||
| RelatedRaw: []string{"A"}, | ||
| Modified: time.Now().UTC(), | ||
| }, | ||
| { | ||
| Key: datastore.NameKey("Vulnerability", "C", nil), | ||
| RelatedRaw: []string{"A", "D"}, | ||
| Modified: time.Now().UTC(), | ||
| }, | ||
| { | ||
| Key: datastore.NameKey("Vulnerability", "D", nil), | ||
| RelatedRaw: []string{"E"}, // Withdrawn, should be ignored | ||
| Modified: time.Now().UTC(), | ||
| IsWithdrawn: true, | ||
| }, | ||
| } | ||
| keys := make([]*datastore.Key, len(vulns)) | ||
| for i, v := range vulns { | ||
| keys[i] = v.Key | ||
| } | ||
|
|
||
| if _, err := dsClient.PutMulti(ctx, keys, vulns); err != nil { | ||
| t.Fatalf("failed to put vulns: %v", err) | ||
| } | ||
|
|
||
| ch := make(chan Update, 100) | ||
| if err := ComputeRelatedGroups(ctx, dsClient, ch); err != nil { | ||
| t.Fatalf("ComputeRelatedGroups failed: %v", err) | ||
| } | ||
| close(ch) | ||
|
|
||
| // Check results | ||
| var groups []models.RelatedGroup | ||
| if _, err := dsClient.GetAll(ctx, datastore.NewQuery("RelatedGroup"), &groups); err != nil { | ||
| t.Fatalf("failed to get related groups: %v", err) | ||
| } | ||
|
|
||
| expected := map[string][]string{ | ||
| "A": {"B", "C"}, | ||
| "B": {"A"}, | ||
| "C": {"A", "D"}, | ||
| "D": {"C", "E"}, | ||
| } | ||
|
|
||
| got := make(map[string][]string) | ||
| for _, g := range groups { | ||
| slices.Sort(g.RelatedIDs) | ||
| got[g.Key.Name] = g.RelatedIDs | ||
| } | ||
|
|
||
| if diff := cmp.Diff(expected, got); diff != "" { | ||
| t.Errorf("RelatedGroups mismatch (-want +got):\n%s", diff) | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.