Skip to content

Commit

Permalink
fix(k8s): no error logged if trivy can't get docker image in kubernet…
Browse files Browse the repository at this point in the history
…es mode (aquasecurity#2521)

* Enable k8s logging and increase log level of the image scan errors

* Rework errors reporting

* Rework GetErrors method into printErrors

Print errors during report writing

* Increase log level for scan errors logging
  • Loading branch information
dmazhar-cogniance authored Jul 21, 2022
1 parent e1e02d7 commit 8467790
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/k8s/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func run(ctx context.Context, opts flag.Options, cluster string, artifacts []*ar
if err != nil {
return xerrors.Errorf("k8s scan error: %w", err)
}

if err := report.Write(r, report.Option{
Format: opts.Format,
Report: opts.ReportFormat,
Expand Down
17 changes: 17 additions & 0 deletions pkg/k8s/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/aquasecurity/trivy-kubernetes/pkg/artifacts"
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"

"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/types"
)

Expand Down Expand Up @@ -129,6 +130,8 @@ type Writer interface {

// Write writes the results in the give format
func Write(report Report, option Option, securityChecks []string, showEmpty bool) error {
report.printErrors()

switch option.Format {
case jsonFormat:
jwriter := JSONWriter{Output: option.Output, Report: option.Report}
Expand Down Expand Up @@ -220,3 +223,17 @@ func CreateResource(artifact *artifacts.Artifact, report types.Report, err error

return r
}

func (r Report) printErrors() {
for _, resource := range r.Vulnerabilities {
if resource.Error != "" {
log.Logger.Errorf("Error during vulnerabilities scan: %s", resource.Error)
}
}

for _, resource := range r.Misconfigurations {
if resource.Error != "" {
log.Logger.Errorf("Error during misconfiguration scan: %s", resource.Error)
}
}
}
2 changes: 1 addition & 1 deletion pkg/k8s/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (s *Scanner) scanVulns(ctx context.Context, artifact *artifacts.Artifact) (
imageReport, err := s.runner.ScanImage(ctx, s.opts)

if err != nil {
log.Logger.Debugf("failed to scan image %s: %s", image, err)
log.Logger.Warnf("failed to scan image %s: %s", image, err)
resources = append(resources, report.CreateResource(artifact, imageReport, err))
continue
}
Expand Down

0 comments on commit 8467790

Please sign in to comment.