engine

package
v2.0.5 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: Apache-2.0 Imports: 17 Imported by: 7 modules, 6 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 engine runs the declarative mapping files of the CTIS interoperability adapters (github.com/openctemio/ctis/v2/interop/<format>).

A mapping file describes one direction (import or export) of one upstream version of one standard format:

v2/interop/<format>/<upstream-version>/import.json
v2/interop/<format>/<upstream-version>/export.json

It lists every member of the upstream format the adapter knows and what becomes of it (a CTIS member, a named Go hook, or a stated reason to drop it), the tables the conversion looks values up in (severity maps, enum tables), rules the engine applies itself (a source member copied to a CTIS member through named transforms), and how the adapter recognises a document of that version. The engine

  • loads and checks a mapping strictly (Load);
  • picks the mapping of the version a document declares (Select);
  • keeps every member the mapping does not list (Collect), bounded and scrubbed, so an adapter stores it under extensions["<namespace>"] and the reverse adapter writes it back (Reemit): an upstream member added before the adapter maps it is not lost;
  • applies the copy rules ("set") to the records the adapter built.

The structural parts of a conversion that are not field to field (SARIF code flows, a CycloneDX dependency graph, VEX product resolution) are Go functions of the adapter, registered by name as hooks; a mapping names the hook that maps each such member and Load refuses a hook the adapter does not register. The language has no expressions, loops, code, file or network access.

Index

Constants

View Source
const (
	MaxMappingBytes = 512 << 10
	MaxFields       = 2000
	MaxTables       = 64
	MaxTableEntries = 512
	MaxDetect       = 8
	MaxRecords      = 16
	MaxTransforms   = 8
	MaxNotes        = 64
	MaxNoteBytes    = 4096
)

Limits of a mapping file.

View Source
const (
	MaxPathSegments = 64
	MaxPathBytes    = 1024
)

MaxPathSegments and MaxPathBytes bound a path or a pointer.

View Source
const APIVersion = "openctem.io/interop/v1"

APIVersion is the apiVersion of a mapping file.

View Source
const Container = "(container)"

Container is the Target of a member that only groups others.

View Source
const MaxFold = 16

MaxFold bounds the fold rules of a mapping.

View Source
const MaxSetBytes = 64 << 10

MaxSetBytes bounds a value a set rule writes.

Variables

View Source
var (
	DefaultLimits = Limits{MaxEntries: 64, MaxBytes: 16 << 10, MaxDepth: 12, MaxRecords: 10000, MaxTotalBytes: 8 << 20}
	HardLimits    = Limits{MaxEntries: 256, MaxBytes: 32 << 10, MaxDepth: 16, MaxRecords: 100000, MaxTotalBytes: 32 << 20}
)

DefaultLimits are the passthrough limits of a mapping without its own; HardLimits are the most a mapping can set. MaxBytes stays well below ctis.MaxExtensionsBytes, which bounds all namespaces of an object, and MaxDepth leaves room for ctis.RedactSecretFinding, which walks a finding to a depth of 32, to reach every kept string.

View Source
var ErrMapping = errors.New("interop mapping")

ErrMapping wraps every error of Load.

View Source
var ErrPassthrough = errors.New("engine: passthrough")

ErrPassthrough is returned for an input the passthrough cannot walk.

View Source
var ErrReemit = errors.New("engine: reemit")

ErrReemit is returned by Reemit for an output that is not a JSON object or array.

View Source
var ErrSetTarget = errors.New("engine: set target is not a pointer to the record type")

ErrSetTarget is returned by Set for a target that is not a pointer to the record kind's type.

View Source
var ErrVersion = errors.New("engine: no mapping for the version the document declares")

ErrVersion is returned by Select for a document no mapping of the set matches.

Functions

func MatchPath

func MatchPath(pattern, p string) bool

MatchPath reports whether the path p matches the pattern.

func PathKey

func PathKey(v string) string

PathKey is the path segment of a member name: control characters, '/', '[' and ']' become '_', and the name is cut to 128 bytes on a character boundary.

