github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/image/archive.go (about)

     1  package image
     2  
     3  import (
     4  	v1 "github.com/google/go-containerregistry/pkg/v1"
     5  	"github.com/hashicorp/go-multierror"
     6  
     7  	"github.com/devseccon/trivy/pkg/fanal/types"
     8  )
     9  
    10  func NewArchiveImage(fileName string) (types.Image, error) {
    11  	img, err := newImage(fileName)
    12  	if err != nil {
    13  		return nil, err
    14  	}
    15  	return archiveImage{
    16  		name:  fileName,
    17  		Image: img,
    18  	}, nil
    19  }
    20  
    21  func newImage(fileName string) (v1.Image, error) {
    22  	var errs error
    23  
    24  	// Docker archive
    25  	img, err := tryDockerArchive(fileName)
    26  	if err == nil {
    27  		// Return v1.Image if the file can be opened as Docker archive
    28  		return img, nil
    29  	}
    30  	errs = multierror.Append(errs, err)
    31  
    32  	// OCI layout
    33  	img, err = tryOCI(fileName)
    34  	if err == nil {
    35  		// Return v1.Image if the directory can be opened as OCI Image Format
    36  		return img, nil
    37  	}
    38  	errs = multierror.Append(errs, err)
    39  
    40  	return nil, errs
    41  }
    42  
    43  type archiveImage struct {
    44  	v1.Image
    45  	name string
    46  }
    47  
    48  func (img archiveImage) Name() string {
    49  	return img.name
    50  }
    51  
    52  func (img archiveImage) ID() (string, error) {
    53  	return ID(img)
    54  }
    55  
    56  // RepoTags returns empty as an archive doesn't support RepoTags
    57  func (archiveImage) RepoTags() []string {
    58  	return nil
    59  }
    60  
    61  // RepoDigests returns empty as an archive doesn't support RepoDigests
    62  func (archiveImage) RepoDigests() []string {
    63  	return nil
    64  }