Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 34 additions & 23 deletions json-schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,37 @@
"artifacts": {
"items": {
"properties": {
"found-by": {
"items": {
"type": "string"
},
"type": "array"
},
"locations": {
"items": {
"anyOf": [
{
"type": "string"
},
{
"properties": {
"layer-index": {
"type": "integer"
},
"path": {
"type": "string"
}
},
"required": [
"layer-index",
"path"
],
"type": "object"
}
]
},
"type": "array"
},
"metadata": {
"properties": {
"architecture": {
Expand Down Expand Up @@ -425,27 +456,6 @@
"name": {
"type": "string"
},
"sources": {
"items": {
"properties": {
"found-by": {
"type": "string"
},
"locations": {
"items": {
"type": "string"
},
"type": "array"
}
},
"required": [
"found-by",
"locations"
],
"type": "object"
},
"type": "array"
},
"type": {
"type": "string"
},
Expand All @@ -454,8 +464,9 @@
}
},
"required": [
"found-by",
"locations",
"name",
"sources",
"type",
"version"
],
Expand Down Expand Up @@ -520,4 +531,4 @@
"artifacts"
],
"type": "object"
}
}
31 changes: 31 additions & 0 deletions syft/presenter/json/artifact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package json

import (
"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/scope"
)

type Artifact struct {
Name string `json:"name"`
Version string `json:"version"`
Type string `json:"type"`
FoundBy []string `json:"found-by"`
Locations Locations `json:"locations,omitempty"`
Metadata interface{} `json:"metadata,omitempty"`
}

func NewArtifact(p *pkg.Package, s scope.Scope) (Artifact, error) {
locations, err := NewLocations(p, s)
if err != nil {
return Artifact{}, err
}

return Artifact{
Name: p.Name,
Version: p.Version,
Type: string(p.Type),
FoundBy: []string{p.FoundBy},
Locations: locations,
Metadata: p.Metadata,
}, nil
}
40 changes: 40 additions & 0 deletions syft/presenter/json/document.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package json

import (
"fmt"

"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/scope"
)

type Document struct {
Artifacts []Artifact `json:"artifacts"`
Image *Image `json:"image,omitempty"`
Directory *string `json:"directory,omitempty"`
}

func NewDocument(catalog *pkg.Catalog, s scope.Scope) (Document, error) {
doc := Document{
Artifacts: make([]Artifact, 0),
}

srcObj := s.Source()
switch src := srcObj.(type) {
case scope.ImageSource:
doc.Image = NewImage(src)
case scope.DirSource:
doc.Directory = &s.DirSrc.Path
default:
return Document{}, fmt.Errorf("unsupported source: %T", src)
}

for _, p := range catalog.Sorted() {
art, err := NewArtifact(p, s)
if err != nil {
return Document{}, err
}
doc.Artifacts = append(doc.Artifacts, art)
}

return doc, nil
}
42 changes: 42 additions & 0 deletions syft/presenter/json/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package json

import "github.com/anchore/syft/syft/scope"

type Image struct {
Layers []Layer `json:"layers"`
Size int64 `json:"size"`
Digest string `json:"digest"`
MediaType string `json:"media-type"`
Tags []string `json:"tags"`
}

type Layer struct {
MediaType string `json:"media-type"`
Digest string `json:"digest"`
Size int64 `json:"size"`
}

func NewImage(src scope.ImageSource) *Image {
// populate artifacts...
tags := make([]string, len(src.Img.Metadata.Tags))
for idx, tag := range src.Img.Metadata.Tags {
tags[idx] = tag.String()
}
img := Image{
Digest: src.Img.Metadata.Digest,
Size: src.Img.Metadata.Size,
MediaType: string(src.Img.Metadata.MediaType),
Tags: tags,
Layers: make([]Layer, len(src.Img.Layers)),
}

// populate image metadata
for idx, l := range src.Img.Layers {
img.Layers[idx] = Layer{
MediaType: string(l.Metadata.MediaType),
Digest: l.Metadata.Digest,
Size: l.Metadata.Size,
}
}
return &img
}
46 changes: 46 additions & 0 deletions syft/presenter/json/location.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package json

import (
"fmt"

"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/scope"
)

type Locations interface{}

type ImageLocation struct {
Path string `json:"path"`
LayerIndex uint `json:"layer-index"`
}

func NewLocations(p *pkg.Package, s scope.Scope) (Locations, error) {
srcObj := s.Source()
switch src := srcObj.(type) {
case scope.ImageSource:
locations := make([]ImageLocation, len(p.Source))
for idx := range p.Source {
entry, err := src.Img.FileCatalog.Get(p.Source[idx])
if err != nil {
return nil, fmt.Errorf("unable to find layer index for source-idx=%d package=%s", idx, p.Name)
}

artifactSource := ImageLocation{
LayerIndex: entry.Source.Metadata.Index,
Path: string(p.Source[idx].Path),
}

locations[idx] = artifactSource
}
return locations, nil

case scope.DirSource:
locations := make([]string, len(p.Source))
for idx := range p.Source {
locations[idx] = string(p.Source[idx].Path)
}
return locations, nil
default:
return nil, fmt.Errorf("unable to determine source: %T", src)
}
}
90 changes: 3 additions & 87 deletions syft/presenter/json/presenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package json

import (
"encoding/json"
"fmt"
"io"

"github.com/anchore/syft/syft/pkg"
Expand All @@ -21,93 +20,10 @@ func NewPresenter(catalog *pkg.Catalog, s scope.Scope) *Presenter {
}
}

type document struct {
Artifacts []artifact `json:"artifacts"`
Image *image `json:"image,omitempty"`
Directory *string `json:"directory,omitempty"`
}

type image struct {
Layers []layer `json:"layers"`
Size int64 `json:"size"`
Digest string `json:"digest"`
MediaType string `json:"media-type"`
Tags []string `json:"tags"`
}

type layer struct {
MediaType string `json:"media-type"`
Digest string `json:"digest"`
Size int64 `json:"size"`
}

type source struct {
FoundBy string `json:"found-by"`
Locations []string `json:"locations"`
}

type artifact struct {
Name string `json:"name"`
Version string `json:"version"`
Type string `json:"type"`
Sources []source `json:"sources"`
Metadata interface{} `json:"metadata,omitempty"`
}

func (pres *Presenter) Present(output io.Writer) error {
doc := document{
Artifacts: make([]artifact, 0),
}

srcObj := pres.scope.Source()
switch src := srcObj.(type) {
case scope.ImageSource:
// populate artifacts...
tags := make([]string, len(src.Img.Metadata.Tags))
for idx, tag := range src.Img.Metadata.Tags {
tags[idx] = tag.String()
}
doc.Image = &image{
Digest: src.Img.Metadata.Digest,
Size: src.Img.Metadata.Size,
MediaType: string(src.Img.Metadata.MediaType),
Tags: tags,
Layers: make([]layer, len(src.Img.Layers)),
}

// populate image metadata
for idx, l := range src.Img.Layers {
doc.Image.Layers[idx] = layer{
MediaType: string(l.Metadata.MediaType),
Digest: l.Metadata.Digest,
Size: l.Metadata.Size,
}
}

case scope.DirSource:
doc.Directory = &pres.scope.DirSrc.Path
default:
return fmt.Errorf("unsupported source: %T", src)
}

for _, p := range pres.catalog.Sorted() {
art := artifact{
Name: p.Name,
Version: p.Version,
Type: string(p.Type),
Sources: make([]source, len(p.Source)),
Metadata: p.Metadata,
}

for idx := range p.Source {
srcObj := source{
FoundBy: p.FoundBy,
Locations: []string{string(p.Source[idx].Path)},
}
art.Sources[idx] = srcObj
}

doc.Artifacts = append(doc.Artifacts, art)
doc, err := NewDocument(pres.catalog, pres.scope)
if err != nil {
return err
}

enc := json.NewEncoder(output)
Expand Down
14 changes: 12 additions & 2 deletions syft/presenter/json/presenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@ func TestJsonDirsPresenter(t *testing.T) {
Name: "package-1",
Version: "1.0.1",
Type: pkg.DebPkg,
FoundBy: "the-cataloger-1",
Source: []file.Reference{
{Path: "/some/path/pkg1"},
},
})
catalog.Add(pkg.Package{
Name: "package-2",
Version: "2.0.1",
Type: pkg.DebPkg,
FoundBy: "the-cataloger-2",
Source: []file.Reference{
{Path: "/some/path/pkg1"},
},
})

s, err := scope.NewScopeFromDir("/some/path", scope.AllLayersScope)
Expand Down Expand Up @@ -77,15 +85,17 @@ func TestJsonImgsPresenter(t *testing.T) {
Source: []file.Reference{
*img.SquashedTree().File("/somefile-1.txt"),
},
Type: pkg.DebPkg,
Type: pkg.DebPkg,
FoundBy: "the-cataloger-1",
})
catalog.Add(pkg.Package{
Name: "package-2",
Version: "2.0.1",
Source: []file.Reference{
*img.SquashedTree().File("/somefile-2.txt"),
},
Type: pkg.DebPkg,
Type: pkg.DebPkg,
FoundBy: "the-cataloger-2",
})

s, err := scope.NewScopeFromImage(img, scope.AllLayersScope)
Expand Down
Loading