func PointerPath

func PointerPath(ptr string) (string, bool)

PointerPath turns a pointer into the path the field patterns match: array indexes become "[]" on the previous segment.

Types

type Direction

type Direction string

Direction of a mapping.

const (
	Import Direction = "import"
	Export Direction = "export"
)

Directions.

type Entry

type Entry struct {
	Pointer string
	Value   json.RawMessage
}

Entry is one kept member to write back: an absolute pointer into the output and its value.

func Unindexed

func Unindexed(entries []Entry) []Entry

Unindexed returns the entries whose pointer crosses no array index: the members an exporter can place without knowing how it orders a list.

type Field

type Field struct {
	// Path of the member (pattern syntax, see MatchPath). Import: in the
	// source document; export: in the output.
	Path string `json:"path"`

	// Target says what keeps the value (import) or where it comes from
	// (export), for readers. "(container)" marks a member that only
	// groups others.
	Target string `json:"target,omitempty"`

	// Ignored says why the member is dropped on purpose. An ignored
	// member, and everything below it, is never passed through.
	Ignored string `json:"ignored,omitempty"`

	// Hook names the Go function of the adapter that maps the member.
	Hook string `json:"hook,omitempty"`

	// Set (import) is a rule the engine applies: the value is copied to
	// this CTIS member of the record (dotted JSON member names, relative to
	// the record), through Transform. A value that a transform rejects
	// leaves the member unset.
	Set       string   `json:"set,omitempty"`
	Record    string   `json:"record,omitempty"`
	Transform []string `json:"transform,omitempty"`
}

Field is one member of the upstream format.

func (Field) Mapped

func (f Field) Mapped() bool

Mapped reports whether the field's value is kept.

type FoldRule

type FoldRule struct {
	From string `json:"from"`
	To   string `json:"to"`
}

FoldRule reads paths that start with From as if they started with To.

type Limits

type Limits struct {
	// Members kept per object (the report or one record).
	MaxEntries int `json:"max_entries,omitempty"`
	// Encoded bytes kept per object.
	MaxBytes int `json:"max_bytes,omitempty"`
	// Nesting depth inside one kept value.
	MaxDepth int `json:"max_depth,omitempty"`
	// Records that keep members.
	MaxRecords int `json:"max_records,omitempty"`
	// Encoded bytes kept for the whole document.
	MaxTotalBytes int `json:"max_total_bytes,omitempty"`
}

Limits bound the passthrough of one document.

type LoadOptions

type LoadOptions struct {
	// Hooks the adapter registers for this direction. A mapping that names
	// another hook is refused.
	Hooks []string
	// Tables the adapter's Go code looks up. A mapping without one of them
	// is refused, so a table cannot go missing silently.
	Tables []string
}

LoadOptions name what the adapter provides.

type Mapping

type Mapping struct {
	APIVersion string    `json:"apiVersion"`
	Format     string    `json:"format"`
	Version    string    `json:"version"`
	Direction  Direction `json:"direction"`

	// Namespace is the extensions namespace the passthrough members are
	// kept under (reverse-DNS, ctis.IsExtensionNamespace).
	Namespace string `json:"namespace"`

	// Title and Source describe the upstream format for readers.
	Title  string `json:"title"`
	Source string `json:"source,omitempty"`

	// VersionPointer (import) points to the member that declares the
	// document's version; the passthrough records that version.
	VersionPointer string `json:"version_pointer,omitempty"`

	// Detect: every predicate must hold for a document of this version
	// (import only).
	Detect []Predicate `json:"detect,omitempty"`

	// Notes are normalization rules that are not per field.
	Notes []string `json:"notes,omitempty"`

	// Fold names path prefixes that are read as another prefix: a
	// recursive structure (CycloneDX nested components) is listed once.
	// Each rule is applied, longest match first, until none applies.
	Fold []FoldRule `json:"fold,omitempty"`

	// Records name the source objects that become one CTIS record each.
	// The passthrough members below a record are kept on that record, and
	// the "set" rules apply to it.
	Records []Record `json:"records,omitempty"`

	// Fields list every member the adapter knows, in document order.
	Fields []Field `json:"fields"`

	// Tables are the value tables the conversion uses.
	Tables map[string]Table `json:"tables,omitempty"`

	// Hooks are the Go functions of the adapter this mapping relies on.
	Hooks []string `json:"hooks,omitempty"`

	// ReemitFrom (export) lists the other upstream versions whose
	// passthrough this mapping writes back: a later version that keeps the
	// members of an earlier one.
	ReemitFrom []string `json:"reemit_from,omitempty"`

	// Passthrough lowers the default passthrough limits.
	Passthrough *Limits `json:"passthrough,omitempty"`
	// contains filtered or unexported fields
}

