database

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const SchemaVersion = 1

Variables

This section is empty.

Functions

func Exists

func Exists(path string) bool

func VersionFromPURL added in v0.7.0

func VersionFromPURL(p string) string

VersionFromPURL extracts the decoded version string from a PURL.

PURL percent-encodes characters that are not safe in a path component, so a Debian version like "7.91+dfsg1-2ubuntu0.1" is stored as "pkg:deb/nmap@7.91%2Bdfsg1-2ubuntu0.1". The raw substring after "@" is therefore not the version: it must be percent-decoded before being displayed or used to build a URL, otherwise "%2B" leaks into the UI and round-tripping the value back into a PURL double-encodes it.

e.g., "pkg:npm/lodash@4.17.21" -> "4.17.21"

Types

type Artifact

type Artifact struct {
	ID             int64          `db:"id" json:"id"`
	VersionPURL    string         `db:"version_purl" json:"version_purl"`
	Filename       string         `db:"filename" json:"filename"`
	UpstreamURL    string         `db:"upstream_url" json:"upstream_url"`
	StoragePath    sql.NullString `db:"storage_path" json:"storage_path,omitempty"`
	ContentHash    sql.NullString `db:"content_hash" json:"content_hash,omitempty"`
	Size           sql.NullInt64  `db:"size" json:"size,omitempty"`
	ContentType    sql.NullString `db:"content_type" json:"content_type,omitempty"`
	FetchedAt      sql.NullTime   `db:"fetched_at" json:"fetched_at,omitempty"`
	HitCount       int64          `db:"hit_count" json:"hit_count"`
	LastAccessedAt sql.NullTime   `db:"last_accessed_at" json:"last_accessed_at,omitempty"`
	CreatedAt      time.Time      `db:"created_at" json:"created_at"`
	UpdatedAt      time.Time      `db:"updated_at" json:"updated_at"`
}

Artifact represents a cached artifact in the database. This table is proxy-specific and not part of git-pkgs.

func (*Artifact) IsCached

func (a *Artifact) IsCached() bool

IsCached returns true if the artifact has been fetched and stored locally.

type CacheStats

type CacheStats struct {
	TotalPackages   int64
	TotalVersions   int64
	TotalArtifacts  int64
	TotalSize       int64
	TotalHits       int64
	EcosystemCounts map[string]int64
}

type CachedArtifact added in v0.7.0

type CachedArtifact struct {
	Ecosystem   string
	StoragePath string
	Artifact    artifacts.Artifact
	Integrity   sql.NullString
}

CachedArtifact contains the fields needed to serve a cached artifact.

type DB

type DB struct {
	*sqlx.DB
	// contains filtered or unexported fields
}

func Create

func Create(path string) (*DB, error)

func Open

func Open(path string) (*DB, error)

func OpenOrCreate

func OpenOrCreate(path string) (*DB, error)

func OpenPostgres

func OpenPostgres(url string) (*DB, error)

func OpenPostgresOrCreate

func OpenPostgresOrCreate(url string) (*DB, error)

func (*DB) ClearArtifactCache

func (db *DB) ClearArtifactCache(versionPURL, filename, storagePath string) (bool, error)

ClearArtifactCache marks an artifact uncached if its record still points at storagePath, and reports whether it did. A record a newer fetch has moved elsewhere is left alone, so a clear never orphans the object that fetch committed.

func (*DB) CountCachedPackages

func (db *DB) CountCachedPackages(ecosystem string) (int64, error)

func (*DB) CountSearchResults

func (db *DB) CountSearchResults(query string, ecosystem string) (int64, error)

func (*DB) CreateSchema

func (db *DB) CreateSchema() error

func (*DB) DeleteVulnerabilitiesForPackage

func (db *DB) DeleteVulnerabilitiesForPackage(ecosystem, name string) error

func (*DB) Dialect

func (db *DB) Dialect() Dialect

func (*DB) DiscardArtifact added in v0.9.0

func (db *DB) DiscardArtifact(versionPURL, filename, storagePath string) error

DiscardArtifact clears the record as ClearArtifactCache does and queues storagePath for deletion, for callers that must not delete an object another request may be reading.

func (*DB) EnsureArtifactsTable

func (db *DB) EnsureArtifactsTable() error

EnsureArtifactsTable adds the artifacts table to an existing database (e.g., a git-pkgs database) if it doesn't already exist.

func (*DB) EnsureMetadataCacheTable

func (db *DB) EnsureMetadataCacheTable() error

EnsureMetadataCacheTable creates the metadata_cache table if it doesn't exist.

func (*DB) GetArtifact

func (db *DB) GetArtifact(versionPURL, filename string) (*Artifact, error)

