Skip to content

Commit 1419661

Browse files
committed
send vuln proto in pubsub data
1 parent b4ca398 commit 1419661

2 files changed

Lines changed: 37 additions & 7 deletions

File tree

go/internal/importer/importer.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"go.opentelemetry.io/otel/propagation"
2525
"go.opentelemetry.io/otel/trace"
2626
"google.golang.org/protobuf/encoding/protojson"
27+
"google.golang.org/protobuf/proto"
2728
"k8s.io/apimachinery/pkg/util/yaml"
2829
)
2930

@@ -311,7 +312,7 @@ func processUpdate(ctx context.Context, config Config, item WorkItem) {
311312
return
312313
}
313314
}
314-
if err := sendToWorker(ctx, config, item, hash, modified); err != nil {
315+
if err := sendToWorker(ctx, config, item, hash, modified, &vulnProto); err != nil {
315316
logger.ErrorContext(ctx, "Failed to send to worker", slog.Any("error", err), slog.String("source", sourceRepoName), slog.String("path", sourcePath))
316317
}
317318
}
@@ -321,23 +322,32 @@ func computeHash(data []byte) string {
321322
return hex.EncodeToString(hash[:])
322323
}
323324

324-
func sendToWorker(ctx context.Context, config Config, item WorkItem, hash string, modifiedTime time.Time) error {
325+
func sendToWorker(ctx context.Context, config Config, item WorkItem, hash string, modifiedTime time.Time, vuln *osvschema.Vulnerability) error {
325326
var srcTimestamp *time.Time
326327
if !item.IsReimport {
327328
// Only track the update latency if we're not doing a reimport of the data
328329
srcTimestamp = &modifiedTime
329330
}
330331

331-
return publishUpdate(ctx, config.Publisher, item.SourceRepository, item.SourcePath, hash, false, srcTimestamp)
332+
return publishUpdate(ctx, config.Publisher, item.SourceRepository, item.SourcePath, hash, false, srcTimestamp, vuln)
332333
}
333334

334335
func sendDeletionToWorker(ctx context.Context, config Config, item WorkItem) error {
335-
return publishUpdate(ctx, config.Publisher, item.SourceRepository, item.SourcePath, "", true, nil)
336+
return publishUpdate(ctx, config.Publisher, item.SourceRepository, item.SourcePath, "", true, nil, nil)
336337
}
337338

338-
func publishUpdate(ctx context.Context, publisher clients.Publisher, source, path, hash string, deleted bool, srcTimestamp *time.Time) error {
339+
func publishUpdate(ctx context.Context, publisher clients.Publisher, source, path, hash string, deleted bool, srcTimestamp *time.Time, vuln *osvschema.Vulnerability) error {
340+
// Send the vulnerability proto in the message data
341+
var data []byte
342+
if vuln != nil {
343+
var err error
344+
data, err = proto.Marshal(vuln)
345+
if err != nil {
346+
return err
347+
}
348+
}
339349
msg := &pubsub.Message{
340-
Data: []byte(""),
350+
Data: data,
341351
Attributes: map[string]string{
342352
"type": "update",
343353
"source": source,

go/internal/importer/importer_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import (
66
"time"
77

88
"github.com/google/osv.dev/go/testutils"
9+
"github.com/ossf/osv-schema/bindings/go/osvschema"
10+
"google.golang.org/protobuf/proto"
11+
"google.golang.org/protobuf/types/known/timestamppb"
912
)
1013

1114
func TestSendToWorker(t *testing.T) {
@@ -21,8 +24,12 @@ func TestSendToWorker(t *testing.T) {
2124
}
2225
hash := "some-hash"
2326
modifiedTime := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
27+
vuln := &osvschema.Vulnerability{
28+
Id: "CVE-2023-1234",
29+
Modified: timestamppb.New(modifiedTime),
30+
}
2431

25-
err := sendToWorker(ctx, config, item, hash, modifiedTime)
32+
err := sendToWorker(ctx, config, item, hash, modifiedTime, vuln)
2633
if err != nil {
2734
t.Errorf("Expected no error, got %v", err)
2835
}
@@ -53,6 +60,19 @@ func TestSendToWorker(t *testing.T) {
5360
if msg.Attributes["src_timestamp"] != "1672531200" {
5461
t.Errorf("Expected src_timestamp=1672531200, got %s", msg.Attributes["src_timestamp"])
5562
}
63+
if len(msg.Data) == 0 {
64+
t.Errorf("Expected vulnerability data to be present")
65+
}
66+
var parsedVuln osvschema.Vulnerability
67+
if err := proto.Unmarshal(msg.Data, &parsedVuln); err != nil {
68+
t.Errorf("Failed to unmarshal vulnerability: %v", err)
69+
}
70+
if parsedVuln.Id != "CVE-2023-1234" {
71+
t.Errorf("Expected vulnerability ID CVE-2023-1234, got %s", parsedVuln.Id)
72+
}
73+
if parsedVuln.Modified.AsTime() != modifiedTime {
74+
t.Errorf("Expected vulnerability modified time %v, got %v", modifiedTime, parsedVuln.Modified.AsTime())
75+
}
5676
}
5777

5878
func TestImporterWorker(t *testing.T) {

0 commit comments

Comments
 (0)