dtrack

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: AGPL-3.0 Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dtrack pushes OpenArtifact's SBOMs and VEX decisions to a Dependency-Track server (https://dependencytrack.org) and records how far each push got. See docs/security/dependency-track.md.

Index

Constants

View Source
const (
	PermBOMUpload       = "BOM_UPLOAD"
	PermProjectCreation = "PROJECT_CREATION_UPLOAD"
	PermViewPortfolio   = "VIEW_PORTFOLIO"
	PermViewVuln        = "VIEW_VULNERABILITY"
	PermVulnAnalysis    = "VULNERABILITY_ANALYSIS"
)

Permissions the integration uses; Check reports which ones the API key lacks.

View Source
const (
	DefaultProjectName    = "{repo}/{name}"
	DefaultProjectVersion = "{version}"
)

Default project naming: one Dependency-Track project per artifact, "<repository>/<name>", at the artifact's version.

View Source
const (
	SourceNone       = "none"
	SourceGlobal     = "global"
	SourceRepository = "repository"
)

Sources of an effective configuration.

View Source
const RequestKind = "dtrack:push"

RequestKind is the kind of the worker request that asks for a push.

View Source
const SettingGlobal = "dependency_track.global"

SettingGlobal is the system_settings key of the global configuration.

Variables

View Source
var ErrCrossOriginRedirect = errors.New("dependency-track: redirect to another origin refused")

ErrCrossOriginRedirect is returned when Dependency-Track answers with a redirect to another origin; the API key is never sent there.

View Source
var ErrInvalidURL = errors.New("dependency-track URL must be an absolute http(s) URL")

ErrInvalidURL is returned for a base URL that is not an absolute http(s) URL.

View Source
var ErrNotFound = errors.New("dependency-track: not found")

ErrNotFound is returned when Dependency-Track answers 404.

Functions

func ArtifactIDFromPayload

func ArtifactIDFromPayload(payload map[string]any) string

ArtifactIDFromPayload is the "name@version" id of an artifact event, "" when the event names no artifact.

func Render

func Render(tmpl, def string, v ProjectVars) string

Render expands tmpl (default def when empty) with vars and bounds the result. A "unknown" version renders empty.

func SaveGlobal

func SaveGlobal(ctx context.Context, db SettingsStore, s Settings) error

SaveGlobal stores the global settings, encrypting the API key.

func ValidateTemplate

func ValidateTemplate(tmpl string) error

ValidateTemplate checks that tmpl only uses the known placeholders ({repo}, {name}, {version}, {artifact_id}, {format}), has no stray braces and fits Dependency-Track's 255 character limit with a margin for the values.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client talks to the REST API of one Dependency-Track server.

func NewClient

func NewClient(baseURL, apiKey string) (*Client, error)

