2016-01-19 20:16:45 +00:00
|
|
|
// Copyright 2015 clair authors
|
|
|
|
//
|
|
|
|
// 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 (
|
2016-01-12 15:40:46 +00:00
|
|
|
"database/sql"
|
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
"github.com/coreos/clair/database"
|
2016-01-08 15:27:30 +00:00
|
|
|
cerrors "github.com/coreos/clair/utils/errors"
|
2015-12-28 20:03:29 +00:00
|
|
|
"github.com/coreos/clair/utils/types"
|
|
|
|
)
|
|
|
|
|
2016-01-08 15:27:30 +00:00
|
|
|
func (pgSQL *pgSQL) insertFeature(feature database.Feature) (int, error) {
|
2015-12-28 20:03:29 +00:00
|
|
|
if feature.Name == "" {
|
|
|
|
return 0, cerrors.NewBadRequestError("could not find/insert invalid Feature")
|
|
|
|
}
|
|
|
|
|
|
|
|
if pgSQL.cache != nil {
|
2016-01-20 00:37:20 +00:00
|
|
|
id, found := pgSQL.cache.Get("feature:" + feature.Namespace.Name + ":" + feature.Name)
|
|
|
|
if found {
|
2015-12-28 20:03:29 +00:00
|
|
|
return id.(int), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find or create Namespace.
|
|
|
|
namespaceID, err := pgSQL.insertNamespace(feature.Namespace)
|
|
|
|
if err != nil {
|
2016-01-08 15:27:30 +00:00
|
|
|
return 0, err
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find or create Feature.
|
2016-01-08 15:27:30 +00:00
|
|
|
var id int
|
2015-12-28 20:03:29 +00:00
|
|
|
err = pgSQL.QueryRow(getQuery("soi_feature"), feature.Name, namespaceID).Scan(&id)
|
2016-01-08 15:27:30 +00:00
|
|
|
if err != nil {
|
2016-01-08 16:17:32 +00:00
|
|
|
return 0, handleError("soi_feature", err)
|
2016-01-08 15:27:30 +00:00
|
|
|
}
|
2015-12-28 20:03:29 +00:00
|
|
|
|
|
|
|
if pgSQL.cache != nil {
|
2016-01-20 00:37:20 +00:00
|
|
|
pgSQL.cache.Add("feature:"+feature.Namespace.Name+":"+feature.Name, id)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2016-01-08 15:27:30 +00:00
|
|
|
return id, nil
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pgSQL *pgSQL) insertFeatureVersion(featureVersion database.FeatureVersion) (id int, err error) {
|
2016-01-08 15:27:30 +00:00
|
|
|
if featureVersion.Version.String() == "" {
|
|
|
|
return 0, cerrors.NewBadRequestError("could not find/insert invalid FeatureVersion")
|
|
|
|
}
|
2015-12-28 20:03:29 +00:00
|
|
|
|
2016-01-08 15:27:30 +00:00
|
|
|
if pgSQL.cache != nil {
|
2016-01-20 00:37:20 +00:00
|
|
|
id, found := pgSQL.cache.Get("featureversion:" + featureVersion.Feature.Namespace.Name + ":" +
|
|
|
|
featureVersion.Feature.Name + ":" + featureVersion.Version.String())
|
|
|
|
if found {
|
2015-12-28 20:03:29 +00:00
|
|
|
return id.(int), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find or create Feature first.
|
|
|
|
featureID, err := pgSQL.insertFeature(featureVersion.Feature)
|
|
|
|
if err != nil {
|
2016-01-08 15:27:30 +00:00
|
|
|
return 0, err
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
2016-01-12 15:40:46 +00:00
|
|
|
featureVersion.Feature.ID = featureID
|
2015-12-28 20:03:29 +00:00
|
|
|
|
|
|
|
// Begin transaction.
|
|
|
|
tx, err := pgSQL.Begin()
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
2016-01-08 16:17:32 +00:00
|
|
|
return 0, handleError("insertFeatureVersion.Begin()", err)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 15:40:46 +00:00
|
|
|
// Set transaction as SERIALIZABLE.
|
|
|
|
// This is how we ensure that the data in Vulnerability_Affects_FeatureVersion is always
|
|
|
|
// consistent.
|
2016-01-18 23:52:16 +00:00
|
|
|
_, err = tx.Exec(getQuery("l_vulnerability_affects_featureversion"))
|
2016-01-12 15:40:46 +00:00
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
2016-01-18 23:52:16 +00:00
|
|
|
return 0, handleError("insertFeatureVersion.l_vulnerability_affects_featureversion", err)
|
2016-01-12 15:40:46 +00:00
|
|
|
}
|
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
// Find or create FeatureVersion.
|
|
|
|
var newOrExisting string
|
2016-01-08 15:27:30 +00:00
|
|
|
err = tx.QueryRow(getQuery("soi_featureversion"), featureID, &featureVersion.Version).
|
2015-12-28 20:03:29 +00:00
|
|
|
Scan(&newOrExisting, &featureVersion.ID)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
2016-01-08 16:17:32 +00:00
|
|
|
return 0, handleError("soi_featureversion", err)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
if newOrExisting == "exi" {
|
|
|
|
// That featureVersion already exists, return its id.
|
2016-01-15 20:22:52 +00:00
|
|
|
tx.Commit()
|
2015-12-28 20:03:29 +00:00
|
|
|
return featureVersion.ID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link the new FeatureVersion with every vulnerabilities that affect it, by inserting in
|
|
|
|
// Vulnerability_Affects_FeatureVersion.
|
2016-01-12 15:40:46 +00:00
|
|
|
err = linkFeatureVersionToVulnerabilities(tx, featureVersion)
|
|
|
|
if err != nil {
|
2016-01-15 20:22:52 +00:00
|
|
|
tx.Rollback()
|
2016-01-12 15:40:46 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
2015-12-28 20:03:29 +00:00
|
|
|
|
2016-01-12 15:40:46 +00:00
|
|
|
// Commit transaction.
|
|
|
|
err = tx.Commit()
|
2015-12-28 20:03:29 +00:00
|
|
|
if err != nil {
|
2016-01-12 15:40:46 +00:00
|
|
|
return 0, handleError("insertFeatureVersion.Commit()", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pgSQL.cache != nil {
|
|
|
|
pgSQL.cache.Add("featureversion:"+featureVersion.Feature.Name+":"+
|
2016-01-20 00:37:20 +00:00
|
|
|
featureVersion.Feature.Namespace.Name+":"+featureVersion.Version.String(), featureVersion.ID)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 15:40:46 +00:00
|
|
|
return featureVersion.ID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(Quentin-M): Batch me
|
|
|
|
func (pgSQL *pgSQL) insertFeatureVersions(featureVersions []database.FeatureVersion) ([]int, error) {
|
|
|
|
IDs := make([]int, 0, len(featureVersions))
|
|
|
|
|
|
|
|
for i := 0; i < len(featureVersions); i++ {
|
|
|
|
id, err := pgSQL.insertFeatureVersion(featureVersions[i])
|
|
|
|
if err != nil {
|
|
|
|
return IDs, err
|
|
|
|
}
|
|
|
|
IDs = append(IDs, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return IDs, nil
|
|
|
|
}
|
|
|
|
|
2016-01-15 20:22:52 +00:00
|
|
|
type vulnerabilityAffectsFeatureVersion struct {
|
|
|
|
vulnerabilityID int
|
|
|
|
fixedInID int
|
|
|
|
fixedInVersion types.Version
|
|
|
|
}
|
|
|
|
|
2016-01-12 15:40:46 +00:00
|
|
|
func linkFeatureVersionToVulnerabilities(tx *sql.Tx, featureVersion database.FeatureVersion) error {
|
2015-12-28 20:03:29 +00:00
|
|
|
// Select every vulnerability and the fixed version that affect this Feature.
|
2016-01-12 15:40:46 +00:00
|
|
|
// TODO(Quentin-M): LIMIT
|
|
|
|
rows, err := tx.Query(getQuery("s_vulnerability_fixedin_feature"), featureVersion.Feature.ID)
|
2015-12-28 20:03:29 +00:00
|
|
|
if err != nil {
|
2016-01-12 15:40:46 +00:00
|
|
|
return handleError("s_vulnerability_fixedin_feature", err)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2016-01-15 20:22:52 +00:00
|
|
|
var affects []vulnerabilityAffectsFeatureVersion
|
2015-12-28 20:03:29 +00:00
|
|
|
for rows.Next() {
|
2016-01-15 20:22:52 +00:00
|
|
|
var affect vulnerabilityAffectsFeatureVersion
|
|
|
|
|
|
|
|
err := rows.Scan(&affect.fixedInID, &affect.vulnerabilityID, &affect.fixedInVersion)
|
2015-12-28 20:03:29 +00:00
|
|
|
if err != nil {
|
2016-01-12 15:40:46 +00:00
|
|
|
return handleError("s_vulnerability_fixedin_feature.Scan()", err)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 20:22:52 +00:00
|
|
|
if featureVersion.Version.Compare(affect.fixedInVersion) < 0 {
|
2015-12-28 20:03:29 +00:00
|
|
|
// The version of the FeatureVersion we are inserting is lower than the fixed version on this
|
|
|
|
// Vulnerability, thus, this FeatureVersion is affected by it.
|
2016-01-15 20:22:52 +00:00
|
|
|
affects = append(affects, affect)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-08 16:17:32 +00:00
|
|
|
if err = rows.Err(); err != nil {
|
2016-01-12 15:40:46 +00:00
|
|
|
return handleError("s_vulnerability_fixedin_feature.Rows()", err)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
2016-01-15 20:22:52 +00:00
|
|
|
rows.Close()
|
|
|
|
|
|
|
|
// Insert into Vulnerability_Affects_FeatureVersion.
|
|
|
|
for _, affect := range affects {
|
2016-01-18 23:52:16 +00:00
|
|
|
// TODO(Quentin-M): Batch me.
|
2016-01-15 20:22:52 +00:00
|
|
|
_, err := tx.Exec(getQuery("i_vulnerability_affects_featureversion"), affect.vulnerabilityID,
|
|
|
|
featureVersion.ID, affect.fixedInID)
|
|
|
|
if err != nil {
|
|
|
|
return handleError("i_vulnerability_affects_featureversion", err)
|
|
|
|
}
|
|
|
|
}
|
2015-12-28 20:03:29 +00:00
|
|
|
|
2016-01-12 15:40:46 +00:00
|
|
|
return nil
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|