Skip to content

Commit

Permalink
feat: optional config to disable external api calls (#84)
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Jogeleit <frank.jogeleit@web.de>
  • Loading branch information
fjogeleit authored Feb 11, 2025
1 parent 4534b8d commit 4d505d6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
8 changes: 7 additions & 1 deletion plugins/trivy/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,18 @@ func (a CoreAPI) FromValues(values secrets.Values) CoreAPI {
return a
}

type API struct {
Disable bool `mapstructure:"disable"`
}

type Trivy struct {
DBDir string `mapstructure:"dbDir"`
API API `mapstructure:"api"`
}

type Github struct {
Token string `mapstructure:"token"`
Disable bool `mapstructure:"disable"`
Token string `mapstructure:"token"`
}

type Config struct {
Expand Down
8 changes: 8 additions & 0 deletions plugins/trivy/pkg/config/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ func (r *Resolver) CoreClient(ctx context.Context) (*core.Client, error) {
}

func (r *Resolver) CVEClient() (*cveawg.Client, error) {
if r.config.Trivy.API.Disable {
return nil, nil
}

options := []api.ClientOption{}

if r.config.Logging.API && r.config.Logging.LogLevel < 0 {
Expand All @@ -211,6 +215,10 @@ func (r *Resolver) CVEClient() (*cveawg.Client, error) {
}

func (r *Resolver) GHClient() *gh.Client {
if r.config.Github.Disable {
return nil
}

options := []gh.ClientOption{}

if r.config.Logging.API && r.config.Logging.LogLevel < 0 {
Expand Down
2 changes: 2 additions & 0 deletions plugins/trivy/pkg/server/vulnr/v1/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ func (h *APIHandler) List(ctx *gin.Context) {
for _, p := range list {
p := p
g.Go(func() error {
zap.L().Debug("fetching finding details", zap.String("name", p.Name))
v, err := h.service.GetDescription(ctx, p.Name)
if err != nil {
return fmt.Errorf("%s: %w", p.Name, err)
}

zap.L().Debug("finding details found", zap.String("name", p.Name))
v.Category = p.Category
v.Severity = p.Severity

Expand Down
20 changes: 17 additions & 3 deletions plugins/trivy/pkg/vulnr/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ func (s *Service) Get(ctx context.Context, name string) (*Vulnerability, error)
}

var details *Vulnerability
if strings.HasPrefix(name, "GHSA") {
if s.ghClient != nil && strings.HasPrefix(name, "GHSA") {
ghsa, err := s.ghClient.Get(ctx, name)
if err != nil {
return nil, err
}

details = MapSecurityAdvisory(ghsa)
} else {
} else if s.cveAPI != nil {
cve, err := s.cveAPI.GetCVE(ctx, name)
if err != nil {
return nil, err
Expand All @@ -43,6 +43,13 @@ func (s *Service) Get(ctx context.Context, name string) (*Vulnerability, error)
}

details = MapCVE(cve, trivyCVE)
} else {
trivyCVE, err := s.db.Get(name)
if err != nil {
zap.L().Warn("unable to load CVE from TrivyDB", zap.String("cve", name), zap.Error(err))
}

details = MapFromTrivyDB(name, trivyCVE)
}

s.cache.Set(name, details, gocache.DefaultExpiration)
Expand All @@ -55,7 +62,7 @@ func (s *Service) GetDescription(ctx context.Context, name string) (*Vulnerabili
return cached.(*Vulnerability), nil
}

if strings.HasPrefix(name, "GHSA") {
if s.ghClient != nil && strings.HasPrefix(name, "GHSA") {
ghsa, err := s.ghClient.Get(ctx, name)
if err != nil {
return nil, err
Expand All @@ -76,6 +83,13 @@ func (s *Service) GetDescription(ctx context.Context, name string) (*Vulnerabili
zap.L().Warn("unable to load CVE from TrivyDB", zap.String("cve", name), zap.Error(err))
}

if s.cveAPI == nil {
return &Vulnerability{
ID: name,
Title: name,
}, nil
}

cve, err := s.cveAPI.GetCVE(ctx, name)
if err != nil {
zap.L().Error("unable to load CVE from CVEAWG API", zap.String("cve", name), zap.Error(err))
Expand Down

0 comments on commit 4d505d6

Please sign in to comment.