2016-01-21 23:09:23 +00:00
|
|
|
package pgsql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/coreos/clair/database"
|
|
|
|
cerrors "github.com/coreos/clair/utils/errors"
|
2016-01-26 22:57:32 +00:00
|
|
|
"github.com/guregu/null/zero"
|
2016-01-21 23:09:23 +00:00
|
|
|
"github.com/pborman/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
// do it in tx so we won't insert/update a vuln without notification and vice-versa.
|
2016-01-22 20:59:46 +00:00
|
|
|
// name and created doesn't matter.
|
2016-02-04 16:34:01 +00:00
|
|
|
func createNotification(tx *sql.Tx, oldVulnerabilityID, newVulnerabilityID int) error {
|
|
|
|
defer observeQueryTime("createNotification", "all", time.Now())
|
2016-01-21 23:09:23 +00:00
|
|
|
|
2016-01-22 20:59:46 +00:00
|
|
|
// Insert Notification.
|
2016-02-04 16:34:01 +00:00
|
|
|
oldVulnerabilityNullableID := sql.NullInt64{Int64: int64(oldVulnerabilityID), Valid: oldVulnerabilityID != 0}
|
|
|
|
newVulnerabilityNullableID := sql.NullInt64{Int64: int64(newVulnerabilityID), Valid: newVulnerabilityID != 0}
|
2016-02-23 00:15:38 +00:00
|
|
|
_, err := tx.Exec(insertNotification, uuid.New(), oldVulnerabilityNullableID, newVulnerabilityNullableID)
|
2016-01-21 23:09:23 +00:00
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
2016-02-23 00:15:38 +00:00
|
|
|
return handleError("insertNotification", err)
|
2016-01-21 23:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-22 20:59:46 +00:00
|
|
|
// Get one available notification name (!locked && !deleted && (!notified || notified_but_timed-out)).
|
|
|
|
// Does not fill new/old vuln.
|
|
|
|
func (pgSQL *pgSQL) GetAvailableNotification(renotifyInterval time.Duration) (database.VulnerabilityNotification, error) {
|
2016-01-24 03:02:34 +00:00
|
|
|
defer observeQueryTime("GetAvailableNotification", "all", time.Now())
|
|
|
|
|
2016-01-22 20:59:46 +00:00
|
|
|
before := time.Now().Add(-renotifyInterval)
|
2016-02-23 00:15:38 +00:00
|
|
|
row := pgSQL.QueryRow(searchNotificationAvailable, before)
|
2016-02-04 16:34:01 +00:00
|
|
|
notification, err := pgSQL.scanNotification(row, false)
|
2016-01-22 20:59:46 +00:00
|
|
|
|
2016-02-23 00:15:38 +00:00
|
|
|
return notification, handleError("searchNotificationAvailable", err)
|
2016-01-21 23:09:23 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 22:57:32 +00:00
|
|
|
func (pgSQL *pgSQL) GetNotification(name string, limit int, page database.VulnerabilityNotificationPageNumber) (database.VulnerabilityNotification, database.VulnerabilityNotificationPageNumber, error) {
|
2016-01-24 03:02:34 +00:00
|
|
|
defer observeQueryTime("GetNotification", "all", time.Now())
|
|
|
|
|
2016-01-22 20:59:46 +00:00
|
|
|
// Get Notification.
|
2016-02-23 00:15:38 +00:00
|
|
|
notification, err := pgSQL.scanNotification(pgSQL.QueryRow(searchNotification, name), true)
|
2016-01-26 22:57:32 +00:00
|
|
|
if err != nil {
|
2016-02-23 00:15:38 +00:00
|
|
|
return notification, page, handleError("searchNotification", err)
|
2016-01-26 22:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load vulnerabilities' LayersIntroducingVulnerability.
|
|
|
|
page.OldVulnerability, err = pgSQL.loadLayerIntroducingVulnerability(
|
2016-02-04 16:34:01 +00:00
|
|
|
notification.OldVulnerability,
|
|
|
|
limit,
|
|
|
|
page.OldVulnerability,
|
|
|
|
)
|
|
|
|
|
2016-01-26 22:57:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return notification, page, err
|
|
|
|
}
|
|
|
|
|
|
|
|
page.NewVulnerability, err = pgSQL.loadLayerIntroducingVulnerability(
|
2016-02-04 16:34:01 +00:00
|
|
|
notification.NewVulnerability,
|
|
|
|
limit,
|
|
|
|
page.NewVulnerability,
|
|
|
|
)
|
|
|
|
|
2016-01-26 22:57:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return notification, page, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return notification, page, nil
|
|
|
|
}
|
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
func (pgSQL *pgSQL) scanNotification(row *sql.Row, hasVulns bool) (database.VulnerabilityNotification, error) {
|
|
|
|
var notification database.VulnerabilityNotification
|
2016-01-26 22:57:32 +00:00
|
|
|
var created zero.Time
|
|
|
|
var notified zero.Time
|
|
|
|
var deleted zero.Time
|
2016-02-04 16:34:01 +00:00
|
|
|
var oldVulnerabilityNullableID sql.NullInt64
|
|
|
|
var newVulnerabilityNullableID sql.NullInt64
|
2016-01-21 23:09:23 +00:00
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
// Scan notification.
|
2016-01-26 22:57:32 +00:00
|
|
|
if hasVulns {
|
2016-02-04 16:34:01 +00:00
|
|
|
err := row.Scan(
|
|
|
|
¬ification.ID,
|
|
|
|
¬ification.Name,
|
|
|
|
&created,
|
|
|
|
¬ified,
|
|
|
|
&deleted,
|
|
|
|
&oldVulnerabilityNullableID,
|
|
|
|
&newVulnerabilityNullableID,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return notification, err
|
|
|
|
}
|
2016-01-26 22:57:32 +00:00
|
|
|
} else {
|
2016-02-04 16:34:01 +00:00
|
|
|
err := row.Scan(¬ification.ID, ¬ification.Name, &created, ¬ified, &deleted)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return notification, err
|
|
|
|
}
|
2016-01-21 23:09:23 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 22:57:32 +00:00
|
|
|
notification.Created = created.Time
|
|
|
|
notification.Notified = notified.Time
|
|
|
|
notification.Deleted = deleted.Time
|
|
|
|
|
|
|
|
if hasVulns {
|
2016-02-04 16:34:01 +00:00
|
|
|
if oldVulnerabilityNullableID.Valid {
|
|
|
|
vulnerability, err := pgSQL.findVulnerabilityByIDWithDeleted(int(oldVulnerabilityNullableID.Int64))
|
|
|
|
if err != nil {
|
|
|
|
return notification, err
|
|
|
|
}
|
|
|
|
|
|
|
|
notification.OldVulnerability = &vulnerability
|
2016-01-26 22:57:32 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
if newVulnerabilityNullableID.Valid {
|
|
|
|
vulnerability, err := pgSQL.findVulnerabilityByIDWithDeleted(int(newVulnerabilityNullableID.Int64))
|
|
|
|
if err != nil {
|
|
|
|
return notification, err
|
|
|
|
}
|
|
|
|
|
|
|
|
notification.NewVulnerability = &vulnerability
|
2016-01-26 22:57:32 +00:00
|
|
|
}
|
2016-01-22 20:59:46 +00:00
|
|
|
}
|
2016-01-26 22:57:32 +00:00
|
|
|
|
2016-02-04 16:34:01 +00:00
|
|
|
return notification, nil
|
2016-01-26 22:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fills Vulnerability.LayersIntroducingVulnerability.
|
|
|
|
// limit -1: won't do anything
|
|
|
|
// limit 0: will just get the startID of the second page
|
|
|
|
func (pgSQL *pgSQL) loadLayerIntroducingVulnerability(vulnerability *database.Vulnerability, limit, startID int) (int, error) {
|
|
|
|
tf := time.Now()
|
|
|
|
|
|
|
|
if vulnerability == nil {
|
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// A startID equals to -1 means that we reached the end already.
|
|
|
|
if startID == -1 || limit == -1 {
|
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// We do `defer observeQueryTime` here because we don't want to observe invalid calls.
|
|
|
|
defer observeQueryTime("loadLayerIntroducingVulnerability", "all", tf)
|
|
|
|
|
|
|
|
// Query with limit + 1, the last item will be used to know the next starting ID.
|
2016-02-23 00:15:38 +00:00
|
|
|
rows, err := pgSQL.Query(searchNotificationLayerIntroducingVulnerability,
|
2016-01-26 22:57:32 +00:00
|
|
|
vulnerability.ID, startID, limit+1)
|
2016-01-22 20:59:46 +00:00
|
|
|
if err != nil {
|
2016-02-23 00:15:38 +00:00
|
|
|
return 0, handleError("searchVulnerabilityFixedInFeature", err)
|
2016-01-21 23:09:23 +00:00
|
|
|
}
|
2016-01-26 22:57:32 +00:00
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
var layers []database.Layer
|
|
|
|
for rows.Next() {
|
|
|
|
var layer database.Layer
|
|
|
|
|
|
|
|
if err := rows.Scan(&layer.ID, &layer.Name); err != nil {
|
2016-02-23 00:15:38 +00:00
|
|
|
return -1, handleError("searchNotificationLayerIntroducingVulnerability.Scan()", err)
|
2016-01-26 22:57:32 +00:00
|
|
|
}
|
2016-01-21 23:09:23 +00:00
|
|
|
|
2016-01-26 22:57:32 +00:00
|
|
|
layers = append(layers, layer)
|
|
|
|
}
|
|
|
|
if err = rows.Err(); err != nil {
|
2016-02-23 00:15:38 +00:00
|
|
|
return -1, handleError("searchNotificationLayerIntroducingVulnerability.Rows()", err)
|
2016-01-26 22:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size := limit
|
|
|
|
if len(layers) < limit {
|
|
|
|
size = len(layers)
|
|
|
|
}
|
|
|
|
vulnerability.LayersIntroducingVulnerability = layers[:size]
|
|
|
|
|
|
|
|
nextID := -1
|
|
|
|
if len(layers) > limit {
|
|
|
|
nextID = layers[limit].ID
|
|
|
|
}
|
2016-01-21 23:09:23 +00:00
|
|
|
|
2016-01-26 22:57:32 +00:00
|
|
|
return nextID, nil
|
2016-01-21 23:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pgSQL *pgSQL) SetNotificationNotified(name string) error {
|
2016-01-24 03:02:34 +00:00
|
|
|
defer observeQueryTime("SetNotificationNotified", "all", time.Now())
|
|
|
|
|
2016-02-23 00:15:38 +00:00
|
|
|
if _, err := pgSQL.Exec(updatedNotificationNotified, name); err != nil {
|
|
|
|
return handleError("updatedNotificationNotified", err)
|
2016-01-21 23:09:23 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pgSQL *pgSQL) DeleteNotification(name string) error {
|
2016-01-24 03:02:34 +00:00
|
|
|
defer observeQueryTime("DeleteNotification", "all", time.Now())
|
|
|
|
|
2016-02-23 00:15:38 +00:00
|
|
|
result, err := pgSQL.Exec(removeNotification, name)
|
2016-01-21 23:09:23 +00:00
|
|
|
if err != nil {
|
2016-02-23 00:15:38 +00:00
|
|
|
return handleError("removeNotification", err)
|
2016-01-21 23:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
affected, err := result.RowsAffected()
|
|
|
|
if err != nil {
|
2016-02-23 00:15:38 +00:00
|
|
|
return handleError("removeNotification.RowsAffected()", err)
|
2016-01-21 23:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if affected <= 0 {
|
|
|
|
return cerrors.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|