2017-01-13 07:08:52 +00:00
|
|
|
// Copyright 2017 clair authors
|
2016-01-19 20:16:45 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
package pgsql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2017-07-26 23:23:54 +00:00
|
|
|
"sort"
|
2016-12-28 01:45:11 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
"github.com/deckarep/golang-set"
|
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
"github.com/coreos/clair/database"
|
2017-01-13 07:08:52 +00:00
|
|
|
"github.com/coreos/clair/pkg/commonerr"
|
2015-12-28 20:03:29 +00:00
|
|
|
)
|
|
|
|
|
2018-09-19 19:38:07 +00:00
|
|
|
const (
|
|
|
|
soiLayer = `
|
|
|
|
WITH new_layer AS (
|
|
|
|
INSERT INTO layer (hash)
|
|
|
|
SELECT CAST ($1 AS VARCHAR)
|
|
|
|
WHERE NOT EXISTS (SELECT id FROM layer WHERE hash = $1)
|
|
|
|
RETURNING id
|
|
|
|
)
|
|
|
|
SELECT id FROM new_Layer
|
|
|
|
UNION
|
|
|
|
SELECT id FROM layer WHERE hash = $1`
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
findLayerFeatures = `
|
|
|
|
SELECT f.name, f.version, f.version_format, lf.detector_id
|
|
|
|
FROM layer_feature AS lf, feature AS f
|
|
|
|
WHERE lf.feature_id = f.id
|
|
|
|
AND lf.layer_id = $1`
|
2018-09-19 19:38:07 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
findLayerNamespaces = `
|
|
|
|
SELECT ns.name, ns.version_format, ln.detector_id
|
|
|
|
FROM layer_namespace AS ln, namespace AS ns
|
|
|
|
WHERE ln.namespace_id = ns.id
|
|
|
|
AND ln.layer_id = $1`
|
2018-09-19 19:38:07 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
findLayerID = `SELECT id FROM layer WHERE hash = $1`
|
2018-09-19 19:38:07 +00:00
|
|
|
)
|
2018-10-08 15:11:30 +00:00
|
|
|
|
|
|
|
// dbLayerNamespace represents the layer_namespace table.
|
|
|
|
type dbLayerNamespace struct {
|
|
|
|
layerID int64
|
|
|
|
namespaceID int64
|
|
|
|
detectorID int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// dbLayerFeature represents the layer_feature table
|
|
|
|
type dbLayerFeature struct {
|
|
|
|
layerID int64
|
|
|
|
featureID int64
|
|
|
|
detectorID int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *pgSession) FindLayer(hash string) (database.Layer, bool, error) {
|
|
|
|
layer := database.Layer{Hash: hash}
|
|
|
|
if hash == "" {
|
|
|
|
return layer, false, commonerr.NewBadRequestError("non empty layer hash is expected.")
|
|
|
|
}
|
|
|
|
|
|
|
|
layerID, ok, err := tx.findLayerID(hash)
|
|
|
|
if err != nil || !ok {
|
|
|
|
return layer, ok, err
|
|
|
|
}
|
|
|
|
|
|
|
|
detectorMap, err := tx.findAllDetectors()
|
2015-12-28 20:03:29 +00:00
|
|
|
if err != nil {
|
2017-07-26 23:23:54 +00:00
|
|
|
return layer, false, err
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
if layer.By, err = tx.findLayerDetectors(layerID); err != nil {
|
|
|
|
return layer, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if layer.Features, err = tx.findLayerFeatures(layerID, detectorMap); err != nil {
|
|
|
|
return layer, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if layer.Namespaces, err = tx.findLayerNamespaces(layerID, detectorMap); err != nil {
|
|
|
|
return layer, false, err
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
2017-05-12 20:59:17 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
return layer, true, nil
|
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
func sanitizePersistLayerInput(hash string, features []database.LayerFeature, namespaces []database.LayerNamespace, detectedBy []database.Detector) error {
|
2018-09-05 15:34:49 +00:00
|
|
|
if hash == "" {
|
2018-10-08 15:11:30 +00:00
|
|
|
return commonerr.NewBadRequestError("expected non-empty layer hash")
|
2017-05-12 20:59:17 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
detectedBySet := mapset.NewSet()
|
|
|
|
for _, d := range detectedBy {
|
|
|
|
detectedBySet.Add(d)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
for _, f := range features {
|
|
|
|
if !detectedBySet.Contains(f.By) {
|
|
|
|
return database.ErrInvalidParameters
|
|
|
|
}
|
2018-09-11 20:09:08 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
for _, n := range namespaces {
|
|
|
|
if !detectedBySet.Contains(n.By) {
|
|
|
|
return database.ErrInvalidParameters
|
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
}
|
2016-02-20 01:55:54 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PersistLayer saves the content of a layer to the database.
|
|
|
|
func (tx *pgSession) PersistLayer(hash string, features []database.LayerFeature, namespaces []database.LayerNamespace, detectedBy []database.Detector) error {
|
2018-09-11 20:09:08 +00:00
|
|
|
var (
|
2018-10-08 15:11:30 +00:00
|
|
|
err error
|
|
|
|
id int64
|
|
|
|
detectorIDs []int64
|
2018-09-11 20:09:08 +00:00
|
|
|
)
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
if err = sanitizePersistLayerInput(hash, features, namespaces, detectedBy); err != nil {
|
2017-07-26 23:23:54 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-01-24 03:02:34 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
if id, err = tx.soiLayer(hash); err != nil {
|
2017-07-26 23:23:54 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-01-24 03:02:34 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
if detectorIDs, err = tx.findDetectorIDs(detectedBy); err != nil {
|
|
|
|
if err == commonerr.ErrNotFound {
|
|
|
|
return database.ErrMissingEntities
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-12-28 20:03:29 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
if err = tx.persistLayerDetectors(id, detectorIDs); err != nil {
|
2017-07-26 23:23:54 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-01-24 03:02:34 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
if err = tx.persistAllLayerFeatures(id, features); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = tx.persistAllLayerNamespaces(id, namespaces); err != nil {
|
2017-07-26 23:23:54 +00:00
|
|
|
return err
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
return nil
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
func (tx *pgSession) persistAllLayerNamespaces(layerID int64, namespaces []database.LayerNamespace) error {
|
|
|
|
detectorMap, err := tx.findAllDetectors()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-07-26 23:23:54 +00:00
|
|
|
}
|
2015-12-28 20:03:29 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
// TODO(sidac): This kind of type conversion is very useless and wasteful,
|
|
|
|
// we need interfaces around the database models to reduce these kind of
|
|
|
|
// operations.
|
|
|
|
rawNamespaces := make([]database.Namespace, 0, len(namespaces))
|
|
|
|
for _, ns := range namespaces {
|
|
|
|
rawNamespaces = append(rawNamespaces, ns.Namespace)
|
2017-07-26 23:23:54 +00:00
|
|
|
}
|
2018-10-08 15:11:30 +00:00
|
|
|
|
|
|
|
rawNamespaceIDs, err := tx.findNamespaceIDs(rawNamespaces)
|
2016-01-12 15:40:46 +00:00
|
|
|
if err != nil {
|
2018-10-08 15:11:30 +00:00
|
|
|
return err
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
dbLayerNamespaces := make([]dbLayerNamespace, 0, len(namespaces))
|
|
|
|
for i, ns := range namespaces {
|
|
|
|
detectorID := detectorMap.byValue[ns.By]
|
|
|
|
namespaceID := rawNamespaceIDs[i].Int64
|
|
|
|
if !rawNamespaceIDs[i].Valid {
|
|
|
|
return database.ErrMissingEntities
|
|
|
|
}
|
2015-12-28 20:03:29 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
dbLayerNamespaces = append(dbLayerNamespaces, dbLayerNamespace{layerID, namespaceID, detectorID})
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
return tx.persistLayerNamespaces(dbLayerNamespaces)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *pgSession) persistAllLayerFeatures(layerID int64, features []database.LayerFeature) error {
|
|
|
|
detectorMap, err := tx.findAllDetectors()
|
2017-07-26 23:23:54 +00:00
|
|
|
if err != nil {
|
2018-10-08 15:11:30 +00:00
|
|
|
return err
|
2017-07-26 23:23:54 +00:00
|
|
|
}
|
2015-12-28 20:03:29 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
rawFeatures := make([]database.Feature, 0, len(features))
|
|
|
|
for _, f := range features {
|
|
|
|
rawFeatures = append(rawFeatures, f.Feature)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
featureIDs, err := tx.findFeatureIDs(rawFeatures)
|
2017-07-26 23:23:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
dbFeatures := make([]dbLayerFeature, 0, len(features))
|
|
|
|
for i, f := range features {
|
|
|
|
detectorID := detectorMap.byValue[f.By]
|
|
|
|
featureID := featureIDs[i].Int64
|
|
|
|
if !featureIDs[i].Valid {
|
|
|
|
return database.ErrMissingEntities
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
dbFeatures = append(dbFeatures, dbLayerFeature{layerID, featureID, detectorID})
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
if err := tx.persistLayerFeatures(dbFeatures); err != nil {
|
|
|
|
return err
|
2017-07-26 23:23:54 +00:00
|
|
|
}
|
2018-10-08 15:11:30 +00:00
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
func (tx *pgSession) persistLayerFeatures(features []dbLayerFeature) error {
|
|
|
|
if len(features) == 0 {
|
2017-07-26 23:23:54 +00:00
|
|
|
return nil
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
sort.Slice(features, func(i, j int) bool {
|
|
|
|
return features[i].featureID < features[j].featureID
|
|
|
|
})
|
2017-05-12 20:59:17 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
keys := make([]interface{}, len(features)*3)
|
|
|
|
for i, feature := range features {
|
|
|
|
keys[i*3] = feature.layerID
|
|
|
|
keys[i*3+1] = feature.featureID
|
|
|
|
keys[i*3+2] = feature.detectorID
|
2017-05-12 20:59:17 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
_, err := tx.Exec(queryPersistLayerFeature(len(features)), keys...)
|
2017-07-26 23:23:54 +00:00
|
|
|
if err != nil {
|
2018-10-08 15:11:30 +00:00
|
|
|
return handleError("queryPersistLayerFeature", err)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-12-28 20:03:29 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
func (tx *pgSession) persistLayerNamespaces(namespaces []dbLayerNamespace) error {
|
|
|
|
if len(namespaces) == 0 {
|
|
|
|
return nil
|
2016-01-12 15:40:46 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
// for every bulk persist operation, the input data should be sorted.
|
|
|
|
sort.Slice(namespaces, func(i, j int) bool {
|
|
|
|
return namespaces[i].namespaceID < namespaces[j].namespaceID
|
|
|
|
})
|
2015-12-28 20:03:29 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
elementSize := 3
|
|
|
|
keys := make([]interface{}, len(namespaces)*elementSize)
|
|
|
|
for i, row := range namespaces {
|
|
|
|
keys[i*3] = row.layerID
|
|
|
|
keys[i*3+1] = row.namespaceID
|
|
|
|
keys[i*3+2] = row.detectorID
|
2017-07-26 23:23:54 +00:00
|
|
|
}
|
2017-05-12 20:59:17 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
_, err := tx.Exec(queryPersistLayerNamespace(len(namespaces)), keys...)
|
2017-05-12 20:59:17 +00:00
|
|
|
if err != nil {
|
2018-10-08 15:11:30 +00:00
|
|
|
return handleError("queryPersistLayerNamespace", err)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
func (tx *pgSession) findLayerNamespaces(layerID int64, detectors detectorMap) ([]database.LayerNamespace, error) {
|
|
|
|
rows, err := tx.Query(findLayerNamespaces, layerID)
|
2017-07-26 23:23:54 +00:00
|
|
|
if err != nil {
|
2018-10-08 15:11:30 +00:00
|
|
|
return nil, handleError("findLayerNamespaces", err)
|
2017-07-26 23:23:54 +00:00
|
|
|
}
|
2016-01-08 15:27:30 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
namespaces := []database.LayerNamespace{}
|
2017-07-26 23:23:54 +00:00
|
|
|
for rows.Next() {
|
2018-10-08 15:11:30 +00:00
|
|
|
var (
|
|
|
|
namespace database.LayerNamespace
|
|
|
|
detectorID int64
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := rows.Scan(&namespace.Name, &namespace.VersionFormat, &detectorID); err != nil {
|
2017-07-26 23:23:54 +00:00
|
|
|
return nil, err
|
2016-01-08 15:27:30 +00:00
|
|
|
}
|
2018-10-08 15:11:30 +00:00
|
|
|
|
|
|
|
namespace.By = detectors.byID[detectorID]
|
|
|
|
namespaces = append(namespaces, namespace)
|
2016-01-08 15:27:30 +00:00
|
|
|
}
|
2018-10-08 15:11:30 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
return namespaces, nil
|
|
|
|
}
|
2016-01-08 15:27:30 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
func (tx *pgSession) findLayerFeatures(layerID int64, detectors detectorMap) ([]database.LayerFeature, error) {
|
|
|
|
rows, err := tx.Query(findLayerFeatures, layerID)
|
2016-01-08 15:27:30 +00:00
|
|
|
if err != nil {
|
2018-10-08 15:11:30 +00:00
|
|
|
return nil, handleError("findLayerFeatures", err)
|
2016-01-08 15:27:30 +00:00
|
|
|
}
|
2018-10-08 15:11:30 +00:00
|
|
|
defer rows.Close()
|
2016-01-08 15:27:30 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
features := []database.LayerFeature{}
|
2017-07-26 23:23:54 +00:00
|
|
|
for rows.Next() {
|
2018-10-08 15:11:30 +00:00
|
|
|
var (
|
|
|
|
detectorID int64
|
|
|
|
feature database.LayerFeature
|
|
|
|
)
|
|
|
|
if err := rows.Scan(&feature.Name, &feature.Version, &feature.VersionFormat, &detectorID); err != nil {
|
|
|
|
return nil, handleError("findLayerFeatures", err)
|
2016-01-08 15:27:30 +00:00
|
|
|
}
|
2018-10-08 15:11:30 +00:00
|
|
|
|
|
|
|
feature.By = detectors.byID[detectorID]
|
|
|
|
features = append(features, feature)
|
2016-01-08 15:27:30 +00:00
|
|
|
}
|
2018-10-08 15:11:30 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
return features, nil
|
2016-01-08 15:27:30 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
func (tx *pgSession) findLayerID(hash string) (int64, bool, error) {
|
|
|
|
var layerID int64
|
|
|
|
err := tx.QueryRow(findLayerID, hash).Scan(&layerID)
|
2016-01-20 18:57:51 +00:00
|
|
|
if err != nil {
|
2017-07-26 23:23:54 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2018-10-08 15:11:30 +00:00
|
|
|
return layerID, false, nil
|
2017-07-26 23:23:54 +00:00
|
|
|
}
|
2018-10-08 15:11:30 +00:00
|
|
|
|
|
|
|
return layerID, false, handleError("findLayerID", err)
|
2016-01-20 18:57:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
return layerID, true, nil
|
2018-09-07 15:31:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
func (tx *pgSession) findLayerIDs(hashes []string) ([]int64, bool, error) {
|
|
|
|
layerIDs := make([]int64, 0, len(hashes))
|
|
|
|
for _, hash := range hashes {
|
|
|
|
id, ok, err := tx.findLayerID(hash)
|
|
|
|
if !ok {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
2018-09-07 15:31:35 +00:00
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
layerIDs = append(layerIDs, id)
|
2016-01-20 18:57:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
return layerIDs, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *pgSession) soiLayer(hash string) (int64, error) {
|
|
|
|
var id int64
|
|
|
|
if err := tx.QueryRow(soiLayer, hash).Scan(&id); err != nil {
|
|
|
|
return 0, handleError("soiLayer", err)
|
2016-01-20 18:57:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:11:30 +00:00
|
|
|
return id, nil
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|