diff --git a/database/pgsql/complex_test.go b/database/pgsql/complex_test.go index cd585a68..b49903fb 100644 --- a/database/pgsql/complex_test.go +++ b/database/pgsql/complex_test.go @@ -27,11 +27,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/coreos/clair/database" + "github.com/coreos/clair/ext/versionfmt/dpkg" "github.com/coreos/clair/utils" "github.com/coreos/clair/utils/types" - - // dpkg versioning is used to parse test packages. - _ "github.com/coreos/clair/ext/versionfmt/dpkg" ) const ( @@ -51,7 +49,7 @@ func TestRaceAffects(t *testing.T) { feature := database.Feature{ Namespace: database.Namespace{ Name: "TestRaceAffectsFeatureNamespace1", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, }, Name: "TestRaceAffecturesFeature1", } diff --git a/database/pgsql/feature.go b/database/pgsql/feature.go index 35a21a69..34d6a2d8 100644 --- a/database/pgsql/feature.go +++ b/database/pgsql/feature.go @@ -16,7 +16,6 @@ package pgsql import ( "database/sql" - "fmt" "strings" "time" @@ -66,7 +65,6 @@ func (pgSQL *pgSQL) insertFeature(feature database.Feature) (int, error) { func (pgSQL *pgSQL) insertFeatureVersion(fv database.FeatureVersion) (id int, err error) { err = versionfmt.Valid(fv.Feature.Namespace.VersionFormat, fv.Version) if err != nil { - fmt.Println(err) return 0, cerrors.NewBadRequestError("could not find/insert invalid FeatureVersion") } @@ -205,7 +203,7 @@ func linkFeatureVersionToVulnerabilities(tx *sql.Tx, featureVersion database.Fea // TODO(Quentin-M): LIMIT rows, err := tx.Query(searchVulnerabilityFixedInFeature, featureVersion.Feature.ID) if err != nil { - return handleError("searchVulnerabilityFixedInFeature", err) + return err } defer rows.Close() diff --git a/database/pgsql/feature_test.go b/database/pgsql/feature_test.go index cef5c037..5b7f8078 100644 --- a/database/pgsql/feature_test.go +++ b/database/pgsql/feature_test.go @@ -20,9 +20,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/coreos/clair/database" - - // dpkg versioning is used to parse test packages. - _ "github.com/coreos/clair/ext/versionfmt/dpkg" + "github.com/coreos/clair/ext/versionfmt/dpkg" ) func TestInsertFeature(t *testing.T) { @@ -49,7 +47,7 @@ func TestInsertFeature(t *testing.T) { feature := database.Feature{ Namespace: database.Namespace{ Name: "TestInsertFeatureNamespace1", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, }, Name: "TestInsertFeature1", } @@ -76,7 +74,7 @@ func TestInsertFeature(t *testing.T) { Feature: database.Feature{ Namespace: database.Namespace{ Name: "TestInsertFeatureNamespace2", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, }, Name: "TestInsertFeature2", }, @@ -86,7 +84,7 @@ func TestInsertFeature(t *testing.T) { Feature: database.Feature{ Namespace: database.Namespace{ Name: "TestInsertFeatureNamespace2", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, }, Name: "TestInsertFeature2", }, @@ -103,7 +101,7 @@ func TestInsertFeature(t *testing.T) { Feature: database.Feature{ Namespace: database.Namespace{ Name: "TestInsertFeatureNamespace1", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, }, Name: "TestInsertFeature1", }, diff --git a/database/pgsql/layer_test.go b/database/pgsql/layer_test.go index 48637678..dcd420b7 100644 --- a/database/pgsql/layer_test.go +++ b/database/pgsql/layer_test.go @@ -21,11 +21,9 @@ import ( "github.com/stretchr/testify/assert" "github.com/coreos/clair/database" + "github.com/coreos/clair/ext/versionfmt/dpkg" cerrors "github.com/coreos/clair/utils/errors" "github.com/coreos/clair/utils/types" - - // dpkg versioning is used to parse test packages. - _ "github.com/coreos/clair/ext/versionfmt/dpkg" ) func TestFindLayer(t *testing.T) { @@ -144,7 +142,7 @@ func testInsertLayerTree(t *testing.T, datastore database.Datastore) { Feature: database.Feature{ Namespace: database.Namespace{ Name: "TestInsertLayerNamespace2", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, }, Name: "TestInsertLayerFeature1", }, @@ -154,7 +152,7 @@ func testInsertLayerTree(t *testing.T, datastore database.Datastore) { Feature: database.Feature{ Namespace: database.Namespace{ Name: "TestInsertLayerNamespace2", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, }, Name: "TestInsertLayerFeature2", }, @@ -164,7 +162,7 @@ func testInsertLayerTree(t *testing.T, datastore database.Datastore) { Feature: database.Feature{ Namespace: database.Namespace{ Name: "TestInsertLayerNamespace2", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, }, Name: "TestInsertLayerFeature3", }, @@ -174,7 +172,7 @@ func testInsertLayerTree(t *testing.T, datastore database.Datastore) { Feature: database.Feature{ Namespace: database.Namespace{ Name: "TestInsertLayerNamespace3", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, }, Name: "TestInsertLayerFeature2", }, @@ -184,7 +182,7 @@ func testInsertLayerTree(t *testing.T, datastore database.Datastore) { Feature: database.Feature{ Namespace: database.Namespace{ Name: "TestInsertLayerNamespace3", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, }, Name: "TestInsertLayerFeature3", }, @@ -194,7 +192,7 @@ func testInsertLayerTree(t *testing.T, datastore database.Datastore) { Feature: database.Feature{ Namespace: database.Namespace{ Name: "TestInsertLayerNamespace3", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, }, Name: "TestInsertLayerFeature4", }, @@ -210,7 +208,7 @@ func testInsertLayerTree(t *testing.T, datastore database.Datastore) { Parent: &database.Layer{Name: "TestInsertLayer1"}, Namespace: &database.Namespace{ Name: "TestInsertLayerNamespace1", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, }, }, // This layer changes the namespace and adds Features. @@ -219,7 +217,7 @@ func testInsertLayerTree(t *testing.T, datastore database.Datastore) { Parent: &database.Layer{Name: "TestInsertLayer2"}, Namespace: &database.Namespace{ Name: "TestInsertLayerNamespace2", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, }, Features: []database.FeatureVersion{f1, f2, f3}, }, @@ -237,7 +235,7 @@ func testInsertLayerTree(t *testing.T, datastore database.Datastore) { Parent: &database.Layer{Name: "TestInsertLayer3"}, Namespace: &database.Namespace{ Name: "TestInsertLayerNamespace3", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, }, Features: []database.FeatureVersion{ // Deletes TestInsertLayerFeature1. @@ -295,7 +293,7 @@ func testInsertLayerUpdate(t *testing.T, datastore database.Datastore) { Feature: database.Feature{ Namespace: database.Namespace{ Name: "TestInsertLayerNamespace3", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, }, Name: "TestInsertLayerFeature7", }, @@ -308,7 +306,7 @@ func testInsertLayerUpdate(t *testing.T, datastore database.Datastore) { Parent: l3.Parent, Namespace: &database.Namespace{ Name: "TestInsertLayerNamespaceUpdated1", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, }, Features: []database.FeatureVersion{f7}, } diff --git a/database/pgsql/namespace_test.go b/database/pgsql/namespace_test.go index 52f1c319..0990b6f4 100644 --- a/database/pgsql/namespace_test.go +++ b/database/pgsql/namespace_test.go @@ -21,9 +21,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/coreos/clair/database" - - // dpkg versioning is used to parse test packages. - _ "github.com/coreos/clair/ext/versionfmt/dpkg" + "github.com/coreos/clair/ext/versionfmt/dpkg" ) func TestInsertNamespace(t *testing.T) { @@ -42,12 +40,12 @@ func TestInsertNamespace(t *testing.T) { // Insert Namespace and ensure we can find it. id1, err := datastore.insertNamespace(database.Namespace{ Name: "TestInsertNamespace1", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, }) assert.Nil(t, err) id2, err := datastore.insertNamespace(database.Namespace{ Name: "TestInsertNamespace1", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, }) assert.Nil(t, err) assert.Equal(t, id1, id2) diff --git a/database/pgsql/notification_test.go b/database/pgsql/notification_test.go index afff0fd2..476b5452 100644 --- a/database/pgsql/notification_test.go +++ b/database/pgsql/notification_test.go @@ -22,11 +22,9 @@ import ( "github.com/coreos/clair/database" "github.com/coreos/clair/ext/versionfmt" + "github.com/coreos/clair/ext/versionfmt/dpkg" cerrors "github.com/coreos/clair/utils/errors" "github.com/coreos/clair/utils/types" - - // dpkg versioning is used to parse test packages. - _ "github.com/coreos/clair/ext/versionfmt/dpkg" ) func TestNotification(t *testing.T) { @@ -46,7 +44,7 @@ func TestNotification(t *testing.T) { Name: "TestNotificationFeature1", Namespace: database.Namespace{ Name: "TestNotificationNamespace1", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, }, } @@ -54,7 +52,7 @@ func TestNotification(t *testing.T) { Name: "TestNotificationFeature2", Namespace: database.Namespace{ Name: "TestNotificationNamespace1", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, }, } diff --git a/database/pgsql/vulnerability_test.go b/database/pgsql/vulnerability_test.go index 125cedb5..e1d8d4c4 100644 --- a/database/pgsql/vulnerability_test.go +++ b/database/pgsql/vulnerability_test.go @@ -22,11 +22,9 @@ import ( "github.com/coreos/clair/database" "github.com/coreos/clair/ext/versionfmt" + "github.com/coreos/clair/ext/versionfmt/dpkg" cerrors "github.com/coreos/clair/utils/errors" "github.com/coreos/clair/utils/types" - - // dpkg versioning is used to parse test packages. - _ "github.com/coreos/clair/ext/versionfmt/dpkg" ) func TestFindVulnerability(t *testing.T) { @@ -49,7 +47,7 @@ func TestFindVulnerability(t *testing.T) { Severity: types.High, Namespace: database.Namespace{ Name: "debian:7", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, }, FixedIn: []database.FeatureVersion{ { @@ -74,7 +72,7 @@ func TestFindVulnerability(t *testing.T) { Description: "A vulnerability affecting nothing", Namespace: database.Namespace{ Name: "debian:7", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, }, Severity: types.Unknown, } @@ -118,11 +116,11 @@ func TestInsertVulnerability(t *testing.T) { // Create some data. n1 := database.Namespace{ Name: "TestInsertVulnerabilityNamespace1", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, } n2 := database.Namespace{ Name: "TestInsertVulnerabilityNamespace2", - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, } f1 := database.FeatureVersion{ diff --git a/ext/versionfmt/dpkg/parser.go b/ext/versionfmt/dpkg/parser.go index e091b1dd..d170c9ca 100644 --- a/ext/versionfmt/dpkg/parser.go +++ b/ext/versionfmt/dpkg/parser.go @@ -23,6 +23,9 @@ import ( "github.com/coreos/clair/ext/versionfmt" ) +// ParserName is the name by which the dpkg parser is registered. +const ParserName = "dpkg" + type version struct { epoch int version string @@ -278,5 +281,5 @@ func signum(a int) int { } func init() { - versionfmt.RegisterParser("dpkg", parser{}) + versionfmt.RegisterParser(ParserName, parser{}) } diff --git a/ext/versionfmt/driver.go b/ext/versionfmt/driver.go index b277e0ab..84073a95 100644 --- a/ext/versionfmt/driver.go +++ b/ext/versionfmt/driver.go @@ -27,7 +27,7 @@ const ( // MinVersion is a special package version which is always sorted first. MinVersion = "#MINV#" - // MaxVersion is a special package version which is always sorted last + // MaxVersion is a special package version which is always sorted last. MaxVersion = "#MAXV#" ) diff --git a/ext/versionfmt/rpm/parser.go b/ext/versionfmt/rpm/parser.go index 2491dc0c..42fb71ab 100644 --- a/ext/versionfmt/rpm/parser.go +++ b/ext/versionfmt/rpm/parser.go @@ -25,6 +25,9 @@ import ( "github.com/coreos/clair/ext/versionfmt" ) +// ParserName is the name by which the rpm parser is registered. +const ParserName = "rpm" + var ( // alphanumPattern is a regular expression to match all sequences of numeric // characters or alphanumeric characters. @@ -273,5 +276,5 @@ func containsRune(s []rune, e rune) bool { } func init() { - versionfmt.RegisterParser("rpm", parser{}) + versionfmt.RegisterParser(ParserName, parser{}) } diff --git a/ext/versionfmt/rpm/parser_test.go b/ext/versionfmt/rpm/parser_test.go index ed4578ff..fe316bfc 100644 --- a/ext/versionfmt/rpm/parser_test.go +++ b/ext/versionfmt/rpm/parser_test.go @@ -86,74 +86,62 @@ func TestParseAndCompare(t *testing.T) { expected int v2 string }{ + // Oracle Linux corner cases. + {"2.9.1-6.0.1.el7_2.3", GREATER, "2.9.1-6.el7_2.3"}, + {"3.10.0-327.28.3.el7", GREATER, "3.10.0-327.el7"}, + {"3.14.3-23.3.el6_8", GREATER, "3.14.3-23.el6_7"}, + {"2.23.2-22.el7_1", LESS, "2.23.2-22.el7_1.1"}, + // Tests imported from tests/rpmvercmp.at {"1.0", EQUAL, "1.0"}, {"1.0", LESS, "2.0"}, {"2.0", GREATER, "1.0"}, - {"2.0.1", EQUAL, "2.0.1"}, {"2.0", LESS, "2.0.1"}, {"2.0.1", GREATER, "2.0"}, - {"2.0.1a", EQUAL, "2.0.1a"}, {"2.0.1a", GREATER, "2.0.1"}, {"2.0.1", LESS, "2.0.1a"}, - {"5.5p1", EQUAL, "5.5p1"}, {"5.5p1", LESS, "5.5p2"}, {"5.5p2", GREATER, "5.5p1"}, - {"5.5p10", EQUAL, "5.5p10"}, {"5.5p1", LESS, "5.5p10"}, {"5.5p10", GREATER, "5.5p1"}, - {"10xyz", LESS, "10.1xyz"}, {"10.1xyz", GREATER, "10xyz"}, - {"xyz10", EQUAL, "xyz10"}, {"xyz10", LESS, "xyz10.1"}, {"xyz10.1", GREATER, "xyz10"}, - {"xyz.4", EQUAL, "xyz.4"}, {"xyz.4", LESS, "8"}, {"8", GREATER, "xyz.4"}, {"xyz.4", LESS, "2"}, {"2", GREATER, "xyz.4"}, - {"5.5p2", LESS, "5.6p1"}, {"5.6p1", GREATER, "5.5p2"}, - {"5.6p1", LESS, "6.5p1"}, {"6.5p1", GREATER, "5.6p1"}, - {"6.0.rc1", GREATER, "6.0"}, {"6.0", LESS, "6.0.rc1"}, - {"10b2", GREATER, "10a1"}, {"10a2", LESS, "10b2"}, - {"1.0aa", EQUAL, "1.0aa"}, {"1.0a", LESS, "1.0aa"}, {"1.0aa", GREATER, "1.0a"}, - {"10.0001", EQUAL, "10.0001"}, {"10.0001", EQUAL, "10.1"}, {"10.1", EQUAL, "10.0001"}, {"10.0001", LESS, "10.0039"}, {"10.0039", GREATER, "10.0001"}, - {"4.999.9", LESS, "5.0"}, {"5.0", GREATER, "4.999.9"}, - {"20101121", EQUAL, "20101121"}, {"20101121", LESS, "20101122"}, {"20101122", GREATER, "20101121"}, - {"2_0", EQUAL, "2_0"}, {"2.0", EQUAL, "2_0"}, {"2_0", EQUAL, "2.0"}, - - // RhBug:178798 case {"a", EQUAL, "a"}, {"a+", EQUAL, "a+"}, {"a+", EQUAL, "a_"}, @@ -166,8 +154,6 @@ func TestParseAndCompare(t *testing.T) { {"_+", EQUAL, "_+"}, {"+", EQUAL, "_"}, {"_", EQUAL, "+"}, - - // Basic testcases for tilde sorting {"1.0~rc1", EQUAL, "1.0~rc1"}, {"1.0~rc1", LESS, "1.0"}, {"1.0", GREATER, "1.0~rc1"}, @@ -176,27 +162,6 @@ func TestParseAndCompare(t *testing.T) { {"1.0~rc1~git123", EQUAL, "1.0~rc1~git123"}, {"1.0~rc1~git123", LESS, "1.0~rc1"}, {"1.0~rc1", GREATER, "1.0~rc1~git123"}, - - // These are included here to document current, arguably buggy behaviors - // for reference purposes and for easy checking against unintended - // behavior changes. - // - // AT_BANNER([RPM version comparison oddities]) - // RhBug:811992 case - // {"1b.fc17", EQUAL, "1b.fc17"}, - // {"1b.fc17", LESS, "1.fc17"}, - // {"1.fc17", GREATER, "1b.fc17"}, - // {"1g.fc17", EQUAL, "1g.fc17"}, - // {"1g.fc17", GREATER, "1.fc17"}, - // {"1.fc17", LESS, "1g.fc17"}, - - // Non-ascii characters are considered equal so these are all the same, eh... - // {"1.1.α", EQUAL, "1.1.α"}, - // {"1.1.α", EQUAL, "1.1.β"}, - // {"1.1.β", EQUAL, "1.1.α"}, - // {"1.1.αα", EQUAL, "1.1.α"}, - // {"1.1.α", EQUAL, "1.1.ββ"}, - // {"1.1.ββ", EQUAL, "1.1.αα"}, } var ( diff --git a/updater/fetchers/alpine/alpine.go b/updater/fetchers/alpine/alpine.go index 64f66e69..3850d9dc 100644 --- a/updater/fetchers/alpine/alpine.go +++ b/updater/fetchers/alpine/alpine.go @@ -30,13 +30,11 @@ import ( "github.com/coreos/clair/database" "github.com/coreos/clair/ext/versionfmt" + "github.com/coreos/clair/ext/versionfmt/dpkg" "github.com/coreos/clair/updater" "github.com/coreos/clair/utils" cerrors "github.com/coreos/clair/utils/errors" "github.com/coreos/clair/utils/types" - - // dpkg versioning is used to parse Alpine Linux packages. - _ "github.com/coreos/clair/ext/versionfmt/dpkg" ) const ( @@ -223,7 +221,7 @@ func parse33YAML(r io.Reader) (vulns []database.Vulnerability, err error) { for _, pack := range file.Packages { pkg := pack.Pkg for _, fix := range pkg.Fixes { - err = versionfmt.Valid("dpkg", pkg.Version) + err = versionfmt.Valid(dpkg.ParserName, pkg.Version) if err != nil { log.Warningf("could not parse package version '%s': %s. skipping", pkg.Version, err.Error()) continue @@ -236,8 +234,11 @@ func parse33YAML(r io.Reader) (vulns []database.Vulnerability, err error) { FixedIn: []database.FeatureVersion{ { Feature: database.Feature{ - Namespace: database.Namespace{Name: "alpine:" + file.Distro}, - Name: pkg.Name, + Namespace: database.Namespace{ + Name: "alpine:" + file.Distro, + VersionFormat: dpkg.ParserName, + }, + Name: pkg.Name, }, Version: pkg.Version, }, @@ -274,7 +275,7 @@ func parse34YAML(r io.Reader) (vulns []database.Vulnerability, err error) { for _, pack := range file.Packages { pkg := pack.Pkg for version, vulnStrs := range pkg.Fixes { - err := versionfmt.Valid("dpkg", version) + err := versionfmt.Valid(dpkg.ParserName, version) if err != nil { log.Warningf("could not parse package version '%s': %s. skipping", version, err.Error()) continue @@ -288,8 +289,11 @@ func parse34YAML(r io.Reader) (vulns []database.Vulnerability, err error) { vuln.FixedIn = []database.FeatureVersion{ { Feature: database.Feature{ - Namespace: database.Namespace{Name: "alpine:" + file.Distro}, - Name: pkg.Name, + Namespace: database.Namespace{ + Name: "alpine:" + file.Distro, + VersionFormat: dpkg.ParserName, + }, + Name: pkg.Name, }, Version: version, }, diff --git a/updater/fetchers/debian/debian.go b/updater/fetchers/debian/debian.go index dca65357..4838e622 100644 --- a/updater/fetchers/debian/debian.go +++ b/updater/fetchers/debian/debian.go @@ -27,12 +27,10 @@ import ( "github.com/coreos/clair/database" "github.com/coreos/clair/ext/versionfmt" + "github.com/coreos/clair/ext/versionfmt/dpkg" "github.com/coreos/clair/updater" cerrors "github.com/coreos/clair/utils/errors" "github.com/coreos/clair/utils/types" - - // dpkg versioning is used to parse Debian packages. - _ "github.com/coreos/clair/ext/versionfmt/dpkg" ) const ( @@ -185,7 +183,7 @@ func parseDebianJSON(data *jsonData) (vulnerabilities []database.Vulnerability, } else if releaseNode.Status == "resolved" { // Resolved means that the vulnerability has been fixed in // "fixed_version" (if affected). - err = versionfmt.Valid("dpkg", releaseNode.FixedVersion) + err = versionfmt.Valid(dpkg.ParserName, releaseNode.FixedVersion) if err != nil { log.Warningf("could not parse package version '%s': %s. skipping", releaseNode.FixedVersion, err.Error()) continue @@ -198,7 +196,8 @@ func parseDebianJSON(data *jsonData) (vulnerabilities []database.Vulnerability, Feature: database.Feature{ Name: pkgName, Namespace: database.Namespace{ - Name: "debian:" + database.DebianReleasesMapping[releaseName], + Name: "debian:" + database.DebianReleasesMapping[releaseName], + VersionFormat: dpkg.ParserName, }, }, Version: version, diff --git a/updater/fetchers/debian/debian_test.go b/updater/fetchers/debian/debian_test.go index 495e7b51..37cb0fe0 100644 --- a/updater/fetchers/debian/debian_test.go +++ b/updater/fetchers/debian/debian_test.go @@ -22,6 +22,7 @@ import ( "github.com/coreos/clair/database" "github.com/coreos/clair/ext/versionfmt" + "github.com/coreos/clair/ext/versionfmt/dpkg" "github.com/coreos/clair/utils/types" "github.com/stretchr/testify/assert" ) @@ -42,15 +43,20 @@ func TestDebianParser(t *testing.T) { expectedFeatureVersions := []database.FeatureVersion{ { Feature: database.Feature{ - Namespace: database.Namespace{Name: "debian:8"}, - Name: "aptdaemon", + Namespace: database.Namespace{ + Name: "debian:8", + VersionFormat: dpkg.ParserName, + }, + Name: "aptdaemon", }, Version: versionfmt.MaxVersion, }, { Feature: database.Feature{ - Namespace: database.Namespace{Name: "debian:unstable"}, - + Namespace: database.Namespace{ + Name: "debian:unstable", + VersionFormat: dpkg.ParserName, + }, Name: "aptdaemon", }, Version: "1.1.1+bzr982-1", @@ -68,22 +74,31 @@ func TestDebianParser(t *testing.T) { expectedFeatureVersions := []database.FeatureVersion{ { Feature: database.Feature{ - Namespace: database.Namespace{Name: "debian:8"}, - Name: "aptdaemon", + Namespace: database.Namespace{ + Name: "debian:8", + VersionFormat: dpkg.ParserName, + }, + Name: "aptdaemon", }, Version: "0.7.0", }, { Feature: database.Feature{ - Namespace: database.Namespace{Name: "debian:unstable"}, - Name: "aptdaemon", + Namespace: database.Namespace{ + Name: "debian:unstable", + VersionFormat: dpkg.ParserName, + }, + Name: "aptdaemon", }, Version: "0.7.0", }, { Feature: database.Feature{ - Namespace: database.Namespace{Name: "debian:8"}, - Name: "asterisk", + Namespace: database.Namespace{ + Name: "debian:8", + VersionFormat: dpkg.ParserName, + }, + Name: "asterisk", }, Version: "0.5.56", }, @@ -100,8 +115,11 @@ func TestDebianParser(t *testing.T) { expectedFeatureVersions := []database.FeatureVersion{ { Feature: database.Feature{ - Namespace: database.Namespace{Name: "debian:8"}, - Name: "asterisk", + Namespace: database.Namespace{ + Name: "debian:8", + VersionFormat: dpkg.ParserName, + }, + Name: "asterisk", }, Version: versionfmt.MinVersion, }, diff --git a/updater/fetchers/oracle/oracle.go b/updater/fetchers/oracle/oracle.go index 44029cd7..fceae36f 100644 --- a/updater/fetchers/oracle/oracle.go +++ b/updater/fetchers/oracle/oracle.go @@ -25,13 +25,11 @@ import ( "github.com/coreos/clair/database" "github.com/coreos/clair/ext/versionfmt" + "github.com/coreos/clair/ext/versionfmt/rpm" "github.com/coreos/clair/updater" cerrors "github.com/coreos/clair/utils/errors" "github.com/coreos/clair/utils/types" "github.com/coreos/pkg/capnslog" - - // rpm versioning is used to parse Oracle Linux packages. - _ "github.com/coreos/clair/ext/versionfmt/rpm" ) const ( @@ -286,7 +284,7 @@ func toFeatureVersions(criteria criteria) []database.FeatureVersion { const prefixLen = len(" is earlier than ") featureVersion.Feature.Name = strings.TrimSpace(c.Comment[:strings.Index(c.Comment, " is earlier than ")]) version := c.Comment[strings.Index(c.Comment, " is earlier than ")+prefixLen:] - err := versionfmt.Valid("rpm", version) + err := versionfmt.Valid(rpm.ParserName, version) if err != nil { log.Warningf("could not parse package version '%s': %s. skipping", version, err.Error()) } else { @@ -296,7 +294,7 @@ func toFeatureVersions(criteria criteria) []database.FeatureVersion { } featureVersion.Feature.Namespace.Name = "oracle" + ":" + strconv.Itoa(osVersion) - featureVersion.Feature.Namespace.VersionFormat = "rpm" + featureVersion.Feature.Namespace.VersionFormat = rpm.ParserName if featureVersion.Feature.Namespace.Name != "" && featureVersion.Feature.Name != "" && featureVersion.Version != "" { featureVersionParameters[featureVersion.Feature.Namespace.Name+":"+featureVersion.Feature.Name] = featureVersion diff --git a/updater/fetchers/oracle/oracle_test.go b/updater/fetchers/oracle/oracle_test.go index a0daa204..d23ef866 100644 --- a/updater/fetchers/oracle/oracle_test.go +++ b/updater/fetchers/oracle/oracle_test.go @@ -21,11 +21,9 @@ import ( "testing" "github.com/coreos/clair/database" + "github.com/coreos/clair/ext/versionfmt/rpm" "github.com/coreos/clair/utils/types" "github.com/stretchr/testify/assert" - - // rpm versioning is used to parse Oracle Linux packages. - _ "github.com/coreos/clair/ext/versionfmt/rpm" ) func TestOracleParser(t *testing.T) { @@ -48,7 +46,7 @@ func TestOracleParser(t *testing.T) { Feature: database.Feature{ Namespace: database.Namespace{ Name: "oracle:7", - VersionFormat: "rpm", + VersionFormat: rpm.ParserName, }, Name: "xerces-c", }, @@ -58,7 +56,7 @@ func TestOracleParser(t *testing.T) { Feature: database.Feature{ Namespace: database.Namespace{ Name: "oracle:7", - VersionFormat: "rpm", + VersionFormat: rpm.ParserName, }, Name: "xerces-c-devel", }, @@ -68,7 +66,7 @@ func TestOracleParser(t *testing.T) { Feature: database.Feature{ Namespace: database.Namespace{ Name: "oracle:7", - VersionFormat: "rpm", + VersionFormat: rpm.ParserName, }, Name: "xerces-c-doc", }, @@ -95,7 +93,7 @@ func TestOracleParser(t *testing.T) { Feature: database.Feature{ Namespace: database.Namespace{ Name: "oracle:6", - VersionFormat: "rpm", + VersionFormat: rpm.ParserName, }, Name: "firefox", }, @@ -105,7 +103,7 @@ func TestOracleParser(t *testing.T) { Feature: database.Feature{ Namespace: database.Namespace{ Name: "oracle:7", - VersionFormat: "rpm", + VersionFormat: rpm.ParserName, }, Name: "firefox", }, diff --git a/updater/fetchers/rhel/rhel.go b/updater/fetchers/rhel/rhel.go index 286d4500..1a0c423d 100644 --- a/updater/fetchers/rhel/rhel.go +++ b/updater/fetchers/rhel/rhel.go @@ -27,12 +27,10 @@ import ( "github.com/coreos/clair/database" "github.com/coreos/clair/ext/versionfmt" + "github.com/coreos/clair/ext/versionfmt/rpm" "github.com/coreos/clair/updater" cerrors "github.com/coreos/clair/utils/errors" "github.com/coreos/clair/utils/types" - - // rpm versioning is used to parse Oracle Linux packages. - _ "github.com/coreos/clair/ext/versionfmt/rpm" ) const ( @@ -289,12 +287,12 @@ func toFeatureVersions(criteria criteria) []database.FeatureVersion { const prefixLen = len(" is earlier than ") featureVersion.Feature.Name = strings.TrimSpace(c.Comment[:strings.Index(c.Comment, " is earlier than ")]) version := c.Comment[strings.Index(c.Comment, " is earlier than ")+prefixLen:] - err := versionfmt.Valid("rpm", version) + err := versionfmt.Valid(rpm.ParserName, version) if err != nil { log.Warningf("could not parse package version '%s': %s. skipping", version, err.Error()) } else { featureVersion.Version = version - featureVersion.Feature.Namespace.VersionFormat = "rpm" + featureVersion.Feature.Namespace.VersionFormat = rpm.ParserName } } } diff --git a/updater/fetchers/rhel/rhel_test.go b/updater/fetchers/rhel/rhel_test.go index 8e3a215c..2b87eec9 100644 --- a/updater/fetchers/rhel/rhel_test.go +++ b/updater/fetchers/rhel/rhel_test.go @@ -21,11 +21,9 @@ import ( "testing" "github.com/coreos/clair/database" + "github.com/coreos/clair/ext/versionfmt/rpm" "github.com/coreos/clair/utils/types" "github.com/stretchr/testify/assert" - - // rpm versioning is used to parse RHEL packages. - _ "github.com/coreos/clair/ext/versionfmt/rpm" ) func TestRHELParser(t *testing.T) { @@ -46,7 +44,7 @@ func TestRHELParser(t *testing.T) { Feature: database.Feature{ Namespace: database.Namespace{ Name: "centos:7", - VersionFormat: "rpm", + VersionFormat: rpm.ParserName, }, Name: "xerces-c", }, @@ -56,7 +54,7 @@ func TestRHELParser(t *testing.T) { Feature: database.Feature{ Namespace: database.Namespace{ Name: "centos:7", - VersionFormat: "rpm", + VersionFormat: rpm.ParserName, }, Name: "xerces-c-devel", }, @@ -66,7 +64,7 @@ func TestRHELParser(t *testing.T) { Feature: database.Feature{ Namespace: database.Namespace{ Name: "centos:7", - VersionFormat: "rpm", + VersionFormat: rpm.ParserName, }, Name: "xerces-c-doc", }, @@ -93,7 +91,7 @@ func TestRHELParser(t *testing.T) { Feature: database.Feature{ Namespace: database.Namespace{ Name: "centos:6", - VersionFormat: "rpm", + VersionFormat: rpm.ParserName, }, Name: "firefox", }, @@ -103,7 +101,7 @@ func TestRHELParser(t *testing.T) { Feature: database.Feature{ Namespace: database.Namespace{ Name: "centos:7", - VersionFormat: "rpm", + VersionFormat: rpm.ParserName, }, Name: "firefox", }, diff --git a/updater/fetchers/ubuntu/ubuntu.go b/updater/fetchers/ubuntu/ubuntu.go index d171ab90..68291557 100644 --- a/updater/fetchers/ubuntu/ubuntu.go +++ b/updater/fetchers/ubuntu/ubuntu.go @@ -30,6 +30,7 @@ import ( "github.com/coreos/clair/database" "github.com/coreos/clair/ext/versionfmt" + "github.com/coreos/clair/ext/versionfmt/dpkg" "github.com/coreos/clair/updater" "github.com/coreos/clair/utils" cerrors "github.com/coreos/clair/utils/errors" @@ -350,7 +351,7 @@ func parseUbuntuCVE(fileContent io.Reader) (vulnerability database.Vulnerability if md["status"] == "released" { if md["note"] != "" { var err error - err = versionfmt.Valid("dpkg", md["note"]) + err = versionfmt.Valid(dpkg.ParserName, md["note"]) if err != nil { log.Warningf("could not parse package version '%s': %s. skipping", md["note"], err) } diff --git a/worker/detectors/feature/apk/apk.go b/worker/detectors/feature/apk/apk.go index db641b74..1d8594f2 100644 --- a/worker/detectors/feature/apk/apk.go +++ b/worker/detectors/feature/apk/apk.go @@ -22,10 +22,8 @@ import ( "github.com/coreos/clair/database" "github.com/coreos/clair/ext/versionfmt" + "github.com/coreos/clair/ext/versionfmt/dpkg" "github.com/coreos/clair/worker/detectors" - - // dpkg versioning is used to parse apk packages. - _ "github.com/coreos/clair/ext/versionfmt/dpkg" ) var log = capnslog.NewPackageLogger("github.com/coreos/clair", "worker/detectors/packages") @@ -60,7 +58,7 @@ func (d *detector) Detect(data map[string][]byte) ([]database.FeatureVersion, er ipkg.Feature.Name = line[2:] case line[:2] == "V:": version := string(line[2:]) - err := versionfmt.Valid("dpkg", version) + err := versionfmt.Valid(dpkg.ParserName, version) if err != nil { log.Warningf("could not parse package version '%s': %s. skipping", version, err.Error()) } else { diff --git a/worker/detectors/feature/dpkg/dpkg.go b/worker/detectors/feature/dpkg/dpkg.go index 6b658d14..f6a03dda 100644 --- a/worker/detectors/feature/dpkg/dpkg.go +++ b/worker/detectors/feature/dpkg/dpkg.go @@ -23,10 +23,8 @@ import ( "github.com/coreos/clair/database" "github.com/coreos/clair/ext/versionfmt" + "github.com/coreos/clair/ext/versionfmt/dpkg" "github.com/coreos/clair/worker/detectors" - - // dpkg versioning is used to parse dpkg packages. - _ "github.com/coreos/clair/ext/versionfmt/dpkg" ) var ( @@ -79,7 +77,7 @@ func (detector *DpkgFeaturesDetector) Detect(data map[string][]byte) ([]database pkg.Feature.Name = md["name"] if md["version"] != "" { version := md["version"] - err = versionfmt.Valid("dpkg", version) + err = versionfmt.Valid(dpkg.ParserName, version) if err != nil { log.Warningf("could not parse package version '%s': %s. skipping", string(line[1]), err.Error()) } else { @@ -93,7 +91,7 @@ func (detector *DpkgFeaturesDetector) Detect(data map[string][]byte) ([]database // 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", version) + err = versionfmt.Valid(dpkg.ParserName, version) if err != nil { log.Warningf("could not parse package version '%s': %s. skipping", string(line[1]), err.Error()) } else { diff --git a/worker/detectors/feature/rpm/rpm.go b/worker/detectors/feature/rpm/rpm.go index 342fdd60..ba45f610 100644 --- a/worker/detectors/feature/rpm/rpm.go +++ b/worker/detectors/feature/rpm/rpm.go @@ -24,12 +24,10 @@ import ( "github.com/coreos/clair/database" "github.com/coreos/clair/ext/versionfmt" + "github.com/coreos/clair/ext/versionfmt/rpm" "github.com/coreos/clair/utils" cerrors "github.com/coreos/clair/utils/errors" "github.com/coreos/clair/worker/detectors" - - // rpm versioning is used to parse rpm packages. - _ "github.com/coreos/clair/ext/versionfmt/rpm" ) var log = capnslog.NewPackageLogger("github.com/coreos/clair", "rpm") @@ -93,7 +91,7 @@ func (detector *RpmFeaturesDetector) Detect(data map[string][]byte) ([]database. // Parse version version := strings.Replace(line[1], "(none):", "", -1) - err := versionfmt.Valid("rpm", version) + err := versionfmt.Valid(rpm.ParserName, version) if err != nil { log.Warningf("could not parse package version '%s': %s. skipping", line[1], err.Error()) continue diff --git a/worker/detectors/namespace/alpinerelease/alpinerelease.go b/worker/detectors/namespace/alpinerelease/alpinerelease.go index 3587cef0..0730ad50 100644 --- a/worker/detectors/namespace/alpinerelease/alpinerelease.go +++ b/worker/detectors/namespace/alpinerelease/alpinerelease.go @@ -21,6 +21,7 @@ import ( "strings" "github.com/coreos/clair/database" + "github.com/coreos/clair/ext/versionfmt/dpkg" "github.com/coreos/clair/worker/detectors" ) @@ -50,7 +51,7 @@ func (d *detector) Detect(data map[string][]byte) *database.Namespace { versionNumbers := strings.Split(match[0], ".") return &database.Namespace{ Name: osName + ":" + "v" + versionNumbers[0] + "." + versionNumbers[1], - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, } } } diff --git a/worker/detectors/namespace/aptsources/aptsources.go b/worker/detectors/namespace/aptsources/aptsources.go index ea927501..3c201c4c 100644 --- a/worker/detectors/namespace/aptsources/aptsources.go +++ b/worker/detectors/namespace/aptsources/aptsources.go @@ -19,6 +19,7 @@ import ( "strings" "github.com/coreos/clair/database" + "github.com/coreos/clair/ext/versionfmt/dpkg" "github.com/coreos/clair/worker/detectors" ) @@ -77,7 +78,7 @@ func (detector *AptSourcesNamespaceDetector) Detect(data map[string][]byte) *dat if OS != "" && version != "" { return &database.Namespace{ Name: OS + ":" + version, - VersionFormat: "dpkg", + VersionFormat: dpkg.ParserName, } } return nil diff --git a/worker/detectors/namespace/lsbrelease/lsbrelease.go b/worker/detectors/namespace/lsbrelease/lsbrelease.go index e6c6d4c8..8f9ba81a 100644 --- a/worker/detectors/namespace/lsbrelease/lsbrelease.go +++ b/worker/detectors/namespace/lsbrelease/lsbrelease.go @@ -20,6 +20,8 @@ import ( "strings" "github.com/coreos/clair/database" + "github.com/coreos/clair/ext/versionfmt/dpkg" + "github.com/coreos/clair/ext/versionfmt/rpm" "github.com/coreos/clair/worker/detectors" ) @@ -28,8 +30,8 @@ var ( lsbReleaseVersionRegexp = regexp.MustCompile(`^DISTRIB_RELEASE=(.*)`) ) -// AptSourcesNamespaceDetector implements NamespaceDetector and detects the Namespace from the -// /etc/lsb-release file. +// LsbReleaseNamespaceDetector implements NamespaceDetector and detects the +// Namespace from the /etc/lsb-release file. // // This detector is necessary for Ubuntu Precise. type LsbReleaseNamespaceDetector struct{} @@ -73,9 +75,9 @@ func (detector *LsbReleaseNamespaceDetector) Detect(data map[string][]byte) *dat var versionFormat string switch OS { case "debian", "ubuntu": - versionFormat = "dpkg" + versionFormat = dpkg.ParserName case "centos", "rhel", "fedora", "amzn", "ol", "oracle": - versionFormat = "rpm" + versionFormat = rpm.ParserName default: return nil } diff --git a/worker/detectors/namespace/osrelease/osrelease.go b/worker/detectors/namespace/osrelease/osrelease.go index b2f7db02..d6467bdf 100644 --- a/worker/detectors/namespace/osrelease/osrelease.go +++ b/worker/detectors/namespace/osrelease/osrelease.go @@ -20,6 +20,8 @@ import ( "strings" "github.com/coreos/clair/database" + "github.com/coreos/clair/ext/versionfmt/dpkg" + "github.com/coreos/clair/ext/versionfmt/rpm" "github.com/coreos/clair/worker/detectors" ) @@ -76,9 +78,9 @@ func (detector *OsReleaseNamespaceDetector) Detect(data map[string][]byte) *data var versionFormat string switch OS { case "debian", "ubuntu": - versionFormat = "dpkg" + versionFormat = dpkg.ParserName case "centos", "rhel", "fedora", "amzn", "ol", "oracle": - versionFormat = "rpm" + versionFormat = rpm.ParserName default: return nil } diff --git a/worker/detectors/namespace/redhatrelease/redhatrelease.go b/worker/detectors/namespace/redhatrelease/redhatrelease.go index 2d672129..4271c47c 100644 --- a/worker/detectors/namespace/redhatrelease/redhatrelease.go +++ b/worker/detectors/namespace/redhatrelease/redhatrelease.go @@ -19,6 +19,7 @@ import ( "strings" "github.com/coreos/clair/database" + "github.com/coreos/clair/ext/versionfmt/rpm" "github.com/coreos/clair/worker/detectors" "github.com/coreos/pkg/capnslog" ) @@ -60,7 +61,7 @@ func (detector *RedhatReleaseNamespaceDetector) Detect(data map[string][]byte) * if len(r) == 4 { return &database.Namespace{ Name: strings.ToLower(r[1]) + ":" + r[3], - VersionFormat: "rpm", + VersionFormat: rpm.ParserName, } } @@ -70,7 +71,7 @@ func (detector *RedhatReleaseNamespaceDetector) Detect(data map[string][]byte) * // TODO(vbatts) this is a hack until https://github.com/coreos/clair/pull/193 return &database.Namespace{ Name: "centos" + ":" + r[3], - VersionFormat: "rpm", + VersionFormat: rpm.ParserName, } } @@ -79,7 +80,7 @@ func (detector *RedhatReleaseNamespaceDetector) Detect(data map[string][]byte) * if len(r) == 4 { return &database.Namespace{ Name: strings.ToLower(r[1]) + ":" + r[3], - VersionFormat: "rpm", + VersionFormat: rpm.ParserName, } } diff --git a/worker/worker_test.go b/worker/worker_test.go index bc7683ad..fb8c270f 100644 --- a/worker/worker_test.go +++ b/worker/worker_test.go @@ -22,6 +22,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/coreos/clair/database" + "github.com/coreos/clair/ext/versionfmt/dpkg" cerrors "github.com/coreos/clair/utils/errors" // Register the required detectors. @@ -89,7 +90,7 @@ func TestProcessWithDistUpgrade(t *testing.T) { for _, nufv := range nonUpgradedFeatureVersions { nufv.Feature.Namespace.Name = "debian:7" - nufv.Feature.Namespace.VersionFormat = "dpkg" + nufv.Feature.Namespace.VersionFormat = dpkg.ParserName assert.Contains(t, wheezy.Features, nufv) } } @@ -102,12 +103,12 @@ func TestProcessWithDistUpgrade(t *testing.T) { for _, nufv := range nonUpgradedFeatureVersions { nufv.Feature.Namespace.Name = "debian:7" - nufv.Feature.Namespace.VersionFormat = "dpkg" + nufv.Feature.Namespace.VersionFormat = dpkg.ParserName assert.Contains(t, jessie.Features, nufv) } for _, nufv := range nonUpgradedFeatureVersions { nufv.Feature.Namespace.Name = "debian:8" - nufv.Feature.Namespace.VersionFormat = "dpkg" + nufv.Feature.Namespace.VersionFormat = dpkg.ParserName assert.NotContains(t, jessie.Features, nufv) } }