database

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2015 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package database implements every database models and the functions that manipulate them.

Index

Constants

View Source
const (
	FieldLayerIsValue           = "layer"
	FieldLayerID                = "id"
	FieldLayerParent            = "parent"
	FieldLayerSuccessors        = "successors"
	FieldLayerOS                = "os"
	FieldLayerInstalledPackages = "adds"
	FieldLayerRemovedPackages   = "removes"
	FieldLayerEngineVersion     = "engineVersion"

	FieldLayerPackages = "adds/removes"
)
View Source
const (
	FieldPackageIsValue         = "package"
	FieldPackageOS              = "os"
	FieldPackageName            = "name"
	FieldPackageVersion         = "version"
	FieldPackageNextVersion     = "nextVersion"
	FieldPackagePreviousVersion = "previousVersion"
)
View Source
const (
	FieldVulnerabilityIsValue     = "vulnerability"
	FieldVulnerabilityID          = "id"
	FieldVulnerabilityLink        = "link"
	FieldVulnerabilityPriority    = "priority"
	FieldVulnerabilityDescription = "description"
	FieldVulnerabilityFixedIn     = "fixedIn"
)
View Source
const (
	// FieldIs is the graph predicate defining the type of an entity.
	FieldIs = "is"
)

Variables

View Source
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")
)
View Source
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

View Source
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 Close

func Close()

Close closes a Cayley database

func CountNotificationsToSend

func CountNotificationsToSend() (int, error)

CountNotificationsToSend returns the number of pending notifications Note that it also count the locked notifications.

func DeleteVulnerability

func DeleteVulnerability(id string) error

DeleteVulnerability deletes the vulnerability having the given ID

func GetFlagValue

func GetFlagValue(name string) (string, error)

GetFlagValue returns the value of the flag given by its name (or an empty string if the flag does not exist)

func Healthcheck

func Healthcheck() health.Status

Healthcheck simply adds and then remove a quad in Cayley to ensure it is working It returns true when everything is ok

func InsertLayer

func InsertLayer(layer *Layer) error

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

func InsertPackages(packageParameters []*Package) error

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

func Lock(name string, duration time.Duration, owner string) (bool, time.Time)

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 LockInfo

func LockInfo(name string) (string, time.Time, error)

LockInfo returns the owner of a lock specified by its name and its expiration time

func MarkNotificationAsSent

func MarkNotificationAsSent(node string)

MarkNotificationAsSent marks a notification as sent.

func Open

func Open(dbType, dbPath string) error

Open opens a Cayley database, creating it if necessary and return its handle

func Unlock

func Unlock(name, owner string)

Unlock unlocks a lock specified by its name if I own it

func UpdateFlag

func UpdateFlag(name, value string) error

UpdateFlag creates a flag or update an existing flag's value

Types

type AbstractPackage

type AbstractPackage struct {
	OS   string
	Name string

	AllVersions   bool
	BeforeVersion types.Version
}

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

func (ByVersion) Len

func (p ByVersion) Len() int

func (ByVersion) Less

func (p ByVersion) Less(i, j int) bool

func (ByVersion) Swap

func (p ByVersion) Swap(i, j int)

type DefaultWrapper

type DefaultWrapper struct{}

DefaultWrapper is an implementation of NotificationWrapper that supports NewVulnerabilityNotification notifications.

func (*DefaultWrapper) Unwrap

func (*DefaultWrapper) Wrap

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

func FindAllLayersByAddedPackageNodes(nodes []string, selectedFields []string) ([]*Layer, error)

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

func FindOneLayerByID(ID string, selectedFields []string) (*Layer, error)

FindOneLayerByID finds and returns a single layer having the given ID, selecting the specified fields and hardcoding its ID

func FindOneLayerByNode

func FindOneLayerByNode(node string, selectedFields []string) (*Layer, error)

FindOneLayerByNode finds and returns a single package by its node, selecting the specified fields

func (*Layer) AllPackages

func (l *Layer) AllPackages() ([]string, error)

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) GetNode

func (l *Layer) GetNode() string

GetNode returns the node name of a Layer Requires the key field: ID

func (*Layer) OperatingSystem

func (l *Layer) OperatingSystem() (string, error)

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

func (*Layer) Parent

func (l *Layer) Parent(selectedFields []string) (*Layer, error)

Parent find and returns the parent layer of l, selecting the specified fields It requires that FieldLayerParent field 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

type NotificationWrap struct {
	Type string
	Data string
}

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

func FindAllPackagesByBranch(OS, name string, selectedFields []string) ([]*Package, error)

FindAllPackagesByBranch finds and returns all packages that belong to the given Branch, selecting the specified fields

func FindAllPackagesByNodes

func FindAllPackagesByNodes(nodes []string, selectedFields []string) ([]*Package, error)

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

func (p *Package) Branch() string

Branch returns an unique string defined the Branch of p (os, name) Requires the key fields: OS, Name

func (*Package) GetNode

func (p *Package) GetNode() string

GetNode returns an unique identifier for the graph node Requires the key fields: OS, Name, Version

func (*Package) Key

func (p *Package) Key() string

Key returns an unique string defining p Requires the key fields: OS, Name, Version

func (*Package) NextVersion

func (p *Package) NextVersion(selectedFields []string) (*Package, error)

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

func (p *Package) NextVersions(selectedFields []string) ([]*Package, error)

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

func (p *Package) PreviousVersion(selectedFields []string) (*Package, error)

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

func (p *Package) PreviousVersions(selectedFields []string) ([]*Package, error)

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 (*VulnerabilityPackageChangedNotification) GetType

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 (*VulnerabilityPriorityIncreasedNotification) GetType

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL