github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/k8s/report/table.go (about)

     1  package report
     2  
     3  import (
     4  	"io"
     5  	"sync"
     6  
     7  	"golang.org/x/xerrors"
     8  
     9  	dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
    10  	pkgReport "github.com/devseccon/trivy/pkg/report/table"
    11  )
    12  
    13  type TableWriter struct {
    14  	Report        string
    15  	Output        io.Writer
    16  	Severities    []dbTypes.Severity
    17  	ColumnHeading []string
    18  }
    19  
    20  const (
    21  	NamespaceColumn         = "Namespace"
    22  	ResourceColumn          = "Resource"
    23  	VulnerabilitiesColumn   = "Vulnerabilities"
    24  	MisconfigurationsColumn = "Misconfigurations"
    25  	SecretsColumn           = "Secrets"
    26  	RbacAssessmentColumn    = "RBAC Assessment"
    27  	InfraAssessmentColumn   = "Kubernetes Infra Assessment"
    28  )
    29  
    30  func WorkloadColumns() []string {
    31  	return []string{VulnerabilitiesColumn, MisconfigurationsColumn, SecretsColumn}
    32  }
    33  
    34  func RoleColumns() []string {
    35  	return []string{RbacAssessmentColumn}
    36  }
    37  
    38  func InfraColumns() []string {
    39  	return []string{InfraAssessmentColumn}
    40  }
    41  
    42  func (tw TableWriter) Write(report Report) error {
    43  	switch tw.Report {
    44  	case AllReport:
    45  		t := pkgReport.Writer{Output: tw.Output, Severities: tw.Severities, ShowMessageOnce: &sync.Once{}}
    46  		for _, r := range report.Resources {
    47  			if r.Report.Results.Failed() {
    48  				err := t.Write(r.Report)
    49  				if err != nil {
    50  					return err
    51  				}
    52  			}
    53  		}
    54  	case SummaryReport:
    55  		writer := NewSummaryWriter(tw.Output, tw.Severities, tw.ColumnHeading)
    56  		return writer.Write(report)
    57  	default:
    58  		return xerrors.Errorf(`report %q not supported. Use "summary" or "all"`, tw.Report)
    59  	}
    60  
    61  	return nil
    62  }