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"
"bytes"
"github.com/deckarep/golang-set"
log "github.com/sirupsen/logrus"
"github.com/coreos/clair/database"
@ -34,6 +35,16 @@ func init() {
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) {
file, exists := files["lib/apk/db/installed"]
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
// package into a feature that will be stored in a set to guarantee
// uniqueness.
pkgSet := make(map[string]database.Feature)
ipkg := database.Feature{}
packages := mapset.NewSet()
pkg := featurefmt.PackageInfo{}
scanner := bufio.NewScanner(bytes.NewBuffer(file))
for scanner.Scan() {
line := scanner.Text()
if len(line) < 2 {
if valid(&pkg) {
addSourceVersion(&pkg)
packages.Add(pkg)
pkg.Reset()
}
continue
}
// Parse the package name or version.
switch {
case line[:2] == "P:":
ipkg.Name = line[2:]
case line[:2] == "V:":
switch line[:2] {
case "P:":
pkg.PackageName = line[2:]
case "V:":
version := string(line[2:])
err := versionfmt.Valid(dpkg.ParserName, version)
if err != nil {
log.WithError(err).WithField("version", version).Warning("could not parse package version. skipping")
continue
} else {
ipkg.Version = version
pkg.PackageVersion = version
}
case line == "":
// Restart if the parser reaches another package definition before
// 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{}
case "o:":
pkg.SourceName = line[2:]
}
}
// Convert the map into a slice and attach the version format
pkgs := make([]database.Feature, 0, len(pkgSet))
for _, pkg := range pkgSet {
pkg.VersionFormat = dpkg.ParserName
pkgs = append(pkgs, pkg)
// in case of no terminal line
if valid(&pkg) {
addSourceVersion(&pkg)
packages.Add(pkg)
}
return pkgs, nil
return featurefmt.PackageSetToFeatures(dpkg.ParserName, packages), nil
}
func (l lister) RequiredFilenames() []string {

View File

@ -17,38 +17,30 @@ package apk
import (
"testing"
"github.com/coreos/clair/database"
"github.com/coreos/clair/ext/featurefmt/featurefmttest"
"github.com/coreos/clair/ext/featurefmt"
"github.com/coreos/clair/ext/versionfmt/dpkg"
"github.com/coreos/clair/pkg/tarutil"
)
func TestAPKFeatureDetection(t *testing.T) {
testFeatures := []database.Feature{
{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{
for _, test := range []featurefmt.TestCase{
{
Features: testFeatures,
Files: tarutil.FilesMap{
"lib/apk/db/installed": featurefmttest.LoadFileForTest("apk/testdata/installed"),
"valid case",
map[string]string{"lib/apk/db/installed": "apk/testdata/valid"},
[]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)
}