clair/ext/featurefmt/rpm/rpm.go

209 lines
6.1 KiB
Go
Raw Normal View History

2017-01-13 07:08:52 +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 rpm implements a featurefmt.Lister for rpm packages.
package rpm
2015-11-13 19:11:28 +00:00
import (
"bufio"
"fmt"
2015-11-13 19:11:28 +00:00
"io/ioutil"
"os"
"os/exec"
2015-11-13 19:11:28 +00:00
"strings"
"github.com/deckarep/golang-set"
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/rpm"
2017-01-13 07:08:52 +00:00
"github.com/coreos/clair/pkg/commonerr"
"github.com/coreos/clair/pkg/strutil"
2017-01-13 23:49:02 +00:00
"github.com/coreos/clair/pkg/tarutil"
2015-11-13 19:11:28 +00:00
)
var ignoredPackages = []string{
"gpg-pubkey", // Ignore gpg-pubkey packages which are fake packages used to store GPG keys - they are not versionned properly.
}
2017-01-13 23:49:02 +00:00
type lister struct{}
2015-11-13 19:11:28 +00:00
func init() {
featurefmt.RegisterLister("rpm", "1.0", &lister{})
2015-11-13 19:11:28 +00:00
}
func isIgnored(packageName string) bool {
for _, pkg := range ignoredPackages {
if pkg == packageName {
return true
}
}
return false
}
func valid(pkg *database.Feature) bool {
return pkg.Name != "" && pkg.Version != "" &&
((pkg.SourceName == "" && pkg.SourceVersion != "") ||
(pkg.SourceName != "" && pkg.SourceVersion != ""))
}
func (l lister) ListFeatures(files tarutil.FilesMap) ([]database.Feature, error) {
2017-01-13 23:49:02 +00:00
f, hasFile := files["var/lib/rpm/Packages"]
2015-11-13 19:11:28 +00:00
if !hasFile {
return []database.Feature{}, nil
2015-11-13 19:11:28 +00:00
}
// Write the required "Packages" file to disk
tmpDir, err := ioutil.TempDir(os.TempDir(), "rpm")
defer os.RemoveAll(tmpDir)
if err != nil {
log.WithError(err).Error("could not create temporary folder for RPM detection")
return []database.Feature{}, commonerr.ErrFilesystem
2015-11-13 19:11:28 +00:00
}
err = ioutil.WriteFile(tmpDir+"/Packages", f, 0700)
if err != nil {
log.WithError(err).Error("could not create temporary file for RPM detection")
return []database.Feature{}, commonerr.ErrFilesystem
2015-11-13 19:11:28 +00:00
}
// Extract binary package names because RHSA refers to binary package names.
out, err := exec.Command("rpm", "--dbpath", tmpDir, "-qa", "--qf", "%{NAME} %{EPOCH}:%{VERSION}-%{RELEASE} %{SOURCERPM}\n").CombinedOutput()
2015-11-13 19:11:28 +00:00
if err != nil {
log.WithError(err).WithField("output", string(out)).Error("could not query RPM")
2015-11-13 19:11:28 +00:00
// Do not bubble up because we probably won't be able to fix it,
// the database must be corrupted
return []database.Feature{}, nil
2015-11-13 19:11:28 +00:00
}
packages := mapset.NewSet()
2015-11-13 19:11:28 +00:00
scanner := bufio.NewScanner(strings.NewReader(string(out)))
for scanner.Scan() {
line := strings.Split(scanner.Text(), " ")
if len(line) != 3 {
2015-11-13 19:11:28 +00:00
// We may see warnings on some RPM versions:
// "warning: Generating 12 missing index(es), please wait..."
continue
}
if isIgnored(line[0]) {
2015-11-13 19:11:28 +00:00
continue
}
pkg := database.Feature{Name: line[0], VersionFormat: rpm.ParserName}
pkg.Version = strings.Replace(line[1], "(none):", "", -1)
if err := versionfmt.Valid(rpm.ParserName, pkg.Version); err != nil {
log.WithError(err).WithField("version", line[1]).Warning("skipped unparseable package")
2015-11-13 19:11:28 +00:00
continue
}
if err := parseSourceRPM(line[2], &pkg); err != nil {
log.WithError(err).WithField("sourcerpm", line[2]).Warning("skipped unparseable package")
continue
}
2015-11-13 19:11:28 +00:00
if valid(&pkg) {
packages.Add(pkg)
}
2015-11-13 19:11:28 +00:00
}
return database.ConvertFeatureSetToFeatures(packages), nil
2015-11-13 19:11:28 +00:00
}
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/rpm/Packages"}
}
type rpmParserState string
const (
terminate rpmParserState = "terminate"
parseRPM rpmParserState = "RPM Token"
parseArchitecture rpmParserState = "Architecture Token"
parseRelease rpmParserState = "Release Token"
parseVersion rpmParserState = "Version Token"
)
// parseSourceRPM parses the source rpm package representation string
// http://ftp.rpm.org/max-rpm/ch-rpm-file-format.html
func parseSourceRPM(sourceRPM string, pkg *database.Feature) error {
state := parseRPM
previousCheckPoint := len(sourceRPM)
release := ""
version := ""
for i := len(sourceRPM) - 1; i >= 0; i-- {
switch state {
case parseRPM:
if string(sourceRPM[i]) == "." {
state = parseArchitecture
packageType := strutil.Substring(sourceRPM, i+1, len(sourceRPM))
previousCheckPoint = i
if packageType != "rpm" {
return fmt.Errorf("unexpected package type, expect: 'rpm', got: '%s'", packageType)
}
}
case parseArchitecture:
if string(sourceRPM[i]) == "." {
state = parseRelease
architecture := strutil.Substring(sourceRPM, i+1, previousCheckPoint)
previousCheckPoint = i
if architecture != "src" && architecture != "nosrc" {
return fmt.Errorf("unexpected package architecture, expect: 'src' or 'nosrc', got: '%s'", architecture)
}
}
case parseRelease:
if string(sourceRPM[i]) == "-" {
state = parseVersion
release = strutil.Substring(sourceRPM, i+1, previousCheckPoint)
previousCheckPoint = i
if release == "" {
return fmt.Errorf("unexpected package release, expect: not empty")
}
}
case parseVersion:
if string(sourceRPM[i]) == "-" {
// terminate state
state = terminate
version = strutil.Substring(sourceRPM, i+1, previousCheckPoint)
previousCheckPoint = i
if version == "" {
return fmt.Errorf("unexpected package version, expect: not empty")
}
break
}
}
}
if state != terminate {
return fmt.Errorf("unexpected termination while parsing '%s'", state)
}
concatVersion := version + "-" + release
if err := versionfmt.Valid(rpm.ParserName, concatVersion); err != nil {
return err
}
name := strutil.Substring(sourceRPM, 0, previousCheckPoint)
if name == "" {
return fmt.Errorf("unexpected package name, expect: not empty")
}
pkg.SourceName = name
pkg.SourceVersion = concatVersion
return nil
}