Skip to content

Commit

Permalink
Handle enrichment DB entries without CVEs (#141)
Browse files Browse the repository at this point in the history
Entries in the enrichment DB added before the change to record CVEs have NULL values in the CVE column. This is not handled by the string type, resulting in errors trying to convert existing DB issues to enriched issues. This PR changes the CVE field in the DB issue struct to be sql.NullString, which does have handling for NULL values.
  • Loading branch information
hanawatson committed Oct 21, 2022
1 parent 3cfccd4 commit dab8332
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions pkg/enrichment/db/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

import (
"context"
"database/sql"
"time"

"github.com/golang/protobuf/ptypes"
Expand All @@ -15,15 +16,15 @@ type issue struct {
FalsePositive bool `db:"false_positive"`
UpdatedAt time.Time `db:"updated_at"`

Target string `db:"target"`
Type string `db:"type"`
Title string `db:"title"`
Severity int32 `db:"severity"`
CVSS float64 `db:"cvss"`
Confidence int32 `db:"confidence"`
Description string `db:"description"`
Source string `db:"source"`
CVE string `db:"cve"`
Target string `db:"target"`
Type string `db:"type"`
Title string `db:"title"`
Severity int32 `db:"severity"`
CVSS float64 `db:"cvss"`
Confidence int32 `db:"confidence"`
Description string `db:"description"`
Source string `db:"source"`
CVE sql.NullString `db:"cve"`
}

func toDBIssue(i *v1.EnrichedIssue) (*issue, error) {
Expand All @@ -35,6 +36,7 @@ func toDBIssue(i *v1.EnrichedIssue) (*issue, error) {
if err != nil {
return nil, err
}
cve := sql.NullString{String: i.RawIssue.GetCve(), Valid: true}
return &issue{
Hash: i.GetHash(),
FirstSeen: firstSeen,
Expand All @@ -49,7 +51,7 @@ func toDBIssue(i *v1.EnrichedIssue) (*issue, error) {
Confidence: int32(i.RawIssue.GetConfidence()),
Description: i.RawIssue.GetDescription(),
Source: i.RawIssue.GetSource(),
CVE: i.RawIssue.GetCve(),
CVE: cve,
}, nil
}

Expand All @@ -63,6 +65,13 @@ func toEnrichedIssue(i *issue) (*v1.EnrichedIssue, error) {
if err != nil {
return nil, err
}

// Handle DBIssues with NULL CVE field
cve := ""
if (i.CVE.Valid) {
cve = i.CVE.String
}

return &v1.EnrichedIssue{
Hash: i.Hash,
FirstSeen: firstSeen,
Expand All @@ -78,7 +87,7 @@ func toEnrichedIssue(i *issue) (*v1.EnrichedIssue, error) {
Confidence: v1.Confidence(i.Confidence),
Description: i.Description,
Source: i.Source,
Cve: i.CVE,
Cve: cve,
},
}, nil
}
Expand Down

0 comments on commit dab8332

Please sign in to comment.