vulnsrc: Refactor vulnerability sources to use utility functions

This commit is contained in:
Sida Chen 2018-10-19 16:38:54 -04:00
parent a3f7387ff1
commit 72674ca871
10 changed files with 78 additions and 162 deletions

View File

@ -18,10 +18,8 @@ package alpine
import ( import (
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
@ -30,10 +28,13 @@ import (
"github.com/coreos/clair/ext/versionfmt" "github.com/coreos/clair/ext/versionfmt"
"github.com/coreos/clair/ext/versionfmt/dpkg" "github.com/coreos/clair/ext/versionfmt/dpkg"
"github.com/coreos/clair/ext/vulnsrc" "github.com/coreos/clair/ext/vulnsrc"
"github.com/coreos/clair/pkg/fsutil"
"github.com/coreos/clair/pkg/gitutil" "github.com/coreos/clair/pkg/gitutil"
) )
const ( const (
// This Alpine vulnerability database affects origin packages, which has
// `origin` field of itself.
secdbGitURL = "https://github.com/alpinelinux/alpine-secdb" secdbGitURL = "https://github.com/alpinelinux/alpine-secdb"
updaterFlag = "alpine-secdbUpdater" updaterFlag = "alpine-secdbUpdater"
nvdURLPrefix = "https://cve.mitre.org/cgi-bin/cvename.cgi?name=" nvdURLPrefix = "https://cve.mitre.org/cgi-bin/cvename.cgi?name="
@ -51,61 +52,44 @@ type updater struct {
} }
func (u *updater) Update(db database.Datastore) (resp vulnsrc.UpdateResponse, err error) { func (u *updater) Update(db database.Datastore) (resp vulnsrc.UpdateResponse, err error) {
log.WithField("package", "Alpine").Info("Start fetching vulnerabilities") log.WithField("package", "Alpine").Info("start fetching vulnerabilities")
// Pull the master branch. // Pull the master branch.
var commit string var (
u.repositoryLocalPath, commit, err = gitutil.CloneOrPull(secdbGitURL, u.repositoryLocalPath, updaterFlag) commit string
if err != nil { existingCommit string
return foundCommit bool
} namespaces []string
vulns []database.VulnerabilityWithAffected
)
// Open a database transaction. if u.repositoryLocalPath, commit, err = gitutil.CloneOrPull(secdbGitURL, u.repositoryLocalPath, updaterFlag); err != nil {
tx, err := db.Begin()
if err != nil {
return return
} }
defer tx.Rollback()
// Ask the database for the latest commit we successfully applied.
var dbCommit string
var ok bool
dbCommit, ok, err = tx.FindKeyValue(updaterFlag)
if err != nil {
return
}
if !ok {
dbCommit = ""
}
// Set the updaterFlag to equal the commit processed. // Set the updaterFlag to equal the commit processed.
resp.FlagName = updaterFlag resp.FlagName = updaterFlag
resp.FlagValue = commit resp.FlagValue = commit
if existingCommit, foundCommit, err = database.FindKeyValueAndRollback(db, updaterFlag); err != nil {
return
}
// Short-circuit if there have been no updates. // Short-circuit if there have been no updates.
if commit == dbCommit { if foundCommit && commit == existingCommit {
log.WithField("package", "alpine").Debug("no update") log.WithField("package", "alpine").Debug("no update, skip")
return return
} }
// Get the list of namespaces from the repository. // Get the list of namespaces from the repository.
var namespaces []string if namespaces, err = fsutil.Readdir(u.repositoryLocalPath, fsutil.DirectoriesOnly); err != nil {
namespaces, err = ls(u.repositoryLocalPath, directoriesOnly)
if err != nil {
return return
} }
// Append any changed vulnerabilities to the response. // Append any changed vulnerabilities to the response.
for _, namespace := range namespaces { for _, namespace := range namespaces {
var vulns []database.VulnerabilityWithAffected if vulns, err = parseVulnsFromNamespace(u.repositoryLocalPath, namespace); err != nil {
var note string
vulns, note, err = parseVulnsFromNamespace(u.repositoryLocalPath, namespace)
if err != nil {
return return
} }
if note != "" {
resp.Notes = append(resp.Notes, note)
}
resp.Vulnerabilities = append(resp.Vulnerabilities, vulns...) resp.Vulnerabilities = append(resp.Vulnerabilities, vulns...)
} }
@ -118,74 +102,26 @@ func (u *updater) Clean() {
} }
} }
type lsFilter int func parseVulnsFromNamespace(repositoryPath, namespace string) (vulns []database.VulnerabilityWithAffected, err error) {
const (
filesOnly lsFilter = iota
directoriesOnly
)
func ls(path string, filter lsFilter) ([]string, error) {
dir, err := os.Open(path)
if err != nil {
return nil, err
}
defer dir.Close()
finfos, err := dir.Readdir(0)
if err != nil {
return nil, err
}
var files []string
for _, info := range finfos {
if filter == directoriesOnly && !info.IsDir() {
continue
}
if filter == filesOnly && info.IsDir() {
continue
}
if strings.HasPrefix(info.Name(), ".") {
continue
}
files = append(files, info.Name())
}
return files, nil
}
func parseVulnsFromNamespace(repositoryPath, namespace string) (vulns []database.VulnerabilityWithAffected, note string, err error) {
nsDir := filepath.Join(repositoryPath, namespace) nsDir := filepath.Join(repositoryPath, namespace)
var dbFilenames []string var dbFilenames []string
dbFilenames, err = ls(nsDir, filesOnly) if dbFilenames, err = fsutil.Readdir(nsDir, fsutil.FilesOnly); err != nil {
if err != nil {
return return
} }
for _, filename := range dbFilenames { for _, filename := range dbFilenames {
var file io.ReadCloser var db *secDB
file, err = os.Open(filepath.Join(nsDir, filename)) if db, err = newSecDB(filepath.Join(nsDir, filename)); err != nil {
if err != nil {
return return
} }
var fileVulns []database.VulnerabilityWithAffected vulns = append(vulns, db.Vulnerabilities()...)
fileVulns, err = parseYAML(file)
if err != nil {
return
}
vulns = append(vulns, fileVulns...)
file.Close()
} }
return return
} }
type secDBFile struct { type secDB struct {
Distro string `yaml:"distroversion"` Distro string `yaml:"distroversion"`
Packages []struct { Packages []struct {
Pkg struct { Pkg struct {
@ -195,42 +131,54 @@ type secDBFile struct {
} `yaml:"packages"` } `yaml:"packages"`
} }
func parseYAML(r io.Reader) (vulns []database.VulnerabilityWithAffected, err error) { func newSecDB(filePath string) (file *secDB, err error) {
var rBytes []byte var f io.ReadCloser
rBytes, err = ioutil.ReadAll(r) f, err = os.Open(filePath)
if err != nil { if err != nil {
return return
} }
var file secDBFile defer f.Close()
err = yaml.Unmarshal(rBytes, &file) file = &secDB{}
if err != nil { err = yaml.NewDecoder(f).Decode(file)
return
}
func (file *secDB) Vulnerabilities() (vulns []database.VulnerabilityWithAffected) {
if file == nil {
return return
} }
for _, pack := range file.Packages { namespace := database.Namespace{Name: "alpine:" + file.Distro, VersionFormat: dpkg.ParserName}
pkg := pack.Pkg for _, pkg := range file.Packages {
for version, vulnStrs := range pkg.Fixes { for version, cveNames := range pkg.Pkg.Fixes {
err := versionfmt.Valid(dpkg.ParserName, version) if err := versionfmt.Valid(dpkg.ParserName, version); err != nil {
if err != nil { log.WithError(err).WithFields(log.Fields{
log.WithError(err).WithField("version", version).Warning("could not parse package version. skipping") "version": version,
"package name": pkg.Pkg.Name,
}).Warning("could not parse package version, skipping")
continue continue
} }
for _, vulnStr := range vulnStrs { for _, cve := range cveNames {
var vuln database.VulnerabilityWithAffected vuln := database.VulnerabilityWithAffected{
vuln.Severity = database.UnknownSeverity Vulnerability: database.Vulnerability{
vuln.Name = vulnStr Name: cve,
vuln.Link = nvdURLPrefix + vulnStr Link: nvdURLPrefix + cve,
Severity: database.UnknownSeverity,
Namespace: namespace,
},
}
var fixedInVersion string var fixedInVersion string
if version != versionfmt.MaxVersion { if version != versionfmt.MaxVersion {
fixedInVersion = version fixedInVersion = version
} }
vuln.Affected = []database.AffectedFeature{ vuln.Affected = []database.AffectedFeature{
{ {
AffectedType: affectedType, AffectedType: affectedType,
FeatureName: pkg.Name, FeatureName: pkg.Pkg.Name,
AffectedVersion: version, AffectedVersion: version,
FixedInVersion: fixedInVersion, FixedInVersion: fixedInVersion,
Namespace: database.Namespace{ Namespace: database.Namespace{

View File

@ -15,28 +15,24 @@
package alpine package alpine
import ( import (
"os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestYAMLParsing(t *testing.T) { func TestYAMLParsing(t *testing.T) {
_, filename, _, _ := runtime.Caller(0) _, filename, _, _ := runtime.Caller(0)
path := filepath.Join(filepath.Dir(filename)) path := filepath.Join(filepath.Dir(filename))
secdb, err := newSecDB(filepath.Join(path, "/testdata/v34_main.yaml"))
require.Nil(t, err)
vulns := secdb.Vulnerabilities()
testData, _ := os.Open(path + "/testdata/v34_main.yaml")
defer testData.Close()
vulns, err := parseYAML(testData)
if err != nil {
assert.Nil(t, err)
}
assert.Equal(t, 105, len(vulns)) assert.Equal(t, 105, len(vulns))
assert.Equal(t, "CVE-2016-5387", vulns[0].Name) assert.Equal(t, "CVE-2016-5387", vulns[0].Name)
assert.Equal(t, "alpine:v3.4", vulns[0].Affected[0].Namespace.Name) assert.Equal(t, "alpine:v3.4", vulns[0].Namespace.Name)
assert.Equal(t, "apache2", vulns[0].Affected[0].FeatureName) assert.Equal(t, "apache2", vulns[0].Affected[0].FeatureName)
assert.Equal(t, "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2016-5387", vulns[0].Link) assert.Equal(t, "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2016-5387", vulns[0].Link)
} }

View File

@ -62,22 +62,9 @@ func init() {
func (u *updater) Update(datastore database.Datastore) (resp vulnsrc.UpdateResponse, err error) { func (u *updater) Update(datastore database.Datastore) (resp vulnsrc.UpdateResponse, err error) {
log.WithField("package", "Debian").Info("Start fetching vulnerabilities") log.WithField("package", "Debian").Info("Start fetching vulnerabilities")
latestHash, ok, err := database.FindKeyValueAndRollback(datastore, updaterFlag)
tx, err := datastore.Begin()
if err != nil { if err != nil {
return resp, err return
}
// Get the hash of the latest update's JSON data
latestHash, ok, err := tx.FindKeyValue(updaterFlag)
if err != nil {
return resp, err
}
// NOTE(sida): The transaction won't mutate the database and I want the
// transaction to be short.
if err := tx.Rollback(); err != nil {
return resp, err
} }
if !ok { if !ok {
@ -136,7 +123,7 @@ func buildResponse(jsonReader io.Reader, latestKnownHash string) (resp vulnsrc.U
// Calculate the hash and skip updating if the hash has been seen before. // Calculate the hash and skip updating if the hash has been seen before.
hash = hex.EncodeToString(jsonSHA.Sum(nil)) hash = hex.EncodeToString(jsonSHA.Sum(nil))
if latestKnownHash == hash { if latestKnownHash == hash {
log.WithField("package", "Debian").Debug("no update") log.WithField("package", "Debian").Debug("no update, skip")
return resp, nil return resp, nil
} }

View File

@ -30,7 +30,7 @@ func TestDebianParser(t *testing.T) {
_, filename, _, _ := runtime.Caller(0) _, filename, _, _ := runtime.Caller(0)
// Test parsing testdata/fetcher_debian_test.json // Test parsing testdata/fetcher_debian_test.json
testFile, _ := os.Open(filepath.Join(filepath.Dir(filename)) + "/testdata/fetcher_debian_test.json") testFile, _ := os.Open(filepath.Join(filepath.Dir(filename), "/testdata/fetcher_debian_test.json"))
response, err := buildResponse(testFile, "") response, err := buildResponse(testFile, "")
if assert.Nil(t, err) && assert.Len(t, response.Vulnerabilities, 2) { if assert.Nil(t, err) && assert.Len(t, response.Vulnerabilities, 2) {
for _, vulnerability := range response.Vulnerabilities { for _, vulnerability := range response.Vulnerabilities {

View File

@ -119,15 +119,9 @@ func compareELSA(left, right int) int {
func (u *updater) Update(datastore database.Datastore) (resp vulnsrc.UpdateResponse, err error) { func (u *updater) Update(datastore database.Datastore) (resp vulnsrc.UpdateResponse, err error) {
log.WithField("package", "Oracle Linux").Info("Start fetching vulnerabilities") log.WithField("package", "Oracle Linux").Info("Start fetching vulnerabilities")
// Get the first ELSA we have to manage. // Get the first ELSA we have to manage.
tx, err := datastore.Begin() flagValue, ok, err := database.FindKeyValueAndRollback(datastore, updaterFlag)
if err != nil { if err != nil {
return resp, err return
}
defer tx.Rollback()
flagValue, ok, err := tx.FindKeyValue(updaterFlag)
if err != nil {
return resp, err
} }
if !ok { if !ok {

View File

@ -30,7 +30,7 @@ func TestOracleParser(t *testing.T) {
path := filepath.Join(filepath.Dir(filename)) path := filepath.Join(filepath.Dir(filename))
// Test parsing testdata/fetcher_oracle_test.1.xml // Test parsing testdata/fetcher_oracle_test.1.xml
testFile, _ := os.Open(path + "/testdata/fetcher_oracle_test.1.xml") testFile, _ := os.Open(filepath.Join(path, "/testdata/fetcher_oracle_test.1.xml"))
defer testFile.Close() defer testFile.Close()
vulnerabilities, err := parseELSA(testFile) vulnerabilities, err := parseELSA(testFile)
@ -78,7 +78,7 @@ func TestOracleParser(t *testing.T) {
} }
} }
testFile2, _ := os.Open(path + "/testdata/fetcher_oracle_test.2.xml") testFile2, _ := os.Open(filepath.Join(path, "/testdata/fetcher_oracle_test.2.xml"))
defer testFile2.Close() defer testFile2.Close()
vulnerabilities, err = parseELSA(testFile2) vulnerabilities, err = parseELSA(testFile2)

View File

@ -101,21 +101,12 @@ func init() {
func (u *updater) Update(datastore database.Datastore) (resp vulnsrc.UpdateResponse, err error) { func (u *updater) Update(datastore database.Datastore) (resp vulnsrc.UpdateResponse, err error) {
log.WithField("package", "RHEL").Info("Start fetching vulnerabilities") log.WithField("package", "RHEL").Info("Start fetching vulnerabilities")
tx, err := datastore.Begin()
if err != nil {
return resp, err
}
// Get the first RHSA we have to manage. // Get the first RHSA we have to manage.
flagValue, ok, err := tx.FindKeyValue(updaterFlag) flagValue, ok, err := database.FindKeyValueAndRollback(datastore, updaterFlag)
if err != nil { if err != nil {
return resp, err return resp, err
} }
if err := tx.Rollback(); err != nil {
return resp, err
}
if !ok { if !ok {
flagValue = "" flagValue = ""
} }

View File

@ -31,7 +31,7 @@ func TestRHELParserMultipleCVE(t *testing.T) {
path := filepath.Join(filepath.Dir(filename)) path := filepath.Join(filepath.Dir(filename))
// Test parsing testdata/fetcher_rhel_test.2.xml // Test parsing testdata/fetcher_rhel_test.2.xml
testFile, _ := os.Open(path + "/testdata/fetcher_rhel_test.2.xml") testFile, _ := os.Open(filepath.Join(path, "/testdata/fetcher_rhel_test.2.xml"))
vulnerabilities, err := parseRHSA(testFile) vulnerabilities, err := parseRHSA(testFile)
// Expected // Expected
@ -86,7 +86,7 @@ func TestRHELParserOneCVE(t *testing.T) {
path := filepath.Join(filepath.Dir(filename)) path := filepath.Join(filepath.Dir(filename))
// Test parsing testdata/fetcher_rhel_test.1.xml // Test parsing testdata/fetcher_rhel_test.1.xml
testFile, _ := os.Open(path + "/testdata/fetcher_rhel_test.1.xml") testFile, _ := os.Open(filepath.Join(path, "/testdata/fetcher_rhel_test.1.xml"))
vulnerabilities, err := parseRHSA(testFile) vulnerabilities, err := parseRHSA(testFile)
if assert.Nil(t, err) && assert.Len(t, vulnerabilities, 1) { if assert.Nil(t, err) && assert.Len(t, vulnerabilities, 1) {
assert.Equal(t, "CVE-2015-0252", vulnerabilities[0].Name) assert.Equal(t, "CVE-2015-0252", vulnerabilities[0].Name)

View File

@ -22,6 +22,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"path/filepath"
"regexp" "regexp"
"strings" "strings"
@ -101,12 +102,11 @@ func (u *updater) Update(db database.Datastore) (resp vulnsrc.UpdateResponse, er
defer tx.Rollback() defer tx.Rollback()
// Ask the database for the latest commit we successfully applied. // Ask the database for the latest commit we successfully applied.
var dbCommit string dbCommit, ok, err := database.FindKeyValueAndRollback(db, updaterFlag)
var ok bool
dbCommit, ok, err = tx.FindKeyValue(updaterFlag)
if err != nil { if err != nil {
return return
} }
if !ok { if !ok {
dbCommit = "" dbCommit = ""
} }
@ -161,7 +161,7 @@ func collectModifiedVulnerabilities(commit, dbCommit, repositoryLocalPath string
func processDirectory(repositoryLocalPath, dirName string, modifiedCVE map[string]struct{}) error { func processDirectory(repositoryLocalPath, dirName string, modifiedCVE map[string]struct{}) error {
// Open the directory. // Open the directory.
d, err := os.Open(repositoryLocalPath + "/" + dirName) d, err := os.Open(filepath.Join(repositoryLocalPath, dirName))
if err != nil { if err != nil {
log.WithError(err).Error("could not open Ubuntu vulnerabilities repository's folder") log.WithError(err).Error("could not open Ubuntu vulnerabilities repository's folder")
return vulnsrc.ErrFilesystem return vulnsrc.ErrFilesystem
@ -191,7 +191,7 @@ func collectVulnerabilitiesAndNotes(repositoryLocalPath string, modifiedCVE map[
for cvePath := range modifiedCVE { for cvePath := range modifiedCVE {
// Open the CVE file. // Open the CVE file.
file, err := os.Open(repositoryLocalPath + "/" + cvePath) file, err := os.Open(filepath.Join(repositoryLocalPath, cvePath))
if err != nil { if err != nil {
// This can happen when a file is modified then moved in another commit. // This can happen when a file is modified then moved in another commit.
continue continue

View File

@ -32,7 +32,7 @@ func TestUbuntuParser(t *testing.T) {
path := filepath.Join(filepath.Dir(filename)) path := filepath.Join(filepath.Dir(filename))
// Test parsing testdata/fetcher_ // Test parsing testdata/fetcher_
testData, _ := os.Open(path + "/testdata/fetcher_ubuntu_test.txt") testData, _ := os.Open(filepath.Join(path, "/testdata/fetcher_ubuntu_test.txt"))
defer testData.Close() defer testData.Close()
vulnerability, unknownReleases, err := parseUbuntuCVE(testData) vulnerability, unknownReleases, err := parseUbuntuCVE(testData)
if assert.Nil(t, err) { if assert.Nil(t, err) {