harbor

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package harbor implements Harbor's pluggable scanner adapter API (spec v1.2, https://github.com/goharbor/pluggable-scanner-spec), so that Harbor scans every pushed image with meles. Harbor knows vulnerability and SBOM reports only: malware findings become vulnerabilities of severity Critical, so that Harbor's "prevent vulnerable images from running" blocks infected images at the registry. Optionally the adapter chains an upstream adapter such as Trivy's: a project keeps its CVE scan and gets the malware findings in the same report.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidSeverity

func ValidSeverity(s string) bool

ValidSeverity reports whether s is one of Harbor's severities.

Types

type Adapter

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

Adapter serves the scanner adapter API.

func New

func New(cfg Config) *Adapter

New returns an adapter; Run starts its scan workers.

func (*Adapter) Handler

func (a *Adapter) Handler() http.Handler

Handler returns the HTTP handler: the API under /api/v1 (with the bearer token, if configured) and unauthenticated probes.

func (*Adapter) Run

func (a *Adapter) Run(ctx context.Context)

Run scans queued requests with cfg.Workers workers until ctx is canceled, and drops old reports.

type Artifact

type Artifact struct {
	Repository string `json:"repository"`
	Digest     string `json:"digest"`
	Tag        string `json:"tag,omitempty"`
	MimeType   string `json:"mime_type,omitempty"`
}

Artifact is the image to scan.

type Capability

type Capability struct {
	Type                 string         `json:"type,omitempty"`
	ConsumesMimeTypes    []string       `json:"consumes_mime_types"`
	ProducesMimeTypes    []string       `json:"produces_mime_types"`
	AdditionalAttributes map[string]any `json:"additional_attributes,omitempty"`
}

Capability is what a scanner consumes and produces.

type Config

type Config struct {
	Scanner ImageScanner
	// Version of meles, reported as the scanner version.
	Version string
	// Token, if set, is the bearer token Harbor must send (scanner
	// registration: Authorization "Bearer").
	Token string
	// Rules are ignore rules; ignored entries are left out of the report.
	Rules *ignore.Rules
	// SkippedSeverity is the severity of files that were not scanned
	// (default Critical: an unscanned file must not pass a gate).
	SkippedSeverity string
	// Upstream, if set, is chained: e.g. Trivy's adapter for CVEs.
	Upstream *Upstream
	// Workers is the number of images scanned in parallel.
	Workers int
	// ScanTimeout bounds a scan, including the upstream's.
	ScanTimeout time.Duration
	// KeepReports is how long reports are kept for Harbor to fetch.
	KeepReports time.Duration
	// HarborNamespace completes the short service names Harbor sends as its
	// registry address ("http://harbor-core:80" with use_internal_addr), so
	// that the adapter can run in another namespace.
	HarborNamespace string
	Logger          *slog.Logger
}

Config configures an Adapter.

type ImageScanner

type ImageScanner interface {
	Scan(ctx context.Context, ref string, platforms source.PlatformFilter, progress func(scan.Progress), opts ...scan.Option) (*scan.Report, error)
}

ImageScanner scans one image; *scan.Scanner implements it.

type Metadata

type Metadata struct {
	Scanner      Scanner           `json:"scanner"`
	Capabilities []Capability      `json:"capabilities"`
	Properties   map[string]string `json:"properties,omitempty"`
}

Metadata is the answer to GET /api/v1/metadata.

type Registry

type Registry struct {
	URL string `json:"url"`
	// Authorization is the value of an HTTP Authorization header for the
	// registry, e.g. "Basic …" of a robot account.
	Authorization string `json:"authorization"`
}

Registry is where Harbor stores the artifact, with credentials for it.

type ScanRequest

type ScanRequest struct {
	Registry            Registry         `json:"registry"`
	Artifact            Artifact         `json:"artifact"`
	EnabledCapabilities []map[string]any `json:"enabled_capabilities,omitempty"`
}

ScanRequest is the body of POST /api/v1/scan.

type ScanResponse

type ScanResponse struct {
	ID string `json:"id"`
}

ScanResponse is the answer to POST /api/v1/scan.

type Scanner

type Scanner struct {
	Name    string `json:"name"`
	Vendor  string `json:"vendor"`
	Version string `json:"version"`
}

Scanner identifies a scanner.

type Upstream

type Upstream struct {
	// URL of the adapter, e.g. http://harbor-trivy:8080.
	URL string
	// Authorization is sent as the Authorization header, e.g. "Bearer …".
	Authorization string
	Client        *http.Client
}

Upstream is another scanner adapter, e.g. Trivy's, that the adapter chains: scan requests go to both, and the reports are merged.

func (*Upstream) Metadata

func (u *Upstream) Metadata(ctx context.Context) (Metadata, error)

Metadata returns the upstream adapter's metadata.

func (*Upstream) Report

func (u *Upstream) Report(ctx context.Context, id, accept string, query url.Values) (*http.Response, error)

Report fetches a report as is, for passing it through (e.g. an SBOM).

func (*Upstream) Scan

func (u *Upstream) Scan(ctx context.Context, body []byte) (string, error)

Scan forwards Harbor's scan request unchanged and returns the upstream's scan request ID.

func (*Upstream) WaitVulnerabilities

func (u *Upstream) WaitVulnerabilities(ctx context.Context, id string) (*VulnerabilityReport, error)

WaitVulnerabilities polls the upstream's vulnerability report until it is ready or ctx ends.

type Vulnerability

type Vulnerability struct {
	ID               string         `json:"id"`
	Package          string         `json:"package"`
	Version          string         `json:"version"`
	FixVersion       string         `json:"fix_version,omitempty"`
	Severity         string         `json:"severity"`
	Description      string         `json:"description,omitempty"`
	Links            []string       `json:"links,omitempty"`
	PreferredCVSS    map[string]any `json:"preferred_cvss,omitempty"`
	CWEIDs           []string       `json:"cwe_ids,omitempty"`
	VendorAttributes map[string]any `json:"vendor_attributes,omitempty"`
}

Vulnerability is one entry of a report: a CVE from an upstream scanner, or a malware finding or skipped file of meles.

type VulnerabilityReport

type VulnerabilityReport struct {
	GeneratedAt     time.Time       `json:"generated_at"`
	Artifact        Artifact        `json:"artifact"`
	Scanner         Scanner         `json:"scanner"`
	Severity        string          `json:"severity"`
	Vulnerabilities []Vulnerability `json:"vulnerabilities"`
}

VulnerabilityReport is Harbor's vulnerability report.

Jump to

Keyboard shortcuts

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