github.com/lineaje-labs/syft@v0.98.1-0.20231227153149-9e393f60ff1b/syft/pkg/cataloger/java/zip_wrapped_archive_parser.go (about)

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