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.
|
2015-12-28 20:03:29 +00:00
|
|
|
package rpm
|
2015-11-13 19:11:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2018-10-10 20:28:10 +00:00
|
|
|
"fmt"
|
2015-11-13 19:11:28 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-01-18 02:22:20 +00:00
|
|
|
"os/exec"
|
2015-11-13 19:11:28 +00:00
|
|
|
"strings"
|
|
|
|
|
2018-10-10 20:28:10 +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
|
|
|
|
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"
|
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/rpm"
|
2017-01-13 07:08:52 +00:00
|
|
|
"github.com/coreos/clair/pkg/commonerr"
|
2018-10-10 20:28:10 +00:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2018-10-10 20:28:10 +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() {
|
2018-09-19 18:31:15 +00:00
|
|
|
featurefmt.RegisterLister("rpm", "1.0", &lister{})
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 21:38:07 +00:00
|
|
|
func (l lister) RequiredFilenames() []string {
|
|
|
|
return []string{"var/lib/rpm/Packages"}
|
|
|
|
}
|
|
|
|
|
2018-10-10 20:28:10 +00:00
|
|
|
func isIgnored(packageName string) bool {
|
|
|
|
for _, pkg := range ignoredPackages {
|
|
|
|
if pkg == packageName {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
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
|
|
|
f, hasFile := files["var/lib/rpm/Packages"]
|
2015-11-13 19:11:28 +00:00
|
|
|
if !hasFile {
|
2019-03-06 09:24:14 +00:00
|
|
|
return []database.LayerFeature{}, 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 {
|
2017-05-04 17:21:25 +00:00
|
|
|
log.WithError(err).Error("could not create temporary folder for RPM detection")
|
2019-03-06 09:24:14 +00:00
|
|
|
return []database.LayerFeature{}, commonerr.ErrFilesystem
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(tmpDir+"/Packages", f, 0700)
|
|
|
|
if err != nil {
|
2017-05-04 17:21:25 +00:00
|
|
|
log.WithError(err).Error("could not create temporary file for RPM detection")
|
2019-03-06 09:24:14 +00:00
|
|
|
return []database.LayerFeature{}, commonerr.ErrFilesystem
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
2017-01-18 02:22:20 +00:00
|
|
|
// Extract binary package names because RHSA refers to binary package names.
|
2018-10-10 20:28:10 +00:00
|
|
|
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 {
|
2019-02-19 21:38:07 +00:00
|
|
|
log.WithError(err).WithField("output", string(out)).Error("failed to 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
|
2019-03-06 09:24:14 +00:00
|
|
|
return []database.LayerFeature{}, nil
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 20:28:10 +00:00
|
|
|
packages := mapset.NewSet()
|
2015-11-13 19:11:28 +00:00
|
|
|
scanner := bufio.NewScanner(strings.NewReader(string(out)))
|
|
|
|
for scanner.Scan() {
|
2019-02-19 21:38:07 +00:00
|
|
|
rpmPackage, srpmPackage := parseRPMOutput(scanner.Text())
|
|
|
|
if rpmPackage != nil {
|
|
|
|
packages.Add(*rpmPackage)
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 21:38:07 +00:00
|
|
|
if srpmPackage != nil {
|
|
|
|
packages.Add(*srpmPackage)
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
2019-02-19 21:38:07 +00:00
|
|
|
}
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2019-03-07 12:53:58 +00:00
|
|
|
return database.ConvertFeatureSetToLayerFeatures(packages), nil
|
2019-02-19 21:38:07 +00:00
|
|
|
}
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2019-02-19 21:38:07 +00:00
|
|
|
func parseRPMOutput(raw string) (rpmPackage *database.Feature, srpmPackage *database.Feature) {
|
|
|
|
line := strings.Split(raw, " ")
|
|
|
|
if len(line) != 3 {
|
|
|
|
// We may see warnings on some RPM versions:
|
|
|
|
// "warning: Generating 12 missing index(es), please wait..."
|
|
|
|
return
|
|
|
|
}
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2019-02-19 21:38:07 +00:00
|
|
|
if isIgnored(line[0]) {
|
|
|
|
return
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 21:38:07 +00:00
|
|
|
name, version, srpm := line[0], strings.Replace(line[1], "(none):", "", -1), line[2]
|
|
|
|
if err := versionfmt.Valid(rpm.ParserName, version); err != nil {
|
|
|
|
log.WithError(err).WithFields(log.Fields{"name": name, "version": version}).Warning("skipped unparseable package")
|
|
|
|
return
|
|
|
|
}
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2019-03-06 09:24:14 +00:00
|
|
|
rpmPackage = &database.Feature{name, version, rpm.ParserName, database.BinaryPackage}
|
2019-02-19 21:38:07 +00:00
|
|
|
srpmName, srpmVersion, srpmRelease, _, err := parseSourceRPM(srpm)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).WithFields(log.Fields{"name": name, "sourcerpm": srpm}).Warning("skipped unparseable package")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
srpmVersion = srpmVersion + "-" + srpmRelease
|
|
|
|
if err = versionfmt.Valid(rpm.ParserName, srpmVersion); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-06 09:24:14 +00:00
|
|
|
srpmPackage = &database.Feature{srpmName, srpmVersion, rpm.ParserName, database.SourcePackage}
|
2019-02-19 21:38:07 +00:00
|
|
|
return
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
2018-10-10 20:28:10 +00:00
|
|
|
|
|
|
|
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
|
2019-02-19 21:38:07 +00:00
|
|
|
func parseSourceRPM(sourceRPM string) (name string, version string, release string, architecture string, err error) {
|
2018-10-10 20:28:10 +00:00
|
|
|
state := parseRPM
|
|
|
|
previousCheckPoint := len(sourceRPM)
|
|
|
|
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" {
|
2019-02-19 21:38:07 +00:00
|
|
|
err = fmt.Errorf("unexpected package type, expect: 'rpm', got: '%s'", packageType)
|
|
|
|
return
|
2018-10-10 20:28:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case parseArchitecture:
|
|
|
|
if string(sourceRPM[i]) == "." {
|
|
|
|
state = parseRelease
|
2019-02-19 21:38:07 +00:00
|
|
|
architecture = strutil.Substring(sourceRPM, i+1, previousCheckPoint)
|
2018-10-10 20:28:10 +00:00
|
|
|
previousCheckPoint = i
|
|
|
|
if architecture != "src" && architecture != "nosrc" {
|
2019-02-19 21:38:07 +00:00
|
|
|
err = fmt.Errorf("unexpected package architecture, expect: 'src' or 'nosrc', got: '%s'", architecture)
|
|
|
|
return
|
2018-10-10 20:28:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case parseRelease:
|
|
|
|
if string(sourceRPM[i]) == "-" {
|
|
|
|
state = parseVersion
|
|
|
|
release = strutil.Substring(sourceRPM, i+1, previousCheckPoint)
|
|
|
|
previousCheckPoint = i
|
|
|
|
if release == "" {
|
2019-02-19 21:38:07 +00:00
|
|
|
err = fmt.Errorf("unexpected package release, expect: not empty")
|
|
|
|
return
|
2018-10-10 20:28:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case parseVersion:
|
|
|
|
if string(sourceRPM[i]) == "-" {
|
|
|
|
// terminate state
|
|
|
|
state = terminate
|
|
|
|
version = strutil.Substring(sourceRPM, i+1, previousCheckPoint)
|
|
|
|
previousCheckPoint = i
|
|
|
|
if version == "" {
|
2019-02-19 21:38:07 +00:00
|
|
|
err = fmt.Errorf("unexpected package version, expect: not empty")
|
|
|
|
return
|
2018-10-10 20:28:10 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if state != terminate {
|
2019-02-19 21:38:07 +00:00
|
|
|
err = fmt.Errorf("unexpected termination while parsing '%s'", state)
|
|
|
|
return
|
2018-10-10 20:28:10 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 21:38:07 +00:00
|
|
|
name = strutil.Substring(sourceRPM, 0, previousCheckPoint)
|
2018-10-10 20:28:10 +00:00
|
|
|
if name == "" {
|
2019-02-19 21:38:07 +00:00
|
|
|
err = fmt.Errorf("unexpected package name, expect: not empty")
|
|
|
|
return
|
2018-10-10 20:28:10 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 21:38:07 +00:00
|
|
|
return
|
2018-10-10 20:28:10 +00:00
|
|
|
}
|