github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/pkg/cataloger/kernel/package.go (about) 1 package kernel 2 3 import ( 4 "strings" 5 6 "github.com/anchore/packageurl-go" 7 "github.com/anchore/syft/syft/file" 8 "github.com/anchore/syft/syft/pkg" 9 ) 10 11 const linuxKernelPackageName = "linux-kernel" 12 13 func newLinuxKernelPackage(metadata pkg.LinuxKernel, archiveLocation file.Location) pkg.Package { 14 p := pkg.Package{ 15 Name: linuxKernelPackageName, 16 Version: metadata.Version, 17 Locations: file.NewLocationSet(archiveLocation.WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation)), 18 PURL: packageURL(linuxKernelPackageName, metadata.Version), 19 Type: pkg.LinuxKernelPkg, 20 Metadata: metadata, 21 } 22 23 p.SetID() 24 25 return p 26 } 27 28 func newLinuxKernelModulePackage(metadata pkg.LinuxKernelModule, kmLocation file.Location) pkg.Package { 29 p := pkg.Package{ 30 Name: metadata.Name, 31 Version: metadata.Version, 32 Locations: file.NewLocationSet(kmLocation.WithAnnotation(pkg.EvidenceAnnotationKey, pkg.PrimaryEvidenceAnnotation)), 33 Licenses: pkg.NewLicenseSet(pkg.NewLicensesFromLocation(kmLocation, metadata.License)...), 34 PURL: packageURL(metadata.Name, metadata.Version), 35 Type: pkg.LinuxKernelModulePkg, 36 Metadata: metadata, 37 } 38 39 p.SetID() 40 41 return p 42 } 43 44 // packageURL returns the PURL for the specific Kernel package (see https://github.com/package-url/purl-spec) 45 func packageURL(name, version string) string { 46 var namespace string 47 48 fields := strings.SplitN(name, "/", 2) 49 if len(fields) > 1 { 50 namespace = fields[0] 51 name = fields[1] 52 } 53 54 return packageurl.NewPackageURL( 55 packageurl.TypeGeneric, 56 namespace, 57 name, 58 version, 59 nil, 60 "", 61 ).ToString() 62 }