diff --git a/api/v1/models.go b/api/v1/models.go index 4c6461d1..3970e361 100644 --- a/api/v1/models.go +++ b/api/v1/models.go @@ -63,6 +63,7 @@ func LayerFromDatabaseModel(dbLayer database.Layer, withFeatures, withVulnerabil Name: dbFeatureVersion.Feature.Name, Namespace: dbFeatureVersion.Feature.Namespace.Name, Version: dbFeatureVersion.Version.String(), + AddedBy: dbFeatureVersion.AddedBy.Name, } for _, dbVuln := range dbFeatureVersion.AffectedBy { @@ -148,6 +149,7 @@ type Feature struct { Namespace string `json:"Namespace,omitempty"` Version string `json:"Version,omitempty"` Vulnerabilities []Vulnerability `json:"Vulnerabilities,omitempty"` + AddedBy string `json:"AddedBy,omitempty"` } func FeatureFromDatabaseModel(dbFeatureVersion database.FeatureVersion) Feature { @@ -160,6 +162,7 @@ func FeatureFromDatabaseModel(dbFeatureVersion database.FeatureVersion) Feature Name: dbFeatureVersion.Feature.Name, Namespace: dbFeatureVersion.Feature.Namespace.Name, Version: versionStr, + AddedBy: dbFeatureVersion.AddedBy.Name, } } diff --git a/database/models.go b/database/models.go index 97de789e..a44291b8 100644 --- a/database/models.go +++ b/database/models.go @@ -56,6 +56,9 @@ type FeatureVersion struct { Feature Feature Version types.Version AffectedBy []Vulnerability + + // For output purposes. Only make sense when the feature version is in the context of an image. + AddedBy Layer } type Vulnerability struct { diff --git a/database/pgsql/layer.go b/database/pgsql/layer.go index 4aa298df..f8c9cac8 100644 --- a/database/pgsql/layer.go +++ b/database/pgsql/layer.go @@ -109,7 +109,8 @@ func (pgSQL *pgSQL) getLayerFeatureVersions(layerID int) ([]database.FeatureVers err = rows.Scan(&featureVersion.ID, &modification, &featureVersion.Feature.Namespace.ID, &featureVersion.Feature.Namespace.Name, &featureVersion.Feature.ID, - &featureVersion.Feature.Name, &featureVersion.ID, &featureVersion.Version) + &featureVersion.Feature.Name, &featureVersion.ID, &featureVersion.Version, + &featureVersion.AddedBy.ID, &featureVersion.AddedBy.Name) if err != nil { return featureVersions, handleError("s_layer_featureversion.Scan()", err) } diff --git a/database/pgsql/queries.go b/database/pgsql/queries.go index beaaaeeb..537d9cb7 100644 --- a/database/pgsql/queries.go +++ b/database/pgsql/queries.go @@ -86,22 +86,22 @@ func init() { WHERE l.name = $1;` queries["s_layer_featureversion"] = ` - WITH RECURSIVE layer_tree(id, parent_id, depth, path, cycle) AS( - SELECT l.id, l.parent_id, 1, ARRAY[l.id], false - FROM Layer l - WHERE l.id = $1 - UNION ALL - SELECT l.id, l.parent_id, lt.depth + 1, path || l.id, l.id = ANY(path) - FROM Layer l, layer_tree lt - WHERE l.id = lt.parent_id - ) - SELECT ldf.featureversion_id, ldf.modification, fn.id, fn.name, f.id, f.name, fv.id, fv.version - FROM Layer_diff_FeatureVersion ldf - JOIN ( - SELECT row_number() over (ORDER BY depth DESC), id FROM layer_tree - ) AS ltree (ordering, id) ON ldf.layer_id = ltree.id, FeatureVersion fv, Feature f, Namespace fn - WHERE ldf.featureversion_id = fv.id AND fv.feature_id = f.id AND f.namespace_id = fn.id - ORDER BY ltree.ordering` + WITH RECURSIVE layer_tree(id, name, parent_id, depth, path, cycle) AS( + SELECT l.id, l.name, l.parent_id, 1, ARRAY[l.id], false + FROM Layer l + WHERE l.id = $1 + UNION ALL + SELECT l.id, l.name, l.parent_id, lt.depth + 1, path || l.id, l.id = ANY(path) + FROM Layer l, layer_tree lt + 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 + FROM Layer_diff_FeatureVersion ldf + JOIN ( + SELECT row_number() over (ORDER BY depth DESC), id, name FROM layer_tree + ) AS ltree (ordering, id, name) ON ldf.layer_id = ltree.id, FeatureVersion fv, Feature f, Namespace fn + WHERE ldf.featureversion_id = fv.id AND fv.feature_id = f.id AND f.namespace_id = fn.id + ORDER BY ltree.ordering` queries["s_featureversions_vulnerabilities"] = ` SELECT vafv.featureversion_id, v.id, v.name, v.description, v.link, v.severity, v.metadata,