gotrivy

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 15 Imported by: 0

README

GoTrivy

Go CI Go Lint Go SAST Coverage Docs Docs Visualization License

gotrivy is a Golang helper for github.com/aquasecurity/trivy (reference).

The primary purpose of this library is currently to create XSLX reports from a JSON report file. Trivy provides reports in Table and JSON formats, along with a custom Template capability. This libary provides an additional XLSX option via github.com/grokify/gocharts. This can be run from the CLI as cmd/gotrivy/main.go or it can be done programmatically by inspecting the code of that file.

gotrivy.Report is an extension of github.com/aquasecurity/trivy/pkg/types.Report.

findingspec

gotrivy converts Trivy reports to the shared findingspec finding model, a scanner-neutral intermediate representation. Converting to findingspec.Finding lets Trivy results be aggregated, filtered, and reported alongside findings from other scanners using one common schema.

Scanning

In addition to parsing an existing Trivy JSON report, gotrivy.ScanFilepath runs a trivy fs scan by shelling out to the installed trivy binary and returns the parsed report. The cmd/gotrivyscan CLI scans a path and writes an XLSX report.

For in-process scanning that imports Trivy as a Go library (no trivy binary required), see the separate github.com/grokify/gotrivyext module.

Installation

go install github.com/grokify/gotrivy/cmd/gotrivy

Usage

gotrivy -i <path-to-report.json> [-o path-to-report.xlsx]

If an output file isn't provided, a default output filename and path is used setting the filename to the original filename with a .xlsx suffix in the current directory.

References

Scan Image

The following is an example of scanning a local image:

% docker image ls
REPOSITORY                       TAG       IMAGE ID       CREATED        SIZE
grokify/ringcentral-permahooks   v0.2.3    af80576e5e7d   6 months ago   640MB
% trivy image -f json grokify/ringcentral-permahooks > trivy-report.json
% gotrivy -i trivy-report.json -o trivy-report.xlsx
Scan JAR
% trivy -d fs path/to/jar
% trivy -d fs path/to/pom.xml

Extract pom.xml from JAR file:

% unzip myfile.jar pom.xml
% trivy -d fs pom.xml 
2024-10-30T01:26:21.429-0700	DEBUG	Severities: ["UNKNOWN" "LOW" "MEDIUM" "HIGH" "CRITICAL"]
2024-10-30T01:26:21.429-0700	DEBUG	Ignore statuses	{"statuses": null}
2024-10-30T01:26:21.440-0700	DEBUG	cache dir:  /path/to/Caches/trivy
2024-10-30T01:26:21.440-0700	DEBUG	DB update was skipped because the local DB is the latest
2024-10-30T01:26:21.440-0700	DEBUG	DB Schema: 2, UpdatedAt: 2024-10-30 06:47:03.247108911 +0000 UTC, NextUpdate: 2024-10-31 06:47:03.24710874 +0000 UTC, DownloadedAt: 2024-10-30 07:37:27.722974 +0000 UTC
2024-10-30T01:26:21.440-0700	INFO	Vulnerability scanning is enabled
2024-10-30T01:26:21.440-0700	DEBUG	Vulnerability type:  [os library]
2024-10-30T01:26:21.440-0700	INFO	Secret scanning is enabled
2024-10-30T01:26:21.440-0700	INFO	If your scanning is slow, please try '--scanners vuln' to disable secret scanning
2024-10-30T01:26:21.440-0700	INFO	Please see also https://aquasecurity.github.io/trivy/v0.46/docs/scanner/secret/#recommendation for faster secret detection
2024-10-30T01:26:21.441-0700	DEBUG	No secret config detected: trivy-secret.yaml
2024-10-30T01:26:21.441-0700	DEBUG	The nuget packages directory couldn't be found. License search disabled
2024-10-30T01:26:21.441-0700	DEBUG	Walk the file tree rooted at 'pom.xml' in parallel
2024-10-30T01:26:21.441-0700	DEBUG	Resolving org.json:json:20220924...
2024-10-30T01:26:21.625-0700	DEBUG	Start parent: org.sonatype.oss:oss-parent:9
2024-10-30T01:26:21.626-0700	DEBUG	Exit parent: org.sonatype.oss:oss-parent:9
2024-10-30T01:26:21.638-0700	DEBUG	OS is not detected.
2024-10-30T01:26:21.638-0700	DEBUG	Detected OS: unknown
2024-10-30T01:26:21.638-0700	INFO	Number of language-specific files: 1
2024-10-30T01:26:21.638-0700	INFO	Detecting pom vulnerabilities...
2024-10-30T01:26:21.638-0700	DEBUG	Detecting library vulnerabilities, type: pom, path: pom.xml

