github.com/anchore/syft@v1.38.2/syft/pkg/cataloger/kernel/package.go (about)

     1  package kernel
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	"github.com/anchore/packageurl-go"
     8  	"github.com/anchore/syft/syft/cpe"
     9  	"github.com/anchore/syft/syft/file"
    10  	"github.com/anchore/syft/syft/pkg"
    11  )
    12  
    13  const linuxKernelPackageName = "linux-kernel"
    14  
    15  func createLinuxKernelCPEs(version string) []cpe.CPE {
    16  	c := cpe.NewWithAny()
    17  	c.Part = "o"
    18  	c.Product = "linux_kernel"
    19  	c.Vendor = "linux"
    20  	c.Version = version
    21  	if cpe.ValidateString(c.String()) != nil {
    22  		return nil
    23  	}
    24  
    25  	return []cpe.CPE{{Attributes: c, Source: cpe.NVDDictionaryLookupSource}}
    26  }
    27  
    28  func newLinuxKernelPackage(metadata pkg.LinuxKernel, archiveLocation file.Location) pkg.Package {
    29  	p := pkg.Package{
    30  		Name:      linuxKernelPackageName,
    31  		Version:   metadata.Version,
    32  		Locations: file.NewLocationSet(archiveLocation.WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation)),
    33  		PURL:      packageURL(linuxKernelPackageName, metadata.Version),
    34  		Type:      pkg.LinuxKernelPkg,
    35  		Metadata:  metadata,
    36  		CPEs:      createLinuxKernelCPEs(metadata.Version),
    37  	}
    38  
    39  	p.SetID()
    40  
    41  	return p
    42  }
    43  
    44  func newLinuxKernelModulePackage(ctx context.Context, metadata pkg.LinuxKernelModule, kmLocation file.Location) pkg.Package {
    45  	p := pkg.Package{
    46  		Name:      metadata.Name,
    47  		Version:   metadata.Version,
    48  		Locations: file.NewLocationSet(kmLocation.WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation)),
    49  		Licenses:  pkg.NewLicenseSet(pkg.NewLicensesFromLocationWithContext(ctx, kmLocation, metadata.License)...),
    50  		PURL:      packageURL(metadata.Name, metadata.Version),
    51  		Type:      pkg.LinuxKernelModulePkg,
    52  		Metadata:  metadata,
    53  	}
    54  
    55  	p.SetID()
    56  
    57  	return p
    58  }
    59  
    60  // packageURL returns the PURL for the specific Kernel package (see https://github.com/package-url/purl-spec)
    61  func packageURL(name, version string) string {
    62  	var namespace string
    63  
    64  	fields := strings.SplitN(name, "/", 2)
    65  	if len(fields) > 1 {
    66  		namespace = fields[0]
    67  		name = fields[1]
    68  	}
    69  
    70  	return packageurl.NewPackageURL(
    71  		packageurl.TypeGeneric,
    72  		namespace,
    73  		name,
    74  		version,
    75  		nil,
    76  		"",
    77  	).ToString()
    78  }