github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/pkg/cataloger/java/zip_wrapped_archive_parser.go (about)

     1  package java
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	intFile "github.com/anchore/syft/internal/file"
     8  	"github.com/anchore/syft/syft/artifact"
     9  	"github.com/anchore/syft/syft/file"
    10  	"github.com/anchore/syft/syft/pkg"
    11  	"github.com/anchore/syft/syft/pkg/cataloger/generic"
    12  )
    13  
    14  var genericZipGlobs = []string{
    15  	"**/*.zip",
    16  }
    17  
    18  // TODO: when the generic archive cataloger is implemented, this should be removed (https://github.com/anchore/syft/issues/246)
    19  
    20  // parseZipWrappedJavaArchive is a parser function for java archive contents contained within arbitrary zip files.
    21  
    22  type genericZipWrappedJavaArchiveParser struct {
    23  	cfg ArchiveCatalogerConfig
    24  }
    25  
    26  func newGenericZipWrappedJavaArchiveParser(cfg ArchiveCatalogerConfig) genericZipWrappedJavaArchiveParser {
    27  	return genericZipWrappedJavaArchiveParser{
    28  		cfg: cfg,
    29  	}
    30  }
    31  
    32  func (gzp genericZipWrappedJavaArchiveParser) parseZipWrappedJavaArchive(ctx context.Context, _ file.Resolver, _ *generic.Environment, reader file.LocationReadCloser) ([]pkg.Package, []artifact.Relationship, error) {
    33  	contentPath, archivePath, cleanupFn, err := saveArchiveToTmp(reader.Path(), reader)
    34  	// note: even on error, we should always run cleanup functions
    35  	defer cleanupFn()
    36  	if err != nil {
    37  		return nil, nil, err
    38  	}
    39  
    40  	// we use our zip helper functions instead of that from the archiver package or the standard lib. Why? These helper
    41  	// functions support zips with shell scripts prepended to the file. Specifically, the helpers use the central
    42  	// header at the end of the file to determine where the beginning of the zip payload is (unlike the standard lib
    43  	// or archiver).
    44  	fileManifest, err := intFile.NewZipFileManifest(archivePath)
    45  	if err != nil {
    46  		return nil, nil, fmt.Errorf("unable to read files from java archive: %w", err)
    47  	}
    48  
    49  	// look for java archives within the zip archive
    50  	return discoverPkgsFromZip(ctx, reader.Location, archivePath, contentPath, fileManifest, nil, gzp.cfg)
    51  }