Mapping is one mapping file.

func Load

func Load(data []byte, opts LoadOptions) (*Mapping, error)

Load parses and checks a mapping file. Unknown members are errors.

func MustLoad

func MustLoad(data []byte, opts LoadOptions) *Mapping

MustLoad is Load for the mapping files an adapter embeds; it panics on an error, which the adapter's tests catch.

func (*Mapping) Collect

func (m *Mapping) Collect(data []byte) (*Passthrough, error)

Collect walks a JSON document once and keeps every member its import mapping does not list, and the values of the set rules. It never fails on content it cannot keep (it drops it and counts it); it fails only on input that is not JSON or is nested deeper than 256.

func (*Mapping) Digest

func (m *Mapping) Digest() string

Digest is "sha256:<hex>" of the mapping file.

func (*Mapping) FoldPath

func (m *Mapping) FoldPath(p string) string

FoldPath applies the fold rules to a path.

func (*Mapping) Kept

func (m *Mapping) Kept(ext ctis.Extensions) (version string, entries []Entry, ok bool)

Kept reads the passthrough an import stored under the mapping's namespace in ext: the upstream version it came from and its members (pointers as stored: absolute for the report, relative for a record). The members are checked again (pointer syntax, limits, scrub rules), because a CTIS document may come from anyone. ok is false when ext holds nothing for the namespace, or its version is neither the mapping's nor one the mapping re-emits from.

func (*Mapping) Lookup

func (m *Mapping) Lookup(table, value string) (string, bool)

Lookup looks a value up in a table: the value is normalized by the table's match, then mapped; a value the map does not hold gets the default. ok is false when there is no value. Lookup panics for a table the mapping does not have: the adapter names its tables in LoadOptions.Tables.

func (*Mapping) Match

func (m *Mapping) Match(p string) (Field, bool)

Match returns the first field whose path matches p.

func (*Mapping) Owns

func (m *Mapping) Owns(pointer string) bool

Owns reports whether the export mapping lists the output member at the pointer, or one below it: the adapter writes it, and Reemit never sets it.

func (*Mapping) Reemit

func (m *Mapping) Reemit(out []byte, entries []Entry, indent string) ([]byte, int, error)

Reemit writes kept members back into an encoded output. A member is written only where the output has none yet and the export mapping does not own the place (Owns); missing objects on the way are created, missing array elements are not. It returns the new output (indented with indent when indent is not empty) and the number of members it could not place. Without entries the output is returned unchanged.

func (*Mapping) Reverse

func (m *Mapping) Reverse(table, value string) []string

Reverse returns the keys of a table that map to value, sorted.

func (*Mapping) Store

func (m *Mapping) Store(ext *ctis.Extensions, value map[string]any)

Store sets the extension value under the mapping's namespace; a nil value stores nothing.

type Passthrough

type Passthrough struct {
	Doc Unmapped

	// Dropped counts members left out by a limit or by the scrub rules.
	Dropped int
	// contains filtered or unexported fields
}

Passthrough is what Collect kept of one document.

func (*Passthrough) Apply

func (p *Passthrough) Apply(ptr string, target any) error

