Skip to content

Commit

Permalink
refactor: better integration of the parser into Trivy (#6183)
Browse files Browse the repository at this point in the history
Signed-off-by: knqyf263 <[email protected]>
  • Loading branch information
knqyf263 authored Feb 26, 2024
1 parent 069aae5 commit eef7c4f
Show file tree
Hide file tree
Showing 58 changed files with 171 additions and 196 deletions.
1 change: 1 addition & 0 deletions .github/workflows/semantic-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,5 @@ jobs:
helm
report
db
parser
deps
1 change: 1 addition & 0 deletions docs/community/contribute/pr.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ others:
- helm
- report
- db
- parser
- deps

The `<scope>` can be empty (e.g. if the change is a global or difficult to assign to a single component), in which case the parentheses are omitted.
Expand Down
6 changes: 3 additions & 3 deletions pkg/dependency/parser/c/conan/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"golang.org/x/exp/slices"
"golang.org/x/xerrors"

dio "github.com/aquasecurity/trivy/pkg/dependency/parser/io"
"github.com/aquasecurity/trivy/pkg/dependency/parser/log"
"github.com/aquasecurity/trivy/pkg/dependency/parser/types"
"github.com/aquasecurity/trivy/pkg/log"
xio "github.com/aquasecurity/trivy/pkg/x/io"
)

type LockFile struct {
Expand All @@ -35,7 +35,7 @@ func NewParser() types.Parser {
return &Parser{}
}

func (p *Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
func (p *Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
var lock LockFile
input, err := io.ReadAll(r)
if err != nil {
Expand Down
16 changes: 9 additions & 7 deletions pkg/dependency/parser/conda/meta/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (

"golang.org/x/xerrors"

dio "github.com/aquasecurity/trivy/pkg/dependency/parser/io"
"github.com/aquasecurity/trivy/pkg/dependency/parser/types"
xio "github.com/aquasecurity/trivy/pkg/x/io"
)

type packageJSON struct {
Expand All @@ -24,7 +24,7 @@ func NewParser() types.Parser {
// Parse parses Anaconda (a.k.a. conda) environment metadata.
// e.g. <conda-root>/envs/<env>/conda-meta/<package>.json
// For details see https://conda.io/projects/conda/en/latest/user-guide/concepts/environments.html
func (p *Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
func (p *Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
var data packageJSON
err := json.NewDecoder(r).Decode(&data)
if err != nil {
Expand All @@ -35,9 +35,11 @@ func (p *Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency,
return nil, nil, xerrors.Errorf("unable to parse conda package")
}

return []types.Library{{
Name: data.Name,
Version: data.Version,
License: data.License, // can be empty
}}, nil, nil
return []types.Library{
{
Name: data.Name,
Version: data.Version,
License: data.License, // can be empty
},
}, nil, nil
}
4 changes: 2 additions & 2 deletions pkg/dependency/parser/dart/pub/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"golang.org/x/xerrors"
"gopkg.in/yaml.v3"

dio "github.com/aquasecurity/trivy/pkg/dependency/parser/io"
"github.com/aquasecurity/trivy/pkg/dependency/parser/types"
xio "github.com/aquasecurity/trivy/pkg/x/io"
)

const (
Expand All @@ -31,7 +31,7 @@ type Dep struct {
Version string `yaml:"version"`
}

func (Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
func (Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
l := &lock{}
if err := yaml.NewDecoder(r).Decode(&l); err != nil {
return nil, nil, xerrors.Errorf("failed to decode pubspec.lock: %w", err)
Expand Down
17 changes: 11 additions & 6 deletions pkg/dependency/parser/dotnet/core_deps/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"github.com/liamg/jfather"
"golang.org/x/xerrors"

dio "github.com/aquasecurity/trivy/pkg/dependency/parser/io"
"github.com/aquasecurity/trivy/pkg/dependency/parser/log"
"github.com/aquasecurity/trivy/pkg/dependency/parser/types"
"github.com/aquasecurity/trivy/pkg/log"
xio "github.com/aquasecurity/trivy/pkg/x/io"
)

type Parser struct{}
Expand All @@ -18,7 +18,7 @@ func NewParser() types.Parser {
return &Parser{}
}

func (p *Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
func (p *Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
var depsFile dotNetDependencies

input, err := io.ReadAll(r)
Expand All @@ -43,9 +43,14 @@ func (p *Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency,
}

libraries = append(libraries, types.Library{
Name: split[0],
Version: split[1],
Locations: []types.Location{{StartLine: lib.StartLine, EndLine: lib.EndLine}},
Name: split[0],
Version: split[1],
Locations: []types.Location{
{
StartLine: lib.StartLine,
EndLine: lib.EndLine,
},
},
})
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/dependency/parser/golang/binary/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

"golang.org/x/xerrors"

dio "github.com/aquasecurity/trivy/pkg/dependency/parser/io"
"github.com/aquasecurity/trivy/pkg/dependency/parser/types"
xio "github.com/aquasecurity/trivy/pkg/x/io"
)

var (
Expand Down Expand Up @@ -36,7 +36,7 @@ func NewParser() types.Parser {
}

// Parse scans file to try to report the Go and module versions.
func (p *Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
func (p *Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
info, err := buildinfo.Read(r)
if err != nil {
return nil, nil, convertError(err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/dependency/parser/golang/mod/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"golang.org/x/mod/modfile"
"golang.org/x/xerrors"

dio "github.com/aquasecurity/trivy/pkg/dependency/parser/io"
"github.com/aquasecurity/trivy/pkg/dependency/parser/types"
xio "github.com/aquasecurity/trivy/pkg/x/io"
)

var (
Expand Down Expand Up @@ -65,7 +65,7 @@ func resolveVCSUrl(modulePath string) string {
}

// Parse parses a go.mod file
func (p *Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
func (p *Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
libs := make(map[string]types.Library)

goModData, err := io.ReadAll(r)
Expand Down
4 changes: 2 additions & 2 deletions pkg/dependency/parser/golang/sum/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/dependency/parser/golang/mod"
dio "github.com/aquasecurity/trivy/pkg/dependency/parser/io"
"github.com/aquasecurity/trivy/pkg/dependency/parser/types"
xio "github.com/aquasecurity/trivy/pkg/x/io"
)

type Parser struct{}
Expand All @@ -18,7 +18,7 @@ func NewParser() types.Parser {
}

// Parse parses a go.sum file
func (p *Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
func (p *Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
var libs []types.Library
uniqueLibs := make(map[string]string)

Expand Down
4 changes: 2 additions & 2 deletions pkg/dependency/parser/gradle/lockfile/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"fmt"
"strings"

dio "github.com/aquasecurity/trivy/pkg/dependency/parser/io"
"github.com/aquasecurity/trivy/pkg/dependency/parser/types"
"github.com/aquasecurity/trivy/pkg/dependency/parser/utils"
xio "github.com/aquasecurity/trivy/pkg/x/io"
)

type Parser struct{}
Expand All @@ -16,7 +16,7 @@ func NewParser() types.Parser {
return &Parser{}
}

func (Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
func (Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
var libs []types.Library
scanner := bufio.NewScanner(r)
var lineNum int
Expand Down
6 changes: 3 additions & 3 deletions pkg/dependency/parser/hex/mix/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"strings"
"unicode"

dio "github.com/aquasecurity/trivy/pkg/dependency/parser/io"
"github.com/aquasecurity/trivy/pkg/dependency/parser/log"
"github.com/aquasecurity/trivy/pkg/dependency/parser/types"
"github.com/aquasecurity/trivy/pkg/dependency/parser/utils"
"github.com/aquasecurity/trivy/pkg/log"
xio "github.com/aquasecurity/trivy/pkg/x/io"
)

// Parser is a parser for mix.lock
Expand All @@ -19,7 +19,7 @@ func NewParser() types.Parser {
return &Parser{}
}

func (Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
func (Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
var libs []types.Library
scanner := bufio.NewScanner(r)
var lineNumber int // It is used to save dependency location
Expand Down
25 changes: 0 additions & 25 deletions pkg/dependency/parser/io/io.go

This file was deleted.

10 changes: 5 additions & 5 deletions pkg/dependency/parser/java/jar/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"go.uber.org/zap"
"golang.org/x/xerrors"

dio "github.com/aquasecurity/trivy/pkg/dependency/parser/io"
"github.com/aquasecurity/trivy/pkg/dependency/parser/log"
"github.com/aquasecurity/trivy/pkg/dependency/parser/types"
"github.com/aquasecurity/trivy/pkg/log"
xio "github.com/aquasecurity/trivy/pkg/x/io"
)

var (
Expand Down Expand Up @@ -73,15 +73,15 @@ func NewParser(c Client, opts ...Option) types.Parser {
return p
}

func (p *Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
func (p *Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
libs, deps, err := p.parseArtifact(p.rootFilePath, p.size, r)
if err != nil {
return nil, nil, xerrors.Errorf("unable to parse %s: %w", p.rootFilePath, err)
}
return removeLibraryDuplicates(libs), deps, nil
}

func (p *Parser) parseArtifact(filePath string, size int64, r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
func (p *Parser) parseArtifact(filePath string, size int64, r xio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
log.Logger.Debugw("Parsing Java artifacts...", zap.String("file", filePath))

// Try to extract artifactId and version from the file name
Expand Down Expand Up @@ -147,7 +147,7 @@ func (p *Parser) parseArtifact(filePath string, size int64, r dio.ReadSeekerAt)
return libs, nil, nil
}

func (p *Parser) traverseZip(filePath string, size int64, r dio.ReadSeekerAt, fileProps Properties) (
func (p *Parser) traverseZip(filePath string, size int64, r xio.ReadSeekerAt, fileProps Properties) (
[]types.Library, manifest, bool, error) {
var libs []types.Library
var m manifest
Expand Down
2 changes: 1 addition & 1 deletion pkg/dependency/parser/java/jar/sonatype/log.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package sonatype

import "github.com/aquasecurity/trivy/pkg/dependency/parser/log"
import "github.com/aquasecurity/trivy/pkg/log"

// logger implements LeveledLogger
// https://github.com/hashicorp/go-retryablehttp/blob/991b9d0a42d13014e3689dd49a94c02be01f4237/client.go#L285-L290
Expand Down
2 changes: 1 addition & 1 deletion pkg/dependency/parser/java/pom/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/samber/lo"
"golang.org/x/exp/slices"

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

var (
Expand Down
6 changes: 3 additions & 3 deletions pkg/dependency/parser/java/pom/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import (
"golang.org/x/net/html/charset"
"golang.org/x/xerrors"

dio "github.com/aquasecurity/trivy/pkg/dependency/parser/io"
"github.com/aquasecurity/trivy/pkg/dependency/parser/log"
"github.com/aquasecurity/trivy/pkg/dependency/parser/types"
"github.com/aquasecurity/trivy/pkg/dependency/parser/utils"
"github.com/aquasecurity/trivy/pkg/log"
xio "github.com/aquasecurity/trivy/pkg/x/io"
)

const (
Expand Down Expand Up @@ -83,7 +83,7 @@ func NewParser(filePath string, opts ...option) types.Parser {
}
}

func (p *parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
func (p *parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
content, err := parsePom(r)
if err != nil {
return nil, nil, xerrors.Errorf("failed to parse POM: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/dependency/parser/java/pom/pom.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"github.com/samber/lo"
"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/dependency/parser/log"
"github.com/aquasecurity/trivy/pkg/dependency/parser/types"
"github.com/aquasecurity/trivy/pkg/dependency/parser/utils"
"github.com/aquasecurity/trivy/pkg/log"
)

type pom struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/dependency/parser/julia/manifest/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"golang.org/x/exp/maps"
"golang.org/x/xerrors"

dio "github.com/aquasecurity/trivy/pkg/dependency/parser/io"
"github.com/aquasecurity/trivy/pkg/dependency/parser/types"
xio "github.com/aquasecurity/trivy/pkg/x/io"
)

type primitiveManifest struct {
Expand All @@ -31,7 +31,7 @@ func NewParser() types.Parser {
return &Parser{}
}

func (p *Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
func (p *Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
var oldDeps map[string][]primitiveDependency
var primMan primitiveManifest
var manMetadata toml.MetaData
Expand Down
24 changes: 0 additions & 24 deletions pkg/dependency/parser/log/log.go

This file was deleted.

Loading

0 comments on commit eef7c4f

Please sign in to comment.