Skip to content

Commit

Permalink
clientfix
Browse files Browse the repository at this point in the history
  • Loading branch information
an1l4 committed Feb 28, 2024
1 parent 88e6c1c commit 0676feb
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 24 deletions.
2 changes: 1 addition & 1 deletion agent/kubviz/plugins/trivy/trivy_sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func PublishTrivySbomReport(report map[string]interface{}, js nats.JetStreamCont
}
log.Printf("Trivy sbom report with Id %v has been published\n", metrics.ID)
log.Printf("Trivy sbom report with Id %v has been published\n", metrics.ClusterName)
log.Printf("Trivy sbom report with Id %v has been published\n", metrics.Report)
//log.Printf("Trivy sbom report with Id %v has been published\n", metrics.Report)

return nil
}
Expand Down
56 changes: 49 additions & 7 deletions client/pkg/clickhouse/db_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,16 +857,58 @@ func (c *DBClient) InsertTrivySbomMetrics(metrics model.Sbom) {
log.Fatalf("error preparing statement: %v", err)
}

data := metrics.Report

bomComponents, ok := data["components"].([]interface{})
if !ok {
log.Println("error: components not found or not in expected format")
}
var componentName, bomRef, serialNumber, bomFormat, purl, componentType string
var version int32

// Iterate over the components to find the desired name
for _, component := range bomComponents {
componentMap, ok := component.(map[string]interface{})
if !ok {
log.Println("error: component not in expected format")
continue
}
if name, ok := componentMap["name"].(string); ok {
componentName = name
break
}
}

metadata, ok := data["metadata"].(map[string]interface{})
if !ok {
log.Println("error: metadata not found or not in expected format")
return
}

component, ok := metadata["component"].(map[string]interface{})
if !ok {
log.Println("error: component not found or not in expected format")
return
}

bomRef, _ = component["bom-ref"].(string)
purl, _ = component["purl"].(string)
componentType, _ = component["type"].(string)

serialNumber, _ = data["serialNumber"].(string)
version, _ = data["version"].(int32)
bomFormat, _ = data["bomFormat"].(string)

if _, err := stmt.Exec(
metrics.ID,
metrics.ClusterName,
// metrics.ComponentName,
// metrics.PackageName,
// metrics.PackageUrl,
// metrics.BomRef,
// metrics.SerialNumber,
// int32(metrics.CycloneDxVersion),
// metrics.BomFormat,
componentName,
componentType,
purl,
bomRef,
serialNumber,
version,
bomFormat,
); err != nil {
log.Fatal(err)
}
Expand Down
32 changes: 16 additions & 16 deletions client/pkg/clients/kubviz_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,22 +146,22 @@ func (n *NATSContext) SubscribeAllKubvizNats(conn clickhouse.DBInterface) {
log.Println()
},
},
// {
// Subject: constants.TRIVY_SBOM_SUBJECT,
// Consumer: cfg.TrivySbomConsumer,
// Handler: func(msg *nats.Msg) {
// msg.Ack()
// var metrics model.SbomData
// err := json.Unmarshal(msg.Data, &metrics)
// if err != nil {
// log.Println("failed to unmarshal from nats", err)
// return
// }
// log.Printf("Trivy sbom Metrics Received: %#v,", metrics)
// conn.InsertTrivySbomMetrics(metrics)
// log.Println()
// },
// },
{
Subject: constants.TRIVY_SBOM_SUBJECT,
Consumer: cfg.TrivySbomConsumer,
Handler: func(msg *nats.Msg) {
msg.Ack()
var metrics model.Sbom
err := json.Unmarshal(msg.Data, &metrics)
if err != nil {
log.Println("failed to unmarshal from nats", err)
return
}
log.Printf("Trivy sbom Metrics Received: %#v,", metrics)
conn.InsertTrivySbomMetrics(metrics)
log.Println()
},
},
{
Subject: constants.KubvizSubject,
Consumer: cfg.KubvizConsumer,
Expand Down

0 comments on commit 0676feb

Please sign in to comment.