2017-01-13 23:49:02 +00:00
|
|
|
// Copyright 2017 clair authors
|
2016-11-10 20:05:47 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-01-13 23:49:02 +00:00
|
|
|
// Package apk implements a featurefmt.Lister for APK packages.
|
2016-11-10 20:05:47 +00:00
|
|
|
package apk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
|
2018-10-10 21:23:52 +00:00
|
|
|
"github.com/deckarep/golang-set"
|
2017-05-04 17:21:25 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2016-12-28 01:45:11 +00:00
|
|
|
|
2016-11-10 20:05:47 +00:00
|
|
|
"github.com/coreos/clair/database"
|
2017-01-13 23:49:02 +00:00
|
|
|
"github.com/coreos/clair/ext/featurefmt"
|
2016-12-28 01:45:11 +00:00
|
|
|
"github.com/coreos/clair/ext/versionfmt"
|
2017-01-03 21:00:20 +00:00
|
|
|
"github.com/coreos/clair/ext/versionfmt/dpkg"
|
2017-01-13 23:49:02 +00:00
|
|
|
"github.com/coreos/clair/pkg/tarutil"
|
2016-11-10 20:05:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2018-09-19 18:31:15 +00:00
|
|
|
featurefmt.RegisterLister("apk", "1.0", &lister{})
|
2016-11-10 20:05:47 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 23:49:02 +00:00
|
|
|
type lister struct{}
|
2016-11-10 20:05:47 +00:00
|
|
|
|
2018-10-15 20:26:24 +00:00
|
|
|
func valid(pkg *database.Feature) bool {
|
|
|
|
return pkg.Name != "" && pkg.Version != ""
|
2018-10-10 21:23:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 09:24:14 +00:00
|
|
|
func (l lister) ListFeatures(files tarutil.FilesMap) ([]database.LayerFeature, error) {
|
2017-01-13 23:49:02 +00:00
|
|
|
file, exists := files["lib/apk/db/installed"]
|
2016-11-10 20:05:47 +00:00
|
|
|
if !exists {
|
2019-03-06 09:24:14 +00:00
|
|
|
return []database.LayerFeature{}, nil
|
2016-11-10 20:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2018-10-10 21:23:52 +00:00
|
|
|
packages := mapset.NewSet()
|
2018-10-15 20:26:24 +00:00
|
|
|
pkg := database.Feature{VersionFormat: dpkg.ParserName}
|
2016-11-10 20:05:47 +00:00
|
|
|
scanner := bufio.NewScanner(bytes.NewBuffer(file))
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if len(line) < 2 {
|
2018-10-10 21:23:52 +00:00
|
|
|
if valid(&pkg) {
|
2019-02-19 21:38:07 +00:00
|
|
|
pkg.Type = database.BinaryPackage
|
2018-10-10 21:23:52 +00:00
|
|
|
packages.Add(pkg)
|
2018-10-15 20:26:24 +00:00
|
|
|
pkg = database.Feature{VersionFormat: dpkg.ParserName}
|
2018-10-10 21:23:52 +00:00
|
|
|
}
|
2016-11-10 20:05:47 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the package name or version.
|
2018-10-15 20:26:24 +00:00
|
|
|
// Alpine package doesn't have specific source package. The "origin"
|
|
|
|
// package is sub package.
|
2018-10-10 21:23:52 +00:00
|
|
|
switch line[:2] {
|
|
|
|
case "P:":
|
2018-10-15 20:26:24 +00:00
|
|
|
pkg.Name = line[2:]
|
2018-10-10 21:23:52 +00:00
|
|
|
case "V:":
|
2016-12-28 01:45:11 +00:00
|
|
|
version := string(line[2:])
|
2017-01-03 21:00:20 +00:00
|
|
|
err := versionfmt.Valid(dpkg.ParserName, version)
|
2016-11-10 20:05:47 +00:00
|
|
|
if err != nil {
|
2017-05-04 17:21:25 +00:00
|
|
|
log.WithError(err).WithField("version", version).Warning("could not parse package version. skipping")
|
2018-10-10 21:23:52 +00:00
|
|
|
continue
|
2016-12-28 01:45:11 +00:00
|
|
|
} else {
|
2018-10-15 20:26:24 +00:00
|
|
|
pkg.Version = version
|
2016-11-10 20:05:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 21:23:52 +00:00
|
|
|
// in case of no terminal line
|
|
|
|
if valid(&pkg) {
|
2019-02-19 21:38:07 +00:00
|
|
|
pkg.Type = database.BinaryPackage
|
2018-10-10 21:23:52 +00:00
|
|
|
packages.Add(pkg)
|
2016-11-10 20:05:47 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 12:53:58 +00:00
|
|
|
return database.ConvertFeatureSetToLayerFeatures(packages), nil
|
2016-11-10 20:05:47 +00:00
|
|
|
}
|
|
|
|
|
2017-01-13 23:49:02 +00:00
|
|
|
func (l lister) RequiredFilenames() []string {
|
2019-03-22 10:13:24 +00:00
|
|
|
return []string{"^lib/apk/db/installed"}
|
2016-11-10 20:05:47 +00:00
|
|
|
}
|