pom.xml (pom)

Total: 2 (UNKNOWN: 0, LOW: 0, MEDIUM: 0, HIGH: 2, CRITICAL: 0)

┌───────────────┬────────────────┬──────────┬────────┬───────────────────┬───────────────┬────────────────────────────────────────────┐
│    Library    │ Vulnerability  │ Severity │ Status │ Installed Version │ Fixed Version │                   Title                    │
├───────────────┼────────────────┼──────────┼────────┼───────────────────┼───────────────┼────────────────────────────────────────────┤
│ org.json:json │ CVE-2022-45688 │ HIGH     │ fixed  │ 20220924          │ 20230227      │ json stack overflow vulnerability          │
│               │                │          │        │                   │               │ https://avd.aquasec.com/nvd/cve-2022-45688 │
│               ├────────────────┤          │        │                   ├───────────────┼────────────────────────────────────────────┤
│               │ CVE-2023-5072  │          │        │                   │ 20231013      │ JSON-java: parser confusion leads to OOM   │
│               │                │          │        │                   │               │ https://avd.aquasec.com/nvd/cve-2023-5072  │
└───────────────┴────────────────┴──────────┴────────┴───────────────────┴───────────────┴────────────────────────────────────────────┘

Update Trivy Databases

% trivy image --download-db-only
% trivy image --download-java-db-only
% trivy image --reset

WebGoat Example

% trivy fs target/webgoat-2025.4-SNAPSHOT.jar --skip-files ".*\.class,.*META-INF.*"

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrReportNotLoaded = errors.New("report not loaded")

Functions

func DetectedVulnerabilitiesTable

func DetectedVulnerabilitiesTable(vulns []types.DetectedVulnerability) (*table.Table, error)

func ScanFilepath added in v0.5.0

func ScanFilepath(ctx context.Context, targetPath string) (types.Report, error)

ScanFilepath executes trivy filesystem scan using the trivy command-line tool. This provides a clean Go API that internally calls the trivy binary. Equivalent to: trivy fs --format json --scanners vuln <path>

Note: This implementation uses exec.Command instead of direct Go API integration to avoid pulling Trivy's full scanning dependency tree into every importer. It requires the trivy binary to be installed and on PATH. The interface remains identical to what a pure Go API would provide.

func ToFinding added in v0.5.0

ToFinding converts a single Trivy DetectedVulnerability (within its Result and report artifact context) into a findingspec.Finding. A non-nil art marks the finding as a container scan (type "container"); nil means an SCA scan.

Types

type FindingSpecOptions added in v0.5.0

type FindingSpecOptions struct {
	// Repo sets Location.Repo for multi-repo/workspace sweeps, optional.
	Repo string
}

FindingSpecOptions configures conversion of Trivy output to findingspec.

type Report

type Report struct {
	*types.Report
}

Report is an extended struct for `github.com/aquasecurity/trivy/pkg/types.Report`.

func ReadFile

func ReadFile(filename string) (*Report, error)

func (*Report) ByLibraryHistogramSets added in v0.3.0

func (r *Report) ByLibraryHistogramSets() (*histogram.HistogramSets, error)

func (*Report) BySeverityHistogramSet added in v0.4.0

func (r *Report) BySeverityHistogramSet() (*histogram.HistogramSet, error)

func (*Report) ByYearHistogramSets added in v0.4.0

func (r *Report) ByYearHistogramSets() (*histogram.HistogramSets, error)

func (*Report) Findings added in v0.5.0

func (r *Report) Findings(opts FindingSpecOptions) *findingspec.FindingSet

Findings converts the Trivy report into a findingspec.FindingSet of normalized security vulnerability findings.

func (*Report) ResultsCount

func (r *Report) ResultsCount() int

func (*Report) SeverityCounts

func (r *Report) SeverityCounts() map[string]uint

SeverityCounts returns the number of vulnerabiltiies detected by severity.

func (*Report) TableSet

func (r *Report) TableSet(addDates bool) (*table.TableSet, error)

func (*Report) VulnerabiliesTable

func (r *Report) VulnerabiliesTable(addDates bool) (*table.Table, error)

VulnerabiliesTable returns a table of vulnerabilities with a column for result set. It is formatted with Markdown links which can be rendered in XLSX using `github.com/grokify/gocharts/data/table`.

func (*Report) VulnerabilityCount

func (r *Report) VulnerabilityCount() int

Directories

Path Synopsis
cmd
gotrivy command
gotrivyscan command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL