Skip to content

Commit

Permalink
Refactor to remove duplicated code, reflect some review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Levente Kovacs committed Jun 3, 2024
1 parent da71578 commit 39c24d0
Show file tree
Hide file tree
Showing 53 changed files with 144 additions and 749 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ require (
github.com/alecthomas/chroma v0.10.0
github.com/antchfx/htmlquery v1.3.0
github.com/apparentlymart/go-cidr v1.1.0
github.com/aquasecurity/go-dep-parser v0.0.0-20240213093706-423cd04548a5
github.com/aws/smithy-go v1.20.2
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/go-version v1.6.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -752,8 +752,6 @@ github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
github.com/aquasecurity/bolt-fixtures v0.0.0-20200903104109-d34e7f983986 h1:2a30xLN2sUZcMXl50hg+PJCIDdJgIvIbVcKqLJ/ZrtM=
github.com/aquasecurity/bolt-fixtures v0.0.0-20200903104109-d34e7f983986/go.mod h1:NT+jyeCzXk6vXR5MTkdn4z64TgGfE5HMLC8qfj5unl8=
github.com/aquasecurity/go-dep-parser v0.0.0-20240213093706-423cd04548a5 h1:VhlDpfiVYpjCJt+tL2IpJV/T0OkAAKM545yMP4Hlsl0=
github.com/aquasecurity/go-dep-parser v0.0.0-20240213093706-423cd04548a5/go.mod h1:6sh6f0Ixlt+oIMEPGxJyDOmzB03tK2v2rsAlc/9q12g=
github.com/aquasecurity/go-gem-version v0.0.0-20201115065557-8eed6fe000ce h1:QgBRgJvtEOBtUXilDb1MLi1p1MWoyFDXAu5DEUl5nwM=
github.com/aquasecurity/go-gem-version v0.0.0-20201115065557-8eed6fe000ce/go.mod h1:HXgVzOPvXhVGLJs4ZKO817idqr/xhwsTcj17CLYY74s=
github.com/aquasecurity/go-mock-aws v0.0.0-20240523055201-a4152219967f h1:NRq3oUfkheKgoYPjNUApUtClKaBRcc6KzdcBHqZPrAM=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Ported from https://github.com/golang/go/blob/e9c96835971044aa4ace37c7787de231bbde05d9/src/cmd/go/internal/version/exe.go

package binary
package executable

import (
"bytes"
Expand All @@ -11,7 +11,7 @@ import (
)

// An exe is a generic interface to an OS executable (ELF, Mach-O, PE, XCOFF).
type exe interface {
type Exe interface {
// ReadData reads and returns up to size byte starting at virtual address addr.
ReadData(addr, size uint64) ([]byte, error)

Expand All @@ -20,7 +20,7 @@ type exe interface {
}

// openExe opens file and returns it as an exe.
func openExe(r io.Reader) (exe, error) {
func OpenExe(r io.Reader) (Exe, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Ported from https://github.com/golang/go/blob/e9c96835971044aa4ace37c7787de231bbde05d9/src/cmd/go/internal/version/version.go

package binary
package nodejsparser

import (
"bytes"
Expand All @@ -9,6 +9,7 @@ import (

"github.com/aquasecurity/trivy/pkg/dependency"
"github.com/aquasecurity/trivy/pkg/dependency/types"
exe "github.com/aquasecurity/trivy/pkg/dependency/parser/executable"
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
xio "github.com/aquasecurity/trivy/pkg/x/io"
)
Expand All @@ -25,7 +26,7 @@ func NewParser() types.Parser {

// Parse scans file to try to report the NodeJS version.
func (p *Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
x, err := openExe(r)
x, err := exe.OpenExe(r)
if err != nil {
return nil, nil, ErrUnrecognizedExe
}
Expand All @@ -37,7 +38,7 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency,

var libs []types.Library
libs = append(libs, types.Library{
ID: packageID(mod, vers),
ID: dependency.ID(ftypes.NodeJsExecutable, mod, vers),
Name: mod,
Version: vers,
})
Expand All @@ -46,7 +47,7 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency,
}

// findVers finds and returns the NodeJS version in the executable x.
func findVers(x exe) (vers, mod string) {
func findVers(x exe.Exe) (vers, mod string) {
text, size := x.DataStart()
data, err := x.ReadData(text, size)
if err != nil {
Expand All @@ -67,7 +68,3 @@ func findVers(x exe) (vers, mod string) {

return "node", vers
}

func packageID(name, version string) string {
return dependency.ID(ftypes.NodeJsGeneric, name, version)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package binary
package nodejsparser

import (
"os"
Expand Down Expand Up @@ -43,7 +43,7 @@ func TestParse(t *testing.T) {
got, _, err := parser.Parse(f)
if tt.wantErr != "" {
require.NotNil(t, err)
assert.Contains(t, err.Error(), tt.wantErr)
require.ErrorContains(t, err, tt.wantErr)
return
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Ported from https://github.com/golang/go/blob/e9c96835971044aa4ace37c7787de231bbde05d9/src/cmd/go/internal/version/version.go

package binary
package phpparser

import (
"bytes"
Expand All @@ -9,6 +9,7 @@ import (

"github.com/aquasecurity/trivy/pkg/dependency"
"github.com/aquasecurity/trivy/pkg/dependency/types"
exe "github.com/aquasecurity/trivy/pkg/dependency/parser/executable"
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
xio "github.com/aquasecurity/trivy/pkg/x/io"
)
Expand All @@ -26,7 +27,7 @@ func NewParser() types.Parser {

// Parse scans file to try to report the Python version.
func (p *Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
x, err := openExe(r)
x, err := exe.OpenExe(r)
if err != nil {
return nil, nil, ErrUnrecognizedExe
}
Expand All @@ -38,7 +39,7 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency,

var libs []types.Library
libs = append(libs, types.Library{
ID: packageID(name, vers),
ID: dependency.ID(ftypes.PhpExecutable, name, vers),
Name: name,
Version: vers,
})
Expand All @@ -47,7 +48,7 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency,
}

// findVers finds and returns the PHP version in the executable x.
func findVers(x exe) (vers, mod string) {
func findVers(x exe.Exe) (vers, mod string) {
text, size := x.DataStart()
data, err := x.ReadData(text, size)
if err != nil {
Expand All @@ -68,7 +69,3 @@ func findVers(x exe) (vers, mod string) {

return "php", vers
}

func packageID(name, version string) string {
return dependency.ID(ftypes.PhpGeneric, name, version)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package binary
package phpparser

import (
"os"
Expand Down Expand Up @@ -43,7 +43,7 @@ func TestParse(t *testing.T) {
got, _, err := parser.Parse(f)
if tt.wantErr != "" {
require.NotNil(t, err)
assert.Contains(t, err.Error(), tt.wantErr)
require.ErrorContains(t, err, tt.wantErr)
return
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package binary
package pythonparser

import (
"os"
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestParse(t *testing.T) {
got, _, err := parser.Parse(f)
if tt.wantErr != "" {
require.NotNil(t, err)
assert.Contains(t, err.Error(), tt.wantErr)
require.ErrorContains(t, err, tt.wantErr)
return
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Ported from https://github.com/golang/go/blob/e9c96835971044aa4ace37c7787de231bbde05d9/src/cmd/go/internal/version/version.go

package binary
package pythonparser

import (
"bytes"
Expand All @@ -9,6 +9,7 @@ import (

"github.com/aquasecurity/trivy/pkg/dependency"
"github.com/aquasecurity/trivy/pkg/dependency/types"
exe "github.com/aquasecurity/trivy/pkg/dependency/parser/executable"
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
xio "github.com/aquasecurity/trivy/pkg/x/io"
)
Expand All @@ -26,7 +27,7 @@ func NewParser() types.Parser {

// Parse scans file to try to report the Python version.
func (p *Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
x, err := openExe(r)
x, err := exe.OpenExe(r)
if err != nil {
return nil, nil, ErrUnrecognizedExe
}
Expand All @@ -38,7 +39,7 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency,

var libs []types.Library
libs = append(libs, types.Library{
ID: packageID(name, vers),
ID: dependency.ID(ftypes.PythonExecutable, name, vers),
Name: name,
Version: vers,
})
Expand All @@ -47,7 +48,7 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]types.Library, []types.Dependency,
}

// findVers finds and returns the Python version in the executable x.
func findVers(x exe) (mod, vers string) {
func findVers(x exe.Exe) (mod, vers string) {
text, size := x.DataStart()
data, err := x.ReadData(text, size)
if err != nil {
Expand All @@ -69,7 +70,3 @@ func findVers(x exe) (mod, vers string) {

return "python", vers
}

func packageID(name, version string) string {
return dependency.ID(ftypes.PythonGeneric, name, version)
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
83 changes: 0 additions & 83 deletions pkg/dependency/parser/php/binary/exe.go

This file was deleted.

Loading

0 comments on commit 39c24d0

Please sign in to comment.