Merge pull request #29 from anchore/add-os-types

Start of distro identification support
This commit is contained in:
Alex Goodman 2020-06-01 10:47:31 -04:00 committed by GitHub
commit 83e486d6ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 116 additions and 17 deletions

View file

@ -55,6 +55,14 @@ func doRunCmd(cmd *cobra.Command, args []string) int {
}
defer stereoscope.Cleanup()
log.Info("Identifying Distro")
distro, err := imgbom.IdentifyDistro(img)
if err != nil {
log.Errorf("error identifying Distro: %w", err)
} else {
log.Info(" Distro: %s", distro)
}
log.Info("Cataloging image")
catalog, err := imgbom.CatalogImage(img, appConfig.ScopeOpt)
if err != nil {

1
go.mod
View file

@ -10,6 +10,7 @@ require (
github.com/google/go-containerregistry v0.0.0-20200521151920-a873a21aff23 // indirect
github.com/gopherjs/gopherjs v0.0.0-20190910122728-9d188e94fb99 // indirect
github.com/hashicorp/go-multierror v1.1.0
github.com/hashicorp/go-version v1.2.0
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.3.1
github.com/sergi/go-diff v1.1.0

2
go.sum
View file

@ -238,6 +238,8 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E=
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=

View file

@ -1,4 +0,0 @@
package imgbom
// note: must be a single word, all lowercase
const LibraryName = "imgbom"

31
imgbom/distro/distro.go Normal file
View file

@ -0,0 +1,31 @@
package distro
import (
"fmt"
hashiVer "github.com/hashicorp/go-version"
)
type Distro struct {
Type Type
Version *hashiVer.Version
}
func NewDistro(t Type, ver string) (Distro, error) {
verObj, err := hashiVer.NewVersion(ver)
if err != nil {
return Distro{}, fmt.Errorf("could not create distro version: %w", err)
}
return Distro{
Type: t,
Version: verObj,
}, nil
}
func (d Distro) MajorVersion() int {
return d.Version.Segments()[0]
}
func (d Distro) String() string {
return fmt.Sprintf("%s %s", d.Type, d.Version)
}

11
imgbom/distro/identify.go Normal file
View file

@ -0,0 +1,11 @@
package distro
import (
"github.com/anchore/stereoscope/pkg/image"
)
func Identify(img *image.Image) (Distro, error) {
// TODO: implement me based off of https://github.com/anchore/anchore-engine/blob/78b23d7e8f007005c070673405b5e23730a660e0/anchore_engine/analyzers/utils.py#L131
return NewDistro(UnknownDistro, "0.0.0")
}

52
imgbom/distro/type.go Normal file
View file

@ -0,0 +1,52 @@
package distro
const (
UnknownDistro Type = iota
Debian
// Ubuntu
// RedHat
// CentOS
// Fedora
// Alpine
// Busybox
// AmazonLinux
// OracleLinux
// ArchLinux
)
type Type int
var distroStr = []string{
"UnknownDistro",
"Debian",
// "Ubuntu",
// "RedHat",
// "CentOS",
// "Fedora",
// "Alpine",
// "Busybox",
// "AmazonLinux",
// "OracleLinux",
// "ArchLinux",
}
var All = []Type{
Debian,
// Ubuntu,
// RedHat,
// CentOS,
// Fedora,
// Alpine,
// Busybox,
// AmazonLinux,
// OracleLinux,
// ArchLinux,
}
func (t Type) String() string {
if int(t) >= len(distroStr) || t < 0 {
return distroStr[0]
}
return distroStr[t]
}

View file

@ -2,19 +2,27 @@ package imgbom
import (
"github.com/anchore/imgbom/imgbom/analyzer"
"github.com/anchore/imgbom/imgbom/distro"
"github.com/anchore/imgbom/imgbom/logger"
"github.com/anchore/imgbom/imgbom/pkg"
"github.com/anchore/imgbom/imgbom/scope"
"github.com/anchore/imgbom/internal/log"
"github.com/anchore/stereoscope/pkg/image"
)
// TODO: add os detection results as return value
func IdentifyDistro(img *image.Image) (distro.Distro, error) {
return distro.Identify(img)
}
func CatalogImage(img *image.Image, o scope.Option) (*pkg.Catalog, error) {
s, err := scope.NewScope(img, o)
if err != nil {
return nil, err
}
// TODO: add OS detection here...
return analyzer.Analyze(s)
}
func SetLogger(logger logger.Logger) {
log.Log = logger
}

View file

@ -1,10 +0,0 @@
package imgbom
import (
"github.com/anchore/imgbom/imgbom/logger"
"github.com/anchore/imgbom/internal/log"
)
func SetLogger(logger logger.Logger) {
log.Log = logger
}