mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-23 07:29:00 -08:00
docker: Accept interfaces, return structs
Signed-off-by: Simarpreet Singh <simar@linux.com>
This commit is contained in:
@@ -60,22 +60,22 @@ type layer struct {
|
|||||||
Content io.ReadCloser
|
Content io.ReadCloser
|
||||||
}
|
}
|
||||||
|
|
||||||
type DockerExtractor struct {
|
type Extractor struct {
|
||||||
Client *client.Client
|
Client *client.Client
|
||||||
Cache cache.Cache
|
Cache cache.Cache
|
||||||
Option types.DockerOption
|
Option types.DockerOption
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDockerExtractor(option types.DockerOption) (extractor.Extractor, error) {
|
func NewDockerExtractor(option types.DockerOption) (Extractor, error) {
|
||||||
RegisterRegistry(&gcr.GCR{})
|
RegisterRegistry(&gcr.GCR{})
|
||||||
RegisterRegistry(&ecr.ECR{})
|
RegisterRegistry(&ecr.ECR{})
|
||||||
|
|
||||||
cli, err := client.NewClientWithOpts(client.FromEnv)
|
cli, err := client.NewClientWithOpts(client.FromEnv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, xerrors.Errorf("error initializing docker extractor: %w", err)
|
return Extractor{}, xerrors.Errorf("error initializing docker extractor: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return DockerExtractor{
|
return Extractor{
|
||||||
Option: option,
|
Option: option,
|
||||||
Client: cli,
|
Client: cli,
|
||||||
Cache: cache.Initialize(utils.CacheDir()),
|
Cache: cache.Initialize(utils.CacheDir()),
|
||||||
@@ -122,7 +122,7 @@ func applyLayers(layerPaths []string, filesInLayers map[string]extractor.FileMap
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d DockerExtractor) createRegistryClient(ctx context.Context, domain string) (*registry.Registry, error) {
|
func (d Extractor) createRegistryClient(ctx context.Context, domain string) (*registry.Registry, error) {
|
||||||
auth, err := GetToken(ctx, domain, d.Option)
|
auth, err := GetToken(ctx, domain, d.Option)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, xerrors.Errorf("failed to get auth config: %w", err)
|
return nil, xerrors.Errorf("failed to get auth config: %w", err)
|
||||||
@@ -144,7 +144,7 @@ func (d DockerExtractor) createRegistryClient(ctx context.Context, domain string
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d DockerExtractor) SaveLocalImage(ctx context.Context, imageName string) (io.Reader, error) {
|
func (d Extractor) SaveLocalImage(ctx context.Context, imageName string) (io.Reader, error) {
|
||||||
var err error
|
var err error
|
||||||
r := d.Cache.Get(imageName)
|
r := d.Cache.Get(imageName)
|
||||||
if r == nil {
|
if r == nil {
|
||||||
@@ -162,7 +162,7 @@ func (d DockerExtractor) SaveLocalImage(ctx context.Context, imageName string) (
|
|||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d DockerExtractor) saveLocalImage(ctx context.Context, imageName string) (io.ReadCloser, error) {
|
func (d Extractor) saveLocalImage(ctx context.Context, imageName string) (io.ReadCloser, error) {
|
||||||
r, err := d.Client.ImageSave(ctx, []string{imageName})
|
r, err := d.Client.ImageSave(ctx, []string{imageName})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, xerrors.New("error in docker image save")
|
return nil, xerrors.New("error in docker image save")
|
||||||
@@ -170,7 +170,7 @@ func (d DockerExtractor) saveLocalImage(ctx context.Context, imageName string) (
|
|||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d DockerExtractor) Extract(ctx context.Context, imageName string, filenames []string) (extractor.FileMap, error) {
|
func (d Extractor) Extract(ctx context.Context, imageName string, filenames []string) (extractor.FileMap, error) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), d.Option.Timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), d.Option.Timeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
@@ -237,7 +237,7 @@ func downloadConfigFile(ctx context.Context, r *registry.Registry, image registr
|
|||||||
return config, nil
|
return config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d DockerExtractor) extractLayerFiles(layerCh chan layer, errCh chan error, ctx context.Context, filenames []string, filesInLayers map[string]extractor.FileMap, opqInLayers map[string]extractor.OPQDirs) error {
|
func (d Extractor) extractLayerFiles(layerCh chan layer, errCh chan error, ctx context.Context, filenames []string, filesInLayers map[string]extractor.FileMap, opqInLayers map[string]extractor.OPQDirs) error {
|
||||||
var l layer
|
var l layer
|
||||||
select {
|
select {
|
||||||
case l = <-layerCh:
|
case l = <-layerCh:
|
||||||
@@ -257,7 +257,7 @@ func (d DockerExtractor) extractLayerFiles(layerCh chan layer, errCh chan error,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d DockerExtractor) extractLayerWorker(dig digest.Digest, r *registry.Registry, ctx context.Context, image registry.Image, errCh chan error, layerCh chan layer) {
|
func (d Extractor) extractLayerWorker(dig digest.Digest, r *registry.Registry, ctx context.Context, image registry.Image, errCh chan error, layerCh chan layer) {
|
||||||
var rc io.Reader
|
var rc io.Reader
|
||||||
// Use cache
|
// Use cache
|
||||||
rc = d.Cache.Get(string(dig))
|
rc = d.Cache.Get(string(dig))
|
||||||
@@ -294,7 +294,7 @@ func getValidManifest(ctx context.Context, r *registry.Registry, image registry.
|
|||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d DockerExtractor) ExtractFromFile(ctx context.Context, r io.Reader, filenames []string) (extractor.FileMap, error) {
|
func (d Extractor) ExtractFromFile(ctx context.Context, r io.Reader, filenames []string) (extractor.FileMap, error) {
|
||||||
manifests := make([]manifest, 0)
|
manifests := make([]manifest, 0)
|
||||||
filesInLayers := map[string]extractor.FileMap{}
|
filesInLayers := map[string]extractor.FileMap{}
|
||||||
opqInLayers := make(map[string]extractor.OPQDirs)
|
opqInLayers := make(map[string]extractor.OPQDirs)
|
||||||
@@ -365,7 +365,7 @@ func (d DockerExtractor) ExtractFromFile(ctx context.Context, r io.Reader, filen
|
|||||||
return fileMap, nil
|
return fileMap, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d DockerExtractor) ExtractFiles(layer io.Reader, filenames []string) (extractor.FileMap, extractor.OPQDirs, error) {
|
func (d Extractor) ExtractFiles(layer io.Reader, filenames []string) (extractor.FileMap, extractor.OPQDirs, error) {
|
||||||
data := make(map[string][]byte)
|
data := make(map[string][]byte)
|
||||||
opqDirs := extractor.OPQDirs{}
|
opqDirs := extractor.OPQDirs{}
|
||||||
|
|
||||||
@@ -426,7 +426,7 @@ func (d DockerExtractor) ExtractFiles(layer io.Reader, filenames []string) (extr
|
|||||||
return data, opqDirs, nil
|
return data, opqDirs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d DockerExtractor) isIgnored(filePath string) bool {
|
func (d Extractor) isIgnored(filePath string) bool {
|
||||||
for _, path := range strings.Split(filePath, utils.PathSeparator) {
|
for _, path := range strings.Split(filePath, utils.PathSeparator) {
|
||||||
if utils.StringInSlice(path, library.IgnoreDirs) {
|
if utils.StringInSlice(path, library.IgnoreDirs) {
|
||||||
return true
|
return true
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ func TestExtractFromFile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
d := DockerExtractor{}
|
d := Extractor{}
|
||||||
fm, err := d.ExtractFromFile(context.TODO(), f, v.filenames)
|
fm, err := d.ExtractFromFile(context.TODO(), f, v.filenames)
|
||||||
if v.err != err {
|
if v.err != err {
|
||||||
t.Errorf("err: got %v, want %v", v.err, err)
|
t.Errorf("err: got %v, want %v", v.err, err)
|
||||||
@@ -175,7 +175,7 @@ func TestExtractFiles(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
d := DockerExtractor{}
|
d := Extractor{}
|
||||||
fm, opqDirs, err := d.ExtractFiles(f, v.filenames)
|
fm, opqDirs, err := d.ExtractFiles(f, v.filenames)
|
||||||
if v.err != err {
|
if v.err != err {
|
||||||
t.Errorf("err: got %v, want %v", v.err, err)
|
t.Errorf("err: got %v, want %v", v.err, err)
|
||||||
@@ -211,7 +211,7 @@ func TestDockerExtractor_SaveLocalImage(t *testing.T) {
|
|||||||
_ = os.RemoveAll(tempCacheDir)
|
_ = os.RemoveAll(tempCacheDir)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
de := DockerExtractor{
|
de := Extractor{
|
||||||
Option: types.DockerOption{},
|
Option: types.DockerOption{},
|
||||||
Client: c,
|
Client: c,
|
||||||
Cache: cache.Initialize(tempCacheDir),
|
Cache: cache.Initialize(tempCacheDir),
|
||||||
@@ -311,7 +311,7 @@ func TestDockerExtractor_Extract(t *testing.T) {
|
|||||||
_ = os.RemoveAll(tempCacheDir)
|
_ = os.RemoveAll(tempCacheDir)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
de := DockerExtractor{
|
de := Extractor{
|
||||||
Option: types.DockerOption{
|
Option: types.DockerOption{
|
||||||
AuthURL: ts.URL,
|
AuthURL: ts.URL,
|
||||||
NonSSL: true,
|
NonSSL: true,
|
||||||
|
|||||||
Reference in New Issue
Block a user