Skip to content

Commit

Permalink
Added logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dorneanu committed Aug 19, 2024
1 parent 9cbca57 commit 948e879
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
1 change: 1 addition & 0 deletions scanners/nvd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ require (
require (
github.com/kelseyhightower/envconfig v1.4.0
github.com/machinebox/graphql v0.2.2
github.com/sirupsen/logrus v1.9.3
)
7 changes: 7 additions & 0 deletions scanners/nvd/go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
Expand All @@ -22,12 +23,17 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
Expand All @@ -40,5 +46,6 @@ google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFW
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
34 changes: 27 additions & 7 deletions scanners/nvd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,27 @@ package main
import (
"fmt"
"time"
"os"

"github.com/kelseyhightower/envconfig"
log "github.com/sirupsen/logrus"
"github.wdf.sap.corp/cc/heureka/scanners/nvd/models"
"github.wdf.sap.corp/cc/heureka/scanners/nvd/processor"
"github.wdf.sap.corp/cc/heureka/scanners/nvd/scanner"
)

func init() {
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&log.JSONFormatter{})

// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
log.SetOutput(os.Stdout)

// Only log the warning severity or above.
log.SetLevel(log.DebugLevel)
}

func main() {
var scannerCfg scanner.Config
err := envconfig.Process("heureka", &scannerCfg)
Expand All @@ -32,29 +46,35 @@ func main() {
}

cves, err := scanner.GetCVEs(filter)

if err != nil {
fmt.Println(err)
log.WithFields(log.Fields{
"error": err,
}).Error("Couldn't get CVEs")
}

var processorCfg processor.Config
err = envconfig.Process("heureka", &processorCfg)
if err != nil {
fmt.Println(err)
log.WithFields(log.Fields{
"error": err,
}).Error("Couldn't configure new processor")
}

processor := processor.NewProcessor(processorCfg)
err = processor.Setup()

if err != nil {
fmt.Println(err)
return
log.WithFields(log.Fields{
"error": err,
}).Error("Couldn't setup new processor")
}

for _, cve := range cves {
err = processor.Process(&cve.Cve)
if err != nil {
fmt.Println(err)
log.WithFields(log.Fields{
"error": err,
"CVEID": &cve.Cve.Id,
}).Warn("Couldn't process CVE")
}
}
}
30 changes: 23 additions & 7 deletions scanners/nvd/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"

"github.com/machinebox/graphql"
log "github.com/sirupsen/logrus"
"github.wdf.sap.corp/cc/heureka/scanners/nvd/models"
)

Expand All @@ -30,13 +31,19 @@ func (p *Processor) Setup() error {
// Check if there is already an IssueRepository with the same name
existentingIssueRepositoryId, err := p.GetIssueRepositoryId()
if err != nil {
fmt.Printf("err: %s", err)
log.Warnf("There is no IssueRepository: %s", err)

// Create new IssueRepository
newIssueRepositoryId, err := p.CreateIssueRepository()
if err != nil {
fmt.Println(err)
log.WithFields(log.Fields{
"error": err,
}).Error("Couldn't create new IssueRepository")
}
log.WithFields(log.Fields{
"issueRepositoryId": newIssueRepositoryId,
}).Info("Created new IssueRepository")

p.IssueRepositoryId = newIssueRepositoryId
} else {
p.IssueRepositoryId = existentingIssueRepositoryId
Expand All @@ -62,6 +69,7 @@ func (p *Processor) GetIssueRepositoryId() (string, error) {

err := p.Client.Run(context.Background(), req, &issueRepositoryConnectionResp)
if err != nil {
log.Error("Couldn't fetch IssueRepositoryId")
return "", err
}

Expand Down Expand Up @@ -96,7 +104,7 @@ func (p *Processor) CreateIssueRepository() (string, error) {

err := p.Client.Run(context.Background(), req, &createIssueRepositoryResp)
if err != nil {
fmt.Println(err)
log.Error("Couldn't create IssueRepository")
return "", err
}
repositoryId = createIssueRepositoryResp.IssueRepository.Id
Expand All @@ -123,12 +131,12 @@ func (p *Processor) GetIssueId(cve *models.Cve) (string, error) {

err := p.Client.Run(context.Background(), req, &issueConnectionResp)
if err != nil {
log.Error("Couldn't fetch IssueId")
return "", err
}

if issueConnectionResp.IssueConnection.TotalCount > 0 {
for _, issueEdge := range issueConnectionResp.IssueConnection.Edges {
fmt.Printf("id: %s", issueEdge.Node.Id)
issueId = issueEdge.Node.Id
}

Expand Down Expand Up @@ -185,7 +193,9 @@ func (p *Processor) Process(cve *models.Cve) error {
// Create new Issue
issueId, err := p.CreateIssue(cve)
if err != nil {
fmt.Printf("couldn't create new Issue")
log.WithFields(log.Fields{
"error": err,
}).Error("Couldn't create new Issue")
}

// Create new IssueVariant
Expand All @@ -194,7 +204,13 @@ func (p *Processor) Process(cve *models.Cve) error {
return fmt.Errorf("couldn't create new IssueVariant")
}

fmt.Printf("Created new Issue: %s\n", issueId)
fmt.Printf("Created new IssueVariant: %s\n", issueVariantId)
log.WithFields(log.Fields{
"issueID": issueId,
}).Info("Created new Issue")

log.WithFields(log.Fields{
"issueVariantId": issueVariantId,
}).Info("Created new IssueVariant")

return nil
}

0 comments on commit 948e879

Please sign in to comment.