func (*DB) GetArtifactByPath

func (db *DB) GetArtifactByPath(storagePath string) (*Artifact, error)

func (*DB) GetArtifactsByVersionPURL

func (db *DB) GetArtifactsByVersionPURL(versionPURL string) ([]Artifact, error)

func (*DB) GetCacheStats

func (db *DB) GetCacheStats() (*CacheStats, error)

func (*DB) GetCachedArtifact added in v0.7.0

func (db *DB) GetCachedArtifact(packagePURL, versionPURL, filename string) (*CachedArtifact, error)

GetCachedArtifact returns the fields needed to serve a cached artifact.

func (*DB) GetCachedArtifactCount

func (db *DB) GetCachedArtifactCount() (int64, error)

func (*DB) GetDuePendingDeletes added in v0.9.0

func (db *DB) GetDuePendingDeletes(cutoff time.Time, limit int) ([]string, error)

GetDuePendingDeletes returns up to limit paths queued before cutoff, oldest first. A path a record points at again is skipped.

func (*DB) GetEnrichmentStats

func (db *DB) GetEnrichmentStats() (*EnrichmentStats, error)

func (*DB) GetLeastRecentlyUsedArtifacts

func (db *DB) GetLeastRecentlyUsedArtifacts(limit int) ([]Artifact, error)

func (*DB) GetMetadataCache

func (db *DB) GetMetadataCache(ecosystem, name string) (*MetadataCacheEntry, error)

func (*DB) GetMostPopularPackages

func (db *DB) GetMostPopularPackages(limit int) ([]PopularPackage, error)

func (*DB) GetPackageByEcosystemName

func (db *DB) GetPackageByEcosystemName(ecosystem, name string) (*Package, error)

func (*DB) GetPackageByPURL

func (db *DB) GetPackageByPURL(purl string) (*Package, error)

func (*DB) GetPackagesNeedingVulnSync

func (db *DB) GetPackagesNeedingVulnSync(limit int, minAge time.Duration) ([]Package, error)

func (*DB) GetRecentlyCachedPackages

func (db *DB) GetRecentlyCachedPackages(limit int) ([]RecentPackage, error)

func (*DB) GetTotalCacheSize

func (db *DB) GetTotalCacheSize() (int64, error)

func (*DB) GetVersionByPURL

func (db *DB) GetVersionByPURL(purl string) (*Version, error)

func (*DB) GetVersionsByPackagePURL

func (db *DB) GetVersionsByPackagePURL(packagePURL string) ([]Version, error)

func (*DB) GetVulnCountForPackage

func (db *DB) GetVulnCountForPackage(ecosystem, name string) (int64, error)

func (*DB) GetVulnerabilitiesForPackage

func (db *DB) GetVulnerabilitiesForPackage(ecosystem, name string) ([]Vulnerability, error)

func (*DB) GetVulnsSyncedAt

func (db *DB) GetVulnsSyncedAt(ecosystem, name string) (time.Time, error)

func (*DB) HasColumn

func (db *DB) HasColumn(table, column string) (bool, error)

HasColumn checks if a column exists in a table.

func (*DB) HasTable

func (db *DB) HasTable(name string) (bool, error)

HasTable checks if a table exists in the database.

func (*DB) ListCachedPackages

func (db *DB) ListCachedPackages(ecosystem string, sortBy string, limit int, offset int) ([]PackageListItem, error)

func (*DB) MarkArtifactCached

func (db *DB) MarkArtifactCached(versionPURL, filename, storagePath, contentHash string, size int64, contentType string) error

func (*DB) MigrateSchema

func (db *DB) MigrateSchema() error

MigrateSchema applies any unapplied migrations in order. For a fully migrated database this executes a single SELECT query.

func (*DB) OptimizeForBulkWrites

func (db *DB) OptimizeForBulkWrites() error

func (*DB) OptimizeForReads

func (db *DB) OptimizeForReads() error

func (*DB) Path

func (db *DB) Path() string

func (*DB) QueuePendingDelete added in v0.9.0

func (db *DB) QueuePendingDelete(path string) error

QueuePendingDelete queues a storage path no record points at any more. Queueing a path again, as reclaim does after a failed delete, restarts its grace period and moves it behind the rest.

func (*DB) Rebind

func (db *DB) Rebind(query string) string

func (*DB) RecordArtifactHit

func (db *DB) RecordArtifactHit(versionPURL, filename string) error

func (*DB) RemovePendingDelete added in v0.9.0

func (db *DB) RemovePendingDelete(path string) error

RemovePendingDelete drops a deleted path from the queue.

func (*DB) SchemaVersion

func (db *DB) SchemaVersion() (int, error)

