Normalize the json image/dir source (#180)

* normalize the json image/dir source

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>

* update json image presenter golden file

Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
Alex Goodman 2020-09-25 14:07:38 -04:00 committed by GitHub
parent c46d004a3b
commit 49800b6747
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 136 additions and 81 deletions

View File

@ -200,6 +200,9 @@
} }
] ]
}, },
"name": {
"type": "string"
},
"originPackage": { "originPackage": {
"type": "string" "type": "string"
}, },
@ -444,9 +447,15 @@
"source": { "source": {
"type": "string" "type": "string"
}, },
"sourceRpm": {
"type": "string"
},
"url": { "url": {
"type": "string" "type": "string"
}, },
"vendor": {
"type": "string"
},
"version": { "version": {
"type": "string" "type": "string"
} }
@ -474,61 +483,78 @@
}, },
"type": "array" "type": "array"
}, },
"directory": { "source": {
"type": "string"
},
"image": {
"properties": { "properties": {
"digest": { "target": {
"type": "string" "anyOf": [
}, {
"layers": { "type": "string"
"items": {
"properties": {
"digest": {
"type": "string"
},
"mediaType": {
"type": "string"
},
"size": {
"type": "integer"
}
}, },
"required": [ {
"digest", "properties": {
"mediaType", "digest": {
"size" "type": "string"
], },
"type": "object" "layers": {
}, "items": {
"type": "array" "properties": {
"digest": {
"type": "string"
},
"mediaType": {
"type": "string"
},
"size": {
"type": "integer"
}
},
"required": [
"digest",
"mediaType",
"size"
],
"type": "object"
},
"type": "array"
},
"mediaType": {
"type": "string"
},
"size": {
"type": "integer"
},
"tags": {
"items": {
"type": "string"
},
"type": "array"
}
},
"required": [
"digest",
"layers",
"mediaType",
"size",
"tags"
],
"type": "object"
}
]
}, },
"mediaType": { "type": {
"type": "string" "type": "string"
},
"size": {
"type": "integer"
},
"tags": {
"items": {
"type": "string"
},
"type": "array"
} }
}, },
"required": [ "required": [
"digest", "target",
"layers", "type"
"mediaType",
"size",
"tags"
], ],
"type": "object" "type": "object"
} }
}, },
"required": [ "required": [
"artifacts" "artifacts",
"source"
], ],
"type": "object" "type": "object"
} }

View File

@ -3,7 +3,7 @@ A "one-stop-shop" for helper utilities for all major functionality provided by c
Here is what the main execution path for syft does: Here is what the main execution path for syft does:
1. Parse a user image string to get a stereoscope image.Image object 1. Parse a user image string to get a stereoscope image.Source object
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

View File

@ -1,16 +1,13 @@
package json package json
import ( import (
"fmt"
"github.com/anchore/syft/syft/pkg" "github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/scope" "github.com/anchore/syft/syft/scope"
) )
type Document struct { type Document struct {
Artifacts []Artifact `json:"artifacts"` Artifacts []Artifact `json:"artifacts"`
Image *Image `json:"image,omitempty"` Source Source `json:"source"`
Directory *string `json:"directory,omitempty"`
} }
func NewDocument(catalog *pkg.Catalog, s scope.Scope) (Document, error) { func NewDocument(catalog *pkg.Catalog, s scope.Scope) (Document, error) {
@ -18,15 +15,11 @@ func NewDocument(catalog *pkg.Catalog, s scope.Scope) (Document, error) {
Artifacts: make([]Artifact, 0), Artifacts: make([]Artifact, 0),
} }
srcObj := s.Source() src, err := NewSource(s)
switch src := srcObj.(type) { if err != nil {
case scope.ImageSource: return Document{}, nil
doc.Image = NewImage(src)
case scope.DirSource:
doc.Directory = &s.DirSrc.Path
default:
return Document{}, fmt.Errorf("unsupported source: %T", src)
} }
doc.Source = src
for _, p := range catalog.Sorted() { for _, p := range catalog.Sorted() {
art, err := NewArtifact(p, s) art, err := NewArtifact(p, s)

View File

@ -0,0 +1,30 @@
package json
import (
"fmt"
"github.com/anchore/syft/syft/scope"
)
type Source struct {
Type string `json:"type"`
Target interface{} `json:"target"`
}
func NewSource(s scope.Scope) (Source, error) {
srcObj := s.Source()
switch src := srcObj.(type) {
case scope.ImageSource:
return Source{
Type: "image",
Target: NewImage(src),
}, nil
case scope.DirSource:
return Source{
Type: "directory",
Target: s.DirSrc.Path,
}, nil
default:
return Source{}, fmt.Errorf("unsupported source: %T", src)
}
}

View File

@ -23,5 +23,8 @@
] ]
} }
], ],
"directory": "/some/path" "source": {
"type": "directory",
"target": "/some/path"
}
} }

View File

@ -29,29 +29,32 @@
] ]
} }
], ],
"image": { "source": {
"layers": [ "type": "image",
{ "target": {
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", "layers": [
"digest": "sha256:e158b57d6f5a96ef5fd22f2fe76c70b5ba6ff5b2619f9d83125b2aad0492ac7b", {
"size": 22 "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
}, "digest": "sha256:e158b57d6f5a96ef5fd22f2fe76c70b5ba6ff5b2619f9d83125b2aad0492ac7b",
{ "size": 22
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", },
"digest": "sha256:da21056e7bf4308ecea0c0836848a7fe92f38fdcf35bc09ee6d98e7ab7beeebf", {
"size": 16 "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
}, "digest": "sha256:da21056e7bf4308ecea0c0836848a7fe92f38fdcf35bc09ee6d98e7ab7beeebf",
{ "size": 16
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", },
"digest": "sha256:f0e18aa6032c24659a9c741fc36ca56f589782ea132061ccf6f52b952403da94", {
"size": 27 "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
} "digest": "sha256:f0e18aa6032c24659a9c741fc36ca56f589782ea132061ccf6f52b952403da94",
], "size": 27
"size": 65, }
"digest": "sha256:2731251dc34951c0e50fcc643b4c5f74922dad1a5d98f302b504cf46cd5d9368", ],
"mediaType": "application/vnd.docker.distribution.manifest.v2+json", "size": 65,
"tags": [ "digest": "sha256:2731251dc34951c0e50fcc643b4c5f74922dad1a5d98f302b504cf46cd5d9368",
"stereoscope-fixture-image-simple:04e16e44161c8888a1a963720fd0443cbf7eef8101434c431de8725cd98cc9f7" "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
] "tags": [
"stereoscope-fixture-image-simple:04e16e44161c8888a1a963720fd0443cbf7eef8101434c431de8725cd98cc9f7"
]
}
} }
} }