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
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
func (tx *pgSession) FindLayer(hash string) (database.Layer, database.Processors, bool, error) {
|
|
|
|
l, p, _, ok, err := tx.findLayer(hash)
|
|
|
|
return l, p, ok, err
|
|
|
|
}
|
2016-01-24 03:02:34 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
func (tx *pgSession) FindLayerWithContent(hash string) (database.LayerWithContent, bool, error) {
|
2016-12-28 01:45:11 +00:00
|
|
|
var (
|
2017-07-26 23:23:54 +00:00
|
|
|
layer database.LayerWithContent
|
|
|
|
layerID int64
|
|
|
|
ok bool
|
|
|
|
err error
|
2016-12-28 01:45:11 +00:00
|
|
|
)
|
2016-01-24 03:02:34 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
layer.Layer, layer.ProcessedBy, layerID, ok, err = tx.findLayer(hash)
|
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
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
if !ok {
|
|
|
|
return layer, false, nil
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
2017-05-12 20:59:17 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
layer.Features, err = tx.findLayerFeatures(layerID)
|
|
|
|
layer.Namespaces, err = tx.findLayerNamespaces(layerID)
|
|
|
|
return layer, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *pgSession) PersistLayer(layer database.Layer) error {
|
|
|
|
if layer.Hash == "" {
|
|
|
|
return commonerr.NewBadRequestError("Empty Layer Hash is not allowed")
|
2017-05-12 20:59:17 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
|
|
|
|
_, err := tx.Exec(queryPersistLayer(1), layer.Hash)
|
|
|
|
if err != nil {
|
|
|
|
return handleError("queryPersistLayer", err)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-02-20 01:55:54 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
// PersistLayerContent relates layer identified by hash with namespaces,
|
|
|
|
// features and processors provided. If the layer, namespaces, features are not
|
|
|
|
// in database, the function returns an error.
|
|
|
|
func (tx *pgSession) PersistLayerContent(hash string, namespaces []database.Namespace, features []database.Feature, processedBy database.Processors) error {
|
|
|
|
if hash == "" {
|
|
|
|
return commonerr.NewBadRequestError("Empty layer hash is not allowed")
|
|
|
|
}
|
2016-02-20 01:55:54 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
var layerID int64
|
|
|
|
err := tx.QueryRow(searchLayer, hash).Scan(&layerID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-24 03:02:34 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
if err = tx.persistLayerNamespace(layerID, namespaces); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-24 03:02:34 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
if err = tx.persistLayerFeatures(layerID, features); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-28 20:03:29 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
if err = tx.persistLayerDetectors(layerID, processedBy.Detectors); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-24 03:02:34 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
if err = tx.persistLayerListers(layerID, processedBy.Listers); err != nil {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
func (tx *pgSession) persistLayerDetectors(id int64, detectors []string) error {
|
|
|
|
if len(detectors) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2015-12-28 20:03:29 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
// Sorting is needed before inserting into database to prevent deadlock.
|
|
|
|
sort.Strings(detectors)
|
|
|
|
keys := make([]interface{}, len(detectors)*2)
|
|
|
|
for i, d := range detectors {
|
|
|
|
keys[i*2] = id
|
|
|
|
keys[i*2+1] = d
|
|
|
|
}
|
|
|
|
_, err := tx.Exec(queryPersistLayerDetectors(len(detectors)), keys...)
|
2016-01-12 15:40:46 +00:00
|
|
|
if err != nil {
|
2017-07-26 23:23:54 +00:00
|
|
|
return handleError("queryPersistLayerDetectors", 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
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
func (tx *pgSession) persistLayerListers(id int64, listers []string) error {
|
|
|
|
if len(listers) == 0 {
|
|
|
|
return nil
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
sort.Strings(listers)
|
|
|
|
keys := make([]interface{}, len(listers)*2)
|
|
|
|
for i, d := range listers {
|
|
|
|
keys[i*2] = id
|
|
|
|
keys[i*2+1] = d
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
_, err := tx.Exec(queryPersistLayerListers(len(listers)), keys...)
|
|
|
|
if err != nil {
|
|
|
|
return handleError("queryPersistLayerDetectors", err)
|
|
|
|
}
|
|
|
|
return nil
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
func (tx *pgSession) persistLayerFeatures(id int64, features []database.Feature) error {
|
|
|
|
if len(features) == 0 {
|
2015-12-28 20:03:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
fIDs, err := tx.findFeatureIDs(features)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
ids := make([]int, len(fIDs))
|
|
|
|
for i, fID := range fIDs {
|
|
|
|
if !fID.Valid {
|
|
|
|
return errNamespaceNotFound
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
ids[i] = int(fID.Int64)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
sort.IntSlice(ids).Sort()
|
|
|
|
keys := make([]interface{}, len(features)*2)
|
|
|
|
for i, fID := range ids {
|
|
|
|
keys[i*2] = id
|
|
|
|
keys[i*2+1] = fID
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
_, err = tx.Exec(queryPersistLayerFeature(len(features)), keys...)
|
|
|
|
if err != nil {
|
|
|
|
return handleError("queryPersistLayerFeature", err)
|
|
|
|
}
|
2015-12-28 20:03:29 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
func (tx *pgSession) persistLayerNamespace(id int64, namespaces []database.Namespace) error {
|
|
|
|
if len(namespaces) == 0 {
|
|
|
|
return nil
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
nsIDs, err := tx.findNamespaceIDs(namespaces)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-24 03:02:34 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
// for every bulk persist operation, the input data should be sorted.
|
|
|
|
ids := make([]int, len(nsIDs))
|
|
|
|
for i, nsID := range nsIDs {
|
|
|
|
if !nsID.Valid {
|
|
|
|
panic(errNamespaceNotFound)
|
2016-01-12 15:40:46 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
ids[i] = int(nsID.Int64)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
sort.IntSlice(ids).Sort()
|
2017-05-12 20:59:17 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
keys := make([]interface{}, len(namespaces)*2)
|
|
|
|
for i, nsID := range ids {
|
|
|
|
keys[i*2] = id
|
|
|
|
keys[i*2+1] = nsID
|
2017-05-12 20:59:17 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
_, err = tx.Exec(queryPersistLayerNamespace(len(namespaces)), keys...)
|
|
|
|
if err != nil {
|
|
|
|
return handleError("queryPersistLayerNamespace", 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
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
func (tx *pgSession) persistProcessors(listerQuery, listerQueryName, detectorQuery, detectorQueryName string, id int64, processors database.Processors) error {
|
|
|
|
stmt, err := tx.Prepare(listerQuery)
|
2016-01-12 15:40:46 +00:00
|
|
|
if err != nil {
|
2017-07-26 23:23:54 +00:00
|
|
|
return handleError(listerQueryName, err)
|
2016-01-12 15:40:46 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
for _, l := range processors.Listers {
|
|
|
|
_, err := stmt.Exec(id, l)
|
2016-01-08 15:27:30 +00:00
|
|
|
if err != nil {
|
2017-07-26 23:23:54 +00:00
|
|
|
stmt.Close()
|
|
|
|
return handleError(listerQueryName, err)
|
2016-01-08 15:27:30 +00:00
|
|
|
}
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
if err := stmt.Close(); err != nil {
|
|
|
|
return handleError(listerQueryName, err)
|
|
|
|
}
|
2017-05-12 20:59:17 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
stmt, err = tx.Prepare(detectorQuery)
|
2017-05-12 20:59:17 +00:00
|
|
|
if err != nil {
|
2017-07-26 23:23:54 +00:00
|
|
|
return handleError(detectorQueryName, err)
|
2017-05-12 20:59:17 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
for _, d := range processors.Detectors {
|
|
|
|
_, err := stmt.Exec(id, d)
|
2017-05-12 20:59:17 +00:00
|
|
|
if err != nil {
|
2017-07-26 23:23:54 +00:00
|
|
|
stmt.Close()
|
|
|
|
return handleError(detectorQueryName, err)
|
2017-05-12 20:59:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
if err := stmt.Close(); err != nil {
|
|
|
|
return handleError(detectorQueryName, err)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
func (tx *pgSession) findLayerNamespaces(layerID int64) ([]database.Namespace, error) {
|
|
|
|
var namespaces []database.Namespace
|
2016-01-08 15:27:30 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
rows, err := tx.Query(searchLayerNamespaces, layerID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, handleError("searchLayerFeatures", err)
|
|
|
|
}
|
2016-01-08 15:27:30 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
for rows.Next() {
|
|
|
|
ns := database.Namespace{}
|
|
|
|
err := rows.Scan(&ns.Name, &ns.VersionFormat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-01-08 15:27:30 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
namespaces = append(namespaces, ns)
|
2016-01-08 15:27:30 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
return namespaces, nil
|
|
|
|
}
|
2016-01-08 15:27:30 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
func (tx *pgSession) findLayerFeatures(layerID int64) ([]database.Feature, error) {
|
|
|
|
var features []database.Feature
|
|
|
|
|
|
|
|
rows, err := tx.Query(searchLayerFeatures, layerID)
|
2016-01-08 15:27:30 +00:00
|
|
|
if err != nil {
|
2017-07-26 23:23:54 +00:00
|
|
|
return nil, handleError("searchLayerFeatures", err)
|
2016-01-08 15:27:30 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
for rows.Next() {
|
|
|
|
f := database.Feature{}
|
|
|
|
err := rows.Scan(&f.Name, &f.Version, &f.VersionFormat)
|
2016-01-08 15:27:30 +00:00
|
|
|
if err != nil {
|
2017-07-26 23:23:54 +00:00
|
|
|
return nil, err
|
2016-01-08 15:27:30 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
features = append(features, f)
|
2016-01-08 15:27:30 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
return features, nil
|
2016-01-08 15:27:30 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
func (tx *pgSession) findLayer(hash string) (database.Layer, database.Processors, int64, bool, error) {
|
|
|
|
var (
|
|
|
|
layerID int64
|
|
|
|
layer = database.Layer{Hash: hash}
|
|
|
|
processors database.Processors
|
|
|
|
)
|
2016-01-08 15:27:30 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
if hash == "" {
|
|
|
|
return layer, processors, layerID, false, commonerr.NewBadRequestError("Empty Layer Hash is not allowed")
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
2016-01-08 15:27:30 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
err := tx.QueryRow(searchLayer, 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 {
|
|
|
|
return layer, processors, layerID, false, nil
|
|
|
|
}
|
|
|
|
return layer, processors, layerID, false, err
|
2016-01-20 18:57:51 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
processors.Detectors, err = tx.findProcessors(searchLayerDetectors, "searchLayerDetectors", "detector", layerID)
|
2016-01-20 18:57:51 +00:00
|
|
|
if err != nil {
|
2017-07-26 23:23:54 +00:00
|
|
|
return layer, processors, layerID, false, err
|
2016-01-20 18:57:51 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
processors.Listers, err = tx.findProcessors(searchLayerListers, "searchLayerListers", "lister", layerID)
|
|
|
|
if err != nil {
|
|
|
|
return layer, processors, layerID, false, err
|
2016-01-20 18:57:51 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
return layer, processors, layerID, true, nil
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|