add image metadata as catalogFromJSON return

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman 2020-11-16 08:19:57 -05:00
parent 6f7a4fd3e4
commit 91baabe5a1
No known key found for this signature in database
GPG Key ID: 5CB45AE22BAB7EA7
6 changed files with 23 additions and 21 deletions

View File

@ -1,6 +1,6 @@
package file package file
// Locations: https://research.swtch.com/glob.go // Source: https://research.swtch.com/glob.go
func GlobMatch(pattern, name string) bool { func GlobMatch(pattern, name string) bool {
px := 0 px := 0
nx := 0 nx := 0

View File

@ -7,8 +7,8 @@ Here is what the main execution path for syft does:
2. Invoke all catalogers to catalog the image, adding discovered packages to a single catalog object 2. Invoke all catalogers to catalog the image, adding discovered packages to a single catalog object
3. Invoke a single presenter to show the contents of the catalog 3. Invoke a single presenter to show the contents of the catalog
A Locations object encapsulates the image object to be cataloged and the user options (catalog all layers vs. squashed layer), A Source object encapsulates the image object to be cataloged and the user options (catalog all layers vs. squashed layer),
providing a way to inspect paths and file content within the image. The Locations object, not the image object, is used providing a way to inspect paths and file content within the image. The Source object, not the image object, is used
throughout the main execution path. This abstraction allows for decoupling of what is cataloged (a docker image, an OCI throughout the main execution path. This abstraction allows for decoupling of what is cataloged (a docker image, an OCI
image, a filesystem, etc) and how it is cataloged (the individual catalogers). image, a filesystem, etc) and how it is cataloged (the individual catalogers).
@ -82,12 +82,12 @@ func CatalogFromScope(s source.Source) (*pkg.Catalog, error) {
return cataloger.Catalog(s.Resolver, catalogers...) return cataloger.Catalog(s.Resolver, catalogers...)
} }
// TODO: we shouldn't return the jsonPresenter.Image object! this is leaky // CatalogFromJSON takes an existing syft report and generates catalog primitives.
func CatalogFromJSON(reader io.Reader) (*pkg.Catalog, *distro.Distro, error) { func CatalogFromJSON(reader io.Reader) (*pkg.Catalog, *distro.Distro, *source.ImageMetadata, error) {
var doc jsonPresenter.Document var doc jsonPresenter.Document
decoder := json.NewDecoder(reader) decoder := json.NewDecoder(reader)
if err := decoder.Decode(&doc); err != nil { if err := decoder.Decode(&doc); err != nil {
return nil, nil, err return nil, nil, nil, err
} }
var pkgs = make([]pkg.Package, len(doc.Artifacts)) var pkgs = make([]pkg.Package, len(doc.Artifacts))
@ -106,16 +106,16 @@ func CatalogFromJSON(reader io.Reader) (*pkg.Catalog, *distro.Distro, error) {
d, err := distro.NewDistro(distroType, doc.Distro.Version, doc.Distro.IDLike) d, err := distro.NewDistro(distroType, doc.Distro.Version, doc.Distro.IDLike)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, nil, err
} }
//var theImg *jsonPresenter.Image var imageMetadata *source.ImageMetadata
//if doc.Locations.Type == "image" { if doc.Source.Type == "image" {
// img := doc.Locations.Target.(jsonPresenter.Image) payload := doc.Source.Target.(source.ImageMetadata)
// theImg = &img imageMetadata = &payload
//} }
return catalog, &d, nil return catalog, &d, imageMetadata, nil
} }
// SetLogger sets the logger object used for all syft logging calls. // SetLogger sets the logger object used for all syft logging calls.

View File

@ -8,7 +8,7 @@ import (
"github.com/anchore/syft/internal/version" "github.com/anchore/syft/internal/version"
) )
// Locations: https://cyclonedx.org/ext/bom-descriptor/ // Source: https://cyclonedx.org/ext/bom-descriptor/
// BomDescriptor represents all metadata surrounding the BOM report (such as when the BOM was made, with which tool, and the item being cataloged). // BomDescriptor represents all metadata surrounding the BOM report (such as when the BOM was made, with which tool, and the item being cataloged).
type BomDescriptor struct { type BomDescriptor struct {

View File

@ -9,7 +9,7 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
) )
// Locations: https://github.com/CycloneDX/specification // Source: https://github.com/CycloneDX/specification
// Document represents a CycloneDX BOM Document. // Document represents a CycloneDX BOM Document.
type Document struct { type Document struct {

View File

@ -35,7 +35,7 @@ func TestNewFromImage(t *testing.T) {
Layers: []*image.Layer{layer}, Layers: []*image.Layer{layer},
} }
t.Run("create a new Locations object from image", func(t *testing.T) { t.Run("create a new source object from image", func(t *testing.T) {
_, err := NewFromImage(&img, AllLayersScope, "") _, err := NewFromImage(&img, AllLayersScope, "")
if err != nil { if err != nil {
t.Errorf("unexpected error when creating a new Locations from img: %+v", err) t.Errorf("unexpected error when creating a new Locations from img: %+v", err)

View File

@ -31,24 +31,26 @@ func TestCatalogFromJSON(t *testing.T) {
tarPath := imagetest.GetFixtureImageTarPath(t, test.fixture) tarPath := imagetest.GetFixtureImageTarPath(t, test.fixture)
defer cleanup() defer cleanup()
expectedCatalog, s, expectedDistro, err := syft.Catalog("docker-archive:"+tarPath, source.AllLayersScope) expectedCatalog, expectedSource, expectedDistro, err := syft.Catalog("docker-archive:"+tarPath, source.AllLayersScope)
if err != nil { if err != nil {
t.Fatalf("failed to catalog image: %+v", err) t.Fatalf("failed to catalog image: %+v", err)
} }
var buf bytes.Buffer var buf bytes.Buffer
jsonPres := json.NewPresenter(expectedCatalog, s.Metadata, *expectedDistro) jsonPres := json.NewPresenter(expectedCatalog, expectedSource.Metadata, *expectedDistro)
if err = jsonPres.Present(&buf); err != nil { if err = jsonPres.Present(&buf); err != nil {
t.Fatalf("failed to write to presenter: %+v", err) t.Fatalf("failed to write to presenter: %+v", err)
} }
// TODO: test img actualCatalog, actualDistro, imageMetadata, err := syft.CatalogFromJSON(&buf)
actualCatalog, actualDistro, err := syft.CatalogFromJSON(&buf)
if err != nil { if err != nil {
t.Fatalf("failed to import document: %+v", err) t.Fatalf("failed to import document: %+v", err)
} }
for _, d := range deep.Equal(*imageMetadata, expectedSource.Metadata.ImageMetadata) {
t.Errorf(" image metadata diff: %+v", d)
}
for _, d := range deep.Equal(actualDistro, expectedDistro) { for _, d := range deep.Equal(actualDistro, expectedDistro) {
t.Errorf(" distro diff: %+v", d) t.Errorf(" distro diff: %+v", d)
} }