rename json artifact to package + update resolver integrity checks

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman 2020-11-17 12:36:07 -05:00
parent 569a598df7
commit 030427bb33
No known key found for this signature in database
GPG Key ID: 5CB45AE22BAB7EA7
5 changed files with 28 additions and 21 deletions

View File

@ -8,12 +8,14 @@ import (
"github.com/anchore/syft/syft/source" "github.com/anchore/syft/syft/source"
) )
type Artifact struct { // Package represents a pkg.Package object specialized for JSON marshaling and unmarshaling.
artifactBasicMetadata type Package struct {
artifactCustomMetadata packageBasicMetadata
packageCustomMetadata
} }
type artifactBasicMetadata struct { // packageBasicMetadata contains non-ambiguous values (type-wise) from pkg.Package.
type packageBasicMetadata struct {
Name string `json:"name"` Name string `json:"name"`
Version string `json:"version"` Version string `json:"version"`
Type pkg.Type `json:"type"` Type pkg.Type `json:"type"`
@ -23,19 +25,22 @@ type artifactBasicMetadata struct {
Language pkg.Language `json:"language"` Language pkg.Language `json:"language"`
} }
type artifactCustomMetadata struct { // packageCustomMetadata contains ambiguous values (type-wise) from pkg.Package.
type packageCustomMetadata struct {
MetadataType pkg.MetadataType `json:"metadataType"` MetadataType pkg.MetadataType `json:"metadataType"`
Metadata interface{} `json:"metadata,omitempty"` Metadata interface{} `json:"metadata,omitempty"`
} }
type artifactMetadataUnpacker struct { // packageMetadataUnpacker is all values needed from Package to disambiguate ambiguous fields during json unmarshaling.
type packageMetadataUnpacker struct {
MetadataType string `json:"metadataType"` MetadataType string `json:"metadataType"`
Metadata json.RawMessage `json:"metadata"` Metadata json.RawMessage `json:"metadata"`
} }
func NewArtifact(p *pkg.Package) (Artifact, error) { // NewPackage crates a new Package from the given pkg.Package.
return Artifact{ func NewPackage(p *pkg.Package) (Package, error) {
artifactBasicMetadata: artifactBasicMetadata{ return Package{
packageBasicMetadata: packageBasicMetadata{
Name: p.Name, Name: p.Name,
Version: p.Version, Version: p.Version,
Type: p.Type, Type: p.Type,
@ -44,14 +49,15 @@ func NewArtifact(p *pkg.Package) (Artifact, error) {
Licenses: p.Licenses, Licenses: p.Licenses,
Language: p.Language, Language: p.Language,
}, },
artifactCustomMetadata: artifactCustomMetadata{ packageCustomMetadata: packageCustomMetadata{
MetadataType: p.MetadataType, MetadataType: p.MetadataType,
Metadata: p.Metadata, Metadata: p.Metadata,
}, },
}, nil }, nil
} }
func (a Artifact) ToPackage() pkg.Package { // ToPackage generates a pkg.Package from the current Package.
func (a Package) ToPackage() pkg.Package {
return pkg.Package{ return pkg.Package{
// does not include found-by and locations // does not include found-by and locations
Name: a.Name, Name: a.Name,
@ -66,15 +72,16 @@ func (a Artifact) ToPackage() pkg.Package {
} }
} }
// UnmarshalJSON is a custom unmarshaller for handling basic values and values with ambiguous types.
// nolint:funlen // nolint:funlen
func (a *Artifact) UnmarshalJSON(b []byte) error { func (a *Package) UnmarshalJSON(b []byte) error {
var basic artifactBasicMetadata var basic packageBasicMetadata
if err := json.Unmarshal(b, &basic); err != nil { if err := json.Unmarshal(b, &basic); err != nil {
return err return err
} }
a.artifactBasicMetadata = basic a.packageBasicMetadata = basic
var unpacker artifactMetadataUnpacker var unpacker packageMetadataUnpacker
if err := json.Unmarshal(b, &unpacker); err != nil { if err := json.Unmarshal(b, &unpacker); err != nil {
return err return err
} }

View File

@ -10,7 +10,7 @@ import (
// Document represents the syft cataloging findings as a JSON document // Document represents the syft cataloging findings as a JSON document
type Document struct { type Document struct {
Artifacts []Artifact `json:"artifacts"` // Artifacts is the list of packages discovered and placed into the catalog Artifacts []Package `json:"artifacts"` // Artifacts is the list of packages discovered and placed into the catalog
Source Source `json:"source"` // Source represents the original object that was cataloged Source Source `json:"source"` // Source represents the original object that was cataloged
Distro Distribution `json:"distro"` // Distro represents the Linux distribution that was detected from the source Distro Distribution `json:"distro"` // Distro represents the Linux distribution that was detected from the source
Descriptor Descriptor `json:"descriptor"` // Descriptor is a block containing self-describing information about syft Descriptor Descriptor `json:"descriptor"` // Descriptor is a block containing self-describing information about syft
@ -24,7 +24,7 @@ func NewDocument(catalog *pkg.Catalog, srcMetadata source.Metadata, d distro.Dis
} }
doc := Document{ doc := Document{
Artifacts: make([]Artifact, 0), Artifacts: make([]Package, 0),
Source: src, Source: src,
Distro: NewDistribution(d), Distro: NewDistribution(d),
Descriptor: Descriptor{ Descriptor: Descriptor{
@ -34,7 +34,7 @@ func NewDocument(catalog *pkg.Catalog, srcMetadata source.Metadata, d distro.Dis
} }
for _, p := range catalog.Sorted() { for _, p := range catalog.Sorted() {
art, err := NewArtifact(p) art, err := NewPackage(p)
if err != nil { if err != nil {
return Document{}, err return Document{}, err
} }

View File

@ -8,7 +8,7 @@ import (
"github.com/anchore/stereoscope/pkg/image" "github.com/anchore/stereoscope/pkg/image"
) )
var _ Resolver = &AllLayersResolver{} var _ Resolver = (*AllLayersResolver)(nil)
// AllLayersResolver implements path and content access for the AllLayers source option for container image data sources. // AllLayersResolver implements path and content access for the AllLayers source option for container image data sources.
type AllLayersResolver struct { type AllLayersResolver struct {

View File

@ -11,7 +11,7 @@ import (
"github.com/bmatcuk/doublestar" "github.com/bmatcuk/doublestar"
) )
var _ Resolver = &DirectoryResolver{} var _ Resolver = (*DirectoryResolver)(nil)
// DirectoryResolver implements path and content access for the directory data source. // DirectoryResolver implements path and content access for the directory data source.
type DirectoryResolver struct { type DirectoryResolver struct {

View File

@ -7,7 +7,7 @@ import (
"github.com/anchore/stereoscope/pkg/image" "github.com/anchore/stereoscope/pkg/image"
) )
var _ Resolver = &ImageSquashResolver{} var _ Resolver = (*ImageSquashResolver)(nil)
// ImageSquashResolver implements path and content access for the Squashed source option for container image data sources. // ImageSquashResolver implements path and content access for the Squashed source option for container image data sources.
type ImageSquashResolver struct { type ImageSquashResolver struct {