func (*DB) SearchPackages

func (db *DB) SearchPackages(query string, ecosystem string, limit int, offset int) ([]SearchResult, error)

func (*DB) SetVersionPublishedAt added in v0.8.0

func (db *DB) SetVersionPublishedAt(versionPURL, packagePURL string, publishedAt time.Time) error

SetVersionPublishedAt records a version's publish time, creating the versions row if the proxy has not seen the version yet. It only writes published_at, so it never disturbs enrichment data on an existing row.

func (*DB) SetVulnsSyncedAt

func (db *DB) SetVulnsSyncedAt(ecosystem, name string) error

func (*DB) UpsertArtifact

func (db *DB) UpsertArtifact(a *Artifact) error

UpsertArtifact records a. A storage path the record stops pointing at is queued for deletion, since each fetch stores its own object and nothing else would remove it. The write applies only if the record still holds the path read before it, so commits racing on one artifact each queue the path they replaced.

func (*DB) UpsertMetadataCache

func (db *DB) UpsertMetadataCache(entry *MetadataCacheEntry) error

func (*DB) UpsertPackage

func (db *DB) UpsertPackage(pkg *Package) error

func (*DB) UpsertVersion

func (db *DB) UpsertVersion(v *Version) error

func (*DB) UpsertVulnerability

func (db *DB) UpsertVulnerability(v *Vulnerability) error

type Dialect

type Dialect string
const (
	DialectSQLite   Dialect = "sqlite"
	DialectPostgres Dialect = "postgres"
)

type EnrichmentStats

type EnrichmentStats struct {
	TotalPackages        int64
	EnrichedPackages     int64
	VulnSyncedPackages   int64
	TotalVulnerabilities int64
	CriticalVulns        int64
	HighVulns            int64
	MediumVulns          int64
	LowVulns             int64
}

type MetadataCacheEntry

type MetadataCacheEntry struct {
	ID              int64          `db:"id" json:"id"`
	Ecosystem       string         `db:"ecosystem" json:"ecosystem"`
	Name            string         `db:"name" json:"name"`
	StoragePath     string         `db:"storage_path" json:"storage_path"`
	ETag            sql.NullString `db:"etag" json:"etag,omitempty"`
	Link            sql.NullString `db:"link" json:"link,omitempty"`
	ContentType     sql.NullString `db:"content_type" json:"content_type,omitempty"`
	ContentEncoding sql.NullString `db:"content_encoding" json:"content_encoding,omitempty"`
	ContentDigest   sql.NullString `db:"content_digest" json:"content_digest,omitempty"`
	Size            sql.NullInt64  `db:"size" json:"size,omitempty"`
	LastModified    sql.NullTime   `db:"last_modified" json:"last_modified,omitempty"`
	FetchedAt       sql.NullTime   `db:"fetched_at" json:"fetched_at,omitempty"`
	CreatedAt       time.Time      `db:"created_at" json:"created_at"`
	UpdatedAt       time.Time      `db:"updated_at" json:"updated_at"`
}

MetadataCacheEntry represents a cached metadata blob for offline serving.

type Package

type Package struct {
	ID            int64          `db:"id" json:"id"`
	PURL          string         `db:"purl" json:"purl"`
	Ecosystem     string         `db:"ecosystem" json:"ecosystem"`
	Name          string         `db:"name" json:"name"`
	LatestVersion sql.NullString `db:"latest_version" json:"latest_version,omitempty"`
	License       sql.NullString `db:"license" json:"license,omitempty"`
	Description   sql.NullString `db:"description" json:"description,omitempty"`
	Homepage      sql.NullString `db:"homepage" json:"homepage,omitempty"`
	RepositoryURL sql.NullString `db:"repository_url" json:"repository_url,omitempty"`
	RegistryURL   sql.NullString `db:"registry_url" json:"registry_url,omitempty"`
	SupplierName  sql.NullString `db:"supplier_name" json:"supplier_name,omitempty"`
	SupplierType  sql.NullString `db:"supplier_type" json:"supplier_type,omitempty"`
	Source        sql.NullString `db:"source" json:"source,omitempty"`
	EnrichedAt    sql.NullTime   `db:"enriched_at" json:"enriched_at,omitempty"`
	VulnsSyncedAt sql.NullTime   `db:"vulns_synced_at" json:"vulns_synced_at,omitempty"`
	CreatedAt     time.Time      `db:"created_at" json:"created_at"`
	UpdatedAt     time.Time      `db:"updated_at" json:"updated_at"`
}

Package represents a package in the database. Schema is compatible with git-pkgs.

type PackageListItem

