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.
|
|
|
|
|
2016-01-12 15:40:46 +00:00
|
|
|
package pgsql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2016-02-01 23:41:40 +00:00
|
|
|
"encoding/json"
|
2016-01-20 00:17:08 +00:00
|
|
|
"fmt"
|
2016-02-01 23:41:40 +00:00
|
|
|
"reflect"
|
2016-01-24 03:02:34 +00:00
|
|
|
"time"
|
2016-01-12 15:40:46 +00:00
|
|
|
|
|
|
|
"github.com/coreos/clair/database"
|
|
|
|
"github.com/coreos/clair/utils"
|
|
|
|
cerrors "github.com/coreos/clair/utils/errors"
|
|
|
|
"github.com/coreos/clair/utils/types"
|
|
|
|
"github.com/guregu/null/zero"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (pgSQL *pgSQL) FindVulnerability(namespaceName, name string) (database.Vulnerability, error) {
|
2016-02-02 18:29:59 +00:00
|
|
|
return findVulnerability(pgSQL, namespaceName, name, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func findVulnerability(queryer Queryer, namespaceName, name string, forUpdate bool) (database.Vulnerability, error) {
|
|
|
|
defer observeQueryTime("findVulnerability", "all", time.Now())
|
2016-01-24 03:02:34 +00:00
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
queryName := "f_vulnerability"
|
|
|
|
query := getQuery("f_vulnerability_base") + getQuery("f_vulnerability_+by_name_namespace")
|
|
|
|
if forUpdate {
|
|
|
|
queryName = queryName + "+for_update"
|
|
|
|
query = query + getQuery("f_vulnerability_for_update")
|
2016-01-12 15:40:46 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
return scanVulnerability(queryer, queryName, queryer.QueryRow(query, namespaceName, name))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pgSQL *pgSQL) findVulnerabilityByIDWithDeleted(id int) (database.Vulnerability, error) {
|
|
|
|
defer observeQueryTime("findVulnerabilityByIDWithDeleted", "all", time.Now())
|
|
|
|
|
2016-02-02 18:29:59 +00:00
|
|
|
queryName := "f_vulnerability"
|
2016-02-04 16:34:01 +00:00
|
|
|
query := getQuery("f_vulnerability_base") + getQuery("f_vulnerability_+by_id")
|
|
|
|
|
|
|
|
return scanVulnerability(pgSQL, queryName, pgSQL.QueryRow(query, id))
|
|
|
|
}
|
|
|
|
|
|
|
|
func scanVulnerability(queryer Queryer, queryName string, vulnerabilityRow *sql.Row) (database.Vulnerability, error) {
|
|
|
|
var vulnerability database.Vulnerability
|
|
|
|
|
|
|
|
err := vulnerabilityRow.Scan(
|
|
|
|
&vulnerability.ID,
|
|
|
|
&vulnerability.Name,
|
|
|
|
&vulnerability.Namespace.ID,
|
|
|
|
&vulnerability.Namespace.Name,
|
|
|
|
&vulnerability.Description,
|
|
|
|
&vulnerability.Link,
|
|
|
|
&vulnerability.Severity,
|
|
|
|
&vulnerability.Metadata,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return vulnerability, handleError(queryName+".Scan()", err)
|
2016-02-02 18:29:59 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
if vulnerability.ID == 0 {
|
|
|
|
return vulnerability, cerrors.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query the FixedIn FeatureVersion now.
|
|
|
|
rows, err := queryer.Query(getQuery("f_vulnerability_fixedin"), vulnerability.ID)
|
2016-01-12 15:40:46 +00:00
|
|
|
if err != nil {
|
2016-02-04 16:34:01 +00:00
|
|
|
return vulnerability, handleError("f_vulnerability_fixedin.Scan()", err)
|
2016-01-12 15:40:46 +00:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var featureVersionID zero.Int
|
|
|
|
var featureVersionVersion zero.String
|
|
|
|
var featureVersionFeatureName zero.String
|
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
err := rows.Scan(
|
|
|
|
&featureVersionVersion,
|
|
|
|
&featureVersionID,
|
|
|
|
&featureVersionFeatureName,
|
|
|
|
)
|
|
|
|
|
2016-01-12 15:40:46 +00:00
|
|
|
if err != nil {
|
2016-02-04 16:34:01 +00:00
|
|
|
return vulnerability, handleError("f_vulnerability_fixedin.Scan()", err)
|
2016-01-12 15:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !featureVersionID.IsZero() {
|
|
|
|
// Note that the ID we fill in featureVersion is actually a Feature ID, and not
|
|
|
|
// a FeatureVersion ID.
|
|
|
|
featureVersion := database.FeatureVersion{
|
|
|
|
Model: database.Model{ID: int(featureVersionID.Int64)},
|
|
|
|
Feature: database.Feature{
|
|
|
|
Model: database.Model{ID: int(featureVersionID.Int64)},
|
|
|
|
Namespace: vulnerability.Namespace,
|
|
|
|
Name: featureVersionFeatureName.String,
|
|
|
|
},
|
|
|
|
Version: types.NewVersionUnsafe(featureVersionVersion.String),
|
|
|
|
}
|
|
|
|
vulnerability.FixedIn = append(vulnerability.FixedIn, featureVersion)
|
|
|
|
}
|
|
|
|
}
|
2016-02-04 16:34:01 +00:00
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return vulnerability, handleError("f_vulnerability_fixedin.Rows()", err)
|
2016-01-12 15:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return vulnerability, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FixedIn.Namespace are not necessary, they are overwritten by the vuln.
|
2016-01-13 20:30:58 +00:00
|
|
|
// By setting the fixed version to minVersion, we can say that the vuln does'nt affect anymore.
|
2016-02-04 22:10:19 +00:00
|
|
|
func (pgSQL *pgSQL) InsertVulnerabilities(vulnerabilities []database.Vulnerability, generateNotifications bool) error {
|
2016-01-12 15:40:46 +00:00
|
|
|
for _, vulnerability := range vulnerabilities {
|
2016-02-04 22:10:19 +00:00
|
|
|
err := pgSQL.insertVulnerability(vulnerability, false, generateNotifications)
|
2016-01-12 15:40:46 +00:00
|
|
|
if err != nil {
|
2016-01-20 00:17:08 +00:00
|
|
|
fmt.Printf("%#v\n", vulnerability)
|
2016-01-12 15:40:46 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-04 22:10:19 +00:00
|
|
|
func (pgSQL *pgSQL) insertVulnerability(vulnerability database.Vulnerability, onlyFixedIn, generateNotification bool) error {
|
2016-01-24 03:02:34 +00:00
|
|
|
tf := time.Now()
|
|
|
|
|
2016-01-12 15:40:46 +00:00
|
|
|
// Verify parameters
|
2016-02-04 16:34:01 +00:00
|
|
|
if vulnerability.Name == "" || vulnerability.Namespace.Name == "" {
|
|
|
|
return cerrors.NewBadRequestError("insertVulnerability needs at least the Name and the Namespace")
|
|
|
|
}
|
|
|
|
if !onlyFixedIn && !vulnerability.Severity.IsValid() {
|
|
|
|
msg := fmt.Sprintf("could not insert a vulnerability that has an invalid Severity: %s", vulnerability.Severity)
|
|
|
|
log.Warning(msg)
|
|
|
|
return cerrors.NewBadRequestError(msg)
|
|
|
|
}
|
|
|
|
for i := 0; i < len(vulnerability.FixedIn); i++ {
|
|
|
|
fifv := &vulnerability.FixedIn[i]
|
|
|
|
|
|
|
|
if fifv.Feature.Namespace.Name == "" {
|
|
|
|
// As there is no Namespace on that FixedIn FeatureVersion, set it to the Vulnerability's
|
|
|
|
// Namespace.
|
|
|
|
fifv.Feature.Namespace.Name = vulnerability.Namespace.Name
|
|
|
|
} else if fifv.Feature.Namespace.Name != vulnerability.Namespace.Name {
|
|
|
|
msg := "could not insert an invalid vulnerability that contains FixedIn FeatureVersion that are not in the same namespace as the Vulnerability"
|
2016-01-12 15:40:46 +00:00
|
|
|
log.Warning(msg)
|
|
|
|
return cerrors.NewBadRequestError(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 18:29:59 +00:00
|
|
|
// We do `defer observeQueryTime` here because we don't want to observe invalid vulnerabilities.
|
|
|
|
defer observeQueryTime("insertVulnerability", "all", tf)
|
|
|
|
|
2016-01-12 15:40:46 +00:00
|
|
|
// Begin transaction.
|
|
|
|
tx, err := pgSQL.Begin()
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return handleError("insertVulnerability.Begin()", err)
|
|
|
|
}
|
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
// Find existing vulnerability and its Vulnerability_FixedIn_Features (for update).
|
|
|
|
existingVulnerability, err := findVulnerability(tx, vulnerability.Namespace.Name, vulnerability.Name, true)
|
2016-02-02 18:29:59 +00:00
|
|
|
if err != nil && err != cerrors.ErrNotFound {
|
2016-01-12 15:40:46 +00:00
|
|
|
tx.Rollback()
|
2016-02-02 18:29:59 +00:00
|
|
|
return err
|
2016-01-12 15:40:46 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
if onlyFixedIn {
|
|
|
|
// Because this call tries to update FixedIn FeatureVersion, import all other data from the
|
|
|
|
// existing one.
|
|
|
|
if existingVulnerability.ID == 0 {
|
|
|
|
return cerrors.ErrNotFound
|
2016-01-12 15:40:46 +00:00
|
|
|
}
|
2016-02-04 16:34:01 +00:00
|
|
|
|
|
|
|
fixedIn := vulnerability.FixedIn
|
|
|
|
vulnerability = existingVulnerability
|
|
|
|
vulnerability.FixedIn = fixedIn
|
|
|
|
}
|
|
|
|
|
|
|
|
if existingVulnerability.ID != 0 {
|
|
|
|
updateMetadata := vulnerability.Description != existingVulnerability.Description ||
|
2016-01-14 20:59:01 +00:00
|
|
|
vulnerability.Link != existingVulnerability.Link ||
|
2016-02-02 18:29:59 +00:00
|
|
|
vulnerability.Severity != existingVulnerability.Severity ||
|
2016-02-04 16:34:01 +00:00
|
|
|
!reflect.DeepEqual(castMetadata(vulnerability.Metadata), existingVulnerability.Metadata)
|
|
|
|
|
|
|
|
// Construct the entire list of FixedIn FeatureVersion, by using the
|
|
|
|
// the FixedIn list of the old vulnerability.
|
|
|
|
//
|
|
|
|
// TODO(Quentin-M): We could use !updateFixedIn to just copy FixedIn/Affects rows from the
|
|
|
|
// existing vulnerability in order to make metadata updates much faster.
|
2016-02-05 03:47:01 +00:00
|
|
|
var updateFixedIn bool
|
|
|
|
vulnerability.FixedIn, updateFixedIn = applyFixedInDiff(existingVulnerability.FixedIn, vulnerability.FixedIn)
|
2016-02-04 16:34:01 +00:00
|
|
|
|
|
|
|
if !updateMetadata && !updateFixedIn {
|
|
|
|
tx.Commit()
|
|
|
|
return nil
|
2016-01-12 15:40:46 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
// Mark the old vulnerability as non latest.
|
|
|
|
_, err = tx.Exec(getQuery("r_vulnerability"), vulnerability.Namespace.Name, vulnerability.Name)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return handleError("r_vulnerability", err)
|
|
|
|
}
|
2016-02-05 03:47:01 +00:00
|
|
|
} else {
|
|
|
|
// The vulnerability is new, we don't want to have any types.MinVersion as they are only used
|
|
|
|
// for diffing existing vulnerabilities.
|
|
|
|
var fixedIn []database.FeatureVersion
|
|
|
|
for _, fv := range vulnerability.FixedIn {
|
|
|
|
if fv.Version != types.MinVersion {
|
|
|
|
fixedIn = append(fixedIn, fv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vulnerability.FixedIn = fixedIn
|
2016-01-12 15:40:46 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
// Find or insert Vulnerability's Namespace.
|
|
|
|
namespaceID, err := pgSQL.insertNamespace(vulnerability.Namespace)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-02-02 18:29:59 +00:00
|
|
|
}
|
2016-01-24 03:02:34 +00:00
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
// Insert vulnerability.
|
|
|
|
err = tx.QueryRow(
|
|
|
|
getQuery("i_vulnerability"),
|
|
|
|
namespaceID,
|
|
|
|
vulnerability.Name,
|
|
|
|
vulnerability.Description,
|
|
|
|
vulnerability.Link,
|
|
|
|
&vulnerability.Severity,
|
|
|
|
&vulnerability.Metadata,
|
|
|
|
).Scan(&vulnerability.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
2016-01-12 15:40:46 +00:00
|
|
|
tx.Rollback()
|
2016-02-04 16:34:01 +00:00
|
|
|
return handleError("i_vulnerability", err)
|
2016-01-12 15:40:46 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
// Update Vulnerability_FixedIn_Feature and Vulnerability_Affects_FeatureVersion now.
|
|
|
|
err = pgSQL.insertVulnerabilityFixedInFeatureVersions(tx, vulnerability.ID, vulnerability.FixedIn)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
2016-01-24 03:02:51 +00:00
|
|
|
}
|
2016-01-26 22:57:32 +00:00
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
// Create a notification.
|
2016-02-04 22:10:19 +00:00
|
|
|
if generateNotification {
|
|
|
|
err = createNotification(tx, existingVulnerability.ID, vulnerability.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-24 03:02:51 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 15:40:46 +00:00
|
|
|
// Commit transaction.
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return handleError("insertVulnerability.Commit()", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-01 23:41:40 +00:00
|
|
|
// castMetadata marshals the given database.MetadataMap and unmarshals it again to make sure that
|
|
|
|
// everything has the interface{} type.
|
|
|
|
// It is required when comparing crafted MetadataMap against MetadataMap that we get from the
|
|
|
|
// database.
|
|
|
|
func castMetadata(m database.MetadataMap) database.MetadataMap {
|
|
|
|
c := make(database.MetadataMap)
|
|
|
|
j, _ := json.Marshal(m)
|
|
|
|
json.Unmarshal(j, &c)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
// applyFixedInDiff applies a FeatureVersion diff on a FeatureVersion list and returns the result.
|
|
|
|
func applyFixedInDiff(currentList, diff []database.FeatureVersion) ([]database.FeatureVersion, bool) {
|
|
|
|
currentMap, currentNames := createFeatureVersionNameMap(currentList)
|
|
|
|
diffMap, diffNames := createFeatureVersionNameMap(diff)
|
2016-01-12 15:40:46 +00:00
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
addedNames := utils.CompareStringLists(diffNames, currentNames)
|
|
|
|
inBothNames := utils.CompareStringListsInBoth(diffNames, currentNames)
|
2016-01-12 15:40:46 +00:00
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
different := false
|
|
|
|
|
|
|
|
for _, name := range addedNames {
|
|
|
|
if diffMap[name].Version == types.MinVersion {
|
|
|
|
// MinVersion only makes sense when a Feature is already fixed in some version,
|
|
|
|
// in which case we would be in the "inBothNames".
|
2016-01-14 20:59:01 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
currentMap[name] = diffMap[name]
|
|
|
|
different = true
|
2016-01-12 15:40:46 +00:00
|
|
|
}
|
2016-02-02 18:29:59 +00:00
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
for _, name := range inBothNames {
|
|
|
|
fv := diffMap[name]
|
2016-02-02 18:29:59 +00:00
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
if fv.Version == types.MinVersion {
|
|
|
|
// MinVersion means that the Feature doesn't affect the Vulnerability anymore.
|
|
|
|
delete(currentMap, name)
|
|
|
|
different = true
|
|
|
|
} else if fv.Version != currentMap[name].Version {
|
|
|
|
// The version got updated.
|
|
|
|
currentMap[name] = diffMap[name]
|
|
|
|
different = true
|
2016-02-02 18:29:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
// Convert currentMap to a slice and return it.
|
|
|
|
var newList []database.FeatureVersion
|
|
|
|
for _, fv := range currentMap {
|
|
|
|
newList = append(newList, fv)
|
2016-01-12 15:40:46 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
return newList, different
|
2016-01-12 15:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createFeatureVersionNameMap(features []database.FeatureVersion) (map[string]database.FeatureVersion, []string) {
|
|
|
|
m := make(map[string]database.FeatureVersion, 0)
|
|
|
|
s := make([]string, 0, len(features))
|
|
|
|
|
|
|
|
for i := 0; i < len(features); i++ {
|
|
|
|
featureVersion := features[i]
|
|
|
|
m[featureVersion.Feature.Name] = featureVersion
|
|
|
|
s = append(s, featureVersion.Feature.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, s
|
|
|
|
}
|
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
// insertVulnerabilityFixedInFeatureVersions populates Vulnerability_FixedIn_Feature for the given
|
|
|
|
// vulnerability with the specified database.FeatureVersion list and uses
|
|
|
|
// linkVulnerabilityToFeatureVersions to propagate the changes on Vulnerability_FixedIn_Feature to
|
|
|
|
// Vulnerability_Affects_FeatureVersion.
|
|
|
|
func (pgSQL *pgSQL) insertVulnerabilityFixedInFeatureVersions(tx *sql.Tx, vulnerabilityID int, fixedIn []database.FeatureVersion) error {
|
|
|
|
defer observeQueryTime("insertVulnerabilityFixedInFeatureVersions", "all", time.Now())
|
2016-02-02 18:29:59 +00:00
|
|
|
|
|
|
|
// Insert or find the Features.
|
|
|
|
// TODO(Quentin-M): Batch me.
|
|
|
|
var err error
|
|
|
|
var features []*database.Feature
|
2016-02-04 16:34:01 +00:00
|
|
|
for i := 0; i < len(fixedIn); i++ {
|
|
|
|
features = append(features, &fixedIn[i].Feature)
|
2016-02-02 18:29:59 +00:00
|
|
|
}
|
|
|
|
for _, feature := range features {
|
|
|
|
if feature.ID == 0 {
|
|
|
|
if feature.ID, err = pgSQL.insertFeature(*feature); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock Vulnerability_Affects_FeatureVersion exclusively.
|
|
|
|
// We want to prevent InsertFeatureVersion to modify it.
|
|
|
|
promConcurrentLockVAFV.Inc()
|
|
|
|
defer promConcurrentLockVAFV.Dec()
|
|
|
|
t := time.Now()
|
|
|
|
_, err = tx.Exec(getQuery("l_vulnerability_affects_featureversion"))
|
|
|
|
observeQueryTime("insertVulnerability", "lock", t)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return handleError("insertVulnerability.l_vulnerability_affects_featureversion", err)
|
|
|
|
}
|
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
for _, fv := range fixedIn {
|
|
|
|
var fixedInID int
|
2016-01-12 15:40:46 +00:00
|
|
|
|
|
|
|
// Insert Vulnerability_FixedIn_Feature.
|
2016-02-04 16:34:01 +00:00
|
|
|
err = tx.QueryRow(
|
|
|
|
getQuery("i_vulnerability_fixedin_feature"),
|
|
|
|
vulnerabilityID, fv.Feature.ID,
|
|
|
|
&fv.Version,
|
|
|
|
).Scan(&fixedInID)
|
2016-01-12 15:40:46 +00:00
|
|
|
|
2016-02-02 18:29:59 +00:00
|
|
|
if err != nil {
|
2016-02-04 16:34:01 +00:00
|
|
|
return handleError("i_vulnerability_fixedin_feature", err)
|
2016-02-02 18:29:59 +00:00
|
|
|
}
|
2016-01-12 15:40:46 +00:00
|
|
|
|
2016-02-02 18:29:59 +00:00
|
|
|
// Insert Vulnerability_Affects_FeatureVersion.
|
2016-02-04 16:34:01 +00:00
|
|
|
err = linkVulnerabilityToFeatureVersions(tx, fixedInID, vulnerabilityID, fv.Feature.ID, fv.Version)
|
2016-02-02 18:29:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-12 15:40:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func linkVulnerabilityToFeatureVersions(tx *sql.Tx, fixedInID, vulnerabilityID, featureID int, fixedInVersion types.Version) error {
|
2016-01-18 23:52:16 +00:00
|
|
|
// Find every FeatureVersions of the Feature that the vulnerability affects.
|
2016-01-12 15:40:46 +00:00
|
|
|
// TODO(Quentin-M): LIMIT
|
|
|
|
rows, err := tx.Query(getQuery("f_featureversion_by_feature"), featureID)
|
|
|
|
if err != nil {
|
|
|
|
return handleError("f_featureversion_by_feature", err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2016-01-18 23:52:16 +00:00
|
|
|
var affecteds []database.FeatureVersion
|
2016-01-12 15:40:46 +00:00
|
|
|
for rows.Next() {
|
2016-01-18 23:52:16 +00:00
|
|
|
var affected database.FeatureVersion
|
|
|
|
|
|
|
|
err := rows.Scan(&affected.ID, &affected.Version)
|
2016-01-12 15:40:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return handleError("f_featureversion_by_feature.Scan()", err)
|
|
|
|
}
|
|
|
|
|
2016-01-18 23:52:16 +00:00
|
|
|
if affected.Version.Compare(fixedInVersion) < 0 {
|
|
|
|
// The version of the FeatureVersion is lower than the fixed version of this vulnerability,
|
|
|
|
// thus, this FeatureVersion is affected by it.
|
|
|
|
affecteds = append(affecteds, affected)
|
2016-01-12 15:40:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
return handleError("f_featureversion_by_feature.Rows()", err)
|
|
|
|
}
|
2016-01-18 23:52:16 +00:00
|
|
|
rows.Close()
|
|
|
|
|
|
|
|
// Insert into Vulnerability_Affects_FeatureVersion.
|
|
|
|
for _, affected := range affecteds {
|
|
|
|
// TODO(Quentin-M): Batch me.
|
|
|
|
_, err := tx.Exec(getQuery("i_vulnerability_affects_featureversion"), vulnerabilityID,
|
|
|
|
affected.ID, fixedInID)
|
|
|
|
if err != nil {
|
|
|
|
return handleError("i_vulnerability_affects_featureversion", err)
|
|
|
|
}
|
|
|
|
}
|
2016-01-12 15:40:46 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-28 18:35:07 +00:00
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
func (pgSQL *pgSQL) InsertVulnerabilityFixes(vulnerabilityNamespace, vulnerabilityName string, fixes []database.FeatureVersion) error {
|
|
|
|
defer observeQueryTime("InsertVulnerabilityFixes", "all", time.Now())
|
|
|
|
|
|
|
|
v := database.Vulnerability{
|
|
|
|
Name: vulnerabilityName,
|
|
|
|
Namespace: database.Namespace{
|
|
|
|
Name: vulnerabilityNamespace,
|
|
|
|
},
|
|
|
|
FixedIn: fixes,
|
|
|
|
}
|
|
|
|
|
2016-02-04 22:10:19 +00:00
|
|
|
return pgSQL.insertVulnerability(v, true, true)
|
2016-02-04 16:34:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pgSQL *pgSQL) DeleteVulnerabilityFix(vulnerabilityNamespace, vulnerabilityName, featureName string) error {
|
|
|
|
defer observeQueryTime("DeleteVulnerabilityFix", "all", time.Now())
|
|
|
|
|
|
|
|
v := database.Vulnerability{
|
|
|
|
Name: vulnerabilityName,
|
|
|
|
Namespace: database.Namespace{
|
|
|
|
Name: vulnerabilityNamespace,
|
|
|
|
},
|
|
|
|
FixedIn: []database.FeatureVersion{
|
|
|
|
database.FeatureVersion{
|
|
|
|
Feature: database.Feature{
|
|
|
|
Name: featureName,
|
|
|
|
Namespace: database.Namespace{
|
|
|
|
Name: vulnerabilityNamespace,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Version: types.MinVersion,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-02-04 22:10:19 +00:00
|
|
|
return pgSQL.insertVulnerability(v, true, true)
|
2016-02-04 16:34:01 +00:00
|
|
|
}
|
|
|
|
|
2016-01-28 18:35:07 +00:00
|
|
|
func (pgSQL *pgSQL) DeleteVulnerability(namespaceName, name string) error {
|
2016-01-24 03:02:34 +00:00
|
|
|
defer observeQueryTime("DeleteVulnerability", "all", time.Now())
|
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
// Begin transaction.
|
|
|
|
tx, err := pgSQL.Begin()
|
2016-01-28 18:35:07 +00:00
|
|
|
if err != nil {
|
2016-02-04 16:34:01 +00:00
|
|
|
tx.Rollback()
|
|
|
|
return handleError("DeleteVulnerability.Begin()", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var vulnerabilityID int
|
|
|
|
err = tx.QueryRow(getQuery("r_vulnerability"), namespaceName, name).Scan(&vulnerabilityID)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
2016-01-28 18:35:07 +00:00
|
|
|
return handleError("r_vulnerability", err)
|
|
|
|
}
|
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
// Create a notification.
|
|
|
|
err = createNotification(tx, vulnerabilityID, 0)
|
2016-01-28 18:35:07 +00:00
|
|
|
if err != nil {
|
2016-02-04 16:34:01 +00:00
|
|
|
return err
|
2016-01-28 18:35:07 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
// Commit transaction.
|
|
|
|
err = tx.Commit()
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return handleError("DeleteVulnerability.Commit()", err)
|
2016-01-28 18:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|