Skip to content

Commit

Permalink
Merge pull request #286 from intelops/error-handle
Browse files Browse the repository at this point in the history
error-handle
  • Loading branch information
vijeyash1 authored Nov 30, 2023
2 parents 95b3b16 + 59cc077 commit a61956c
Showing 1 changed file with 172 additions and 65 deletions.
237 changes: 172 additions & 65 deletions client/pkg/clickhouse/db_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,19 @@ func NewDBClient(conf *config.Config) (DBInterface, error) {
}

func (c *DBClient) InsertContainerEventAzure(pushEvent model.AzureContainerPushEventPayload) {
var (
tx, _ = c.conn.Begin()
stmt, _ = tx.Prepare(string(InsertAzureContainerPushEvent))
)

tx, err := c.conn.Begin()
if err != nil {
log.Printf("error beginning transaction: %v", err)
return
}

stmt, err := tx.Prepare(string(InsertAzureContainerPushEvent))
if err != nil {
log.Printf("error preparing statement: %v", err)
return
}

defer stmt.Close()

currentTime := time.Now().UTC()
Expand Down Expand Up @@ -176,10 +185,18 @@ func (c *DBClient) InsertContainerEventAzure(pushEvent model.AzureContainerPushE
}

func (c *DBClient) InsertContainerEventQuay(pushEvent model.QuayImagePushPayload) {
var (
tx, _ = c.conn.Begin()
stmt, _ = tx.Prepare(string(InsertQuayContainerPushEvent))
)
tx, err := c.conn.Begin()
if err != nil {
log.Printf("error beginning transaction: %v", err)
return
}

stmt, err := tx.Prepare(string(InsertQuayContainerPushEvent))
if err != nil {
log.Printf("error preparing statement: %v", err)
return
}

defer stmt.Close()

currentTime := time.Now().UTC()
Expand Down Expand Up @@ -221,10 +238,18 @@ func (c *DBClient) InsertContainerEventQuay(pushEvent model.QuayImagePushPayload
}

func (c *DBClient) InsertContainerEventJfrog(pushEvent model.JfrogContainerPushEventPayload) {
var (
tx, _ = c.conn.Begin()
stmt, _ = tx.Prepare(string(InsertJfrogContainerPushEvent))
)
tx, err := c.conn.Begin()
if err != nil {
log.Printf("error beginning transaction: %v", err)
return
}

stmt, err := tx.Prepare(string(InsertJfrogContainerPushEvent))
if err != nil {
log.Printf("error preparing statement: %v", err)
return
}

defer stmt.Close()

currentTime := time.Now().UTC()
Expand Down Expand Up @@ -266,10 +291,17 @@ func (c *DBClient) InsertContainerEventJfrog(pushEvent model.JfrogContainerPushE
}

func (c *DBClient) InsertRakeesMetrics(metrics model.RakeesMetrics) {
var (
tx, _ = c.conn.Begin()
stmt, _ = tx.Prepare(string(InsertRakees))
)
tx, err := c.conn.Begin()
if err != nil {
log.Printf("error beginning transaction: %v", err)
return
}
stmt, err := tx.Prepare(string(InsertRakees))
if err != nil {
log.Printf("error preparing statement: %v", err)
return
}

defer stmt.Close()

currentTime := time.Now().UTC()
Expand All @@ -291,10 +323,17 @@ func (c *DBClient) InsertRakeesMetrics(metrics model.RakeesMetrics) {
}

func (c *DBClient) InsertKetallEvent(metrics model.Resource) {
var (
tx, _ = c.conn.Begin()
stmt, _ = tx.Prepare(string(InsertKetall))
)
tx, err := c.conn.Begin()
if err != nil {
log.Printf("error beginning transaction: %v", err)
return
}
stmt, err := tx.Prepare(string(InsertKetall))
if err != nil {
log.Printf("error preparing statement: %v", err)
return
}

defer stmt.Close()

currentTime := time.Now().UTC()
Expand All @@ -315,10 +354,17 @@ func (c *DBClient) InsertKetallEvent(metrics model.Resource) {
}

func (c *DBClient) InsertOutdatedEvent(metrics model.CheckResultfinal) {
var (
tx, _ = c.conn.Begin()
stmt, _ = tx.Prepare(string(InsertOutdated))
)
tx, err := c.conn.Begin()
if err != nil {
log.Printf("error beginning transaction: %v", err)
return
}
stmt, err := tx.Prepare(string(InsertOutdated))
if err != nil {
log.Printf("error preparing statement: %v", err)
return
}

defer stmt.Close()

currentTime := time.Now().UTC()
Expand All @@ -341,10 +387,17 @@ func (c *DBClient) InsertOutdatedEvent(metrics model.CheckResultfinal) {
}

func (c *DBClient) InsertDeprecatedAPI(deprecatedAPI model.DeprecatedAPI) {
var (
tx, _ = c.conn.Begin()
stmt, _ = tx.Prepare(string(InsertDepricatedApi))
)
tx, err := c.conn.Begin()
if err != nil {
log.Printf("error beginning transaction: %v", err)
return
}
stmt, err := tx.Prepare(string(InsertDepricatedApi))
if err != nil {
log.Printf("error preparing statement: %v", err)
return
}

defer stmt.Close()

deprecated := uint8(0)
Expand Down Expand Up @@ -374,10 +427,17 @@ func (c *DBClient) InsertDeprecatedAPI(deprecatedAPI model.DeprecatedAPI) {
}

func (c *DBClient) InsertDeletedAPI(deletedAPI model.DeletedAPI) {
var (
tx, _ = c.conn.Begin()
stmt, _ = tx.Prepare(string(InsertDeletedApi))
)
tx, err := c.conn.Begin()
if err != nil {
log.Printf("error beginning transaction: %v", err)
return
}
stmt, err := tx.Prepare(string(InsertDeletedApi))
if err != nil {
log.Printf("error preparing statement: %v", err)
return
}

defer stmt.Close()
deleted := uint8(0)
if deletedAPI.Deleted {
Expand Down Expand Up @@ -408,10 +468,17 @@ func (c *DBClient) InsertDeletedAPI(deletedAPI model.DeletedAPI) {
}

func (c *DBClient) InsertKubvizEvent(metrics model.Metrics) {
var (
tx, _ = c.conn.Begin()
stmt, _ = tx.Prepare(string(InsertKubvizEvent))
)
tx, err := c.conn.Begin()
if err != nil {
log.Printf("error beginning transaction: %v", err)
return
}
stmt, err := tx.Prepare(string(InsertKubvizEvent))
if err != nil {
log.Printf("error preparing statement: %v", err)
return
}

defer stmt.Close()
eventJson, _ := json.Marshal(metrics.Event)
formattedFirstTimestamp := metrics.Event.FirstTimestamp.Time.UTC().Format("2006-01-02 15:04:05")
Expand Down Expand Up @@ -470,10 +537,16 @@ func (c *DBClient) InsertContainerEvent(event string) {
}

func (c *DBClient) InsertKubeScoreMetrics(metrics model.KubeScoreRecommendations) {
var (
tx, _ = c.conn.Begin()
stmt, _ = tx.Prepare(InsertKubeScore)
)
tx, err := c.conn.Begin()
if err != nil {
log.Printf("error beginning transaction: %v", err)
return
}
stmt, err := tx.Prepare(InsertKubeScore)
if err != nil {
log.Printf("error preparing statement: %v", err)
return
}
defer stmt.Close()

currentTime := time.Now().UTC()
Expand All @@ -496,10 +569,16 @@ func (c *DBClient) InsertTrivyMetrics(metrics model.Trivy) {
for _, finding := range metrics.Report.Findings {
for _, result := range finding.Results {
for _, vulnerability := range result.Vulnerabilities {
var (
tx, _ = c.conn.Begin()
stmt, _ = tx.Prepare(InsertTrivyVul)
)
tx, err := c.conn.Begin()
if err != nil {
log.Printf("error beginning transaction: %v", err)
return
}
stmt, err := tx.Prepare(InsertTrivyVul)
if err != nil {
log.Printf("error preparing statement: %v", err)
return
}
if _, err := stmt.Exec(
metrics.ID,
metrics.ClusterName,
Expand Down Expand Up @@ -527,10 +606,17 @@ func (c *DBClient) InsertTrivyMetrics(metrics model.Trivy) {
}

for _, misconfiguration := range result.Misconfigurations {
var (
tx, _ = c.conn.Begin()
stmt, _ = tx.Prepare(InsertTrivyMisconfig)
)
tx, err := c.conn.Begin()
if err != nil {
log.Printf("error beginning transaction: %v", err)
return
}
stmt, err := tx.Prepare(InsertTrivyMisconfig)
if err != nil {
log.Printf("error preparing statement: %v", err)
return
}

defer stmt.Close()

currentTime := time.Now().UTC()
Expand Down Expand Up @@ -566,10 +652,17 @@ func (c *DBClient) InsertTrivyMetrics(metrics model.Trivy) {
func (c *DBClient) InsertTrivyImageMetrics(metrics model.TrivyImage) {
for _, result := range metrics.Report.Results {
for _, vulnerability := range result.Vulnerabilities {
var (
tx, _ = c.conn.Begin()
stmt, _ = tx.Prepare(InsertTrivyImage)
)
tx, err := c.conn.Begin()
if err != nil {
log.Printf("error beginning transaction: %v", err)
return
}
stmt, err := tx.Prepare(InsertTrivyImage)
if err != nil {
log.Printf("error preparing statement: %v", err)
return
}

if _, err := stmt.Exec(
metrics.ID,
metrics.ClusterName,
Expand Down Expand Up @@ -605,11 +698,18 @@ func (c *DBClient) InsertTrivySbomMetrics(metrics model.Sbom) {
result := metrics.Report

if result.CycloneDX != nil {
var (
tx, _ = c.conn.Begin()
stmt, _ = tx.Prepare(InsertTrivySbom)
)
if _,err:= stmt.Exec(
tx, err := c.conn.Begin()
if err != nil {
log.Printf("error beginning transaction: %v", err)
return
}
stmt, err := tx.Prepare(InsertTrivySbom)
if err != nil {
log.Printf("error preparing statement: %v", err)
return
}

if _, err := stmt.Exec(
metrics.ID,
result.CycloneDX.Metadata.Component.Name,
result.CycloneDX.Metadata.Component.PackageURL,
Expand All @@ -619,18 +719,18 @@ func (c *DBClient) InsertTrivySbomMetrics(metrics model.Sbom) {
result.CycloneDX.BOMFormat,
result.CycloneDX.Metadata.Component.Version,
result.CycloneDX.Metadata.Component.MIMEType,
); err!=nil {
); err != nil {
log.Fatal(err)
}
if err:=tx.Commit();err!=nil {
if err := tx.Commit(); err != nil {
log.Fatal(err)
}
stmt.Close()
}else {
} else {
log.Println("sbom payload not available for db insertion, skipping db insertion")

}

}
func (c *DBClient) Close() {
_ = c.conn.Close()
Expand Down Expand Up @@ -732,10 +832,17 @@ func (c *DBClient) RetrieveKubvizEvent() ([]model.DbEvent, error) {
}

func (c *DBClient) InsertContainerEventDockerHub(build model.DockerHubBuild) {
var (
tx, _ = c.conn.Begin()
stmt, _ = tx.Prepare(string(InsertDockerHubBuild))
)
tx, err := c.conn.Begin()
if err != nil {
log.Printf("error beginning transaction: %v", err)
return
}
stmt, err := tx.Prepare(string(InsertDockerHubBuild))

if err != nil {
log.Printf("error preparing statement: %v", err)
return
}
defer stmt.Close()

currentTime := time.Now().UTC()
Expand Down

0 comments on commit a61956c

Please sign in to comment.