ext/featurefmt/apk: Extract origin package information from database

"o" field is used to extract the Package Origin from the APK database.
This commit is contained in:
Sida Chen 2018-10-10 17:23:52 -04:00
parent a057e4a943
commit 2cc61f9fc0
3 changed files with 49 additions and 50 deletions

View File

@ -19,6 +19,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"github.com/deckarep/golang-set"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/coreos/clair/database" "github.com/coreos/clair/database"
@ -34,6 +35,16 @@ func init() {
type lister struct{} type lister struct{}
func valid(pkg *featurefmt.PackageInfo) bool {
return pkg.PackageName != "" && pkg.PackageVersion != ""
}
func addSourceVersion(pkg *featurefmt.PackageInfo) {
if pkg.SourceName != "" {
pkg.SourceVersion = pkg.PackageVersion
}
}
func (l lister) ListFeatures(files tarutil.FilesMap) ([]database.Feature, error) { func (l lister) ListFeatures(files tarutil.FilesMap) ([]database.Feature, error) {
file, exists := files["lib/apk/db/installed"] file, exists := files["lib/apk/db/installed"]
if !exists { if !exists {
@ -43,49 +54,45 @@ func (l lister) ListFeatures(files tarutil.FilesMap) ([]database.Feature, error)
// Iterate over each line in the "installed" file attempting to parse each // Iterate over each line in the "installed" file attempting to parse each
// package into a feature that will be stored in a set to guarantee // package into a feature that will be stored in a set to guarantee
// uniqueness. // uniqueness.
pkgSet := make(map[string]database.Feature) packages := mapset.NewSet()
ipkg := database.Feature{} pkg := featurefmt.PackageInfo{}
scanner := bufio.NewScanner(bytes.NewBuffer(file)) scanner := bufio.NewScanner(bytes.NewBuffer(file))
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
if len(line) < 2 { if len(line) < 2 {
if valid(&pkg) {
addSourceVersion(&pkg)
packages.Add(pkg)
pkg.Reset()
}
continue continue
} }
// Parse the package name or version. // Parse the package name or version.
switch { switch line[:2] {
case line[:2] == "P:": case "P:":
ipkg.Name = line[2:] pkg.PackageName = line[2:]
case line[:2] == "V:": case "V:":
version := string(line[2:]) version := string(line[2:])
err := versionfmt.Valid(dpkg.ParserName, version) err := versionfmt.Valid(dpkg.ParserName, version)
if err != nil { if err != nil {
log.WithError(err).WithField("version", version).Warning("could not parse package version. skipping") log.WithError(err).WithField("version", version).Warning("could not parse package version. skipping")
continue
} else { } else {
ipkg.Version = version pkg.PackageVersion = version
} }
case line == "": case "o:":
// Restart if the parser reaches another package definition before pkg.SourceName = line[2:]
// creating a valid package.
ipkg = database.Feature{}
}
// If we have a whole feature, store it in the set and try to parse a new
// one.
if ipkg.Name != "" && ipkg.Version != "" {
pkgSet[ipkg.Name+"#"+ipkg.Version] = ipkg
ipkg = database.Feature{}
} }
} }
// Convert the map into a slice and attach the version format // in case of no terminal line
pkgs := make([]database.Feature, 0, len(pkgSet)) if valid(&pkg) {
for _, pkg := range pkgSet { addSourceVersion(&pkg)
pkg.VersionFormat = dpkg.ParserName packages.Add(pkg)
pkgs = append(pkgs, pkg)
} }
return pkgs, nil return featurefmt.PackageSetToFeatures(dpkg.ParserName, packages), nil
} }
func (l lister) RequiredFilenames() []string { func (l lister) RequiredFilenames() []string {

View File

@ -17,38 +17,30 @@ package apk
import ( import (
"testing" "testing"
"github.com/coreos/clair/database" "github.com/coreos/clair/ext/featurefmt"
"github.com/coreos/clair/ext/featurefmt/featurefmttest"
"github.com/coreos/clair/ext/versionfmt/dpkg" "github.com/coreos/clair/ext/versionfmt/dpkg"
"github.com/coreos/clair/pkg/tarutil"
) )
func TestAPKFeatureDetection(t *testing.T) { func TestAPKFeatureDetection(t *testing.T) {
testFeatures := []database.Feature{ for _, test := range []featurefmt.TestCase{
{Name: "musl", Version: "1.1.14-r10"},
{Name: "busybox", Version: "1.24.2-r9"},
{Name: "alpine-baselayout", Version: "3.0.3-r0"},
{Name: "alpine-keys", Version: "1.1-r0"},
{Name: "zlib", Version: "1.2.8-r2"},
{Name: "libcrypto1.0", Version: "1.0.2h-r1"},
{Name: "libssl1.0", Version: "1.0.2h-r1"},
{Name: "apk-tools", Version: "2.6.7-r0"},
{Name: "scanelf", Version: "1.1.6-r0"},
{Name: "musl-utils", Version: "1.1.14-r10"},
{Name: "libc-utils", Version: "0.7-r0"},
}
for i := range testFeatures {
testFeatures[i].VersionFormat = dpkg.ParserName
}
testData := []featurefmttest.TestData{
{ {
Features: testFeatures, "valid case",
Files: tarutil.FilesMap{ map[string]string{"lib/apk/db/installed": "apk/testdata/valid"},
"lib/apk/db/installed": featurefmttest.LoadFileForTest("apk/testdata/installed"), []featurefmt.PackageInfo{
{"musl", "1.1.14-r10", "", ""},
{"busybox", "1.24.2-r9", "", ""},
{"alpine-baselayout", "3.0.3-r0", "", ""},
{"alpine-keys", "1.1-r0", "", ""},
{"zlib", "1.2.8-r2", "", ""},
{"libcrypto1.0", "1.0.2h-r1", "openssl", "1.0.2h-r1"},
{"libssl1.0", "1.0.2h-r1", "openssl", "1.0.2h-r1"},
{"apk-tools", "2.6.7-r0", "", ""},
{"scanelf", "1.1.6-r0", "pax-utils", "1.1.6-r0"},
{"musl-utils", "1.1.14-r10", "musl", "1.1.14-r10"},
{"libc-utils", "0.7-r0", "libc-dev", "0.7-r0"},
}, },
}, },
} {
featurefmt.RunTest(t, test, lister{}, dpkg.ParserName)
} }
featurefmttest.TestLister(t, &lister{}, testData)
} }