clair/ext/featurefmt/dpkg/dpkg.go

124 lines
3.7 KiB
Go
Raw Normal View History

2017-01-13 23:49:02 +00:00
// Copyright 2017 clair authors
2015-11-13 19:11:28 +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 dpkg implements a featurefmt.Lister for dpkg packages.
package dpkg
2015-11-13 19:11:28 +00:00
import (
"bufio"
"regexp"
"strings"
log "github.com/sirupsen/logrus"
2015-11-13 19:11:28 +00:00
"github.com/coreos/clair/database"
2017-01-13 23:49:02 +00:00
"github.com/coreos/clair/ext/featurefmt"
"github.com/coreos/clair/ext/versionfmt"
"github.com/coreos/clair/ext/versionfmt/dpkg"
2017-01-13 23:49:02 +00:00
"github.com/coreos/clair/pkg/tarutil"
2015-11-13 19:11:28 +00:00
)
var (
dpkgSrcCaptureRegexp = regexp.MustCompile(`Source: (?P<name>[^\s]*)( \((?P<version>.*)\))?`)
dpkgSrcCaptureRegexpNames = dpkgSrcCaptureRegexp.SubexpNames()
)
2017-01-13 23:49:02 +00:00
type lister struct{}
2015-11-13 19:11:28 +00:00
func init() {
featurefmt.RegisterLister("dpkg", dpkg.ParserName, &lister{})
2015-11-13 19:11:28 +00:00
}
func (l lister) ListFeatures(files tarutil.FilesMap) ([]database.Feature, error) {
2017-01-13 23:49:02 +00:00
f, hasFile := files["var/lib/dpkg/status"]
2015-11-13 19:11:28 +00:00
if !hasFile {
return []database.Feature{}, nil
2015-11-13 19:11:28 +00:00
}
// Create a map to store packages and ensure their uniqueness
packagesMap := make(map[string]database.Feature)
2015-11-13 19:11:28 +00:00
var pkg database.Feature
2015-11-13 19:11:28 +00:00
var err error
scanner := bufio.NewScanner(strings.NewReader(string(f)))
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "Package: ") {
// Package line
// Defines the name of the package
pkg.Name = strings.TrimSpace(strings.TrimPrefix(line, "Package: "))
pkg.Version = ""
} else if strings.HasPrefix(line, "Source: ") {
2015-11-13 19:11:28 +00:00
// Source line (Optionnal)
// Gives the name of the source package
// May also specifies a version
srcCapture := dpkgSrcCaptureRegexp.FindAllStringSubmatch(line, -1)[0]
md := map[string]string{}
for i, n := range srcCapture {
md[dpkgSrcCaptureRegexpNames[i]] = strings.TrimSpace(n)
}
pkg.Name = md["name"]
2015-11-13 19:11:28 +00:00
if md["version"] != "" {
version := md["version"]
err = versionfmt.Valid(dpkg.ParserName, version)
2015-11-13 19:11:28 +00:00
if err != nil {
log.WithError(err).WithField("version", string(line[1])).Warning("could not parse package version. skipping")
} else {
pkg.Version = version
2015-11-13 19:11:28 +00:00
}
}
} else if strings.HasPrefix(line, "Version: ") && pkg.Version == "" {
2015-11-13 19:11:28 +00:00
// Version line
// Defines the version of the package
// This version is less important than a version retrieved from a Source line
// because the Debian vulnerabilities often skips the epoch from the Version field
// which is not present in the Source version, and because +bX revisions don't matter
version := strings.TrimPrefix(line, "Version: ")
err = versionfmt.Valid(dpkg.ParserName, version)
2015-11-13 19:11:28 +00:00
if err != nil {
log.WithError(err).WithField("version", string(line[1])).Warning("could not parse package version. skipping")
} else {
pkg.Version = version
2015-11-13 19:11:28 +00:00
}
} else if line == "" {
pkg.Name = ""
pkg.Version = ""
2015-11-13 19:11:28 +00:00
}
// Add the package to the result array if we have all the informations
if pkg.Name != "" && pkg.Version != "" {
packagesMap[pkg.Name+"#"+pkg.Version] = pkg
pkg.Name = ""
pkg.Version = ""
2015-11-13 19:11:28 +00:00
}
}
// Convert the map to a slice and add version format.
packages := make([]database.Feature, 0, len(packagesMap))
2015-11-13 19:11:28 +00:00
for _, pkg := range packagesMap {
pkg.VersionFormat = dpkg.ParserName
2015-11-13 19:11:28 +00:00
packages = append(packages, pkg)
}
return packages, nil
}
2017-01-13 23:49:02 +00:00
func (l lister) RequiredFilenames() []string {
2015-11-13 19:11:28 +00:00
return []string{"var/lib/dpkg/status"}
}