mirror of
https://github.com/anchore/syft.git
synced 2026-02-12 02:26:42 +01:00
* add info command from generated capabilities Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * correct gentoo and arch ecosystems Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * rename os pkg types Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * better binary cataloger description Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * expose metadata and pacakge types in json Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * expose json schema types Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add completeness tests for metadata types Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * latest generation Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix linting Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * improve testing a docs Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix tests and linting Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * restore goreleaser config Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * tweak diagram Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix pdm Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * chore: java binary data Signed-off-by: Keith Zantow <kzantow@gmail.com> * new capability descriptions for gguf and python Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * correct poetry lock integrity hash claim Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix compile error Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix: remove purl version from overrides Signed-off-by: Keith Zantow <kzantow@gmail.com> * fix lua deps ref Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * keep gguf as ai ecosystem Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * split packages.yaml to multiple files by go package Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * ensure tests do not use go test cache Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * sort json output for info command Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * docs: fix ocaml, php, and portage capabilities yaml Signed-off-by: Will Murphy <willmurphyscode@users.noreply.github.com> * chore: update erlang capabilities Signed-off-by: Keith Zantow <kzantow@gmail.com> * chore: update java capabilities Signed-off-by: Keith Zantow <kzantow@gmail.com> * chore: update javascript capabilities Signed-off-by: Keith Zantow <kzantow@gmail.com> * chore: update linux kernel capabilities Signed-off-by: Keith Zantow <kzantow@gmail.com> * remove missing tests Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix package.yaml references Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * revert license list change Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * check for drift in capability descriptions Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * regenerate capabilities Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * test cleanup Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * use fixture cache in static analysis Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * claim fixtures pre-req for cap generation Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * update documentation with correct regeneration procedure Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * chore: ruby-gemspec-cataloger finds no dependencies Signed-off-by: Will Murphy <willmurphyscode@users.noreply.github.com> * chore: fix python docs and config comment Signed-off-by: Will Murphy <willmurphyscode@users.noreply.github.com> * chore: commit re-generated java yaml Signed-off-by: Will Murphy <willmurphyscode@users.noreply.github.com> * add cataloger selection to caps command Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * re-generate cap yamls Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix tests for cataloger selection Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix cli test Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * add missing tests Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix linting Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * rename cmd to `cataloger info` Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * [wip] change capability description locations Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * [wip] continued Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * [wip] adjust for import cycles Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * correct docs Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix linting Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> --------- Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> Signed-off-by: Keith Zantow <kzantow@gmail.com> Signed-off-by: Will Murphy <willmurphyscode@users.noreply.github.com> Co-authored-by: Keith Zantow <kzantow@gmail.com> Co-authored-by: Will Murphy <willmurphyscode@users.noreply.github.com>
197 lines
8.6 KiB
Go
197 lines
8.6 KiB
Go
package packagemetadata
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
|
|
"github.com/anchore/syft/syft/pkg"
|
|
)
|
|
|
|
type jsonType struct {
|
|
ty any
|
|
name string
|
|
legacyNames []string
|
|
noLookupLegacyName string // legacy name that conflict with other types, thus should not affect the lookup
|
|
}
|
|
|
|
func jsonNames(ty any, name string, legacyNames ...string) jsonType {
|
|
return jsonType{
|
|
ty: ty,
|
|
name: name,
|
|
legacyNames: expandLegacyNameVariants(legacyNames...),
|
|
}
|
|
}
|
|
|
|
func jsonNamesWithoutLookup(ty any, name string, noLookupLegacyName string) jsonType {
|
|
return jsonType{
|
|
ty: ty,
|
|
name: name,
|
|
noLookupLegacyName: noLookupLegacyName,
|
|
}
|
|
}
|
|
|
|
type jsonTypeMapping struct {
|
|
typeToName map[reflect.Type]string
|
|
typeToLegacyName map[reflect.Type]string
|
|
nameToType map[string]reflect.Type
|
|
}
|
|
|
|
func makeJSONTypes(types ...jsonType) jsonTypeMapping {
|
|
out := jsonTypeMapping{
|
|
typeToName: make(map[reflect.Type]string),
|
|
typeToLegacyName: make(map[reflect.Type]string),
|
|
nameToType: make(map[string]reflect.Type),
|
|
}
|
|
for _, t := range types {
|
|
typ := reflect.TypeOf(t.ty)
|
|
out.typeToName[typ] = t.name
|
|
if len(t.noLookupLegacyName) > 0 {
|
|
out.typeToLegacyName[typ] = t.noLookupLegacyName
|
|
} else if len(t.legacyNames) > 0 {
|
|
out.typeToLegacyName[typ] = t.legacyNames[0]
|
|
}
|
|
out.nameToType[strings.ToLower(t.name)] = typ
|
|
for _, name := range t.legacyNames {
|
|
out.nameToType[strings.ToLower(name)] = typ
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// jsonNameFromType is lookup of all known package metadata types to their current JSON name and all previously known aliases.
|
|
// It is important that if a name needs to change that the old name is kept in this map (as an alias) for backwards
|
|
// compatibility to support decoding older JSON documents.
|
|
var jsonTypes = makeJSONTypes(
|
|
jsonNames(pkg.AlpmDBEntry{}, "alpm-db-entry", "AlpmMetadata"),
|
|
jsonNames(pkg.ApkDBEntry{}, "apk-db-entry", "ApkMetadata"),
|
|
jsonNames(pkg.BinarySignature{}, "binary-signature", "BinaryMetadata"),
|
|
jsonNames(pkg.BitnamiSBOMEntry{}, "bitnami-sbom-entry"),
|
|
jsonNames(pkg.CocoaPodfileLockEntry{}, "cocoa-podfile-lock-entry", "CocoapodsMetadataType"),
|
|
jsonNames(pkg.ConanV1LockEntry{}, "c-conan-lock-entry", "ConanLockMetadataType"),
|
|
jsonNames(pkg.ConanV2LockEntry{}, "c-conan-lock-v2-entry"),
|
|
jsonNames(pkg.ConanfileEntry{}, "c-conan-file-entry", "ConanMetadataType"),
|
|
jsonNames(pkg.ConaninfoEntry{}, "c-conan-info-entry"),
|
|
jsonNames(pkg.DartPubspecLockEntry{}, "dart-pubspec-lock-entry", "DartPubMetadata"),
|
|
jsonNames(pkg.DartPubspec{}, "dart-pubspec"),
|
|
jsonNames(pkg.DotnetDepsEntry{}, "dotnet-deps-entry", "DotnetDepsMetadata"),
|
|
jsonNames(pkg.DotnetPortableExecutableEntry{}, "dotnet-portable-executable-entry"),
|
|
jsonNames(pkg.DpkgArchiveEntry{}, "dpkg-archive-entry"),
|
|
jsonNames(pkg.DpkgDBEntry{}, "dpkg-db-entry", "DpkgMetadata"),
|
|
jsonNames(pkg.ELFBinaryPackageNoteJSONPayload{}, "elf-binary-package-note-json-payload"),
|
|
jsonNames(pkg.RubyGemspec{}, "ruby-gemspec", "GemMetadata"),
|
|
jsonNames(pkg.GitHubActionsUseStatement{}, "github-actions-use-statement"),
|
|
jsonNames(pkg.GolangBinaryBuildinfoEntry{}, "go-module-buildinfo-entry", "GolangBinMetadata", "GolangMetadata"),
|
|
jsonNames(pkg.GolangModuleEntry{}, "go-module-entry", "GolangModMetadata"),
|
|
jsonNames(pkg.GolangSourceEntry{}, "go-source-entry"),
|
|
jsonNames(pkg.HackageStackYamlLockEntry{}, "haskell-hackage-stack-lock-entry", "HackageMetadataType"),
|
|
jsonNamesWithoutLookup(pkg.HackageStackYamlEntry{}, "haskell-hackage-stack-entry", "HackageMetadataType"), // the legacy value is split into two types, where the other is preferred
|
|
jsonNames(pkg.JavaArchive{}, "java-archive", "JavaMetadata"),
|
|
jsonNames(pkg.JavaVMInstallation{}, "java-jvm-installation"),
|
|
jsonNames(pkg.MicrosoftKbPatch{}, "microsoft-kb-patch", "KbPatchMetadata"),
|
|
jsonNames(pkg.LinuxKernel{}, "linux-kernel-archive", "LinuxKernel"),
|
|
jsonNames(pkg.LinuxKernelModule{}, "linux-kernel-module", "LinuxKernelModule"),
|
|
jsonNames(pkg.ElixirMixLockEntry{}, "elixir-mix-lock-entry", "MixLockMetadataType"),
|
|
jsonNames(pkg.NixStoreEntry{}, "nix-store-entry", "NixStoreMetadata"),
|
|
jsonNames(pkg.NpmPackage{}, "javascript-npm-package", "NpmPackageJsonMetadata"),
|
|
jsonNames(pkg.NpmPackageLockEntry{}, "javascript-npm-package-lock-entry", "NpmPackageLockJsonMetadata"),
|
|
jsonNames(pkg.YarnLockEntry{}, "javascript-yarn-lock-entry", "YarnLockJsonMetadata"),
|
|
jsonNames(pkg.PnpmLockEntry{}, "javascript-pnpm-lock-entry"),
|
|
jsonNames(pkg.PEBinary{}, "pe-binary"),
|
|
jsonNames(pkg.PhpComposerLockEntry{}, "php-composer-lock-entry", "PhpComposerJsonMetadata"),
|
|
jsonNamesWithoutLookup(pkg.PhpComposerInstalledEntry{}, "php-composer-installed-entry", "PhpComposerJsonMetadata"), // the legacy value is split into two types, where the other is preferred
|
|
//nolint:staticcheck
|
|
jsonNames(pkg.PhpPeclEntry{}, "php-pecl-entry", "PhpPeclMetadata"),
|
|
jsonNames(pkg.PhpPearEntry{}, "php-pear-entry"),
|
|
jsonNames(pkg.PortageEntry{}, "portage-db-entry", "PortageMetadata"),
|
|
jsonNames(pkg.PythonPackage{}, "python-package", "PythonPackageMetadata"),
|
|
jsonNames(pkg.PythonPdmLockEntry{}, "python-pdm-lock-entry"),
|
|
jsonNames(pkg.PythonPipfileLockEntry{}, "python-pipfile-lock-entry", "PythonPipfileLockMetadata"),
|
|
jsonNames(pkg.PythonPoetryLockEntry{}, "python-poetry-lock-entry", "PythonPoetryLockMetadata"),
|
|
jsonNames(pkg.PythonRequirementsEntry{}, "python-pip-requirements-entry", "PythonRequirementsMetadata"),
|
|
jsonNames(pkg.PythonUvLockEntry{}, "python-uv-lock-entry"),
|
|
jsonNames(pkg.ErlangRebarLockEntry{}, "erlang-rebar-lock-entry", "RebarLockMetadataType"),
|
|
jsonNames(pkg.RDescription{}, "r-description", "RDescriptionFileMetadataType"),
|
|
jsonNames(pkg.RpmDBEntry{}, "rpm-db-entry", "RpmMetadata", "RpmdbMetadata"),
|
|
jsonNamesWithoutLookup(pkg.RpmArchive{}, "rpm-archive", "RpmMetadata"), // the legacy value is split into two types, where the other is preferred
|
|
jsonNames(pkg.SwiftPackageManagerResolvedEntry{}, "swift-package-manager-lock-entry", "SwiftPackageManagerMetadata"),
|
|
jsonNames(pkg.SwiplPackEntry{}, "swiplpack-package"),
|
|
jsonNames(pkg.OpamPackage{}, "opam-package"),
|
|
jsonNames(pkg.RustCargoLockEntry{}, "rust-cargo-lock-entry", "RustCargoPackageMetadata"),
|
|
jsonNamesWithoutLookup(pkg.RustBinaryAuditEntry{}, "rust-cargo-audit-entry", "RustCargoPackageMetadata"), // the legacy value is split into two types, where the other is preferred
|
|
jsonNames(pkg.SnapEntry{}, "snap-entry"),
|
|
jsonNames(pkg.WordpressPluginEntry{}, "wordpress-plugin-entry", "WordpressMetadata"),
|
|
jsonNames(pkg.HomebrewFormula{}, "homebrew-formula"),
|
|
jsonNames(pkg.LuaRocksPackage{}, "luarocks-package"),
|
|
jsonNames(pkg.TerraformLockProviderEntry{}, "terraform-lock-provider-entry"),
|
|
jsonNames(pkg.DotnetPackagesLockEntry{}, "dotnet-packages-lock-entry"),
|
|
jsonNames(pkg.CondaMetaPackage{}, "conda-metadata-entry", "CondaPackageMetadata"),
|
|
jsonNames(pkg.GGUFFileHeader{}, "gguf-file-header"),
|
|
)
|
|
|
|
func expandLegacyNameVariants(names ...string) []string {
|
|
var candidates []string
|
|
for _, name := range names {
|
|
candidates = append(candidates, name)
|
|
if strings.HasSuffix(name, "MetadataType") {
|
|
candidates = append(candidates, strings.TrimSuffix(name, "Type"))
|
|
} else if strings.HasSuffix(name, "Metadata") {
|
|
candidates = append(candidates, name+"Type")
|
|
}
|
|
}
|
|
return candidates
|
|
}
|
|
|
|
func AllTypeNames() []string {
|
|
names := make([]string, 0)
|
|
for _, t := range AllTypes() {
|
|
names = append(names, reflect.TypeOf(t).Name())
|
|
}
|
|
return names
|
|
}
|
|
|
|
func JSONName(metadata any) string {
|
|
if name, exists := jsonTypes.typeToName[reflect.TypeOf(metadata)]; exists {
|
|
return name
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func JSONLegacyName(metadata any) string {
|
|
if name, exists := jsonTypes.typeToLegacyName[reflect.TypeOf(metadata)]; exists {
|
|
return name
|
|
}
|
|
return JSONName(metadata)
|
|
}
|
|
|
|
func ReflectTypeFromJSONName(name string) reflect.Type {
|
|
name = strings.ToLower(name)
|
|
return jsonTypes.nameToType[name]
|
|
}
|
|
|
|
// JSONNameFromString converts a Go struct name string (e.g., "pkg.AlpmDBEntry" or "AlpmDBEntry")
|
|
// to its JSON schema name (e.g., "alpm-db-entry"). Returns empty string if not found.
|
|
func JSONNameFromString(typeName string) string {
|
|
// strip "pkg." prefix if present
|
|
typeName = strings.TrimPrefix(typeName, "pkg.")
|
|
|
|
// look through all types to find matching struct name
|
|
for typ, jsonName := range jsonTypes.typeToName {
|
|
if typ.Name() == typeName {
|
|
return jsonName
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// ToUpperCamelCase converts kebab-case to UpperCamelCase
|
|
// e.g., "alpm-db-entry" -> "AlpmDbEntry"
|
|
func ToUpperCamelCase(kebab string) string {
|
|
parts := strings.Split(kebab, "-")
|
|
for i, part := range parts {
|
|
if len(part) > 0 {
|
|
parts[i] = strings.ToUpper(part[0:1]) + part[1:]
|
|
}
|
|
}
|
|
return strings.Join(parts, "")
|
|
}
|