chore: add prefix to image errors (#5601)

Signed-off-by: knqyf263 <knqyf263@gmail.com>
This commit is contained in:
Teppei Fukuda
2023-11-20 21:38:45 +09:00
committed by GitHub
parent ed0022b915
commit c866f1c4e9
4 changed files with 14 additions and 12 deletions

View File

@@ -20,9 +20,9 @@ import (
// Standalone // Standalone
////////////// //////////////
// initializeDockerScanner is for container image scanning in standalone mode // initializeImageScanner is for container image scanning in standalone mode
// e.g. dockerd, container registry, podman, etc. // e.g. dockerd, container registry, podman, etc.
func initializeDockerScanner(ctx context.Context, imageName string, artifactCache cache.ArtifactCache, func initializeImageScanner(ctx context.Context, imageName string, artifactCache cache.ArtifactCache,
localArtifactCache cache.LocalArtifactCache, imageOpt types.ImageOptions, artifactOption artifact.Option) ( localArtifactCache cache.LocalArtifactCache, imageOpt types.ImageOptions, artifactOption artifact.Option) (
scanner.Scanner, func(), error) { scanner.Scanner, func(), error) {
wire.Build(scanner.StandaloneDockerSet) wire.Build(scanner.StandaloneDockerSet)
@@ -67,9 +67,9 @@ func initializeVMScanner(ctx context.Context, filePath string, artifactCache cac
// Client/Server // Client/Server
///////////////// /////////////////
// initializeRemoteDockerScanner is for container image scanning in client/server mode // initializeRemoteImageScanner is for container image scanning in client/server mode
// e.g. dockerd, container registry, podman, etc. // e.g. dockerd, container registry, podman, etc.
func initializeRemoteDockerScanner(ctx context.Context, imageName string, artifactCache cache.ArtifactCache, func initializeRemoteImageScanner(ctx context.Context, imageName string, artifactCache cache.ArtifactCache,
remoteScanOptions client.ScannerOption, imageOpt types.ImageOptions, artifactOption artifact.Option) ( remoteScanOptions client.ScannerOption, imageOpt types.ImageOptions, artifactOption artifact.Option) (
scanner.Scanner, func(), error) { scanner.Scanner, func(), error) {
wire.Build(scanner.RemoteDockerSet) wire.Build(scanner.RemoteDockerSet)

View File

@@ -12,10 +12,10 @@ import (
// imageStandaloneScanner initializes a container image scanner in standalone mode // imageStandaloneScanner initializes a container image scanner in standalone mode
// $ trivy image alpine:3.15 // $ trivy image alpine:3.15
func imageStandaloneScanner(ctx context.Context, conf ScannerConfig) (scanner.Scanner, func(), error) { func imageStandaloneScanner(ctx context.Context, conf ScannerConfig) (scanner.Scanner, func(), error) {
s, cleanup, err := initializeDockerScanner(ctx, conf.Target, conf.ArtifactCache, conf.LocalArtifactCache, s, cleanup, err := initializeImageScanner(ctx, conf.Target, conf.ArtifactCache, conf.LocalArtifactCache,
conf.ArtifactOption.ImageOption, conf.ArtifactOption) conf.ArtifactOption.ImageOption, conf.ArtifactOption)
if err != nil { if err != nil {
return scanner.Scanner{}, func() {}, xerrors.Errorf("unable to initialize a docker scanner: %w", err) return scanner.Scanner{}, func() {}, xerrors.Errorf("unable to initialize an image scanner: %w", err)
} }
return s, cleanup, nil return s, cleanup, nil
} }
@@ -34,10 +34,10 @@ func archiveStandaloneScanner(ctx context.Context, conf ScannerConfig) (scanner.
// $ trivy image --server localhost:4954 alpine:3.15 // $ trivy image --server localhost:4954 alpine:3.15
func imageRemoteScanner(ctx context.Context, conf ScannerConfig) ( func imageRemoteScanner(ctx context.Context, conf ScannerConfig) (
scanner.Scanner, func(), error) { scanner.Scanner, func(), error) {
s, cleanup, err := initializeRemoteDockerScanner(ctx, conf.Target, conf.ArtifactCache, conf.ServerOption, s, cleanup, err := initializeRemoteImageScanner(ctx, conf.Target, conf.ArtifactCache, conf.ServerOption,
conf.ArtifactOption.ImageOption, conf.ArtifactOption) conf.ArtifactOption.ImageOption, conf.ArtifactOption)
if err != nil { if err != nil {
return scanner.Scanner{}, nil, xerrors.Errorf("unable to initialize the remote docker scanner: %w", err) return scanner.Scanner{}, nil, xerrors.Errorf("unable to initialize a remote image scanner: %w", err)
} }
return s, cleanup, nil return s, cleanup, nil
} }

View File

@@ -29,9 +29,9 @@ import (
// Injectors from inject.go: // Injectors from inject.go:
// initializeDockerScanner is for container image scanning in standalone mode // initializeImageScanner is for container image scanning in standalone mode
// e.g. dockerd, container registry, podman, etc. // e.g. dockerd, container registry, podman, etc.
func initializeDockerScanner(ctx context.Context, imageName string, artifactCache cache.ArtifactCache, localArtifactCache cache.LocalArtifactCache, imageOpt types.ImageOptions, artifactOption artifact.Option) (scanner.Scanner, func(), error) { func initializeImageScanner(ctx context.Context, imageName string, artifactCache cache.ArtifactCache, localArtifactCache cache.LocalArtifactCache, imageOpt types.ImageOptions, artifactOption artifact.Option) (scanner.Scanner, func(), error) {
applierApplier := applier.NewApplier(localArtifactCache) applierApplier := applier.NewApplier(localArtifactCache)
ospkgScanner := ospkg.NewScanner() ospkgScanner := ospkg.NewScanner()
langpkgScanner := langpkg.NewScanner() langpkgScanner := langpkg.NewScanner()
@@ -140,9 +140,9 @@ func initializeVMScanner(ctx context.Context, filePath string, artifactCache cac
}, nil }, nil
} }
// initializeRemoteDockerScanner is for container image scanning in client/server mode // initializeRemoteImageScanner is for container image scanning in client/server mode
// e.g. dockerd, container registry, podman, etc. // e.g. dockerd, container registry, podman, etc.
func initializeRemoteDockerScanner(ctx context.Context, imageName string, artifactCache cache.ArtifactCache, remoteScanOptions client.ScannerOption, imageOpt types.ImageOptions, artifactOption artifact.Option) (scanner.Scanner, func(), error) { func initializeRemoteImageScanner(ctx context.Context, imageName string, artifactCache cache.ArtifactCache, remoteScanOptions client.ScannerOption, imageOpt types.ImageOptions, artifactOption artifact.Option) (scanner.Scanner, func(), error) {
v := _wireValue v := _wireValue
clientScanner := client.NewScanner(remoteScanOptions, v...) clientScanner := client.NewScanner(remoteScanOptions, v...)
typesImage, cleanup, err := image.NewContainerImage(ctx, imageName, imageOpt) typesImage, cleanup, err := image.NewContainerImage(ctx, imageName, imageOpt)

View File

@@ -2,6 +2,7 @@ package image
import ( import (
"context" "context"
"fmt"
"strings" "strings"
"github.com/google/go-containerregistry/pkg/name" "github.com/google/go-containerregistry/pkg/name"
@@ -50,6 +51,7 @@ func NewContainerImage(ctx context.Context, imageName string, opt types.ImageOpt
// Return v1.Image if the image is found // Return v1.Image if the image is found
return img, cleanup, nil return img, cleanup, nil
} }
err = multierror.Prefix(err, fmt.Sprintf("%s error:", src))
errs = multierror.Append(errs, err) errs = multierror.Append(errs, err)
} }