add analysis coordination (with dummy analyzer)

This commit is contained in:
Alex Goodman
2020-05-18 11:59:33 -04:00
parent 4cbed05358
commit 1342cc5b06
13 changed files with 260 additions and 161 deletions
+3 -3
View File
@@ -2,13 +2,13 @@ package analyzer
import (
"github.com/anchore/imgbom/imgbom/pkg"
"github.com/anchore/imgbom/imgbom/scope"
"github.com/anchore/stereoscope/pkg/file"
"github.com/anchore/stereoscope/pkg/tree"
)
type Analyzer interface {
SelectFiles(scope.Scope) []file.Reference
SelectFiles([]*tree.FileTree) []file.Reference
// NOTE: one of the errors which is returned is "IterationNeeded", which indicates to the driver to
// continue with another Select/Analyze pass
Analyze(pkg.CatalogWriter, map[file.Reference]string) error
Analyze(map[file.Reference]string) ([]pkg.Package, error)
}
+47 -3
View File
@@ -1,11 +1,24 @@
package analyzer
import (
"github.com/anchore/imgbom/imgbom/analyzer/dummy"
"github.com/anchore/imgbom/imgbom/pkg"
"github.com/anchore/imgbom/imgbom/scope"
"github.com/anchore/stereoscope/pkg/file"
"github.com/hashicorp/go-multierror"
)
var controllerInstance controller
func init() {
controllerInstance = controller{
analyzers: make([]Analyzer, 0),
}
controllerInstance.add(dummy.NewAnalyzer())
}
func Analyze(s scope.Scope) (pkg.Catalog, error) {
return controllerInstance.analyze(s)
}
type controller struct {
@@ -16,6 +29,37 @@ func (c *controller) add(a Analyzer) {
c.analyzers = append(c.analyzers, a)
}
func Add(a Analyzer) {
controllerInstance.add(a)
}
func (c *controller) analyze(s scope.Scope) (pkg.Catalog, error) {
catalog := pkg.NewCatalog()
fileSelection := make([]file.Reference, 0)
// ask analyzers for files to extract from the image tar
for _, a := range c.analyzers {
fileSelection = append(fileSelection, a.SelectFiles(s.Trees)...)
}
// fetch contents for requested selection by analyzers
contents, err := s.Image.MultipleFileContentsByRef(fileSelection...)
if err != nil {
return pkg.Catalog{}, err
}
// perform analysis, accumulating errors for each failed analysis
var errs error
for _, a := range c.analyzers {
packages, err := a.Analyze(contents)
// TODO: check for multiple rounds of analyses by Iterate error
if err != nil {
errs = multierror.Append(errs, err)
}
for _, p := range packages {
catalog.Add(p)
}
}
if errs != nil {
return pkg.Catalog{}, errs
}
return catalog, nil
}
+30
View File
@@ -0,0 +1,30 @@
package dummy
import (
"github.com/anchore/imgbom/imgbom/pkg"
"github.com/anchore/stereoscope/pkg/file"
"github.com/anchore/stereoscope/pkg/tree"
)
// TODO: delete me
type Analyzer struct{}
func NewAnalyzer() *Analyzer {
return &Analyzer{}
}
func (a *Analyzer) SelectFiles(trees []*tree.FileTree) []file.Reference {
return []file.Reference{*trees[0].File("/etc/centos-release")}
}
func (a *Analyzer) Analyze(contents map[file.Reference]string) ([]pkg.Package, error) {
return []pkg.Package{
{
Name: "dummy",
Version: "1.0.0",
Type: pkg.DebPkg,
Metadata: pkg.DummyPackage{Extra: "some extra metadata"},
},
}, nil
}