Skip to content

Commit

Permalink
test: mock VM walker (#5589)
Browse files Browse the repository at this point in the history
Signed-off-by: knqyf263 <knqyf263@gmail.com>
  • Loading branch information
knqyf263 authored Nov 16, 2023
1 parent d9d7f3f commit 7105186
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 346 deletions.
5 changes: 3 additions & 2 deletions pkg/commands/artifact/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package artifact

import (
"context"
"github.com/aquasecurity/trivy/pkg/fanal/artifact/vm"

"github.com/google/wire"

Expand Down Expand Up @@ -56,7 +57,7 @@ func initializeSBOMScanner(ctx context.Context, filePath string, artifactCache c
}

func initializeVMScanner(ctx context.Context, filePath string, artifactCache cache.ArtifactCache,
localArtifactCache cache.LocalArtifactCache, artifactOption artifact.Option) (
localArtifactCache cache.LocalArtifactCache, walker vm.Walker, artifactOption artifact.Option) (
scanner.Scanner, func(), error) {
wire.Build(scanner.StandaloneVMSet)
return scanner.Scanner{}, nil, nil
Expand Down Expand Up @@ -107,7 +108,7 @@ func initializeRemoteSBOMScanner(ctx context.Context, path string, artifactCache

// initializeRemoteVMScanner is for vm scanning in client/server mode
func initializeRemoteVMScanner(ctx context.Context, path string, artifactCache cache.ArtifactCache,
remoteScanOptions client.ScannerOption, artifactOption artifact.Option) (scanner.Scanner, func(), error) {
walker vm.Walker, remoteScanOptions client.ScannerOption, artifactOption artifact.Option) (scanner.Scanner, func(), error) {
wire.Build(scanner.RemoteVMSet)
return scanner.Scanner{}, nil, nil
}
9 changes: 7 additions & 2 deletions pkg/commands/artifact/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/fanal/walker"
"github.com/aquasecurity/trivy/pkg/scanner"
)

Expand Down Expand Up @@ -109,8 +110,10 @@ func sbomRemoteScanner(ctx context.Context, conf ScannerConfig) (scanner.Scanner

// vmStandaloneScanner initializes a VM scanner in standalone mode
func vmStandaloneScanner(ctx context.Context, conf ScannerConfig) (scanner.Scanner, func(), error) {
// TODO: The walker should be initialized in initializeVMScanner after https://github.com/aquasecurity/trivy/pull/5180
w := walker.NewVM(conf.ArtifactOption.SkipFiles, conf.ArtifactOption.SkipDirs)
s, cleanup, err := initializeVMScanner(ctx, conf.Target, conf.ArtifactCache, conf.LocalArtifactCache,
conf.ArtifactOption)
w, conf.ArtifactOption)
if err != nil {
return scanner.Scanner{}, func() {}, xerrors.Errorf("unable to initialize a vm scanner: %w", err)
}
Expand All @@ -119,7 +122,9 @@ func vmStandaloneScanner(ctx context.Context, conf ScannerConfig) (scanner.Scann

// vmRemoteScanner initializes a VM scanner in client/server mode
func vmRemoteScanner(ctx context.Context, conf ScannerConfig) (scanner.Scanner, func(), error) {
s, cleanup, err := initializeRemoteVMScanner(ctx, conf.Target, conf.ArtifactCache, conf.ServerOption, conf.ArtifactOption)
// TODO: The walker should be initialized in initializeVMScanner after https://github.com/aquasecurity/trivy/pull/5180
w := walker.NewVM(conf.ArtifactOption.SkipFiles, conf.ArtifactOption.SkipDirs)
s, cleanup, err := initializeRemoteVMScanner(ctx, conf.Target, conf.ArtifactCache, w, conf.ServerOption, conf.ArtifactOption)
if err != nil {
return scanner.Scanner{}, func() {}, xerrors.Errorf("unable to initialize a remote vm scanner: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/commands/artifact/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pkg/fanal/artifact/vm/ebs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type EBS struct {
}

func newEBS(snapshotID string, vm Storage, region, endpoint string) (*EBS, error) {

ebs, err := ebsfile.New(context.TODO(), config.MakeAWSOptions(region, endpoint)...)
if err != nil {
return nil, xerrors.Errorf("new ebsfile error: %w", err)
Expand Down
Binary file removed pkg/fanal/artifact/vm/testdata/AmazonLinux2.img.gz
Binary file not shown.
1 change: 1 addition & 0 deletions pkg/fanal/artifact/vm/testdata/alpine/etc/alpine-release
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.17.5
21 changes: 21 additions & 0 deletions pkg/fanal/artifact/vm/testdata/alpine/lib/apk/db/installed
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
C:Q1dCsKJvMnxtpg1CoCw+thiaWORo8=
P:musl
V:1.2.3-r5
A:aarch64
S:397125
I:675840
T:the musl c library (libc) implementation
U:https://musl.libc.org/
L:MIT
o:musl
m:Timo Teräs <timo.teras@iki.fi>
t:1684510151
c:b12380f8608f8cdd44347db413e8937ac4a5565b
p:so:libc.musl-aarch64.so.1=1
F:lib
R:ld-musl-aarch64.so.1
a:0:0:755
Z:Q17HWoHxeSxkUYleHiHuyks+G3edE=
R:libc.musl-aarch64.so.1
a:0:0:777
Z:Q14RpiCEfZIqcg1XDcVqp8QEpc9ks=
1 change: 1 addition & 0 deletions pkg/fanal/artifact/vm/testdata/mock.img
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock
1 change: 0 additions & 1 deletion pkg/fanal/artifact/vm/testdata/rawdata.img

This file was deleted.

80 changes: 42 additions & 38 deletions pkg/fanal/artifact/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,52 @@ const (
TypeFile Type = "file"
)

type Walker interface {
Walk(*io.SectionReader, string, walker.WalkFunc) error
}

func NewArtifact(target string, c cache.ArtifactCache, w Walker, opt artifact.Option) (artifact.Artifact, error) {
handlerManager, err := handler.NewManager(opt)
if err != nil {
return nil, xerrors.Errorf("handler init error: %w", err)
}
a, err := analyzer.NewAnalyzerGroup(opt.AnalyzerOptions())
if err != nil {
return nil, xerrors.Errorf("analyzer group error: %w", err)
}

storage := Storage{
cache: c,
analyzer: a,
handlerManager: handlerManager,
walker: w,
artifactOption: opt,
}

targetType := detectType(target)
switch targetType {
case TypeAMI:
target = strings.TrimPrefix(target, TypeAMI.Prefix())
return newAMI(target, storage, opt.AWSRegion, opt.AWSEndpoint)
case TypeEBS:
target = strings.TrimPrefix(target, TypeEBS.Prefix())
e, err := newEBS(target, storage, opt.AWSRegion, opt.AWSEndpoint)
if err != nil {
return nil, xerrors.Errorf("new EBS error: %w", err)
}
return e, nil
case TypeFile:
target = strings.TrimPrefix(target, TypeFile.Prefix())
return newFile(target, storage)
}
return nil, xerrors.Errorf("unsupported format")
}

type Storage struct {
cache cache.ArtifactCache
analyzer analyzer.AnalyzerGroup
handlerManager handler.Manager
walker walker.VM
walker Walker

artifactOption artifact.Option
}
Expand Down Expand Up @@ -114,43 +155,6 @@ func (a *Storage) Analyze(ctx context.Context, r *io.SectionReader) (types.BlobI
return blobInfo, nil
}

func NewArtifact(target string, c cache.ArtifactCache, opt artifact.Option) (artifact.Artifact, error) {
handlerManager, err := handler.NewManager(opt)
if err != nil {
return nil, xerrors.Errorf("handler init error: %w", err)
}
a, err := analyzer.NewAnalyzerGroup(opt.AnalyzerOptions())
if err != nil {
return nil, xerrors.Errorf("analyzer group error: %w", err)
}

storage := Storage{
cache: c,
analyzer: a,
handlerManager: handlerManager,
walker: walker.NewVM(opt.SkipFiles, opt.SkipDirs),
artifactOption: opt,
}

targetType := detectType(target)
switch targetType {
case TypeAMI:
target = strings.TrimPrefix(target, TypeAMI.Prefix())
return newAMI(target, storage, opt.AWSRegion, opt.AWSEndpoint)
case TypeEBS:
target = strings.TrimPrefix(target, TypeEBS.Prefix())
e, err := newEBS(target, storage, opt.AWSRegion, opt.AWSEndpoint)
if err != nil {
return nil, xerrors.Errorf("new EBS error: %w", err)
}
return e, nil
case TypeFile:
target = strings.TrimPrefix(target, TypeFile.Prefix())
return newFile(target, storage)
}
return nil, xerrors.Errorf("unsupported format")
}

func detectType(target string) Type {
switch {
case strings.HasPrefix(target, TypeAMI.Prefix()):
Expand Down
Loading

0 comments on commit 7105186

Please sign in to comment.