add 'version' to namespace
Signed-off-by: liang chenye <liangchenye@huawei.com>
This commit is contained in:
parent
b2e6fd114b
commit
53958f6ea5
@ -64,7 +64,7 @@ type Datastore interface {
|
|||||||
// The Limit and page parameters are used to paginate the return list.
|
// The Limit and page parameters are used to paginate the return list.
|
||||||
// The first given page should be 0. The function will then return the next available page.
|
// The first given page should be 0. The function will then return the next available page.
|
||||||
// If there is no more page, -1 has to be returned.
|
// If there is no more page, -1 has to be returned.
|
||||||
ListVulnerabilities(namespaceName string, limit int, page int) ([]Vulnerability, int, error)
|
ListVulnerabilities(namespace Namespace, limit int, page int) ([]Vulnerability, int, error)
|
||||||
|
|
||||||
// InsertVulnerabilities stores the given Vulnerabilities in the database, updating them if
|
// InsertVulnerabilities stores the given Vulnerabilities in the database, updating them if
|
||||||
// necessary. A vulnerability is uniquely identified by its Namespace and its Name.
|
// necessary. A vulnerability is uniquely identified by its Namespace and its Name.
|
||||||
@ -81,22 +81,22 @@ type Datastore interface {
|
|||||||
InsertVulnerabilities(vulnerabilities []Vulnerability, createNotification bool) error
|
InsertVulnerabilities(vulnerabilities []Vulnerability, createNotification bool) error
|
||||||
|
|
||||||
// FindVulnerability retrieves a Vulnerability from the database, including the FixedIn list.
|
// FindVulnerability retrieves a Vulnerability from the database, including the FixedIn list.
|
||||||
FindVulnerability(namespaceName, name string) (Vulnerability, error)
|
FindVulnerability(namespace Namespace, name string) (Vulnerability, error)
|
||||||
|
|
||||||
// DeleteVulnerability removes a Vulnerability from the database.
|
// DeleteVulnerability removes a Vulnerability from the database.
|
||||||
// It has to create a Notification that will contain the old Vulnerability.
|
// It has to create a Notification that will contain the old Vulnerability.
|
||||||
DeleteVulnerability(namespaceName, name string) error
|
DeleteVulnerability(namespace Namespace, name string) error
|
||||||
|
|
||||||
// InsertVulnerabilityFixes adds new FixedIn Feature or update the Versions of existing ones to
|
// InsertVulnerabilityFixes adds new FixedIn Feature or update the Versions of existing ones to
|
||||||
// the specified Vulnerability in the database.
|
// the specified Vulnerability in the database.
|
||||||
// It has has to create a Notification that will contain the old and the updated Vulnerability.
|
// It has has to create a Notification that will contain the old and the updated Vulnerability.
|
||||||
InsertVulnerabilityFixes(vulnerabilityNamespace, vulnerabilityName string, fixes []FeatureVersion) error
|
InsertVulnerabilityFixes(vulnerabilityNamespace Namespace, vulnerabilityName string, fixes []FeatureVersion) error
|
||||||
|
|
||||||
// DeleteVulnerabilityFix removes a FixedIn Feature from the specified Vulnerability in the
|
// DeleteVulnerabilityFix removes a FixedIn Feature from the specified Vulnerability in the
|
||||||
// database. It can be used to store the fact that a Vulnerability no longer affects the given
|
// database. It can be used to store the fact that a Vulnerability no longer affects the given
|
||||||
// Feature in any Version.
|
// Feature in any Version.
|
||||||
// It has has to create a Notification that will contain the old and the updated Vulnerability.
|
// It has has to create a Notification that will contain the old and the updated Vulnerability.
|
||||||
DeleteVulnerabilityFix(vulnerabilityNamespace, vulnerabilityName, featureName string) error
|
DeleteVulnerabilityFix(vulnerabilityNamespace Namespace, vulnerabilityName, featureName string) error
|
||||||
|
|
||||||
// # Notification
|
// # Notification
|
||||||
// GetAvailableNotification returns the Name, Created, Notified and Deleted fields of a
|
// GetAvailableNotification returns the Name, Created, Notified and Deleted fields of a
|
||||||
|
@ -40,7 +40,24 @@ type Layer struct {
|
|||||||
type Namespace struct {
|
type Namespace struct {
|
||||||
Model
|
Model
|
||||||
|
|
||||||
Name string
|
Name string
|
||||||
|
Version types.Version
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ns *Namespace) IsEmpty() bool {
|
||||||
|
if ns.Name == "" && ns.Version.String() == "" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ns *Namespace) Equal(namespace Namespace) bool {
|
||||||
|
if ns.Name == namespace.Name && ns.Version.Compare(namespace.Version) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
type Feature struct {
|
type Feature struct {
|
||||||
|
@ -45,8 +45,11 @@ func TestRaceAffects(t *testing.T) {
|
|||||||
|
|
||||||
// Insert the Feature on which we'll work.
|
// Insert the Feature on which we'll work.
|
||||||
feature := database.Feature{
|
feature := database.Feature{
|
||||||
Namespace: database.Namespace{Name: "TestRaceAffectsFeatureNamespace1"},
|
Namespace: database.Namespace{
|
||||||
Name: "TestRaceAffecturesFeature1",
|
Name: "TestRaceAffectsFeatureNamespace",
|
||||||
|
Version: types.NewVersionUnsafe("1.0"),
|
||||||
|
},
|
||||||
|
Name: "TestRaceAffecturesFeature1",
|
||||||
}
|
}
|
||||||
_, err = datastore.insertFeature(feature)
|
_, err = datastore.insertFeature(feature)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -31,7 +31,7 @@ func (pgSQL *pgSQL) insertFeature(feature database.Feature) (int, error) {
|
|||||||
// Do cache lookup.
|
// Do cache lookup.
|
||||||
if pgSQL.cache != nil {
|
if pgSQL.cache != nil {
|
||||||
promCacheQueriesTotal.WithLabelValues("feature").Inc()
|
promCacheQueriesTotal.WithLabelValues("feature").Inc()
|
||||||
id, found := pgSQL.cache.Get("feature:" + feature.Namespace.Name + ":" + feature.Name)
|
id, found := pgSQL.cache.Get("feature:" + feature.Namespace.Name + ":" + feature.Namespace.Version.String() + ":" + feature.Name)
|
||||||
if found {
|
if found {
|
||||||
promCacheHitsTotal.WithLabelValues("feature").Inc()
|
promCacheHitsTotal.WithLabelValues("feature").Inc()
|
||||||
return id.(int), nil
|
return id.(int), nil
|
||||||
@ -55,7 +55,7 @@ func (pgSQL *pgSQL) insertFeature(feature database.Feature) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if pgSQL.cache != nil {
|
if pgSQL.cache != nil {
|
||||||
pgSQL.cache.Add("feature:"+feature.Namespace.Name+":"+feature.Name, id)
|
pgSQL.cache.Add("feature:"+feature.Namespace.Name+":"+feature.Namespace.Version.String()+":"+feature.Name, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
return id, nil
|
return id, nil
|
||||||
@ -67,7 +67,7 @@ func (pgSQL *pgSQL) insertFeatureVersion(featureVersion database.FeatureVersion)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Do cache lookup.
|
// Do cache lookup.
|
||||||
cacheIndex := "featureversion:" + featureVersion.Feature.Namespace.Name + ":" + featureVersion.Feature.Name + ":" + featureVersion.Version.String()
|
cacheIndex := "featureversion:" + featureVersion.Feature.Namespace.Name + ":" + featureVersion.Feature.Namespace.Version.String() + ":" + featureVersion.Feature.Name + ":" + featureVersion.Version.String()
|
||||||
if pgSQL.cache != nil {
|
if pgSQL.cache != nil {
|
||||||
promCacheQueriesTotal.WithLabelValues("featureversion").Inc()
|
promCacheQueriesTotal.WithLabelValues("featureversion").Inc()
|
||||||
id, found := pgSQL.cache.Get(cacheIndex)
|
id, found := pgSQL.cache.Get(cacheIndex)
|
||||||
|
@ -44,8 +44,11 @@ func TestInsertFeature(t *testing.T) {
|
|||||||
|
|
||||||
// Insert Feature and ensure we can find it.
|
// Insert Feature and ensure we can find it.
|
||||||
feature := database.Feature{
|
feature := database.Feature{
|
||||||
Namespace: database.Namespace{Name: "TestInsertFeatureNamespace1"},
|
Namespace: database.Namespace{
|
||||||
Name: "TestInsertFeature1",
|
Name: "TestInsertFeatureNamespace",
|
||||||
|
Version: types.NewVersionUnsafe("1.0"),
|
||||||
|
},
|
||||||
|
Name: "TestInsertFeature1",
|
||||||
}
|
}
|
||||||
id1, err := datastore.insertFeature(feature)
|
id1, err := datastore.insertFeature(feature)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
@ -68,15 +71,21 @@ func TestInsertFeature(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
Feature: database.Feature{
|
Feature: database.Feature{
|
||||||
Namespace: database.Namespace{Name: "TestInsertFeatureNamespace2"},
|
Namespace: database.Namespace{
|
||||||
Name: "TestInsertFeature2",
|
Name: "TestInsertFeatureNamespace",
|
||||||
|
Version: types.NewVersionUnsafe("2.0"),
|
||||||
|
},
|
||||||
|
Name: "TestInsertFeature2",
|
||||||
},
|
},
|
||||||
Version: types.NewVersionUnsafe(""),
|
Version: types.NewVersionUnsafe(""),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Feature: database.Feature{
|
Feature: database.Feature{
|
||||||
Namespace: database.Namespace{Name: "TestInsertFeatureNamespace2"},
|
Namespace: database.Namespace{
|
||||||
Name: "TestInsertFeature2",
|
Name: "TestInsertFeatureNamespace",
|
||||||
|
Version: types.NewVersionUnsafe("2.0"),
|
||||||
|
},
|
||||||
|
Name: "TestInsertFeature2",
|
||||||
},
|
},
|
||||||
Version: types.NewVersionUnsafe("bad version"),
|
Version: types.NewVersionUnsafe("bad version"),
|
||||||
},
|
},
|
||||||
@ -89,8 +98,11 @@ func TestInsertFeature(t *testing.T) {
|
|||||||
// Insert FeatureVersion and ensure we can find it.
|
// Insert FeatureVersion and ensure we can find it.
|
||||||
featureVersion := database.FeatureVersion{
|
featureVersion := database.FeatureVersion{
|
||||||
Feature: database.Feature{
|
Feature: database.Feature{
|
||||||
Namespace: database.Namespace{Name: "TestInsertFeatureNamespace1"},
|
Namespace: database.Namespace{
|
||||||
Name: "TestInsertFeature1",
|
Name: "TestInsertFeatureNamespace",
|
||||||
|
Version: types.NewVersionUnsafe("1.0"),
|
||||||
|
},
|
||||||
|
Name: "TestInsertFeature1",
|
||||||
},
|
},
|
||||||
Version: types.NewVersionUnsafe("2:3.0-imba"),
|
Version: types.NewVersionUnsafe("2:3.0-imba"),
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,6 @@ package pgsql
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/coreos/clair/database"
|
"github.com/coreos/clair/database"
|
||||||
@ -66,7 +65,7 @@ func (pgSQL *pgSQL) FindLayer(name string, withFeatures, withVulnerabilities boo
|
|||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var pn database.Namespace
|
var pn database.Namespace
|
||||||
|
|
||||||
err = rows.Scan(&pn.ID, &pn.Name)
|
err = rows.Scan(&pn.ID, &pn.Name, &pn.Version)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return layer, handleError("searchLayerNamespace.Scan()", err)
|
return layer, handleError("searchLayerNamespace.Scan()", err)
|
||||||
}
|
}
|
||||||
@ -89,7 +88,7 @@ func (pgSQL *pgSQL) FindLayer(name string, withFeatures, withVulnerabilities boo
|
|||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var namespace database.Namespace
|
var namespace database.Namespace
|
||||||
|
|
||||||
err = rows.Scan(&namespace.ID, &namespace.Name)
|
err = rows.Scan(&namespace.ID, &namespace.Name, &namespace.Version)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return layer, handleError("searchLayerNamespace.Scan()", err)
|
return layer, handleError("searchLayerNamespace.Scan()", err)
|
||||||
}
|
}
|
||||||
@ -166,8 +165,9 @@ func getLayerFeatureVersions(tx *sql.Tx, layerID int) ([]database.FeatureVersion
|
|||||||
var featureVersion database.FeatureVersion
|
var featureVersion database.FeatureVersion
|
||||||
|
|
||||||
err = rows.Scan(&featureVersion.ID, &modification, &featureVersion.Feature.Namespace.ID,
|
err = rows.Scan(&featureVersion.ID, &modification, &featureVersion.Feature.Namespace.ID,
|
||||||
&featureVersion.Feature.Namespace.Name, &featureVersion.Feature.ID,
|
&featureVersion.Feature.Namespace.Name, &featureVersion.Feature.Namespace.Version,
|
||||||
&featureVersion.Feature.Name, &featureVersion.ID, &featureVersion.Version,
|
&featureVersion.Feature.ID, &featureVersion.Feature.Name,
|
||||||
|
&featureVersion.ID, &featureVersion.Version,
|
||||||
&featureVersion.AddedBy.ID, &featureVersion.AddedBy.Name)
|
&featureVersion.AddedBy.ID, &featureVersion.AddedBy.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return featureVersions, handleError("searchLayerFeatureVersion.Scan()", err)
|
return featureVersions, handleError("searchLayerFeatureVersion.Scan()", err)
|
||||||
@ -222,7 +222,8 @@ func loadAffectedBy(tx *sql.Tx, featureVersions []database.FeatureVersion) error
|
|||||||
var vulnerability database.Vulnerability
|
var vulnerability database.Vulnerability
|
||||||
err := rows.Scan(&featureversionID, &vulnerability.ID, &vulnerability.Name,
|
err := rows.Scan(&featureversionID, &vulnerability.ID, &vulnerability.Name,
|
||||||
&vulnerability.Description, &vulnerability.Link, &vulnerability.Severity,
|
&vulnerability.Description, &vulnerability.Link, &vulnerability.Severity,
|
||||||
&vulnerability.Metadata, &vulnerability.Namespace.Name, &vulnerability.FixedBy)
|
&vulnerability.Metadata, &vulnerability.Namespace.Name, &vulnerability.Namespace.Version,
|
||||||
|
&vulnerability.FixedBy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return handleError("searchFeatureVersionVulnerability.Scan()", err)
|
return handleError("searchFeatureVersionVulnerability.Scan()", err)
|
||||||
}
|
}
|
||||||
@ -286,9 +287,7 @@ func (pgSQL *pgSQL) InsertLayer(layer database.Layer) error {
|
|||||||
// Layer's namespaces has high priority than its parent.
|
// Layer's namespaces has high priority than its parent.
|
||||||
// Once a layer has a 'same' namespace with its parent,
|
// Once a layer has a 'same' namespace with its parent,
|
||||||
// it will only keep its namespace.
|
// it will only keep its namespace.
|
||||||
//TODO: add 'Version' to Namespace and use 'Name' directly
|
mapNamespaceIDs[layer.Namespaces[i].Name] = id
|
||||||
name := strings.Split(layer.Namespaces[i].Name, ":")
|
|
||||||
mapNamespaceIDs[name[0]] = id
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get parent ID.
|
// Get parent ID.
|
||||||
@ -302,11 +301,9 @@ func (pgSQL *pgSQL) InsertLayer(layer database.Layer) error {
|
|||||||
parentID = zero.IntFrom(int64(layer.Parent.ID))
|
parentID = zero.IntFrom(int64(layer.Parent.ID))
|
||||||
|
|
||||||
for _, pn := range layer.Parent.Namespaces {
|
for _, pn := range layer.Parent.Namespaces {
|
||||||
//TODO: add 'Version' to Namespace and use 'Name' directly
|
if _, ok := mapNamespaceIDs[pn.Name]; !ok {
|
||||||
name := strings.Split(pn.Name, ":")
|
|
||||||
if _, ok := mapNamespaceIDs[name[0]]; !ok {
|
|
||||||
// Layer will inherit its parent's namespace
|
// Layer will inherit its parent's namespace
|
||||||
mapNamespaceIDs[name[0]] = pn.ID
|
mapNamespaceIDs[pn.Name] = pn.ID
|
||||||
layer.Namespaces = append(layer.Namespaces, pn)
|
layer.Namespaces = append(layer.Namespaces, pn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,10 @@ func TestFindLayer(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer datastore.Close()
|
defer datastore.Close()
|
||||||
|
|
||||||
|
testDebian7 := database.Namespace{
|
||||||
|
Name: "debian",
|
||||||
|
Version: types.NewVersionUnsafe("7"),
|
||||||
|
}
|
||||||
// Layer-0: no parent, no namespace, no feature, no vulnerability
|
// Layer-0: no parent, no namespace, no feature, no vulnerability
|
||||||
layer, err := datastore.FindLayer("layer-0", false, false)
|
layer, err := datastore.FindLayer("layer-0", false, false)
|
||||||
if assert.Nil(t, err) && assert.NotNil(t, layer) {
|
if assert.Nil(t, err) && assert.NotNil(t, layer) {
|
||||||
@ -56,14 +60,14 @@ func TestFindLayer(t *testing.T) {
|
|||||||
}
|
}
|
||||||
assert.Equal(t, 1, layer.EngineVersion)
|
assert.Equal(t, 1, layer.EngineVersion)
|
||||||
assert.Len(t, layer.Namespaces, 1)
|
assert.Len(t, layer.Namespaces, 1)
|
||||||
assert.Equal(t, "debian:7", layer.Namespaces[0].Name)
|
assert.True(t, testDebian7.Equal(layer.Namespaces[0]))
|
||||||
assert.Len(t, layer.Features, 0)
|
assert.Len(t, layer.Features, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
layer, err = datastore.FindLayer("layer-1", true, false)
|
layer, err = datastore.FindLayer("layer-1", true, false)
|
||||||
if assert.Nil(t, err) && assert.NotNil(t, layer) && assert.Len(t, layer.Features, 2) {
|
if assert.Nil(t, err) && assert.NotNil(t, layer) && assert.Len(t, layer.Features, 2) {
|
||||||
for _, featureVersion := range layer.Features {
|
for _, featureVersion := range layer.Features {
|
||||||
assert.Equal(t, "debian:7", featureVersion.Feature.Namespace.Name)
|
assert.True(t, testDebian7.Equal(featureVersion.Feature.Namespace))
|
||||||
|
|
||||||
switch featureVersion.Feature.Name {
|
switch featureVersion.Feature.Name {
|
||||||
case "wechat":
|
case "wechat":
|
||||||
@ -79,7 +83,7 @@ func TestFindLayer(t *testing.T) {
|
|||||||
layer, err = datastore.FindLayer("layer-1", true, true)
|
layer, err = datastore.FindLayer("layer-1", true, true)
|
||||||
if assert.Nil(t, err) && assert.NotNil(t, layer) && assert.Len(t, layer.Features, 2) {
|
if assert.Nil(t, err) && assert.NotNil(t, layer) && assert.Len(t, layer.Features, 2) {
|
||||||
for _, featureVersion := range layer.Features {
|
for _, featureVersion := range layer.Features {
|
||||||
assert.Equal(t, "debian:7", featureVersion.Feature.Namespace.Name)
|
assert.True(t, testDebian7.Equal(featureVersion.Feature.Namespace))
|
||||||
|
|
||||||
switch featureVersion.Feature.Name {
|
switch featureVersion.Feature.Name {
|
||||||
case "wechat":
|
case "wechat":
|
||||||
@ -88,7 +92,7 @@ func TestFindLayer(t *testing.T) {
|
|||||||
assert.Equal(t, types.NewVersionUnsafe("1.0"), featureVersion.Version)
|
assert.Equal(t, types.NewVersionUnsafe("1.0"), featureVersion.Version)
|
||||||
|
|
||||||
if assert.Len(t, featureVersion.AffectedBy, 1) {
|
if assert.Len(t, featureVersion.AffectedBy, 1) {
|
||||||
assert.Equal(t, "debian:7", featureVersion.AffectedBy[0].Namespace.Name)
|
assert.True(t, testDebian7.Equal(featureVersion.AffectedBy[0].Namespace))
|
||||||
assert.Equal(t, "CVE-OPENSSL-1-DEB7", featureVersion.AffectedBy[0].Name)
|
assert.Equal(t, "CVE-OPENSSL-1-DEB7", featureVersion.AffectedBy[0].Name)
|
||||||
assert.Equal(t, types.High, featureVersion.AffectedBy[0].Severity)
|
assert.Equal(t, types.High, featureVersion.AffectedBy[0].Severity)
|
||||||
assert.Equal(t, "A vulnerability affecting OpenSSL < 2.0 on Debian 7.0", featureVersion.AffectedBy[0].Description)
|
assert.Equal(t, "A vulnerability affecting OpenSSL < 2.0 on Debian 7.0", featureVersion.AffectedBy[0].Description)
|
||||||
@ -137,44 +141,56 @@ func testInsertLayerInvalid(t *testing.T, datastore database.Datastore) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testInsertLayerTree(t *testing.T, datastore database.Datastore) {
|
func testInsertLayerTree(t *testing.T, datastore database.Datastore) {
|
||||||
|
testInsertLayerNamespace1 := database.Namespace{
|
||||||
|
Name: "TestInsertLayerNamespace",
|
||||||
|
Version: types.NewVersionUnsafe("1"),
|
||||||
|
}
|
||||||
|
testInsertLayerNamespace2 := database.Namespace{
|
||||||
|
Name: "TestInsertLayerNamespace",
|
||||||
|
Version: types.NewVersionUnsafe("2"),
|
||||||
|
}
|
||||||
|
testInsertLayerNamespace3 := database.Namespace{
|
||||||
|
Name: "TestInsertLayerNamespace",
|
||||||
|
Version: types.NewVersionUnsafe("3"),
|
||||||
|
}
|
||||||
f1 := database.FeatureVersion{
|
f1 := database.FeatureVersion{
|
||||||
Feature: database.Feature{
|
Feature: database.Feature{
|
||||||
Namespace: database.Namespace{Name: "TestInsertLayerNamespace:2"},
|
Namespace: testInsertLayerNamespace2,
|
||||||
Name: "TestInsertLayerFeature1",
|
Name: "TestInsertLayerFeature1",
|
||||||
},
|
},
|
||||||
Version: types.NewVersionUnsafe("1.0"),
|
Version: types.NewVersionUnsafe("1.0"),
|
||||||
}
|
}
|
||||||
f2 := database.FeatureVersion{
|
f2 := database.FeatureVersion{
|
||||||
Feature: database.Feature{
|
Feature: database.Feature{
|
||||||
Namespace: database.Namespace{Name: "TestInsertLayerNamespace:2"},
|
Namespace: testInsertLayerNamespace2,
|
||||||
Name: "TestInsertLayerFeature2",
|
Name: "TestInsertLayerFeature2",
|
||||||
},
|
},
|
||||||
Version: types.NewVersionUnsafe("0.34"),
|
Version: types.NewVersionUnsafe("0.34"),
|
||||||
}
|
}
|
||||||
f3 := database.FeatureVersion{
|
f3 := database.FeatureVersion{
|
||||||
Feature: database.Feature{
|
Feature: database.Feature{
|
||||||
Namespace: database.Namespace{Name: "TestInsertLayerNamespace:2"},
|
Namespace: testInsertLayerNamespace2,
|
||||||
Name: "TestInsertLayerFeature3",
|
Name: "TestInsertLayerFeature3",
|
||||||
},
|
},
|
||||||
Version: types.NewVersionUnsafe("0.56"),
|
Version: types.NewVersionUnsafe("0.56"),
|
||||||
}
|
}
|
||||||
f4 := database.FeatureVersion{
|
f4 := database.FeatureVersion{
|
||||||
Feature: database.Feature{
|
Feature: database.Feature{
|
||||||
Namespace: database.Namespace{Name: "TestInsertLayerNamespace:3"},
|
Namespace: testInsertLayerNamespace3,
|
||||||
Name: "TestInsertLayerFeature2",
|
Name: "TestInsertLayerFeature2",
|
||||||
},
|
},
|
||||||
Version: types.NewVersionUnsafe("0.34"),
|
Version: types.NewVersionUnsafe("0.34"),
|
||||||
}
|
}
|
||||||
f5 := database.FeatureVersion{
|
f5 := database.FeatureVersion{
|
||||||
Feature: database.Feature{
|
Feature: database.Feature{
|
||||||
Namespace: database.Namespace{Name: "TestInsertLayerNamespace:3"},
|
Namespace: testInsertLayerNamespace3,
|
||||||
Name: "TestInsertLayerFeature3",
|
Name: "TestInsertLayerFeature3",
|
||||||
},
|
},
|
||||||
Version: types.NewVersionUnsafe("0.57"),
|
Version: types.NewVersionUnsafe("0.57"),
|
||||||
}
|
}
|
||||||
f6 := database.FeatureVersion{
|
f6 := database.FeatureVersion{
|
||||||
Feature: database.Feature{
|
Feature: database.Feature{
|
||||||
Namespace: database.Namespace{Name: "TestInsertLayerNamespace:3"},
|
Namespace: testInsertLayerNamespace3,
|
||||||
Name: "TestInsertLayerFeature4",
|
Name: "TestInsertLayerFeature4",
|
||||||
},
|
},
|
||||||
Version: types.NewVersionUnsafe("0.666"),
|
Version: types.NewVersionUnsafe("0.666"),
|
||||||
@ -187,13 +203,13 @@ func testInsertLayerTree(t *testing.T, datastore database.Datastore) {
|
|||||||
{
|
{
|
||||||
Name: "TestInsertLayer2",
|
Name: "TestInsertLayer2",
|
||||||
Parent: &database.Layer{Name: "TestInsertLayer1"},
|
Parent: &database.Layer{Name: "TestInsertLayer1"},
|
||||||
Namespaces: []database.Namespace{database.Namespace{Name: "TestInsertLayerNamespace:1"}},
|
Namespaces: []database.Namespace{testInsertLayerNamespace1},
|
||||||
},
|
},
|
||||||
// This layer changes the namespace and adds Features.
|
// This layer changes the namespace and adds Features.
|
||||||
{
|
{
|
||||||
Name: "TestInsertLayer3",
|
Name: "TestInsertLayer3",
|
||||||
Parent: &database.Layer{Name: "TestInsertLayer2"},
|
Parent: &database.Layer{Name: "TestInsertLayer2"},
|
||||||
Namespaces: []database.Namespace{database.Namespace{Name: "TestInsertLayerNamespace:2"}},
|
Namespaces: []database.Namespace{testInsertLayerNamespace2},
|
||||||
Features: []database.FeatureVersion{f1, f2, f3},
|
Features: []database.FeatureVersion{f1, f2, f3},
|
||||||
},
|
},
|
||||||
// This layer covers the case where the last layer doesn't provide any new Feature.
|
// This layer covers the case where the last layer doesn't provide any new Feature.
|
||||||
@ -208,7 +224,7 @@ func testInsertLayerTree(t *testing.T, datastore database.Datastore) {
|
|||||||
{
|
{
|
||||||
Name: "TestInsertLayer4b",
|
Name: "TestInsertLayer4b",
|
||||||
Parent: &database.Layer{Name: "TestInsertLayer3"},
|
Parent: &database.Layer{Name: "TestInsertLayer3"},
|
||||||
Namespaces: []database.Namespace{database.Namespace{Name: "TestInsertLayerNamespace:3"}},
|
Namespaces: []database.Namespace{testInsertLayerNamespace3},
|
||||||
Features: []database.FeatureVersion{
|
Features: []database.FeatureVersion{
|
||||||
// Deletes TestInsertLayerFeature1.
|
// Deletes TestInsertLayerFeature1.
|
||||||
// Keep TestInsertLayerFeature2 (old Namespace should be kept):
|
// Keep TestInsertLayerFeature2 (old Namespace should be kept):
|
||||||
@ -239,7 +255,7 @@ func testInsertLayerTree(t *testing.T, datastore database.Datastore) {
|
|||||||
|
|
||||||
l4a := retrievedLayers["TestInsertLayer4a"]
|
l4a := retrievedLayers["TestInsertLayer4a"]
|
||||||
if assert.Len(t, l4a.Namespaces, 1) {
|
if assert.Len(t, l4a.Namespaces, 1) {
|
||||||
assert.Equal(t, "TestInsertLayerNamespace:2", l4a.Namespaces[0].Name)
|
assert.True(t, testInsertLayerNamespace2.Equal(l4a.Namespaces[0]))
|
||||||
}
|
}
|
||||||
assert.Len(t, l4a.Features, 3)
|
assert.Len(t, l4a.Features, 3)
|
||||||
for _, featureVersion := range l4a.Features {
|
for _, featureVersion := range l4a.Features {
|
||||||
@ -250,7 +266,7 @@ func testInsertLayerTree(t *testing.T, datastore database.Datastore) {
|
|||||||
|
|
||||||
l4b := retrievedLayers["TestInsertLayer4b"]
|
l4b := retrievedLayers["TestInsertLayer4b"]
|
||||||
if assert.Len(t, l4b.Namespaces, 1) {
|
if assert.Len(t, l4b.Namespaces, 1) {
|
||||||
assert.Equal(t, "TestInsertLayerNamespace:3", l4b.Namespaces[0].Name)
|
assert.True(t, testInsertLayerNamespace3.Equal(l4b.Namespaces[0]))
|
||||||
}
|
}
|
||||||
assert.Len(t, l4b.Features, 3)
|
assert.Len(t, l4b.Features, 3)
|
||||||
for _, featureVersion := range l4b.Features {
|
for _, featureVersion := range l4b.Features {
|
||||||
@ -261,9 +277,17 @@ func testInsertLayerTree(t *testing.T, datastore database.Datastore) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testInsertLayerUpdate(t *testing.T, datastore database.Datastore) {
|
func testInsertLayerUpdate(t *testing.T, datastore database.Datastore) {
|
||||||
|
testInsertLayerNamespace3 := database.Namespace{
|
||||||
|
Name: "TestInsertLayerNamespace",
|
||||||
|
Version: types.NewVersionUnsafe("3"),
|
||||||
|
}
|
||||||
|
testInsertLayerNamespaceUpdated1 := database.Namespace{
|
||||||
|
Name: "TestInsertLayerNamespaceUpdated",
|
||||||
|
Version: types.NewVersionUnsafe("1"),
|
||||||
|
}
|
||||||
f7 := database.FeatureVersion{
|
f7 := database.FeatureVersion{
|
||||||
Feature: database.Feature{
|
Feature: database.Feature{
|
||||||
Namespace: database.Namespace{Name: "TestInsertLayerNamespace:3"},
|
Namespace: testInsertLayerNamespace3,
|
||||||
Name: "TestInsertLayerFeature7",
|
Name: "TestInsertLayerFeature7",
|
||||||
},
|
},
|
||||||
Version: types.NewVersionUnsafe("0.01"),
|
Version: types.NewVersionUnsafe("0.01"),
|
||||||
@ -273,7 +297,7 @@ func testInsertLayerUpdate(t *testing.T, datastore database.Datastore) {
|
|||||||
l3u := database.Layer{
|
l3u := database.Layer{
|
||||||
Name: l3.Name,
|
Name: l3.Name,
|
||||||
Parent: l3.Parent,
|
Parent: l3.Parent,
|
||||||
Namespaces: []database.Namespace{database.Namespace{Name: "TestInsertLayerNamespaceUpdated:1"}},
|
Namespaces: []database.Namespace{testInsertLayerNamespaceUpdated1},
|
||||||
Features: []database.FeatureVersion{f7},
|
Features: []database.FeatureVersion{f7},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -291,7 +315,7 @@ func testInsertLayerUpdate(t *testing.T, datastore database.Datastore) {
|
|||||||
l3uf, err := datastore.FindLayer(l3u.Name, true, false)
|
l3uf, err := datastore.FindLayer(l3u.Name, true, false)
|
||||||
if assert.Nil(t, err) {
|
if assert.Nil(t, err) {
|
||||||
if assert.Len(t, l3.Namespaces, 1) && assert.Len(t, l3uf.Namespaces, 1) {
|
if assert.Len(t, l3.Namespaces, 1) && assert.Len(t, l3uf.Namespaces, 1) {
|
||||||
assert.Equal(t, l3.Namespaces[0].Name, l3uf.Namespaces[0].Name)
|
assert.True(t, l3.Namespaces[0].Equal(l3uf.Namespaces[0]))
|
||||||
}
|
}
|
||||||
assert.Equal(t, l3.EngineVersion, l3uf.EngineVersion)
|
assert.Equal(t, l3.EngineVersion, l3uf.EngineVersion)
|
||||||
assert.Len(t, l3uf.Features, len(l3.Features))
|
assert.Len(t, l3uf.Features, len(l3.Features))
|
||||||
@ -304,7 +328,6 @@ func testInsertLayerUpdate(t *testing.T, datastore database.Datastore) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
l3uf, err = datastore.FindLayer(l3u.Name, true, false)
|
l3uf, err = datastore.FindLayer(l3u.Name, true, false)
|
||||||
fmt.Println(l3u.Namespaces, l3uf.Namespaces)
|
|
||||||
if assert.Nil(t, err) {
|
if assert.Nil(t, err) {
|
||||||
if assert.Len(t, l3u.Namespaces, 1) && assert.Len(t, l3uf.Namespaces, 2) {
|
if assert.Len(t, l3u.Namespaces, 1) && assert.Len(t, l3uf.Namespaces, 2) {
|
||||||
assert.True(t, containNS(l3uf.Namespaces, l3u.Namespaces[0]), "Updated layer should have %#v", l3u.Namespaces[0])
|
assert.True(t, containNS(l3uf.Namespaces, l3u.Namespaces[0]), "Updated layer should have %#v", l3u.Namespaces[0])
|
||||||
@ -359,7 +382,7 @@ func cmpFV(a, b database.FeatureVersion) bool {
|
|||||||
|
|
||||||
func containNS(namespaces []database.Namespace, namespace database.Namespace) bool {
|
func containNS(namespaces []database.Namespace, namespace database.Namespace) bool {
|
||||||
for _, n := range namespaces {
|
for _, n := range namespaces {
|
||||||
if n.Name == namespace.Name {
|
if n.Equal(namespace) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,8 @@
|
|||||||
-- -----------------------------------------------------
|
-- -----------------------------------------------------
|
||||||
CREATE TABLE IF NOT EXISTS Namespace (
|
CREATE TABLE IF NOT EXISTS Namespace (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
name VARCHAR(128) NULL);
|
name VARCHAR(128) NULL,
|
||||||
|
version VARCHAR(128) NULL);
|
||||||
|
|
||||||
|
|
||||||
-- -----------------------------------------------------
|
-- -----------------------------------------------------
|
||||||
|
@ -22,13 +22,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (pgSQL *pgSQL) insertNamespace(namespace database.Namespace) (int, error) {
|
func (pgSQL *pgSQL) insertNamespace(namespace database.Namespace) (int, error) {
|
||||||
if namespace.Name == "" {
|
if namespace.IsEmpty() {
|
||||||
return 0, cerrors.NewBadRequestError("could not find/insert invalid Namespace")
|
return 0, cerrors.NewBadRequestError("could not find/insert invalid Namespace")
|
||||||
}
|
}
|
||||||
|
|
||||||
if pgSQL.cache != nil {
|
if pgSQL.cache != nil {
|
||||||
promCacheQueriesTotal.WithLabelValues("namespace").Inc()
|
promCacheQueriesTotal.WithLabelValues("namespace").Inc()
|
||||||
if id, found := pgSQL.cache.Get("namespace:" + namespace.Name); found {
|
if id, found := pgSQL.cache.Get("namespace:" + namespace.Name + ":" + namespace.Version.String()); found {
|
||||||
promCacheHitsTotal.WithLabelValues("namespace").Inc()
|
promCacheHitsTotal.WithLabelValues("namespace").Inc()
|
||||||
return id.(int), nil
|
return id.(int), nil
|
||||||
}
|
}
|
||||||
@ -38,13 +38,13 @@ func (pgSQL *pgSQL) insertNamespace(namespace database.Namespace) (int, error) {
|
|||||||
defer observeQueryTime("insertNamespace", "all", time.Now())
|
defer observeQueryTime("insertNamespace", "all", time.Now())
|
||||||
|
|
||||||
var id int
|
var id int
|
||||||
err := pgSQL.QueryRow(soiNamespace, namespace.Name).Scan(&id)
|
err := pgSQL.QueryRow(soiNamespace, namespace.Name, &namespace.Version).Scan(&id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, handleError("soiNamespace", err)
|
return 0, handleError("soiNamespace", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if pgSQL.cache != nil {
|
if pgSQL.cache != nil {
|
||||||
pgSQL.cache.Add("namespace:"+namespace.Name, id)
|
pgSQL.cache.Add("namespace:"+namespace.Name+":"+namespace.Version.String(), id)
|
||||||
}
|
}
|
||||||
|
|
||||||
return id, nil
|
return id, nil
|
||||||
@ -60,7 +60,7 @@ func (pgSQL *pgSQL) ListNamespaces() (namespaces []database.Namespace, err error
|
|||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var namespace database.Namespace
|
var namespace database.Namespace
|
||||||
|
|
||||||
err = rows.Scan(&namespace.ID, &namespace.Name)
|
err = rows.Scan(&namespace.ID, &namespace.Name, &namespace.Version)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return namespaces, handleError("listNamespace.Scan()", err)
|
return namespaces, handleError("listNamespace.Scan()", err)
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/coreos/clair/database"
|
"github.com/coreos/clair/database"
|
||||||
|
"github.com/coreos/clair/utils/types"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -36,9 +37,9 @@ func TestInsertNamespace(t *testing.T) {
|
|||||||
assert.Zero(t, id0)
|
assert.Zero(t, id0)
|
||||||
|
|
||||||
// Insert Namespace and ensure we can find it.
|
// Insert Namespace and ensure we can find it.
|
||||||
id1, err := datastore.insertNamespace(database.Namespace{Name: "TestInsertNamespace1"})
|
id1, err := datastore.insertNamespace(database.Namespace{Name: "TestInsertNamespace", Version: types.NewVersionUnsafe("1")})
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
id2, err := datastore.insertNamespace(database.Namespace{Name: "TestInsertNamespace1"})
|
id2, err := datastore.insertNamespace(database.Namespace{Name: "TestInsertNamespace", Version: types.NewVersionUnsafe("1")})
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, id1, id2)
|
assert.Equal(t, id1, id2)
|
||||||
}
|
}
|
||||||
@ -54,12 +55,13 @@ func TestListNamespace(t *testing.T) {
|
|||||||
namespaces, err := datastore.ListNamespaces()
|
namespaces, err := datastore.ListNamespaces()
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
if assert.Len(t, namespaces, 2) {
|
if assert.Len(t, namespaces, 2) {
|
||||||
|
testDebian7 := database.Namespace{Name: "debian", Version: types.NewVersionUnsafe("7")}
|
||||||
|
testDebian8 := database.Namespace{Name: "debian", Version: types.NewVersionUnsafe("8")}
|
||||||
for _, namespace := range namespaces {
|
for _, namespace := range namespaces {
|
||||||
switch namespace.Name {
|
if namespace.Equal(testDebian7) || namespace.Equal(testDebian8) {
|
||||||
case "debian:7", "debian:8":
|
|
||||||
continue
|
continue
|
||||||
default:
|
} else {
|
||||||
assert.Error(t, fmt.Errorf("ListNamespaces should not have returned '%s'", namespace.Name))
|
assert.Error(t, fmt.Errorf("ListNamespaces should not have returned '%s:%s'", namespace.Name, namespace.Version.String()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,13 +24,19 @@ func TestNotification(t *testing.T) {
|
|||||||
|
|
||||||
// Create some data.
|
// Create some data.
|
||||||
f1 := database.Feature{
|
f1 := database.Feature{
|
||||||
Name: "TestNotificationFeature1",
|
Name: "TestNotificationFeature1",
|
||||||
Namespace: database.Namespace{Name: "TestNotificationNamespace1"},
|
Namespace: database.Namespace{
|
||||||
|
Name: "TestNotificationNamespace",
|
||||||
|
Version: types.NewVersionUnsafe("1.0"),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
f2 := database.Feature{
|
f2 := database.Feature{
|
||||||
Name: "TestNotificationFeature2",
|
Name: "TestNotificationFeature2",
|
||||||
Namespace: database.Namespace{Name: "TestNotificationNamespace1"},
|
Namespace: database.Namespace{
|
||||||
|
Name: "TestNotificationNamespace",
|
||||||
|
Version: types.NewVersionUnsafe("1.0"),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
l1 := database.Layer{
|
l1 := database.Layer{
|
||||||
@ -186,7 +192,7 @@ func TestNotification(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Delete a vulnerability and verify the notification.
|
// Delete a vulnerability and verify the notification.
|
||||||
if assert.Nil(t, datastore.DeleteVulnerability(v1b.Namespace.Name, v1b.Name)) {
|
if assert.Nil(t, datastore.DeleteVulnerability(v1b.Namespace, v1b.Name)) {
|
||||||
notification, err = datastore.GetAvailableNotification(time.Second)
|
notification, err = datastore.GetAvailableNotification(time.Second)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.NotEmpty(t, notification.Name)
|
assert.NotEmpty(t, notification.Name)
|
||||||
|
@ -29,17 +29,17 @@ const (
|
|||||||
// namespace.go
|
// namespace.go
|
||||||
soiNamespace = `
|
soiNamespace = `
|
||||||
WITH new_namespace AS (
|
WITH new_namespace AS (
|
||||||
INSERT INTO Namespace(name)
|
INSERT INTO Namespace(name, version)
|
||||||
SELECT CAST($1 AS VARCHAR)
|
SELECT CAST($1 AS VARCHAR), CAST($2 AS VARCHAR)
|
||||||
WHERE NOT EXISTS (SELECT name FROM Namespace WHERE name = $1)
|
WHERE NOT EXISTS (SELECT name FROM Namespace WHERE name = $1 AND version = $2)
|
||||||
RETURNING id
|
RETURNING id
|
||||||
)
|
)
|
||||||
SELECT id FROM Namespace WHERE name = $1
|
SELECT id FROM Namespace WHERE name = $1 AND version = $2
|
||||||
UNION
|
UNION
|
||||||
SELECT id FROM new_namespace`
|
SELECT id FROM new_namespace`
|
||||||
|
|
||||||
searchNamespace = `SELECT id FROM Namespace WHERE name = $1`
|
searchNamespace = `SELECT id FROM Namespace WHERE name = $1 AND version = $2`
|
||||||
listNamespace = `SELECT id, name FROM Namespace`
|
listNamespace = `SELECT id, name, version FROM Namespace`
|
||||||
|
|
||||||
// feature.go
|
// feature.go
|
||||||
soiFeature = `
|
soiFeature = `
|
||||||
@ -92,7 +92,7 @@ const (
|
|||||||
FROM Layer l, layer_tree lt
|
FROM Layer l, layer_tree lt
|
||||||
WHERE l.id = lt.parent_id
|
WHERE l.id = lt.parent_id
|
||||||
)
|
)
|
||||||
SELECT ldf.featureversion_id, ldf.modification, fn.id, fn.name, f.id, f.name, fv.id, fv.version, ltree.id, ltree.name
|
SELECT ldf.featureversion_id, ldf.modification, fn.id, fn.name, fn.version, f.id, f.name, fv.id, fv.version, ltree.id, ltree.name
|
||||||
FROM Layer_diff_FeatureVersion ldf
|
FROM Layer_diff_FeatureVersion ldf
|
||||||
JOIN (
|
JOIN (
|
||||||
SELECT row_number() over (ORDER BY depth DESC), id, name FROM layer_tree
|
SELECT row_number() over (ORDER BY depth DESC), id, name FROM layer_tree
|
||||||
@ -102,7 +102,7 @@ const (
|
|||||||
|
|
||||||
searchFeatureVersionVulnerability = `
|
searchFeatureVersionVulnerability = `
|
||||||
SELECT vafv.featureversion_id, v.id, v.name, v.description, v.link, v.severity, v.metadata,
|
SELECT vafv.featureversion_id, v.id, v.name, v.description, v.link, v.severity, v.metadata,
|
||||||
vn.name, vfif.version
|
vn.name, vn.version, vfif.version
|
||||||
FROM Vulnerability_Affects_FeatureVersion vafv, Vulnerability v,
|
FROM Vulnerability_Affects_FeatureVersion vafv, Vulnerability v,
|
||||||
Namespace vn, Vulnerability_FixedIn_Feature vfif
|
Namespace vn, Vulnerability_FixedIn_Feature vfif
|
||||||
WHERE vafv.featureversion_id = ANY($1::integer[])
|
WHERE vafv.featureversion_id = ANY($1::integer[])
|
||||||
@ -112,7 +112,7 @@ const (
|
|||||||
AND v.deleted_at IS NULL`
|
AND v.deleted_at IS NULL`
|
||||||
|
|
||||||
searchLayerNamespace = `
|
searchLayerNamespace = `
|
||||||
SELECT n.id, n.name
|
SELECT n.id, n.name, n.version
|
||||||
FROM LayerNamespace ln, Namespace n
|
FROM LayerNamespace ln, Namespace n
|
||||||
WHERE ln.layer_id = $1 AND ln.namespace_id = n.id`
|
WHERE ln.layer_id = $1 AND ln.namespace_id = n.id`
|
||||||
|
|
||||||
@ -159,12 +159,12 @@ const (
|
|||||||
|
|
||||||
// vulnerability.go
|
// vulnerability.go
|
||||||
searchVulnerabilityBase = `
|
searchVulnerabilityBase = `
|
||||||
SELECT v.id, v.name, n.id, n.name, v.description, v.link, v.severity, v.metadata
|
SELECT v.id, v.name, n.id, n.name, n.version, v.description, v.link, v.severity, v.metadata
|
||||||
FROM Vulnerability v JOIN Namespace n ON v.namespace_id = n.id`
|
FROM Vulnerability v JOIN Namespace n ON v.namespace_id = n.id`
|
||||||
searchVulnerabilityForUpdate = ` FOR UPDATE OF v`
|
searchVulnerabilityForUpdate = ` FOR UPDATE OF v`
|
||||||
searchVulnerabilityByNamespaceAndName = ` WHERE n.name = $1 AND v.name = $2 AND v.deleted_at IS NULL`
|
searchVulnerabilityByNamespaceAndName = ` WHERE n.name = $1 AND n.version = $2 AND v.name = $3 AND v.deleted_at IS NULL`
|
||||||
searchVulnerabilityByID = ` WHERE v.id = $1`
|
searchVulnerabilityByID = ` WHERE v.id = $1`
|
||||||
searchVulnerabilityByNamespace = ` WHERE n.name = $1 AND v.deleted_at IS NULL
|
searchVulnerabilityByNamespaceID = ` WHERE n.id = $1 AND v.deleted_at IS NULL
|
||||||
AND v.id >= $2
|
AND v.id >= $2
|
||||||
ORDER BY v.id
|
ORDER BY v.id
|
||||||
LIMIT $3`
|
LIMIT $3`
|
||||||
@ -189,8 +189,8 @@ const (
|
|||||||
removeVulnerability = `
|
removeVulnerability = `
|
||||||
UPDATE Vulnerability
|
UPDATE Vulnerability
|
||||||
SET deleted_at = CURRENT_TIMESTAMP
|
SET deleted_at = CURRENT_TIMESTAMP
|
||||||
WHERE namespace_id = (SELECT id FROM Namespace WHERE name = $1)
|
WHERE namespace_id = (SELECT id FROM Namespace WHERE name = $1 AND version = $2)
|
||||||
AND name = $2
|
AND name = $3
|
||||||
AND deleted_at IS NULL
|
AND deleted_at IS NULL
|
||||||
RETURNING id`
|
RETURNING id`
|
||||||
|
|
||||||
|
6
database/pgsql/testdata/data.sql
vendored
6
database/pgsql/testdata/data.sql
vendored
@ -12,9 +12,9 @@
|
|||||||
-- See the License for the specific language governing permissions and
|
-- See the License for the specific language governing permissions and
|
||||||
-- limitations under the License.
|
-- limitations under the License.
|
||||||
|
|
||||||
INSERT INTO namespace (id, name) VALUES
|
INSERT INTO namespace (id, name, version) VALUES
|
||||||
(1, 'debian:7'),
|
(1, 'debian', '7'),
|
||||||
(2, 'debian:8');
|
(2, 'debian', '8');
|
||||||
|
|
||||||
INSERT INTO feature (id, namespace_id, name) VALUES
|
INSERT INTO feature (id, namespace_id, name) VALUES
|
||||||
(1, 1, 'wechat'),
|
(1, 1, 'wechat'),
|
||||||
|
@ -28,12 +28,12 @@ import (
|
|||||||
"github.com/guregu/null/zero"
|
"github.com/guregu/null/zero"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (pgSQL *pgSQL) ListVulnerabilities(namespaceName string, limit int, startID int) ([]database.Vulnerability, int, error) {
|
func (pgSQL *pgSQL) ListVulnerabilities(namespace database.Namespace, limit int, startID int) ([]database.Vulnerability, int, error) {
|
||||||
defer observeQueryTime("listVulnerabilities", "all", time.Now())
|
defer observeQueryTime("listVulnerabilities", "all", time.Now())
|
||||||
|
|
||||||
// Query Namespace.
|
// Query Namespace.
|
||||||
var id int
|
var id int
|
||||||
err := pgSQL.QueryRow(searchNamespace, namespaceName).Scan(&id)
|
err := pgSQL.QueryRow(searchNamespace, namespace.Name, &namespace.Version).Scan(&id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, -1, handleError("searchNamespace", err)
|
return nil, -1, handleError("searchNamespace", err)
|
||||||
} else if id == 0 {
|
} else if id == 0 {
|
||||||
@ -41,10 +41,10 @@ func (pgSQL *pgSQL) ListVulnerabilities(namespaceName string, limit int, startID
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Query.
|
// Query.
|
||||||
query := searchVulnerabilityBase + searchVulnerabilityByNamespace
|
query := searchVulnerabilityBase + searchVulnerabilityByNamespaceID
|
||||||
rows, err := pgSQL.Query(query, namespaceName, startID, limit+1)
|
rows, err := pgSQL.Query(query, id, startID, limit+1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, -1, handleError("searchVulnerabilityByNamespace", err)
|
return nil, -1, handleError("searchVulnerabilityByNamespaceID", err)
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
@ -60,6 +60,7 @@ func (pgSQL *pgSQL) ListVulnerabilities(namespaceName string, limit int, startID
|
|||||||
&vulnerability.Name,
|
&vulnerability.Name,
|
||||||
&vulnerability.Namespace.ID,
|
&vulnerability.Namespace.ID,
|
||||||
&vulnerability.Namespace.Name,
|
&vulnerability.Namespace.Name,
|
||||||
|
&vulnerability.Namespace.Version,
|
||||||
&vulnerability.Description,
|
&vulnerability.Description,
|
||||||
&vulnerability.Link,
|
&vulnerability.Link,
|
||||||
&vulnerability.Severity,
|
&vulnerability.Severity,
|
||||||
@ -83,11 +84,11 @@ func (pgSQL *pgSQL) ListVulnerabilities(namespaceName string, limit int, startID
|
|||||||
return vulns, nextID, nil
|
return vulns, nextID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pgSQL *pgSQL) FindVulnerability(namespaceName, name string) (database.Vulnerability, error) {
|
func (pgSQL *pgSQL) FindVulnerability(namespace database.Namespace, name string) (database.Vulnerability, error) {
|
||||||
return findVulnerability(pgSQL, namespaceName, name, false)
|
return findVulnerability(pgSQL, namespace, name, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func findVulnerability(queryer Queryer, namespaceName, name string, forUpdate bool) (database.Vulnerability, error) {
|
func findVulnerability(queryer Queryer, namespace database.Namespace, name string, forUpdate bool) (database.Vulnerability, error) {
|
||||||
defer observeQueryTime("findVulnerability", "all", time.Now())
|
defer observeQueryTime("findVulnerability", "all", time.Now())
|
||||||
|
|
||||||
queryName := "searchVulnerabilityBase+searchVulnerabilityByNamespaceAndName"
|
queryName := "searchVulnerabilityBase+searchVulnerabilityByNamespaceAndName"
|
||||||
@ -97,7 +98,7 @@ func findVulnerability(queryer Queryer, namespaceName, name string, forUpdate bo
|
|||||||
query = query + searchVulnerabilityForUpdate
|
query = query + searchVulnerabilityForUpdate
|
||||||
}
|
}
|
||||||
|
|
||||||
return scanVulnerability(queryer, queryName, queryer.QueryRow(query, namespaceName, name))
|
return scanVulnerability(queryer, queryName, queryer.QueryRow(query, namespace.Name, &namespace.Version, name))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pgSQL *pgSQL) findVulnerabilityByIDWithDeleted(id int) (database.Vulnerability, error) {
|
func (pgSQL *pgSQL) findVulnerabilityByIDWithDeleted(id int) (database.Vulnerability, error) {
|
||||||
@ -117,6 +118,7 @@ func scanVulnerability(queryer Queryer, queryName string, vulnerabilityRow *sql.
|
|||||||
&vulnerability.Name,
|
&vulnerability.Name,
|
||||||
&vulnerability.Namespace.ID,
|
&vulnerability.Namespace.ID,
|
||||||
&vulnerability.Namespace.Name,
|
&vulnerability.Namespace.Name,
|
||||||
|
&vulnerability.Namespace.Version,
|
||||||
&vulnerability.Description,
|
&vulnerability.Description,
|
||||||
&vulnerability.Link,
|
&vulnerability.Link,
|
||||||
&vulnerability.Severity,
|
&vulnerability.Severity,
|
||||||
@ -193,7 +195,7 @@ func (pgSQL *pgSQL) insertVulnerability(vulnerability database.Vulnerability, on
|
|||||||
tf := time.Now()
|
tf := time.Now()
|
||||||
|
|
||||||
// Verify parameters
|
// Verify parameters
|
||||||
if vulnerability.Name == "" || vulnerability.Namespace.Name == "" {
|
if vulnerability.Name == "" || vulnerability.Namespace.IsEmpty() {
|
||||||
return cerrors.NewBadRequestError("insertVulnerability needs at least the Name and the Namespace")
|
return cerrors.NewBadRequestError("insertVulnerability needs at least the Name and the Namespace")
|
||||||
}
|
}
|
||||||
if !onlyFixedIn && !vulnerability.Severity.IsValid() {
|
if !onlyFixedIn && !vulnerability.Severity.IsValid() {
|
||||||
@ -204,11 +206,12 @@ func (pgSQL *pgSQL) insertVulnerability(vulnerability database.Vulnerability, on
|
|||||||
for i := 0; i < len(vulnerability.FixedIn); i++ {
|
for i := 0; i < len(vulnerability.FixedIn); i++ {
|
||||||
fifv := &vulnerability.FixedIn[i]
|
fifv := &vulnerability.FixedIn[i]
|
||||||
|
|
||||||
if fifv.Feature.Namespace.Name == "" {
|
if fifv.Feature.Namespace.IsEmpty() {
|
||||||
// As there is no Namespace on that FixedIn FeatureVersion, set it to the Vulnerability's
|
// As there is no Namespace on that FixedIn FeatureVersion, set it to the Vulnerability's
|
||||||
// Namespace.
|
// Namespace.
|
||||||
fifv.Feature.Namespace.Name = vulnerability.Namespace.Name
|
fifv.Feature.Namespace.Name = vulnerability.Namespace.Name
|
||||||
} else if fifv.Feature.Namespace.Name != vulnerability.Namespace.Name {
|
fifv.Feature.Namespace.Version = vulnerability.Namespace.Version
|
||||||
|
} else if !fifv.Feature.Namespace.Equal(vulnerability.Namespace) {
|
||||||
msg := "could not insert an invalid vulnerability that contains FixedIn FeatureVersion that are not in the same namespace as the Vulnerability"
|
msg := "could not insert an invalid vulnerability that contains FixedIn FeatureVersion that are not in the same namespace as the Vulnerability"
|
||||||
log.Warning(msg)
|
log.Warning(msg)
|
||||||
return cerrors.NewBadRequestError(msg)
|
return cerrors.NewBadRequestError(msg)
|
||||||
@ -226,7 +229,7 @@ func (pgSQL *pgSQL) insertVulnerability(vulnerability database.Vulnerability, on
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Find existing vulnerability and its Vulnerability_FixedIn_Features (for update).
|
// Find existing vulnerability and its Vulnerability_FixedIn_Features (for update).
|
||||||
existingVulnerability, err := findVulnerability(tx, vulnerability.Namespace.Name, vulnerability.Name, true)
|
existingVulnerability, err := findVulnerability(tx, vulnerability.Namespace, vulnerability.Name, true)
|
||||||
if err != nil && err != cerrors.ErrNotFound {
|
if err != nil && err != cerrors.ErrNotFound {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return err
|
return err
|
||||||
@ -264,7 +267,7 @@ func (pgSQL *pgSQL) insertVulnerability(vulnerability database.Vulnerability, on
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Mark the old vulnerability as non latest.
|
// Mark the old vulnerability as non latest.
|
||||||
_, err = tx.Exec(removeVulnerability, vulnerability.Namespace.Name, vulnerability.Name)
|
_, err = tx.Exec(removeVulnerability, vulnerability.Namespace.Name, &vulnerability.Namespace.Version, vulnerability.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return handleError("removeVulnerability", err)
|
return handleError("removeVulnerability", err)
|
||||||
@ -497,13 +500,14 @@ func linkVulnerabilityToFeatureVersions(tx *sql.Tx, fixedInID, vulnerabilityID,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pgSQL *pgSQL) InsertVulnerabilityFixes(vulnerabilityNamespace, vulnerabilityName string, fixes []database.FeatureVersion) error {
|
func (pgSQL *pgSQL) InsertVulnerabilityFixes(vulnerabilityNamespace database.Namespace, vulnerabilityName string, fixes []database.FeatureVersion) error {
|
||||||
defer observeQueryTime("InsertVulnerabilityFixes", "all", time.Now())
|
defer observeQueryTime("InsertVulnerabilityFixes", "all", time.Now())
|
||||||
|
|
||||||
v := database.Vulnerability{
|
v := database.Vulnerability{
|
||||||
Name: vulnerabilityName,
|
Name: vulnerabilityName,
|
||||||
Namespace: database.Namespace{
|
Namespace: database.Namespace{
|
||||||
Name: vulnerabilityNamespace,
|
Name: vulnerabilityNamespace.Name,
|
||||||
|
Version: vulnerabilityNamespace.Version,
|
||||||
},
|
},
|
||||||
FixedIn: fixes,
|
FixedIn: fixes,
|
||||||
}
|
}
|
||||||
@ -511,20 +515,22 @@ func (pgSQL *pgSQL) InsertVulnerabilityFixes(vulnerabilityNamespace, vulnerabili
|
|||||||
return pgSQL.insertVulnerability(v, true, true)
|
return pgSQL.insertVulnerability(v, true, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pgSQL *pgSQL) DeleteVulnerabilityFix(vulnerabilityNamespace, vulnerabilityName, featureName string) error {
|
func (pgSQL *pgSQL) DeleteVulnerabilityFix(vulnerabilityNamespace database.Namespace, vulnerabilityName, featureName string) error {
|
||||||
defer observeQueryTime("DeleteVulnerabilityFix", "all", time.Now())
|
defer observeQueryTime("DeleteVulnerabilityFix", "all", time.Now())
|
||||||
|
|
||||||
v := database.Vulnerability{
|
v := database.Vulnerability{
|
||||||
Name: vulnerabilityName,
|
Name: vulnerabilityName,
|
||||||
Namespace: database.Namespace{
|
Namespace: database.Namespace{
|
||||||
Name: vulnerabilityNamespace,
|
Name: vulnerabilityNamespace.Name,
|
||||||
|
Version: vulnerabilityNamespace.Version,
|
||||||
},
|
},
|
||||||
FixedIn: []database.FeatureVersion{
|
FixedIn: []database.FeatureVersion{
|
||||||
{
|
{
|
||||||
Feature: database.Feature{
|
Feature: database.Feature{
|
||||||
Name: featureName,
|
Name: featureName,
|
||||||
Namespace: database.Namespace{
|
Namespace: database.Namespace{
|
||||||
Name: vulnerabilityNamespace,
|
Name: vulnerabilityNamespace.Name,
|
||||||
|
Version: vulnerabilityNamespace.Version,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Version: types.MinVersion,
|
Version: types.MinVersion,
|
||||||
@ -535,7 +541,7 @@ func (pgSQL *pgSQL) DeleteVulnerabilityFix(vulnerabilityNamespace, vulnerability
|
|||||||
return pgSQL.insertVulnerability(v, true, true)
|
return pgSQL.insertVulnerability(v, true, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pgSQL *pgSQL) DeleteVulnerability(namespaceName, name string) error {
|
func (pgSQL *pgSQL) DeleteVulnerability(namespace database.Namespace, name string) error {
|
||||||
defer observeQueryTime("DeleteVulnerability", "all", time.Now())
|
defer observeQueryTime("DeleteVulnerability", "all", time.Now())
|
||||||
|
|
||||||
// Begin transaction.
|
// Begin transaction.
|
||||||
@ -546,7 +552,7 @@ func (pgSQL *pgSQL) DeleteVulnerability(namespaceName, name string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var vulnerabilityID int
|
var vulnerabilityID int
|
||||||
err = tx.QueryRow(removeVulnerability, namespaceName, name).Scan(&vulnerabilityID)
|
err = tx.QueryRow(removeVulnerability, namespace.Name, &namespace.Version, name).Scan(&vulnerabilityID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return handleError("removeVulnerability", err)
|
return handleError("removeVulnerability", err)
|
||||||
|
@ -32,8 +32,12 @@ func TestFindVulnerability(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer datastore.Close()
|
defer datastore.Close()
|
||||||
|
|
||||||
|
testExistNamespace := database.Namespace{
|
||||||
|
Name: "debian",
|
||||||
|
Version: types.NewVersionUnsafe("7"),
|
||||||
|
}
|
||||||
// Find a vulnerability that does not exist.
|
// Find a vulnerability that does not exist.
|
||||||
_, err = datastore.FindVulnerability("", "")
|
_, err = datastore.FindVulnerability(database.Namespace{}, "")
|
||||||
assert.Equal(t, cerrors.ErrNotFound, err)
|
assert.Equal(t, cerrors.ErrNotFound, err)
|
||||||
|
|
||||||
// Find a normal vulnerability.
|
// Find a normal vulnerability.
|
||||||
@ -42,7 +46,7 @@ func TestFindVulnerability(t *testing.T) {
|
|||||||
Description: "A vulnerability affecting OpenSSL < 2.0 on Debian 7.0",
|
Description: "A vulnerability affecting OpenSSL < 2.0 on Debian 7.0",
|
||||||
Link: "http://google.com/#q=CVE-OPENSSL-1-DEB7",
|
Link: "http://google.com/#q=CVE-OPENSSL-1-DEB7",
|
||||||
Severity: types.High,
|
Severity: types.High,
|
||||||
Namespace: database.Namespace{Name: "debian:7"},
|
Namespace: testExistNamespace,
|
||||||
FixedIn: []database.FeatureVersion{
|
FixedIn: []database.FeatureVersion{
|
||||||
{
|
{
|
||||||
Feature: database.Feature{Name: "openssl"},
|
Feature: database.Feature{Name: "openssl"},
|
||||||
@ -55,7 +59,7 @@ func TestFindVulnerability(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
v1f, err := datastore.FindVulnerability("debian:7", "CVE-OPENSSL-1-DEB7")
|
v1f, err := datastore.FindVulnerability(testExistNamespace, "CVE-OPENSSL-1-DEB7")
|
||||||
if assert.Nil(t, err) {
|
if assert.Nil(t, err) {
|
||||||
equalsVuln(t, &v1, &v1f)
|
equalsVuln(t, &v1, &v1f)
|
||||||
}
|
}
|
||||||
@ -64,11 +68,11 @@ func TestFindVulnerability(t *testing.T) {
|
|||||||
v2 := database.Vulnerability{
|
v2 := database.Vulnerability{
|
||||||
Name: "CVE-NOPE",
|
Name: "CVE-NOPE",
|
||||||
Description: "A vulnerability affecting nothing",
|
Description: "A vulnerability affecting nothing",
|
||||||
Namespace: database.Namespace{Name: "debian:7"},
|
Namespace: testExistNamespace,
|
||||||
Severity: types.Unknown,
|
Severity: types.Unknown,
|
||||||
}
|
}
|
||||||
|
|
||||||
v2f, err := datastore.FindVulnerability("debian:7", "CVE-NOPE")
|
v2f, err := datastore.FindVulnerability(testExistNamespace, "CVE-NOPE")
|
||||||
if assert.Nil(t, err) {
|
if assert.Nil(t, err) {
|
||||||
equalsVuln(t, &v2, &v2f)
|
equalsVuln(t, &v2, &v2f)
|
||||||
}
|
}
|
||||||
@ -82,16 +86,24 @@ func TestDeleteVulnerability(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer datastore.Close()
|
defer datastore.Close()
|
||||||
|
|
||||||
|
testExistNamespace := database.Namespace{
|
||||||
|
Name: "debian",
|
||||||
|
Version: types.NewVersionUnsafe("7"),
|
||||||
|
}
|
||||||
|
testNonExistNamespace := database.Namespace{
|
||||||
|
Name: "TestDeleteVulnerabilityNamespace",
|
||||||
|
Version: types.NewVersionUnsafe("1.0"),
|
||||||
|
}
|
||||||
// Delete non-existing Vulnerability.
|
// Delete non-existing Vulnerability.
|
||||||
err = datastore.DeleteVulnerability("TestDeleteVulnerabilityNamespace1", "CVE-OPENSSL-1-DEB7")
|
err = datastore.DeleteVulnerability(testNonExistNamespace, "CVE-OPENSSL-1-DEB7")
|
||||||
assert.Equal(t, cerrors.ErrNotFound, err)
|
assert.Equal(t, cerrors.ErrNotFound, err)
|
||||||
err = datastore.DeleteVulnerability("debian:7", "TestDeleteVulnerabilityVulnerability1")
|
err = datastore.DeleteVulnerability(testExistNamespace, "TestDeleteVulnerabilityVulnerability1")
|
||||||
assert.Equal(t, cerrors.ErrNotFound, err)
|
assert.Equal(t, cerrors.ErrNotFound, err)
|
||||||
|
|
||||||
// Delete Vulnerability.
|
// Delete Vulnerability.
|
||||||
err = datastore.DeleteVulnerability("debian:7", "CVE-OPENSSL-1-DEB7")
|
err = datastore.DeleteVulnerability(testExistNamespace, "CVE-OPENSSL-1-DEB7")
|
||||||
if assert.Nil(t, err) {
|
if assert.Nil(t, err) {
|
||||||
_, err := datastore.FindVulnerability("debian:7", "CVE-OPENSSL-1-DEB7")
|
_, err := datastore.FindVulnerability(testExistNamespace, "CVE-OPENSSL-1-DEB7")
|
||||||
assert.Equal(t, cerrors.ErrNotFound, err)
|
assert.Equal(t, cerrors.ErrNotFound, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -105,8 +117,8 @@ func TestInsertVulnerability(t *testing.T) {
|
|||||||
defer datastore.Close()
|
defer datastore.Close()
|
||||||
|
|
||||||
// Create some data.
|
// Create some data.
|
||||||
n1 := database.Namespace{Name: "TestInsertVulnerabilityNamespace1"}
|
n1 := database.Namespace{Name: "TestInsertVulnerabilityNamespace", Version: types.NewVersionUnsafe("1.0")}
|
||||||
n2 := database.Namespace{Name: "TestInsertVulnerabilityNamespace2"}
|
n2 := database.Namespace{Name: "TestInsertVulnerabilityNamespace", Version: types.NewVersionUnsafe("2.0")}
|
||||||
|
|
||||||
f1 := database.FeatureVersion{
|
f1 := database.FeatureVersion{
|
||||||
Feature: database.Feature{
|
Feature: database.Feature{
|
||||||
@ -215,7 +227,7 @@ func TestInsertVulnerability(t *testing.T) {
|
|||||||
}
|
}
|
||||||
err = datastore.InsertVulnerabilities([]database.Vulnerability{v1}, true)
|
err = datastore.InsertVulnerabilities([]database.Vulnerability{v1}, true)
|
||||||
if assert.Nil(t, err) {
|
if assert.Nil(t, err) {
|
||||||
v1f, err := datastore.FindVulnerability(n1.Name, v1.Name)
|
v1f, err := datastore.FindVulnerability(n1, v1.Name)
|
||||||
if assert.Nil(t, err) {
|
if assert.Nil(t, err) {
|
||||||
equalsVuln(t, &v1, &v1f)
|
equalsVuln(t, &v1, &v1f)
|
||||||
}
|
}
|
||||||
@ -231,7 +243,7 @@ func TestInsertVulnerability(t *testing.T) {
|
|||||||
|
|
||||||
err = datastore.InsertVulnerabilities([]database.Vulnerability{v1}, true)
|
err = datastore.InsertVulnerabilities([]database.Vulnerability{v1}, true)
|
||||||
if assert.Nil(t, err) {
|
if assert.Nil(t, err) {
|
||||||
v1f, err := datastore.FindVulnerability(n1.Name, v1.Name)
|
v1f, err := datastore.FindVulnerability(n1, v1.Name)
|
||||||
if assert.Nil(t, err) {
|
if assert.Nil(t, err) {
|
||||||
// We already had f1 before the update.
|
// We already had f1 before the update.
|
||||||
// Add it to the struct for comparison.
|
// Add it to the struct for comparison.
|
||||||
@ -251,7 +263,7 @@ func TestInsertVulnerability(t *testing.T) {
|
|||||||
|
|
||||||
func equalsVuln(t *testing.T, expected, actual *database.Vulnerability) {
|
func equalsVuln(t *testing.T, expected, actual *database.Vulnerability) {
|
||||||
assert.Equal(t, expected.Name, actual.Name)
|
assert.Equal(t, expected.Name, actual.Name)
|
||||||
assert.Equal(t, expected.Namespace.Name, actual.Namespace.Name)
|
assert.True(t, expected.Namespace.Equal(actual.Namespace))
|
||||||
assert.Equal(t, expected.Description, actual.Description)
|
assert.Equal(t, expected.Description, actual.Description)
|
||||||
assert.Equal(t, expected.Link, actual.Link)
|
assert.Equal(t, expected.Link, actual.Link)
|
||||||
assert.Equal(t, expected.Severity, actual.Severity)
|
assert.Equal(t, expected.Severity, actual.Severity)
|
||||||
@ -264,7 +276,7 @@ func equalsVuln(t *testing.T, expected, actual *database.Vulnerability) {
|
|||||||
if expectedFeatureVersion.Feature.Name == actualFeatureVersion.Feature.Name {
|
if expectedFeatureVersion.Feature.Name == actualFeatureVersion.Feature.Name {
|
||||||
found = true
|
found = true
|
||||||
|
|
||||||
assert.Equal(t, expected.Namespace.Name, actualFeatureVersion.Feature.Namespace.Name)
|
assert.True(t, expected.Namespace.Equal(actualFeatureVersion.Feature.Namespace))
|
||||||
assert.Equal(t, expectedFeatureVersion.Version, actualFeatureVersion.Version)
|
assert.Equal(t, expectedFeatureVersion.Version, actualFeatureVersion.Version)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,6 +69,7 @@ func DetectFeatures(data map[string][]byte, namespaces []database.Namespace) ([]
|
|||||||
// Ensure that every feature has a Namespace associated
|
// Ensure that every feature has a Namespace associated
|
||||||
for i := 0; i < len(pkgs); i++ {
|
for i := 0; i < len(pkgs); i++ {
|
||||||
pkgs[i].Feature.Namespace.Name = namespace.Name
|
pkgs[i].Feature.Namespace.Name = namespace.Name
|
||||||
|
pkgs[i].Feature.Namespace.Version = namespace.Version
|
||||||
}
|
}
|
||||||
packages = append(packages, pkgs...)
|
packages = append(packages, pkgs...)
|
||||||
break
|
break
|
||||||
|
@ -19,6 +19,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/coreos/clair/database"
|
"github.com/coreos/clair/database"
|
||||||
|
"github.com/coreos/clair/utils/types"
|
||||||
"github.com/coreos/clair/worker/detectors"
|
"github.com/coreos/clair/worker/detectors"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -75,7 +76,7 @@ func (detector *AptSourcesNamespaceDetector) Detect(data map[string][]byte) *dat
|
|||||||
}
|
}
|
||||||
|
|
||||||
if OS != "" && version != "" {
|
if OS != "" && version != "" {
|
||||||
return &database.Namespace{Name: OS + ":" + version}
|
return &database.Namespace{Name: OS, Version: types.NewVersionUnsafe(version)}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -18,12 +18,13 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/coreos/clair/database"
|
"github.com/coreos/clair/database"
|
||||||
|
"github.com/coreos/clair/utils/types"
|
||||||
"github.com/coreos/clair/worker/detectors/namespace"
|
"github.com/coreos/clair/worker/detectors/namespace"
|
||||||
)
|
)
|
||||||
|
|
||||||
var aptSourcesOSTests = []namespace.NamespaceTest{
|
var aptSourcesOSTests = []namespace.NamespaceTest{
|
||||||
{
|
{
|
||||||
ExpectedNamespace: database.Namespace{Name: "debian:unstable"},
|
ExpectedNamespace: database.Namespace{Name: "debian", Version: types.NewVersionUnsafe("unstable")},
|
||||||
Data: map[string][]byte{
|
Data: map[string][]byte{
|
||||||
"etc/os-release": []byte(
|
"etc/os-release": []byte(
|
||||||
`PRETTY_NAME="Debian GNU/Linux stretch/sid"
|
`PRETTY_NAME="Debian GNU/Linux stretch/sid"
|
||||||
|
@ -20,6 +20,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/coreos/clair/database"
|
"github.com/coreos/clair/database"
|
||||||
|
"github.com/coreos/clair/utils/types"
|
||||||
"github.com/coreos/clair/worker/detectors"
|
"github.com/coreos/clair/worker/detectors"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -70,7 +71,7 @@ func (detector *LsbReleaseNamespaceDetector) Detect(data map[string][]byte) *dat
|
|||||||
}
|
}
|
||||||
|
|
||||||
if OS != "" && version != "" {
|
if OS != "" && version != "" {
|
||||||
return &database.Namespace{Name: OS + ":" + version}
|
return &database.Namespace{Name: OS, Version: types.NewVersionUnsafe(version)}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -18,12 +18,13 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/coreos/clair/database"
|
"github.com/coreos/clair/database"
|
||||||
|
"github.com/coreos/clair/utils/types"
|
||||||
"github.com/coreos/clair/worker/detectors/namespace"
|
"github.com/coreos/clair/worker/detectors/namespace"
|
||||||
)
|
)
|
||||||
|
|
||||||
var lsbReleaseOSTests = []namespace.NamespaceTest{
|
var lsbReleaseOSTests = []namespace.NamespaceTest{
|
||||||
{
|
{
|
||||||
ExpectedNamespace: database.Namespace{Name: "ubuntu:12.04"},
|
ExpectedNamespace: database.Namespace{Name: "ubuntu", Version: types.NewVersionUnsafe("12.04")},
|
||||||
Data: map[string][]byte{
|
Data: map[string][]byte{
|
||||||
"etc/lsb-release": []byte(
|
"etc/lsb-release": []byte(
|
||||||
`DISTRIB_ID=Ubuntu
|
`DISTRIB_ID=Ubuntu
|
||||||
@ -33,7 +34,7 @@ DISTRIB_DESCRIPTION="Ubuntu 12.04 LTS"`),
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{ // We don't care about the minor version of Debian
|
{ // We don't care about the minor version of Debian
|
||||||
ExpectedNamespace: database.Namespace{Name: "debian:7"},
|
ExpectedNamespace: database.Namespace{Name: "debian", Version: types.NewVersionUnsafe("7")},
|
||||||
Data: map[string][]byte{
|
Data: map[string][]byte{
|
||||||
"etc/lsb-release": []byte(
|
"etc/lsb-release": []byte(
|
||||||
`DISTRIB_ID=Debian
|
`DISTRIB_ID=Debian
|
||||||
|
@ -20,6 +20,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/coreos/clair/database"
|
"github.com/coreos/clair/database"
|
||||||
|
"github.com/coreos/clair/utils/types"
|
||||||
"github.com/coreos/clair/worker/detectors"
|
"github.com/coreos/clair/worker/detectors"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -65,7 +66,7 @@ func (detector *OsReleaseNamespaceDetector) Detect(data map[string][]byte) *data
|
|||||||
}
|
}
|
||||||
|
|
||||||
if OS != "" && version != "" {
|
if OS != "" && version != "" {
|
||||||
return &database.Namespace{Name: OS + ":" + version}
|
return &database.Namespace{Name: OS, Version: types.NewVersionUnsafe(version)}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -18,12 +18,13 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/coreos/clair/database"
|
"github.com/coreos/clair/database"
|
||||||
|
"github.com/coreos/clair/utils/types"
|
||||||
"github.com/coreos/clair/worker/detectors/namespace"
|
"github.com/coreos/clair/worker/detectors/namespace"
|
||||||
)
|
)
|
||||||
|
|
||||||
var osReleaseOSTests = []namespace.NamespaceTest{
|
var osReleaseOSTests = []namespace.NamespaceTest{
|
||||||
{
|
{
|
||||||
ExpectedNamespace: database.Namespace{Name: "debian:8"},
|
ExpectedNamespace: database.Namespace{Name: "debian", Version: types.NewVersionUnsafe("8")},
|
||||||
Data: map[string][]byte{
|
Data: map[string][]byte{
|
||||||
"etc/os-release": []byte(
|
"etc/os-release": []byte(
|
||||||
`PRETTY_NAME="Debian GNU/Linux 8 (jessie)"
|
`PRETTY_NAME="Debian GNU/Linux 8 (jessie)"
|
||||||
@ -37,7 +38,7 @@ BUG_REPORT_URL="https://bugs.debian.org/"`),
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ExpectedNamespace: database.Namespace{Name: "ubuntu:15.10"},
|
ExpectedNamespace: database.Namespace{Name: "ubuntu", Version: types.NewVersionUnsafe("15.10")},
|
||||||
Data: map[string][]byte{
|
Data: map[string][]byte{
|
||||||
"etc/os-release": []byte(
|
"etc/os-release": []byte(
|
||||||
`NAME="Ubuntu"
|
`NAME="Ubuntu"
|
||||||
@ -52,7 +53,7 @@ BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"`),
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
{ // Doesn't have quotes around VERSION_ID
|
{ // Doesn't have quotes around VERSION_ID
|
||||||
ExpectedNamespace: database.Namespace{Name: "fedora:20"},
|
ExpectedNamespace: database.Namespace{Name: "fedora", Version: types.NewVersionUnsafe("20")},
|
||||||
Data: map[string][]byte{
|
Data: map[string][]byte{
|
||||||
"etc/os-release": []byte(
|
"etc/os-release": []byte(
|
||||||
`NAME=Fedora
|
`NAME=Fedora
|
||||||
|
@ -19,6 +19,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/coreos/clair/database"
|
"github.com/coreos/clair/database"
|
||||||
|
"github.com/coreos/clair/utils/types"
|
||||||
"github.com/coreos/clair/worker/detectors"
|
"github.com/coreos/clair/worker/detectors"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -46,7 +47,7 @@ func (detector *RedhatReleaseNamespaceDetector) Detect(data map[string][]byte) *
|
|||||||
|
|
||||||
r := redhatReleaseRegexp.FindStringSubmatch(string(f))
|
r := redhatReleaseRegexp.FindStringSubmatch(string(f))
|
||||||
if len(r) == 4 {
|
if len(r) == 4 {
|
||||||
return &database.Namespace{Name: strings.ToLower(r[1]) + ":" + r[3]}
|
return &database.Namespace{Name: strings.ToLower(r[1]), Version: types.NewVersionUnsafe(r[3])}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,18 +18,19 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/coreos/clair/database"
|
"github.com/coreos/clair/database"
|
||||||
|
"github.com/coreos/clair/utils/types"
|
||||||
"github.com/coreos/clair/worker/detectors/namespace"
|
"github.com/coreos/clair/worker/detectors/namespace"
|
||||||
)
|
)
|
||||||
|
|
||||||
var redhatReleaseTests = []namespace.NamespaceTest{
|
var redhatReleaseTests = []namespace.NamespaceTest{
|
||||||
{
|
{
|
||||||
ExpectedNamespace: database.Namespace{Name: "centos:6"},
|
ExpectedNamespace: database.Namespace{Name: "centos", Version: types.NewVersionUnsafe("6")},
|
||||||
Data: map[string][]byte{
|
Data: map[string][]byte{
|
||||||
"etc/centos-release": []byte(`CentOS release 6.6 (Final)`),
|
"etc/centos-release": []byte(`CentOS release 6.6 (Final)`),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ExpectedNamespace: database.Namespace{Name: "centos:7"},
|
ExpectedNamespace: database.Namespace{Name: "centos", Version: types.NewVersionUnsafe("7")},
|
||||||
Data: map[string][]byte{
|
Data: map[string][]byte{
|
||||||
"etc/system-release": []byte(`CentOS Linux release 7.1.1503 (Core)`),
|
"etc/system-release": []byte(`CentOS Linux release 7.1.1503 (Core)`),
|
||||||
},
|
},
|
||||||
|
@ -29,6 +29,6 @@ type NamespaceTest struct {
|
|||||||
|
|
||||||
func TestNamespaceDetector(t *testing.T, detector detectors.NamespaceDetector, tests []NamespaceTest) {
|
func TestNamespaceDetector(t *testing.T, detector detectors.NamespaceDetector, tests []NamespaceTest) {
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
assert.Equal(t, test.ExpectedNamespace, *detector.Detect(test.Data))
|
assert.True(t, test.ExpectedNamespace.Equal(*detector.Detect(test.Data)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,12 +53,20 @@ func TestProcessWithDistUpgrade(t *testing.T) {
|
|||||||
|
|
||||||
wheezy, err := datastore.FindLayer("wheezy", true, false)
|
wheezy, err := datastore.FindLayer("wheezy", true, false)
|
||||||
if assert.Nil(t, err) {
|
if assert.Nil(t, err) {
|
||||||
assert.Equal(t, "debian:7", wheezy.Namespaces[0].Name)
|
testDebian7 := database.Namespace{
|
||||||
|
Name: "debian",
|
||||||
|
Version: types.NewVersionUnsafe("7"),
|
||||||
|
}
|
||||||
|
assert.True(t, testDebian7.Equal(wheezy.Namespaces[0]))
|
||||||
assert.Len(t, wheezy.Features, 52)
|
assert.Len(t, wheezy.Features, 52)
|
||||||
|
|
||||||
jessie, err := datastore.FindLayer("jessie", true, false)
|
jessie, err := datastore.FindLayer("jessie", true, false)
|
||||||
if assert.Nil(t, err) {
|
if assert.Nil(t, err) {
|
||||||
assert.Equal(t, "debian:8", jessie.Namespaces[0].Name)
|
testDebian8 := database.Namespace{
|
||||||
|
Name: "debian",
|
||||||
|
Version: types.NewVersionUnsafe("8"),
|
||||||
|
}
|
||||||
|
assert.True(t, testDebian8.Equal(jessie.Namespaces[0]))
|
||||||
assert.Len(t, jessie.Features, 74)
|
assert.Len(t, jessie.Features, 74)
|
||||||
|
|
||||||
// These FeatureVersions haven't been upgraded.
|
// These FeatureVersions haven't been upgraded.
|
||||||
@ -98,13 +106,13 @@ func TestProcessWithDistUpgrade(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, nufv := range nonUpgradedFeatureVersions {
|
for _, nufv := range nonUpgradedFeatureVersions {
|
||||||
nufv.Feature.Namespace.Name = "debian:7"
|
nufv.Feature.Namespace.Name = "debian"
|
||||||
|
nufv.Feature.Namespace.Version = types.NewVersionUnsafe("7")
|
||||||
|
|
||||||
found := false
|
found := false
|
||||||
for _, fv := range jessie.Features {
|
for _, fv := range jessie.Features {
|
||||||
if fv.Feature.Name == nufv.Feature.Name &&
|
if fv.Feature.Name == nufv.Feature.Name &&
|
||||||
fv.Feature.Namespace.Name == nufv.Feature.Namespace.Name &&
|
fv.Feature.Namespace.Equal(nufv.Feature.Namespace) {
|
||||||
fv.Version == nufv.Version {
|
|
||||||
found = true
|
found = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -113,13 +121,13 @@ func TestProcessWithDistUpgrade(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, nufv := range nonUpgradedFeatureVersions {
|
for _, nufv := range nonUpgradedFeatureVersions {
|
||||||
nufv.Feature.Namespace.Name = "debian:8"
|
nufv.Feature.Namespace.Name = "debian"
|
||||||
|
nufv.Feature.Namespace.Version = types.NewVersionUnsafe("8")
|
||||||
|
|
||||||
found := false
|
found := false
|
||||||
for _, fv := range jessie.Features {
|
for _, fv := range jessie.Features {
|
||||||
if fv.Feature.Name == nufv.Feature.Name &&
|
if fv.Feature.Name == nufv.Feature.Name &&
|
||||||
fv.Feature.Namespace.Name == nufv.Feature.Namespace.Name &&
|
fv.Feature.Namespace.Equal(nufv.Feature.Namespace) {
|
||||||
fv.Version == nufv.Version {
|
|
||||||
found = true
|
found = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user