Documentation ¶
Overview ¶
Package database implements every database models and the functions that manipulate them.
Index ¶
- Constants
- Variables
- func Close()
- func CountNotificationsToSend() (int, error)
- func DeleteVulnerability(id string) error
- func GetFlagValue(name string) (string, error)
- func Healthcheck() health.Status
- func InsertLayer(layer *Layer) error
- func InsertNotifications(notifications []Notification, wrapper NotificationWrapper) error
- func InsertPackages(packageParameters []*Package) error
- func Lock(name string, duration time.Duration, owner string) (bool, time.Time)
- func LockInfo(name string) (string, time.Time, error)
- func MarkNotificationAsSent(node string)
- func Open(dbType, dbPath string) error
- func Unlock(name, owner string)
- func UpdateFlag(name, value string) error
- type AbstractPackage
- type AbstractVulnerability
- type ByVersion
- type DefaultWrapper
- type Layer
- func FindAllLayersByAddedPackageNodes(nodes []string, selectedFields []string) ([]*Layer, error)
- func FindAllLayersIntroducingVulnerability(vulnerabilityID string, selectedFields []string) ([]*Layer, error)
- func FindOneLayerByID(ID string, selectedFields []string) (*Layer, error)
- func FindOneLayerByNode(node string, selectedFields []string) (*Layer, error)
- type NewVulnerabilityNotification
- type Notification
- type NotificationWrap
- type NotificationWrapper
- type Package
- func AbstractPackagesToPackages(abstractPackages []*AbstractPackage) (packages []*Package)
- func FindAllPackagesByBranch(OS, name string, selectedFields []string) ([]*Package, error)
- func FindAllPackagesByNodes(nodes []string, selectedFields []string) ([]*Package, error)
- func FindOnePackage(OS, name string, version types.Version, selectedFields []string) (*Package, error)
- func (p *Package) Branch() string
- func (p *Package) GetNode() string
- func (p *Package) Key() string
- func (p *Package) NextVersion(selectedFields []string) (*Package, error)
- func (p *Package) NextVersions(selectedFields []string) ([]*Package, error)
- func (p *Package) PreviousVersion(selectedFields []string) (*Package, error)
- func (p *Package) PreviousVersions(selectedFields []string) ([]*Package, error)
- type Vulnerability
- type VulnerabilityPackageChangedNotification
- type VulnerabilityPriorityIncreasedNotification
Constants ¶
const ( FieldLayerIsValue = "layer" FieldLayerID = "id" FieldLayerParent = "parent" FieldLayerSuccessors = "successors" FieldLayerOS = "os" FieldLayerInstalledPackages = "adds" FieldLayerRemovedPackages = "removes" FieldLayerEngineVersion = "engineVersion" FieldLayerPackages = "adds/removes" )
const ( FieldPackageIsValue = "package" FieldPackageOS = "os" FieldPackageName = "name" FieldPackageVersion = "version" FieldPackageNextVersion = "nextVersion" FieldPackagePreviousVersion = "previousVersion" )
const ( FieldVulnerabilityIsValue = "vulnerability" FieldVulnerabilityID = "id" FieldVulnerabilityLink = "link" FieldVulnerabilityPriority = "priority" FieldVulnerabilityDescription = "description" FieldVulnerabilityFixedIn = "fixedIn" )
const (
// FieldIs is the graph predicate defining the type of an entity.
FieldIs = "is"
)
Variables ¶
var ( // ErrTransaction is an error that occurs when a database transaction fails. ErrTransaction = errors.New("database: transaction failed (concurrent modification?)") // ErrBackendException is an error that occurs when the database backend does // not work properly (ie. unreachable). ErrBackendException = errors.New("database: could not query backend") // 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") // ErrCantOpen is an error that occurs when the database could not be opened ErrCantOpen = errors.New("database: could not open database") )
var DebianReleasesMapping = map[string]string{
"squeeze": "6",
"wheezy": "7",
"jessie": "8",
"stretch": "9",
"sid": "unstable",
"oldstable": "7",
"stable": "8",
"testing": "9",
"unstable": "unstable",
}
DebianReleasesMapping translates Debian code names and class names to version numbers TODO That should probably be stored in the database or in a file
var FieldLayerAll = []string{FieldLayerID, FieldLayerParent, FieldLayerSuccessors, FieldLayerOS, FieldLayerPackages, FieldLayerEngineVersion}
var FieldPackageAll = []string{FieldPackageOS, FieldPackageName, FieldPackageVersion, FieldPackageNextVersion, FieldPackagePreviousVersion}
var FieldVulnerabilityAll = []string{FieldVulnerabilityID, FieldVulnerabilityLink, FieldVulnerabilityPriority, FieldVulnerabilityDescription, FieldVulnerabilityFixedIn}
var UbuntuReleasesMapping = map[string]string{
"precise": "12.04",
"quantal": "12.10",
"raring": "13.04",
"trusty": "14.04",
"utopic": "14.10",
"vivid": "15.04",
"wily": "15.10",
}
UbuntuReleasesMapping translates Ubuntu code names to version numbers TODO That should probably be stored in the database or in a file
Functions ¶
func CountNotificationsToSend ¶
CountNotificationsToSend returns the number of pending notifications Note that it also count the locked notifications.
func DeleteVulnerability ¶
DeleteVulnerability deletes the vulnerability having the given ID
func GetFlagValue ¶
GetFlagValue returns the value of the flag given by its name (or an empty string if the flag does not exist)
func Healthcheck ¶
Healthcheck simply adds and then remove a quad in Cayley to ensure it is working It returns true when everything is ok
func InsertLayer ¶
InsertLayer insert a single layer in the database
ID, and EngineVersion fields are required. ParentNode, OS, InstalledPackagesNodes and RemovedPackagesNodes are optional, SuccessorsNodes is unnecessary.
The ID MUST be unique for two different layers.
If the Layer already exists, nothing is done, except if the provided engine version is higher than the existing one, in which case, the OS, InstalledPackagesNodes and RemovedPackagesNodes fields will be replaced.
The layer should only contains the newly installed/removed packages There is no safeguard that prevents from marking a package as newly installed while it has already been installed in one of its parent.
func InsertNotifications ¶
func InsertNotifications(notifications []Notification, wrapper NotificationWrapper) error
InsertNotifications stores multiple Notification in the database It uses the given NotificationWrapper to convert these notifications to something that can be stored in the database.
func InsertPackages ¶
InsertPackages inserts several packages in the database in one transaction Packages are stored in linked lists, one per Branch. Each linked list has a start package and an end package defined with types.MinVersion/types.MaxVersion versions
OS, Name and Version fields have to be specified. If the insertion is successfull, the Node field is filled and represents the graph node identifier.
func Lock ¶
Lock tries to set a temporary lock in the database. If a lock already exists with the given name/owner, then the lock is renewed
Lock does not block, instead, it returns true and its expiration time is the lock has been successfully acquired or false otherwise
func MarkNotificationAsSent ¶
func MarkNotificationAsSent(node string)
MarkNotificationAsSent marks a notification as sent.
func Unlock ¶
func Unlock(name, owner string)
Unlock unlocks a lock specified by its name if I own it
func UpdateFlag ¶
UpdateFlag creates a flag or update an existing flag's value
Types ¶
type AbstractPackage ¶
AbstractPackage is a package that abstract types.MaxVersion by modifying using a AllVersion boolean field and renaming Version to BeforeVersion which makes more sense for an usage with a Vulnerability
func PackagesToAbstractPackages ¶
func PackagesToAbstractPackages(packages []*Package) (abstractPackages []*AbstractPackage)
PackagesToAbstractPackages converts several Packages to AbstractPackages
type AbstractVulnerability ¶
type AbstractVulnerability struct { ID string Link string Priority types.Priority Description string AffectedPackages []*AbstractPackage }
AbstractVulnerability represents a Vulnerability as it is defined in the database package but exposes directly a list of AbstractPackage instead of nodes to packages.
func (*AbstractVulnerability) ToVulnerability ¶
func (av *AbstractVulnerability) ToVulnerability(fixedInNodes []string) *Vulnerability
ToVulnerability converts an abstractVulnerability into a Vulnerability
type ByVersion ¶
type ByVersion []*Package
ByVersion implements sort.Interface for []*Package based on the Version field It uses github.com/quentin-m/dpkgcomp internally and makes use of types.MinVersion/types.MaxVersion
type DefaultWrapper ¶
type DefaultWrapper struct{}
DefaultWrapper is an implementation of NotificationWrapper that supports NewVulnerabilityNotification notifications.
func (*DefaultWrapper) Unwrap ¶
func (w *DefaultWrapper) Unwrap(nw *NotificationWrap) (Notification, error)
func (*DefaultWrapper) Wrap ¶
func (w *DefaultWrapper) Wrap(n Notification) (*NotificationWrap, error)
type Layer ¶
type Layer struct { Node string `json:"-"` ID string ParentNode string `json:"-"` SuccessorsNodes []string `json:"-"` OS string InstalledPackagesNodes []string `json:"-"` RemovedPackagesNodes []string `json:"-"` EngineVersion int }
Layer represents an unique container layer
func FindAllLayersByAddedPackageNodes ¶
FindAllLayersByAddedPackageNodes finds and returns all layers that add the given packages (by their nodes), selecting the specified fields
func FindAllLayersIntroducingVulnerability ¶
func FindAllLayersIntroducingVulnerability(vulnerabilityID string, selectedFields []string) ([]*Layer, error)
FindAllLayersIntroducingVulnerability finds and returns the list of layers that introduce the given vulnerability (by its ID), selecting the specified fields
func FindOneLayerByID ¶
FindOneLayerByID finds and returns a single layer having the given ID, selecting the specified fields and hardcoding its ID
func FindOneLayerByNode ¶
FindOneLayerByNode finds and returns a single package by its node, selecting the specified fields
func (*Layer) AllPackages ¶
AllPackages computes the full list of packages that l has and return them as nodes. It requires that FieldLayerParent, FieldLayerContentInstalledPackages, FieldLayerContentRemovedPackages fields has been selected on l
func (*Layer) OperatingSystem ¶
OperatingSystem tries to find the Operating System of a layer using its parents. It requires that FieldLayerParent and FieldLayerOS fields has been selected on l
type NewVulnerabilityNotification ¶
type NewVulnerabilityNotification struct {
VulnerabilityID string
}
A NewVulnerabilityNotification is a notification that informs about a new vulnerability and contains all the layers that introduce that vulnerability
func (*NewVulnerabilityNotification) GetContent ¶
func (n *NewVulnerabilityNotification) GetContent() (interface{}, error)
func (*NewVulnerabilityNotification) GetName ¶
func (n *NewVulnerabilityNotification) GetName() string
func (*NewVulnerabilityNotification) GetType ¶
func (n *NewVulnerabilityNotification) GetType() string
type Notification ¶
type Notification interface { // GetName returns the explicit (humanly meaningful) name of a notification. GetName() string // GetType returns the type of a notification, which is used by a // NotificationWrapper to determine the concrete type of a Notification. GetType() string // GetContent returns the content of the notification. GetContent() (interface{}, error) }
A Notification defines an interface to a message that can be sent by a notifier.Notifier. A NotificationWrapper has to be used to convert it into a NotificationWrap, which can be stored in the database.
func FindOneNotificationToSend ¶
func FindOneNotificationToSend(wrapper NotificationWrapper) (string, Notification, error)
FindOneNotificationToSend finds and returns a notification that is not sent yet and not locked. Returns nil if there is none.
func InsertVulnerabilities ¶
func InsertVulnerabilities(vulnerabilities []*Vulnerability) ([]Notification, error)
InsertVulnerabilities inserts or updates several vulnerabilities in the database in one transaction It ensures that a vulnerability can't be fixed by two packages belonging the same Branch. During an update, if the vulnerability was previously fixed by a version in a branch and a new package of that branch is specified, the previous one is deleted Otherwise, it simply adds the defined packages, there is currently no way to delete affected packages.
ID, Link, Priority and FixedInNodes fields have to be specified. Description is optionnal.
type NotificationWrap ¶
A NotificationWrap wraps a Notification into something that can be stored in the database. A NotificationWrapper has to be used to convert it into a Notification.
type NotificationWrapper ¶
type NotificationWrapper interface { // Wrap packs a Notification instance into a new NotificationWrap. Wrap(n Notification) (*NotificationWrap, error) // Unwrap unpacks an instance of NotificationWrap into a new Notification. Unwrap(nw *NotificationWrap) (Notification, error) }
NotificationWrapper is an interface defined how to convert a Notification to a NotificationWrap object and vice-versa.
func GetDefaultNotificationWrapper ¶
func GetDefaultNotificationWrapper() NotificationWrapper
GetDefaultNotificationWrapper returns the default wrapper
type Package ¶
type Package struct { Node string `json:"-"` OS string Name string Version types.Version NextVersionNode string `json:"-"` PreviousVersionNode string `json:"-"` }
Package represents a package
func AbstractPackagesToPackages ¶
func AbstractPackagesToPackages(abstractPackages []*AbstractPackage) (packages []*Package)
AbstractPackagesToPackages converts several AbstractPackages to Packages
func FindAllPackagesByBranch ¶
FindAllPackagesByBranch finds and returns all packages that belong to the given Branch, selecting the specified fields
func FindAllPackagesByNodes ¶
FindAllPackagesByNodes finds and returns all packages given by their nodes, selecting the specified fields
func FindOnePackage ¶
func FindOnePackage(OS, name string, version types.Version, selectedFields []string) (*Package, error)
FindOnePackage finds and returns a single package having the given OS, name and version, selecting the specified fields
func (*Package) Branch ¶
Branch returns an unique string defined the Branch of p (os, name) Requires the key fields: OS, Name
func (*Package) GetNode ¶
GetNode returns an unique identifier for the graph node Requires the key fields: OS, Name, Version
func (*Package) Key ¶
Key returns an unique string defining p Requires the key fields: OS, Name, Version
func (*Package) NextVersion ¶
NextVersion find and returns the package of the same branch that has a higher version number, selecting the specified fields It requires that FieldPackageNextVersion field has been selected on p
func (*Package) NextVersions ¶
NextVersions find and returns all the packages of the same branch that have a higher version number, selecting the specified fields It requires that FieldPackageNextVersion field has been selected on p The immediate higher version is listed first, and the special end-of-Branch package is last, p is not listed
func (*Package) PreviousVersion ¶
PreviousVersion find and returns the package of the same branch that has an immediate lower version number, selecting the specified fields It requires that FieldPackagePreviousVersion field has been selected on p
func (*Package) PreviousVersions ¶
PreviousVersions find and returns all the packages of the same branch that have a lower version number, selecting the specified fields It requires that FieldPackageNextVersion field has been selected on p The immediate lower version is listed first, and the special start-of-Branch package is last, p is not listed
type Vulnerability ¶
type Vulnerability struct { Node string `json:"-"` ID string Link string Priority types.Priority Description string `json:",omitempty"` FixedInNodes []string `json:"-"` }
Vulnerability represents a vulnerability that is fixed in some Packages
func FindAllVulnerabilitiesByFixedIn ¶
func FindAllVulnerabilitiesByFixedIn(nodes []string, selectedFields []string) ([]*Vulnerability, error)
FindAllVulnerabilitiesByFixedIn finds and returns all vulnerabilities that are fixed in the given packages (speficied by their nodes), selecting the specified fields
func FindOneVulnerability ¶
func FindOneVulnerability(id string, selectedFields []string) (*Vulnerability, error)
FindOneVulnerability finds and returns a single vulnerability having the given ID selecting the specified fields
func (*Vulnerability) GetNode ¶
func (v *Vulnerability) GetNode() string
GetNode returns an unique identifier for the graph node Requires the key field: ID
func (*Vulnerability) ToAbstractVulnerability ¶
func (v *Vulnerability) ToAbstractVulnerability() (*AbstractVulnerability, error)
ToAbstractVulnerability converts a Vulnerability into an AbstractVulnerability.
type VulnerabilityPackageChangedNotification ¶
type VulnerabilityPackageChangedNotification struct { VulnerabilityID string AddedFixedInNodes, RemovedFixedInNodes []string }
A VulnerabilityPackageChangedNotification is a notification that informs that an existing vulnerability's fixed package list has been updated and may not affect some layers anymore or may affect new layers.
func (*VulnerabilityPackageChangedNotification) GetContent ¶
func (n *VulnerabilityPackageChangedNotification) GetContent() (interface{}, error)
func (*VulnerabilityPackageChangedNotification) GetName ¶
func (n *VulnerabilityPackageChangedNotification) GetName() string
func (*VulnerabilityPackageChangedNotification) GetType ¶
func (n *VulnerabilityPackageChangedNotification) GetType() string
type VulnerabilityPriorityIncreasedNotification ¶
type VulnerabilityPriorityIncreasedNotification struct { VulnerabilityID string OldPriority, NewPriority types.Priority }
A VulnerabilityPriorityIncreasedNotification is a notification that informs about the fact that the priority of a vulnerability increased vulnerability and contains all the layers that introduce that vulnerability.
func (*VulnerabilityPriorityIncreasedNotification) GetContent ¶
func (n *VulnerabilityPriorityIncreasedNotification) GetContent() (interface{}, error)
func (*VulnerabilityPriorityIncreasedNotification) GetName ¶
func (n *VulnerabilityPriorityIncreasedNotification) GetName() string
func (*VulnerabilityPriorityIncreasedNotification) GetType ¶
func (n *VulnerabilityPriorityIncreasedNotification) GetType() string