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"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-01-18 02:22:20 +00:00
|
|
|
"os/exec"
|
2015-11-13 19:11:28 +00:00
|
|
|
"strings"
|
|
|
|
|
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"
|
2017-01-13 23:49:02 +00:00
|
|
|
"github.com/coreos/clair/pkg/tarutil"
|
2015-11-13 19:11:28 +00:00
|
|
|
)
|
|
|
|
|
2017-01-13 23:49:02 +00:00
|
|
|
type lister struct{}
|
2015-11-13 19:11:28 +00:00
|
|
|
|
|
|
|
func init() {
|
2017-06-22 18:01:41 +00:00
|
|
|
featurefmt.RegisterLister("rpm", rpm.ParserName, &lister{})
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:22:29 +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/rpm/Packages"]
|
2015-11-13 19:11:28 +00:00
|
|
|
if !hasFile {
|
2017-07-26 23:22:29 +00:00
|
|
|
return []database.Feature{}, nil
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a map to store packages and ensure their uniqueness
|
2017-07-26 23:22:29 +00:00
|
|
|
packagesMap := make(map[string]database.Feature)
|
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")
|
2017-07-26 23:22:29 +00:00
|
|
|
return []database.Feature{}, 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")
|
2017-07-26 23:22:29 +00:00
|
|
|
return []database.Feature{}, 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.
|
|
|
|
out, err := exec.Command("rpm", "--dbpath", tmpDir, "-qa", "--qf", "%{NAME} %{EPOCH}:%{VERSION}-%{RELEASE}\n").CombinedOutput()
|
2015-11-13 19:11:28 +00:00
|
|
|
if err != nil {
|
2017-05-04 17:21:25 +00:00
|
|
|
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
|
2017-07-26 23:22:29 +00:00
|
|
|
return []database.Feature{}, nil
|
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) != 2 {
|
|
|
|
// We may see warnings on some RPM versions:
|
|
|
|
// "warning: Generating 12 missing index(es), please wait..."
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore gpg-pubkey packages which are fake packages used to store GPG keys - they are not versionned properly.
|
|
|
|
if line[0] == "gpg-pubkey" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse version
|
2016-12-28 01:45:11 +00:00
|
|
|
version := strings.Replace(line[1], "(none):", "", -1)
|
2017-01-03 21:00:20 +00:00
|
|
|
err := versionfmt.Valid(rpm.ParserName, version)
|
2015-11-13 19:11:28 +00:00
|
|
|
if err != nil {
|
2017-05-04 17:21:25 +00:00
|
|
|
log.WithError(err).WithField("version", line[1]).Warning("could not parse package version. skipping")
|
2015-11-13 19:11:28 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add package
|
2017-07-26 23:22:29 +00:00
|
|
|
pkg := database.Feature{
|
|
|
|
Name: line[0],
|
2015-12-28 20:03:29 +00:00
|
|
|
Version: version,
|
|
|
|
}
|
2017-07-26 23:22:29 +00:00
|
|
|
packagesMap[pkg.Name+"#"+pkg.Version] = pkg
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the map to a slice
|
2017-07-26 23:22:29 +00:00
|
|
|
packages := make([]database.Feature, 0, len(packagesMap))
|
2015-11-13 19:11:28 +00:00
|
|
|
for _, pkg := range packagesMap {
|
2017-07-26 23:22:29 +00:00
|
|
|
pkg.VersionFormat = rpm.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/rpm/Packages"}
|
|
|
|
}
|