type PackageListItem struct {
	Ecosystem     string         `db:"ecosystem"`
	Name          string         `db:"name"`
	LatestVersion sql.NullString `db:"latest_version"`
	License       sql.NullString `db:"license"`
	Hits          int64          `db:"hits"`
	Size          int64          `db:"size"`
	CachedAt      sql.NullString `db:"cached_at"`
	VulnCount     int64          `db:"vuln_count"`
}

type PopularPackage

type PopularPackage struct {
	Ecosystem string `db:"ecosystem"`
	Name      string `db:"name"`
	Hits      int64  `db:"hits"`
	Size      int64  `db:"size"`
}

type RecentPackage

type RecentPackage struct {
	Ecosystem   string    `db:"ecosystem"`
	Name        string    `db:"name"`
	VersionPURL string    `db:"version_purl"`
	CachedAt    time.Time `db:"fetched_at"`
	Size        int64     `db:"size"`
	// Version is derived from VersionPURL rather than selected, so that the
	// PURL percent-encoding is decoded (e.g. "%2B" back to "+").
	Version string `db:"-"`
}

type SearchResult

type SearchResult struct {
	Ecosystem     string         `db:"ecosystem"`
	Name          string         `db:"name"`
	LatestVersion sql.NullString `db:"latest_version"`
	License       sql.NullString `db:"license"`
	Hits          int64          `db:"hits"`
	Size          int64          `db:"size"`
	CachedAt      sql.NullString `db:"cached_at"`
}

type Version

type Version struct {
	ID          int64          `db:"id" json:"id"`
	PURL        string         `db:"purl" json:"purl"`
	PackagePURL string         `db:"package_purl" json:"package_purl"`
	License     sql.NullString `db:"license" json:"license,omitempty"`
	PublishedAt sql.NullTime   `db:"published_at" json:"published_at,omitempty"`
	Integrity   sql.NullString `db:"integrity" json:"integrity,omitempty"`
	Yanked      bool           `db:"yanked" json:"yanked"`
	Source      sql.NullString `db:"source" json:"source,omitempty"`
	EnrichedAt  sql.NullTime   `db:"enriched_at" json:"enriched_at,omitempty"`
	CreatedAt   time.Time      `db:"created_at" json:"created_at"`
	UpdatedAt   time.Time      `db:"updated_at" json:"updated_at"`
}

Version represents a package version in the database. Schema is compatible with git-pkgs.

func (*Version) DisplayPURL added in v0.7.0

func (v *Version) DisplayPURL() string

DisplayPURL returns the PURL with its path components percent-decoded, for showing in the UI. The stored PURL keeps the canonical encoding (which is what the API and all lookups use); this is only a readable rendering, so that a version like "7.91+dfsg1-2ubuntu0.1" is not shown as "7.91%2Bdfsg1-2ubuntu0.1" and an npm scope is shown as "@babel" rather than "%40babel". Qualifiers and subpath keep their encoding, since decoding those would be ambiguous.

func (*Version) EscapedVersion added in v0.7.0

func (v *Version) EscapedVersion() string

EscapedVersion returns the version escaped for use as a single URL path segment.

Version returns decoded text, which is what should be shown to a user but is not safe to drop into a link: html/template preserves reserved characters and existing escapes in a URL, so "release/1" would split into two path segments, "v1?build" would start a query string, and a literal "%2B" would be read back as "+". Escaping here and decoding in splitWildcardPath round-trips the value, so the link resolves to the version that was stored.

func (*Version) Version

func (v *Version) Version() string

Version extracts the version string from the PURL. e.g., "pkg:npm/lodash@4.17.21" -> "4.17.21"

type Vulnerability

type Vulnerability struct {
	ID           int64           `db:"id" json:"id"`
	VulnID       string          `db:"vuln_id" json:"vuln_id"`
	Ecosystem    string          `db:"ecosystem" json:"ecosystem"`
	PackageName  string          `db:"package_name" json:"package_name"`
	Severity     sql.NullString  `db:"severity" json:"severity,omitempty"`
	Summary      sql.NullString  `db:"summary" json:"summary,omitempty"`
	FixedVersion sql.NullString  `db:"fixed_version" json:"fixed_version,omitempty"`
	CVSSScore    sql.NullFloat64 `db:"cvss_score" json:"cvss_score,omitempty"`
	References   sql.NullString  `db:"references" json:"references,omitempty"`
	FetchedAt    sql.NullTime    `db:"fetched_at" json:"fetched_at,omitempty"`
	CreatedAt    time.Time       `db:"created_at" json:"created_at"`
	UpdatedAt    time.Time       `db:"updated_at" json:"updated_at"`
}

Vulnerability represents a cached vulnerability record.

Jump to

Keyboard shortcuts

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