|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "log/slog" |
| 8 | + "slices" |
| 9 | + "time" |
| 10 | + |
| 11 | + "cloud.google.com/go/datastore" |
| 12 | + "github.com/google/osv.dev/go/logger" |
| 13 | + "github.com/google/osv.dev/go/osv/models" |
| 14 | + "google.golang.org/api/iterator" |
| 15 | +) |
| 16 | + |
| 17 | +// computeRelated computes all related groups for the given vulns. |
| 18 | +// `groups` is a map of vuln IDs to their related IDs. |
| 19 | +// `withdrawnVulns` is a map of withdrawn vulns. |
| 20 | +// Returns a map of vuln IDs to their related IDs, with the inverse relation added. |
| 21 | +// `groups` is modified in place. |
| 22 | +func computeRelated(groups map[string][]string, withdrawnVulns map[string]struct{}) map[string][]string { |
| 23 | + // Add the inverse relation of the groups to the map |
| 24 | + for id, group := range groups { |
| 25 | + if _, ok := withdrawnVulns[id]; ok { |
| 26 | + // We want to prevent withdrawn vulns IDs from being added to related groups, |
| 27 | + // if the withdrawn vuln itself references other non-withdrawn vulns. |
| 28 | + // For example: |
| 29 | + // - If A (withdrawn) relates to B (valid), B should NOT list A. |
| 30 | + // - If A (valid) relates to B (withdrawn), B SHOULD list A. |
| 31 | + continue |
| 32 | + } |
| 33 | + for _, related := range group { |
| 34 | + if slices.Contains(groups[related], id) { |
| 35 | + continue |
| 36 | + } |
| 37 | + groups[related] = append(groups[related], id) |
| 38 | + slices.Sort(groups[related]) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return groups |
| 43 | +} |
| 44 | + |
| 45 | +func updateRelated(ctx context.Context, cl *datastore.Client, id string, relatedIDs []string, ch chan<- Update) error { |
| 46 | + if len(relatedIDs) == 0 { |
| 47 | + logger.Info("Deleting related group due to no related vulns", slog.String("id", id)) |
| 48 | + if err := cl.Delete(ctx, datastore.NameKey("RelatedGroup", id, nil)); err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + ch <- Update{ |
| 52 | + ID: id, |
| 53 | + Timestamp: time.Now().UTC(), |
| 54 | + Field: updateFieldRelated, |
| 55 | + Value: nil, |
| 56 | + } |
| 57 | + |
| 58 | + return nil |
| 59 | + } |
| 60 | + |
| 61 | + group := models.RelatedGroup{ |
| 62 | + RelatedIDs: relatedIDs, |
| 63 | + Modified: time.Now().UTC(), |
| 64 | + } |
| 65 | + if _, err := cl.Put(ctx, datastore.NameKey("RelatedGroup", id, nil), &group); err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + ch <- Update{ |
| 69 | + ID: id, |
| 70 | + Timestamp: group.Modified, |
| 71 | + Field: updateFieldRelated, |
| 72 | + Value: relatedIDs, |
| 73 | + } |
| 74 | + |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +func ComputeRelatedGroups(ctx context.Context, cl *datastore.Client, ch chan<- Update) error { |
| 79 | + // Query for all vulns that have related. |
| 80 | + // It's easier to recompute all groups than to try and figure out which ones |
| 81 | + // need to be recomputed. |
| 82 | + logger.Info("Retrieving vulns for related computation...") |
| 83 | + q := datastore.NewQuery("Vulnerability").FilterField("related_raw", ">", "") |
| 84 | + |
| 85 | + rawRelated := make(map[string][]string) |
| 86 | + withdrawnVulns := make(map[string]struct{}) |
| 87 | + it := cl.Run(ctx, q) |
| 88 | + for { |
| 89 | + var v models.Vulnerability |
| 90 | + _, err := it.Next(&v) |
| 91 | + if errors.Is(err, iterator.Done) { |
| 92 | + break |
| 93 | + } |
| 94 | + if err != nil { |
| 95 | + return fmt.Errorf("failed to iterate vulnerabilities: %w", err) |
| 96 | + } |
| 97 | + if v.IsWithdrawn { |
| 98 | + withdrawnVulns[v.Key.Name] = struct{}{} |
| 99 | + } |
| 100 | + related := slices.Clone(v.RelatedRaw) |
| 101 | + slices.Sort(related) |
| 102 | + related = slices.Compact(related) |
| 103 | + rawRelated[v.Key.Name] = related |
| 104 | + } |
| 105 | + logger.Info("Retrieved vulns with related ids", slog.Int("count", len(rawRelated))) |
| 106 | + |
| 107 | + logger.Info("Retrieving related groups...") |
| 108 | + q = datastore.NewQuery("RelatedGroup") |
| 109 | + it = cl.Run(ctx, q) |
| 110 | + relatedGroups := make(map[string]models.RelatedGroup) |
| 111 | + for { |
| 112 | + var group models.RelatedGroup |
| 113 | + _, err := it.Next(&group) |
| 114 | + if errors.Is(err, iterator.Done) { |
| 115 | + break |
| 116 | + } |
| 117 | + if err != nil { |
| 118 | + return fmt.Errorf("failed to iterate related groups: %w", err) |
| 119 | + } |
| 120 | + relatedGroups[group.Key.Name] = group |
| 121 | + } |
| 122 | + logger.Info("Related groups successfully retrieved", slog.Int("count", len(relatedGroups))) |
| 123 | + |
| 124 | + related := computeRelated(rawRelated, withdrawnVulns) |
| 125 | + |
| 126 | + for id, relatedIDs := range related { |
| 127 | + g, ok := relatedGroups[id] |
| 128 | + delete(relatedGroups, id) |
| 129 | + if !ok || !slices.Equal(g.RelatedIDs, relatedIDs) { |
| 130 | + if err := updateRelated(ctx, cl, id, relatedIDs, ch); err != nil { |
| 131 | + return fmt.Errorf("failed to update related group: %w", err) |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + // The remaining groups in relatedGroups are the ones that are no longer |
| 137 | + // present in the vulns, so we delete them. |
| 138 | + for id := range relatedGroups { |
| 139 | + if err := updateRelated(ctx, cl, id, nil, ch); err != nil { |
| 140 | + return fmt.Errorf("failed to delete related group: %w", err) |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return nil |
| 145 | +} |
0 commit comments