Skip to content

Commit 25c6bf4

Browse files
authored
Merge pull request #271 from intelops/sbom-fix
sbom-fix
2 parents b26daf6 + 5b69882 commit 25c6bf4

File tree

6 files changed

+43
-119
lines changed

6 files changed

+43
-119
lines changed

agent/kubviz/trivy_sbom.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ import (
77
"log"
88
"os/exec"
99

10+
"github.com/aquasecurity/trivy/pkg/sbom/cyclonedx"
1011
"github.com/google/uuid"
1112
"github.com/intelops/kubviz/constants"
1213
"github.com/intelops/kubviz/model"
1314
"github.com/nats-io/nats.go"
1415
"k8s.io/client-go/rest"
1516
)
1617

17-
func publishTrivySbomReport(report model.Sbom, js nats.JetStreamContext) error {
18-
metrics := model.Reports{
18+
func publishTrivySbomReport(report cyclonedx.BOM, js nats.JetStreamContext) error {
19+
metrics := model.Sbom{
1920
ID: uuid.New().String(),
2021
Report: report,
2122
}
@@ -67,7 +68,7 @@ func RunTrivySbomScan(config *rest.Config, js nats.JetStreamContext) error {
6768
continue // Move on to the next image
6869
}
6970

70-
var report model.Sbom
71+
var report cyclonedx.BOM
7172
err = json.Unmarshal(out, &report)
7273
if err != nil {
7374
log.Printf("Error unmarshaling JSON data for image sbom %s: %v", image.PullableImage, err)

client/pkg/clickhouse/db_client.go

Lines changed: 27 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type DBInterface interface {
3333
InsertGitEvent(string)
3434
InsertKubeScoreMetrics(model.KubeScoreRecommendations)
3535
InsertTrivyImageMetrics(metrics model.TrivyImage)
36-
InsertTrivySbomMetrics(metrics model.Reports)
36+
InsertTrivySbomMetrics(metrics model.Sbom)
3737
InsertTrivyMetrics(metrics model.Trivy)
3838
RetriveKetallEvent() ([]model.Resource, error)
3939
RetriveOutdatedEvent() ([]model.CheckResultfinal, error)
@@ -600,55 +600,37 @@ func (c *DBClient) InsertTrivyImageMetrics(metrics model.TrivyImage) {
600600

601601
}
602602
}
603-
func (c *DBClient) InsertTrivySbomMetrics(metrics model.Reports) {
603+
func (c *DBClient) InsertTrivySbomMetrics(metrics model.Sbom) {
604604
log.Println("####started inserting value")
605605
result := metrics.Report
606-
tx, err := c.conn.Begin()
607-
if err != nil {
608-
log.Println("error in conn Begin", err)
609-
}
610-
defer tx.Rollback()
611-
stmt, err := tx.Prepare(InsertTrivySbom)
612-
if err != nil {
613-
log.Println("error in prepare", err)
614-
}
615-
defer stmt.Close()
616-
for _, com := range result.Components {
617-
if len(result.Metadata.Tools) == 0 || len(com.Properties) == 0 || len(com.Hashes) == 0 || len(com.Licenses) == 0 {
618-
continue
606+
607+
if result.CycloneDX != nil {
608+
var (
609+
tx, _ = c.conn.Begin()
610+
stmt, _ = tx.Prepare(InsertTrivySbom)
611+
)
612+
if _,err:= stmt.Exec(
613+
metrics.ID,
614+
result.CycloneDX.Metadata.Component.Name,
615+
result.CycloneDX.Metadata.Component.PackageURL,
616+
result.CycloneDX.Metadata.Component.BOMRef,
617+
result.CycloneDX.SerialNumber,
618+
result.CycloneDX.Version,
619+
result.CycloneDX.BOMFormat,
620+
result.CycloneDX.Metadata.Component.Version,
621+
result.CycloneDX.Metadata.Component.MIMEType,
622+
); err!=nil {
623+
log.Fatal(err)
619624
}
620-
for _, depend := range result.Dependencies {
621-
if _, err := stmt.Exec(
622-
metrics.ID,
623-
result.Schema,
624-
result.BomFormat,
625-
result.SpecVersion,
626-
result.SerialNumber,
627-
int32(result.Version),
628-
result.Metadata.Timestamp,
629-
result.Metadata.Tools[0].Vendor,
630-
result.Metadata.Tools[0].Name,
631-
result.Metadata.Tools[0].Version,
632-
com.BomRef,
633-
com.Type,
634-
com.Name,
635-
com.Version,
636-
com.Properties[0].Name,
637-
com.Properties[0].Value,
638-
com.Hashes[0].Alg,
639-
com.Hashes[0].Content,
640-
com.Licenses[0].Expression,
641-
com.Purl,
642-
depend.Ref,
643-
); err != nil {
644-
log.Fatal(err)
645-
}
625+
if err:=tx.Commit();err!=nil {
626+
log.Fatal(err)
646627
}
628+
stmt.Close()
629+
}else {
630+
log.Println("sbom payload not available for db insertion, skipping db insertion")
631+
647632
}
648-
if err := tx.Commit(); err != nil {
649-
log.Fatal(err)
650-
}
651-
log.Println("value inserted")
633+
652634
}
653635
func (c *DBClient) Close() {
654636
_ = c.conn.Close()

client/pkg/clickhouse/statements.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,6 @@ const InsertTrivyVul string = "INSERT INTO trivy_vul (id, cluster_name, namespac
242242
const InsertTrivyImage string = "INSERT INTO trivyimage (id, cluster_name, artifact_name, vul_id, vul_pkg_id, vul_pkg_name, vul_installed_version, vul_fixed_version, vul_title, vul_severity, vul_published_date, vul_last_modified_date) VALUES ( ?, ?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
243243
const InsertTrivyMisconfig string = "INSERT INTO trivy_misconfig (id, cluster_name, namespace, kind, name, misconfig_id, misconfig_avdid, misconfig_type, misconfig_title, misconfig_desc, misconfig_msg, misconfig_query, misconfig_resolution, misconfig_severity, misconfig_status, EventTime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
244244
const InsertAzureContainerPushEvent DBStatement = "INSERT INTO azurecontainerpush (RegistryURL, RepositoryName, Tag, ImageName, Event, Size, SHAID, EventTime) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?)"
245-
const InsertTrivySbom string = "INSERT INTO trivysbom (id, schema, bom_format,spec_version,serial_number, version, metadata_timestamp,metatool_vendor,metatool_name,metatool_version,component_bom_ref,component_type,component_name,component_version,component_property_name,component_property_value,component_hash_alg,component_hash_content,component_license_exp,component_purl,dependency_ref) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
245+
const InsertTrivySbom string = "INSERT INTO trivysbom (id, image_name, package_url, bom_ref, serial_number, version, bom_format, component_version, component_mimetype) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
246246
const InsertQuayContainerPushEvent DBStatement = "INSERT INTO quaycontainerpush (name, repository, nameSpace, dockerURL, homePage, tag, Event, EventTime) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
247247
const InsertJfrogContainerPushEvent DBStatement = "INSERT INTO jfrogcontainerpush (Domain, EventType, RegistryURL, RepositoryName, SHAID, Size, ImageName, Tag, Event, EventTime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"

client/pkg/clients/kubviz_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (n *NATSContext) SubscribeAllKubvizNats(conn clickhouse.DBInterface) {
118118
Consumer: constants.Trivy_Sbom_Consumer,
119119
Handler: func(msg *nats.Msg) {
120120
msg.Ack()
121-
var metrics model.Reports
121+
var metrics model.Sbom
122122
err := json.Unmarshal(msg.Data, &metrics)
123123
if err != nil {
124124
log.Println("failed to unmarshal in nats", err)

model/trivy_sbom.go

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,12 @@
11
package model
22

33
import (
4-
"time"
4+
"github.com/aquasecurity/trivy/pkg/sbom/cyclonedx"
55
)
66

7-
type Reports struct {
7+
type Sbom struct {
88
ID string
9-
Report Sbom
9+
Report cyclonedx.BOM
1010
}
1111

12-
type Sbom struct {
13-
Schema string `json:"$schema"`
14-
BomFormat string `json:"bomFormat"`
15-
SpecVersion string `json:"specVersion"`
16-
SerialNumber string `json:"serialNumber"`
17-
Version int `json:"version"`
18-
Metadata struct {
19-
Timestamp time.Time `json:"timestamp"`
20-
Tools []struct {
21-
Vendor string `json:"vendor"`
22-
Name string `json:"name"`
23-
Version string `json:"version"`
24-
} `json:"tools"`
25-
Component struct {
26-
BomRef string `json:"bom-ref"`
27-
Type string `json:"type"`
28-
Name string `json:"name"`
29-
Purl string `json:"purl"`
30-
Properties []struct {
31-
Name string `json:"name"`
32-
Value string `json:"value"`
33-
} `json:"properties"`
34-
} `json:"component"`
35-
} `json:"metadata"`
36-
Components []struct {
37-
BomRef string `json:"bom-ref"`
38-
Type string `json:"type"`
39-
Name string `json:"name"`
40-
Version string `json:"version"`
41-
Properties []struct {
42-
Name string `json:"name"`
43-
Value string `json:"value"`
44-
} `json:"properties"`
45-
Hashes []struct {
46-
Alg string `json:"alg"`
47-
Content string `json:"content"`
48-
} `json:"hashes,omitempty"`
49-
Licenses []struct {
50-
Expression string `json:"expression"`
51-
} `json:"licenses,omitempty"`
52-
Purl string `json:"purl,omitempty"`
53-
} `json:"components"`
54-
Dependencies []struct {
55-
Ref string `json:"ref"`
56-
DependsOn []string `json:"dependsOn"`
57-
} `json:"dependencies"`
58-
Vulnerabilities []interface{} `json:"vulnerabilities"`
59-
}
12+

sql/0000015_trivysbom.up.sql

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
11
CREATE TABLE IF NOT EXISTS trivysbom (
22
id UUID,
3-
schema String,
4-
bom_format String,
5-
spec_version String,
3+
image_name String,
4+
package_url String,
5+
bom_ref String,
66
serial_number String,
7-
version INTEGER,
8-
metadata_timestamp DateTime('UTC'),
9-
metatool_vendor String,
10-
metatool_name String,
11-
metatool_version String,
12-
component_bom_ref String,
13-
component_type String,
14-
component_name String,
7+
version INTEGER
8+
bom_format String,
159
component_version String,
16-
component_property_name String,
17-
component_property_value String,
18-
component_hash_alg String,
19-
component_hash_content String,
20-
component_license_exp String,
21-
component_purl String,
22-
dependency_ref String,
10+
component_mime_type String,
2311
ExpiryDate DateTime DEFAULT now() + INTERVAL {{.TTLValue}} {{.TTLUnit}}
2412
) ENGINE = MergeTree()
2513
ORDER BY ExpiryDate

0 commit comments

Comments
 (0)