Apply writes the values of the set rules of the record at ptr into target, a pointer to the record's CTIS type (*ctis.Finding for a finding record). A value a transform rejects, or that does not convert to the member's type, leaves the member as it was.

func (*Passthrough) DocExtension

func (p *Passthrough) DocExtension() map[string]any

DocExtension is the extension value of the members outside the records, nil when there are none.

func (*Passthrough) Empty

func (p *Passthrough) Empty() bool

Empty reports whether nothing was kept.

func (*Passthrough) RecordExtension

func (p *Passthrough) RecordExtension(ptr string, scrub func(string) string) map[string]any

RecordExtension is the extension value of the record at ptr (the record object's pointer), nil when it kept nothing. scrub, when set, is applied to every kept string: an adapter masks a secret finding's raw match with it.

func (*Passthrough) Version

func (p *Passthrough) Version() string

Version is the upstream version the kept members belong to: the version the document declares (Mapping.VersionPointer), else the mapping's.

type Predicate

type Predicate struct {
	// Pointer is an RFC 6901 pointer to a member (no array indexes).
	Pointer string `json:"pointer"`
	// Exactly one of the tests.
	Equals  *string  `json:"equals,omitempty"`
	In      []string `json:"in,omitempty"`
	Prefix  *string  `json:"prefix,omitempty"`
	Matches *string  `json:"matches,omitempty"`
	// Absent: the member is not there (or null).
	Absent bool `json:"absent,omitempty"`
	// OrAbsent: the test also holds when the member is not there.
	OrAbsent bool `json:"or_absent,omitempty"`
	// contains filtered or unexported fields
}

Predicate tests one member of a document.

type Record

type Record struct {
	Name string `json:"name"`
	// Path of the record objects ("/runs[]/results[]").
	Path string `json:"path"`
	// Kind of CTIS record: finding, asset, dependency or vex.
	Kind string `json:"kind"`
}

Record is a source object that becomes one CTIS record.

type Set

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

Set is the mappings of one format and direction, one per upstream version, side by side.

func LoadFS

func LoadFS(fsys fs.FS, dir Direction, opts LoadOptions) (*Set, error)

LoadFS loads the mappings of one direction from an adapter's embedded files: <version>/<direction>.json for every version directory at the root of fsys, sorted by version. A mapping's version must equal its directory. A new upstream version is one more directory.

func MustLoadFS

func MustLoadFS(fsys fs.FS, dir Direction, opts LoadOptions) *Set

MustLoadFS is LoadFS that panics, for the sets an adapter builds at init.

func MustSet

func MustSet(ms ...*Mapping) *Set

MustSet is NewSet that panics, for the sets an adapter builds at init.

func NewSet

func NewSet(ms ...*Mapping) (*Set, error)

NewSet groups mappings of one format and direction. The mappings must have distinct versions.

func (*Set) Latest

func (s *Set) Latest() *Mapping

Latest is the last mapping of the set.

func (*Set) Select

func (s *Set) Select(data []byte) (*Mapping, error)

Select returns the import mapping whose detect predicates all hold for the document. The document is read as a stream, and only the members the predicates name are looked at.

func (*Set) Version

func (s *Set) Version(v string) (*Mapping, bool)

Version returns the mapping of an upstream version.

func (*Set) Versions

func (s *Set) Versions() []string

Versions lists the upstream versions of the set, in the order given.

type Table

type Table struct {
	// Match: "exact" (the default), "fold" (case-insensitive, trimmed) or
	// "token" (trimmed, lower case, '_', '-' and ' ' removed).
	Match string `json:"match,omitempty"`
	// Map from the normalized source value to the target value.
	Map map[string]string `json:"map"`
	// Default for a value the map does not hold; absent: no value.
	Default *string `json:"default,omitempty"`
}

Table is a value table: a source value is looked up after the table's normalization.

type Unmapped

type Unmapped struct {
	Entries   map[string]json.RawMessage
	Truncated bool
	// contains filtered or unexported fields
}

Unmapped holds the kept members of one object.

Jump to

Keyboard shortcuts

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