From 030427bb33767197621d861aba79c45589c50106 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Tue, 17 Nov 2020 12:36:07 -0500 Subject: [PATCH] rename json artifact to package + update resolver integrity checks Signed-off-by: Alex Goodman --- syft/presenter/json/artifact.go | 37 +++++++++++++++++----------- syft/presenter/json/document.go | 6 ++--- syft/source/all_layers_resolver.go | 2 +- syft/source/directory_resolver.go | 2 +- syft/source/image_squash_resolver.go | 2 +- 5 files changed, 28 insertions(+), 21 deletions(-) diff --git a/syft/presenter/json/artifact.go b/syft/presenter/json/artifact.go index d5bf19872..2773ec57f 100644 --- a/syft/presenter/json/artifact.go +++ b/syft/presenter/json/artifact.go @@ -8,12 +8,14 @@ import ( "github.com/anchore/syft/syft/source" ) -type Artifact struct { - artifactBasicMetadata - artifactCustomMetadata +// Package represents a pkg.Package object specialized for JSON marshaling and unmarshaling. +type Package struct { + packageBasicMetadata + packageCustomMetadata } -type artifactBasicMetadata struct { +// packageBasicMetadata contains non-ambiguous values (type-wise) from pkg.Package. +type packageBasicMetadata struct { Name string `json:"name"` Version string `json:"version"` Type pkg.Type `json:"type"` @@ -23,19 +25,22 @@ type artifactBasicMetadata struct { 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"` 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"` Metadata json.RawMessage `json:"metadata"` } -func NewArtifact(p *pkg.Package) (Artifact, error) { - return Artifact{ - artifactBasicMetadata: artifactBasicMetadata{ +// NewPackage crates a new Package from the given pkg.Package. +func NewPackage(p *pkg.Package) (Package, error) { + return Package{ + packageBasicMetadata: packageBasicMetadata{ Name: p.Name, Version: p.Version, Type: p.Type, @@ -44,14 +49,15 @@ func NewArtifact(p *pkg.Package) (Artifact, error) { Licenses: p.Licenses, Language: p.Language, }, - artifactCustomMetadata: artifactCustomMetadata{ + packageCustomMetadata: packageCustomMetadata{ MetadataType: p.MetadataType, Metadata: p.Metadata, }, }, 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{ // does not include found-by and locations 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 -func (a *Artifact) UnmarshalJSON(b []byte) error { - var basic artifactBasicMetadata +func (a *Package) UnmarshalJSON(b []byte) error { + var basic packageBasicMetadata if err := json.Unmarshal(b, &basic); err != nil { return err } - a.artifactBasicMetadata = basic + a.packageBasicMetadata = basic - var unpacker artifactMetadataUnpacker + var unpacker packageMetadataUnpacker if err := json.Unmarshal(b, &unpacker); err != nil { return err } diff --git a/syft/presenter/json/document.go b/syft/presenter/json/document.go index de1ce463b..27cd6e48e 100644 --- a/syft/presenter/json/document.go +++ b/syft/presenter/json/document.go @@ -10,7 +10,7 @@ import ( // Document represents the syft cataloging findings as a JSON document 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 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 @@ -24,7 +24,7 @@ func NewDocument(catalog *pkg.Catalog, srcMetadata source.Metadata, d distro.Dis } doc := Document{ - Artifacts: make([]Artifact, 0), + Artifacts: make([]Package, 0), Source: src, Distro: NewDistribution(d), Descriptor: Descriptor{ @@ -34,7 +34,7 @@ func NewDocument(catalog *pkg.Catalog, srcMetadata source.Metadata, d distro.Dis } for _, p := range catalog.Sorted() { - art, err := NewArtifact(p) + art, err := NewPackage(p) if err != nil { return Document{}, err } diff --git a/syft/source/all_layers_resolver.go b/syft/source/all_layers_resolver.go index 7b94328c1..4b99bda5b 100644 --- a/syft/source/all_layers_resolver.go +++ b/syft/source/all_layers_resolver.go @@ -8,7 +8,7 @@ import ( "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. type AllLayersResolver struct { diff --git a/syft/source/directory_resolver.go b/syft/source/directory_resolver.go index c05d229c4..389914950 100644 --- a/syft/source/directory_resolver.go +++ b/syft/source/directory_resolver.go @@ -11,7 +11,7 @@ import ( "github.com/bmatcuk/doublestar" ) -var _ Resolver = &DirectoryResolver{} +var _ Resolver = (*DirectoryResolver)(nil) // DirectoryResolver implements path and content access for the directory data source. type DirectoryResolver struct { diff --git a/syft/source/image_squash_resolver.go b/syft/source/image_squash_resolver.go index 3df142a58..6be3422e9 100644 --- a/syft/source/image_squash_resolver.go +++ b/syft/source/image_squash_resolver.go @@ -7,7 +7,7 @@ import ( "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. type ImageSquashResolver struct {