NewClient validates baseURL and returns a client that refuses to connect to internal addresses (netutil's SSRF guard, enforced at dial time).

func (*Client) Check

func (c *Client) Check(ctx context.Context, needVEX bool) (*Info, error)

Check verifies the server answers and the API key is accepted, and reports the key's permissions against the ones the integration needs.

func (*Client) Findings

func (c *Client) Findings(ctx context.Context, projectUUID string) ([]Finding, error)

Findings lists the project's findings, suppressed ones included.

func (*Client) Processing

func (c *Client) Processing(ctx context.Context, token string) (bool, error)

Processing reports whether Dependency-Track still processes the upload behind token.

func (*Client) ProjectUUID

func (c *Client) ProjectUUID(ctx context.Context, name, version string) (string, error)

ProjectUUID resolves a project by name and version (ErrNotFound when absent).

func (*Client) UploadBOM

func (c *Client) UploadBOM(ctx context.Context, projectName, projectVersion string, bom []byte) (string, error)

UploadBOM uploads a CycloneDX BOM, creating the project when it does not exist. It returns the processing token.

func (*Client) UploadVEX

func (c *Client) UploadVEX(ctx context.Context, projectUUID string, vex []byte) (string, error)

UploadVEX uploads a CycloneDX VEX document to an existing project.

type Effective

type Effective struct {
	Settings

	// Source is where Enabled came from: SourceRepository (an override row),
	// SourceGlobal, or SourceNone (nothing configured).
	Source string
}

Effective is the configuration that applies to a repository.

func EffectiveFor

func EffectiveFor(ctx context.Context, db SettingsStore, repoID string) (Effective, error)

EffectiveFor loads the global settings and the repository's override.

func Resolve

func Resolve(global Settings, repo *models.RepositoryDependencyTrack) (Effective, error)

Resolve layers the repository override over the global settings. An override row applies explicitly (its enabled flag wins, so a repository can opt in or out); its empty strings and NULL booleans inherit the global values.

func (Effective) Ready

func (e Effective) Ready() bool

Ready reports whether pushing can work: enabled with a URL and an API key.

type Finding

type Finding struct {
	VulnID    string
	Source    string
	Aliases   []string
	Component string // purl, or name@version
	State     string // analysis state, "" when never analyzed
	Suppress  bool
}

Finding is one vulnerability Dependency-Track found in a project's component.

type HTTPError

type HTTPError struct {
	Status int
	Op     string
}

HTTPError is a non-2xx answer from Dependency-Track. The body is not kept: it can echo request data and is never shown to API clients.

func (*HTTPError) Error

func (e *HTTPError) Error() string

type Info

type Info struct {
	Version            string   `json:"version"`
	Permissions        []string `json:"permissions"`
	MissingPermissions []string `json:"missing_permissions"`
}

Info is the outcome of a connection test.

type Job

type Job struct {
	RepoID     string  `json:"repo_id"`
	ArtifactID string  `json:"artifact_id"`
	Trigger    Trigger `json:"trigger"`
}

Job is one artifact to push.

type ProjectVars

type ProjectVars struct {
	Repo, Name, Version, ArtifactID, Format string
}

ProjectVars are the values a project name / version template can use.

type Pusher

type Pusher struct {
	// contains filtered or unexported fields
}

Pusher pushes BOMs and VEX statements in the background. Jobs are de-duplicated per artifact: one waiting job absorbs further requests, and a request arriving while the artifact is being pushed runs again afterwards.

func NewPusher

func NewPusher(db Store, src Sources, log *logger.Logger) *Pusher

NewPusher creates a stopped pusher; Start launches its workers.

func (*Pusher) Enqueue

func (p *Pusher) Enqueue(j Job) bool

Enqueue schedules a push without checking the configuration (use EnqueueIfEnabled for events). It reports false when the queue is full.

func (*Pusher) EnqueueIfEnabled

func (p *Pusher) EnqueueIfEnabled(ctx context.Context, j Job)

EnqueueIfEnabled schedules a push when the repository's effective configuration is ready and has auto-push on; it is what upload and scan events call. Configuration errors are logged, never returned: an event must not fail because of the integration.

func (*Pusher) ForwardTo added in v0.15.0

func (p *Pusher) ForwardTo(s *workq.Store)

ForwardTo turns the pusher into the front of a worker process (server.role=web): Enqueue writes a worker request to s, and nothing is pushed here. Call it before the first Enqueue.

func (*Pusher) ServeFrom added in v0.15.0

func (p *Pusher) ServeFrom(c *workq.Consumer)

ServeFrom makes the pusher push what the requests claimed by c ask for. The pusher must be started.

func (*Pusher) Start

func (p *Pusher) Start(ctx context.Context)

Start launches the workers; they run until Stop or ctx is done.

func (*Pusher) Stop

func (p *Pusher) Stop()

Stop cancels the workers and waits for them.

func (*Pusher) WithTimings

func (p *Pusher) WithTimings(poll, maxWait time.Duration) *Pusher

WithTimings overrides how often and for how long the processing token is polled.

type Settings

type Settings struct {
	Enabled        bool   `json:"enabled"`
	BaseURL        string `json:"base_url"`
	APIKey         string `json:"api_key"`
	ProjectName    string `json:"project_name"`
	ProjectVersion string `json:"project_version"`
	AutoPush       bool   `json:"auto_push"`
	PushVEX        bool   `json:"push_vex"`
}

Settings is a Dependency-Track configuration in plaintext (the API key is encrypted at rest).

func LoadGlobal

func LoadGlobal(ctx context.Context, db SettingsStore) (Settings, error)

LoadGlobal reads the global settings; none stored yields the zero value (disabled).

type SettingsStore

type SettingsStore interface {
	GetSystemSetting(ctx context.Context, key string) (string, error)
	SetSystemSetting(ctx context.Context, key, value string) error
	GetRepositoryDependencyTrack(ctx context.Context, repoID string) (*models.RepositoryDependencyTrack, error)
}

SettingsStore is the part of the database the configuration lives in.

type Sources

type Sources interface {
	// BOM returns the artifact's CycloneDX SBOM (without vulnerabilities:
	// Dependency-Track runs its own analysis) and its component count.
	BOM(ctx context.Context, repo *models.Repository, artifactID string) (doc []byte, components int, err error)
	// VEX returns the artifact's CycloneDX VEX shaped by opts and its statement count.
	VEX(ctx context.Context, repo *models.Repository, artifactID string, opts sbom.VEXOptions) (doc []byte, statements int, err error)
}

Sources builds the documents a push sends, from an artifact's stored state.

type Store

type Store interface {
	SettingsStore
	GetRepositoryByID(ctx context.Context, id string) (*models.Repository, error)
	UpsertDependencyTrackPush(ctx context.Context, s *models.DependencyTrackPush) error
	GetDependencyTrackPush(ctx context.Context, repoID, artifactID string) (*models.DependencyTrackPush, error)
}

Store is the database part the pusher needs.

type Trigger

type Trigger string

Trigger says what caused a push.

const (
	TriggerUpload Trigger = "upload"
	TriggerScan   Trigger = "scan"
	TriggerManual Trigger = "manual"
)

Push triggers.

type UploadPublisher

type UploadPublisher struct {
	// contains filtered or unexported fields
}

UploadPublisher is a webhook.Publisher that forwards every event to the wrapped publisher and, for artifact uploads, schedules a Dependency-Track push when the repository has the integration and auto-push on.

func NewUploadPublisher

func NewUploadPublisher(next webhook.Publisher, p *Pusher) *UploadPublisher

NewUploadPublisher wraps next (which may be nil).

func (*UploadPublisher) Dispatch

func (u *UploadPublisher) Dispatch(ctx context.Context, repoID, eventType string, payload map[string]any)

Dispatch implements webhook.Publisher.

Jump to

Keyboard shortcuts

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