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.
|
|
|
|
|
|
|
|
// Package database defines the Clair's models and a common interface for database implementations.
|
2015-11-13 19:11:28 +00:00
|
|
|
package database
|
|
|
|
|
2016-01-08 19:42:07 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
)
|
2015-11-13 19:11:28 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrBackendException is an error that occurs when the database backend does
|
|
|
|
// not work properly (ie. unreachable).
|
2016-01-08 16:17:32 +00:00
|
|
|
ErrBackendException = errors.New("database: an error occured when querying the backend")
|
|
|
|
|
2015-11-13 19:11:28 +00:00
|
|
|
// ErrInconsistent is an error that occurs when a database consistency check
|
|
|
|
// fails (ie. when an entity which is supposed to be unique is detected twice)
|
|
|
|
ErrInconsistent = errors.New("database: inconsistent database")
|
2016-01-08 16:17:32 +00:00
|
|
|
|
2015-11-13 19:11:28 +00:00
|
|
|
// ErrCantOpen is an error that occurs when the database could not be opened
|
|
|
|
ErrCantOpen = errors.New("database: could not open database")
|
|
|
|
)
|
|
|
|
|
2016-02-12 21:24:37 +00:00
|
|
|
// Datastore is the interface that describes a database backend implementation.
|
2015-12-28 20:03:29 +00:00
|
|
|
type Datastore interface {
|
2016-02-12 21:24:37 +00:00
|
|
|
// # Namespace
|
|
|
|
// ListNamespaces returns the entire list of known Namespaces.
|
2016-01-28 16:26:34 +00:00
|
|
|
ListNamespaces() ([]Namespace, error)
|
|
|
|
|
2016-02-12 21:24:37 +00:00
|
|
|
// # Layer
|
|
|
|
// InsertLayer stores a Layer in the database.
|
|
|
|
// A Layer is uniquely identified by its Name. The Name and EngineVersion fields are mandatory.
|
|
|
|
// If a Parent is specified, it is expected that it has been retrieved using FindLayer.
|
|
|
|
// If a Layer that already exists is inserted and the EngineVersion of the given Layer is higher
|
|
|
|
// than the stored one, the stored Layer should be updated.
|
|
|
|
// The function has to be idempotent, inserting a layer that already exists shouln'd return an
|
|
|
|
// error.
|
2015-12-28 20:03:29 +00:00
|
|
|
InsertLayer(Layer) error
|
2016-02-12 21:24:37 +00:00
|
|
|
|
|
|
|
// FindLayer retrieves a Layer from the database.
|
|
|
|
// withFeatures specifies whether the Features field should be filled. When withVulnerabilities is
|
|
|
|
// true, the Features field should be filled and their AffectedBy fields should contain every
|
|
|
|
// vulnerabilities that affect them.
|
2016-01-21 23:09:38 +00:00
|
|
|
FindLayer(name string, withFeatures, withVulnerabilities bool) (Layer, error)
|
2016-02-12 21:24:37 +00:00
|
|
|
|
|
|
|
// DeleteLayer deletes a Layer from the database and every layers that are based on it,
|
|
|
|
// recursively.
|
2015-12-28 20:03:29 +00:00
|
|
|
DeleteLayer(name string) error
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2016-02-12 21:24:37 +00:00
|
|
|
// # Vulnerability
|
2016-02-26 10:18:45 +00:00
|
|
|
// ListVulnerabilities returns the list of vulnerabilies of a certain Namespace.
|
|
|
|
// The Limit and page parameters are used to paginate the return list.
|
2016-02-29 08:29:40 +00:00
|
|
|
// The first given page should be 0. The function will then return the next available page.
|
|
|
|
// If there is no more page, -1 has to be returned.
|
|
|
|
ListVulnerabilities(namespaceName string, limit int, page int) ([]Vulnerability, int, error)
|
2016-02-26 10:18:45 +00:00
|
|
|
|
2016-02-12 21:24:37 +00:00
|
|
|
// InsertVulnerabilities stores the given Vulnerabilities in the database, updating them if
|
|
|
|
// necessary. A vulnerability is uniquely identified by its Namespace and its Name.
|
|
|
|
// The FixedIn field may only contain a partial list of Features that are affected by the
|
|
|
|
// Vulnerability, along with the version in which the vulnerability is fixed. It is the
|
2016-02-24 23:00:36 +00:00
|
|
|
// responsibility of the implementation to update the list properly. A version equals to
|
2016-02-12 21:24:37 +00:00
|
|
|
// types.MinVersion means that the given Feature is not being affected by the Vulnerability at
|
|
|
|
// all and thus, should be removed from the list. It is important that Features should be unique
|
|
|
|
// in the FixedIn list. For example, it doesn't make sense to have two `openssl` Feature listed as
|
|
|
|
// a Vulnerability can only be fixed in one Version. This is true because Vulnerabilities and
|
|
|
|
// Features are Namespaced (i.e. specific to one operating system).
|
|
|
|
// Each vulnerability insertion or update has to create a Notification that will contain the
|
|
|
|
// old and the updated Vulnerability, unless createNotification equals to true.
|
2016-02-04 22:10:19 +00:00
|
|
|
InsertVulnerabilities(vulnerabilities []Vulnerability, createNotification bool) error
|
2016-02-12 21:24:37 +00:00
|
|
|
|
|
|
|
// FindVulnerability retrieves a Vulnerability from the database, including the FixedIn list.
|
2016-01-12 15:40:46 +00:00
|
|
|
FindVulnerability(namespaceName, name string) (Vulnerability, error)
|
2016-02-12 21:24:37 +00:00
|
|
|
|
|
|
|
// DeleteVulnerability removes a Vulnerability from the database.
|
|
|
|
// It has to create a Notification that will contain the old Vulnerability.
|
2016-01-28 18:35:07 +00:00
|
|
|
DeleteVulnerability(namespaceName, name string) error
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2016-02-12 21:24:37 +00:00
|
|
|
// InsertVulnerabilityFixes adds new FixedIn Feature or update the Versions of existing ones to
|
|
|
|
// the specified Vulnerability in the database.
|
|
|
|
// It has has to create a Notification that will contain the old and the updated Vulnerability.
|
2016-02-02 18:29:59 +00:00
|
|
|
InsertVulnerabilityFixes(vulnerabilityNamespace, vulnerabilityName string, fixes []FeatureVersion) error
|
2016-02-12 21:24:37 +00:00
|
|
|
|
|
|
|
// DeleteVulnerabilityFix removes a FixedIn Feature from the specified Vulnerability in the
|
|
|
|
// database. It can be used to store the fact that a Vulnerability no longer affects the given
|
|
|
|
// Feature in any Version.
|
|
|
|
// It has has to create a Notification that will contain the old and the updated Vulnerability.
|
2016-02-02 18:29:59 +00:00
|
|
|
DeleteVulnerabilityFix(vulnerabilityNamespace, vulnerabilityName, featureName string) error
|
|
|
|
|
2016-02-12 21:24:37 +00:00
|
|
|
// # Notification
|
|
|
|
// GetAvailableNotification returns the Name, Created, Notified and Deleted fields of a
|
|
|
|
// Notification that should be handled. The renotify interval defines how much time after being
|
|
|
|
// marked as Notified by SetNotificationNotified, a Notification that hasn't been deleted should
|
|
|
|
// be returned again by this function. A Notification for which there is a valid Lock with the
|
|
|
|
// same Name should not be returned.
|
|
|
|
GetAvailableNotification(renotifyInterval time.Duration) (VulnerabilityNotification, error)
|
|
|
|
|
|
|
|
// GetNotification returns a Notification, including its OldVulnerability and NewVulnerability
|
|
|
|
// fields. On these Vulnerabilities, LayersIntroducingVulnerability should be filled with
|
|
|
|
// every Layer that introduces the Vulnerability (i.e. adds at least one affected FeatureVersion).
|
|
|
|
// The Limit and page parameters are used to paginate LayersIntroducingVulnerability. The first
|
|
|
|
// given page should be VulnerabilityNotificationFirstPage. The function will then return the next
|
|
|
|
// availage page. If there is no more page, NoVulnerabilityNotificationPage has to be returned.
|
2016-01-26 22:57:32 +00:00
|
|
|
GetNotification(name string, limit int, page VulnerabilityNotificationPageNumber) (VulnerabilityNotification, VulnerabilityNotificationPageNumber, error)
|
2016-02-12 21:24:37 +00:00
|
|
|
|
|
|
|
// SetNotificationNotified marks a Notification as notified and thus, makes it unavailable for
|
|
|
|
// GetAvailableNotification, until the renotify duration is elapsed.
|
2016-01-21 23:09:23 +00:00
|
|
|
SetNotificationNotified(name string) error
|
2016-02-12 21:24:37 +00:00
|
|
|
|
|
|
|
// DeleteNotification marks a Notification as deleted, and thus, makes it unavailable for
|
|
|
|
// GetAvailableNotification.
|
2016-01-21 23:09:23 +00:00
|
|
|
DeleteNotification(name string) error
|
2015-11-16 21:22:16 +00:00
|
|
|
|
2016-02-12 21:24:37 +00:00
|
|
|
// # Key/Value
|
|
|
|
// InsertKeyValue stores or updates a simple key/value pair in the database.
|
2015-12-28 20:03:29 +00:00
|
|
|
InsertKeyValue(key, value string) error
|
2016-02-12 21:24:37 +00:00
|
|
|
|
|
|
|
// GetKeyValue retrieves a value from the database from the given key.
|
|
|
|
// It returns an empty string if there is no such key.
|
2015-12-28 20:03:29 +00:00
|
|
|
GetKeyValue(key string) (string, error)
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2016-02-12 21:24:37 +00:00
|
|
|
// # Lock
|
|
|
|
// Lock creates or renew a Lock in the database with the given name, owner and duration.
|
|
|
|
// After the specified duration, the Lock expires by itself if it hasn't been unlocked, and thus,
|
|
|
|
// let other users create a Lock with the same name. However, the owner can renew its Lock by
|
|
|
|
// setting renew to true. Lock should not block, it should instead returns whether the Lock has
|
|
|
|
// been successfully acquired/renewed. If it's the case, the expiration time of that Lock is
|
|
|
|
// returned as well.
|
2016-01-08 19:42:07 +00:00
|
|
|
Lock(name string, owner string, duration time.Duration, renew bool) (bool, time.Time)
|
2016-02-12 21:24:37 +00:00
|
|
|
|
|
|
|
// Unlock releases an existing Lock.
|
2016-01-08 19:42:07 +00:00
|
|
|
Unlock(name, owner string)
|
2016-02-12 21:24:37 +00:00
|
|
|
|
|
|
|
// FindLock returns the owner of a Lock specified by the name, and its experation time if it
|
|
|
|
// exists.
|
2016-01-08 19:42:07 +00:00
|
|
|
FindLock(name string) (string, time.Time, error)
|
2015-11-13 19:11:28 +00:00
|
|
|
|
2016-02-12 21:24:37 +00:00
|
|
|
// # Miscellaneous
|
|
|
|
// Ping returns the health status of the database.
|
2016-01-22 20:57:57 +00:00
|
|
|
Ping() bool
|
2016-02-12 21:24:37 +00:00
|
|
|
|
|
|
|
// Close closes the database and free any allocated resource.
|
2015-12-28 20:03:29 +00:00
|
|
|
Close()
|
2015-11-13 19:11:28 +00:00
|
|
|
}
|