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
- Variables
- func MatchPath(pattern, p string) bool
- func PathKey(v string) string
- func PointerPath(ptr string) (string, bool)
- type Direction
- type Entry
- type Field
- type FoldRule
- type Limits
- type LoadOptions
- type Mapping
- func (m *Mapping) Collect(data []byte) (*Passthrough, error)
- func (m *Mapping) Digest() string
- func (m *Mapping) FoldPath(p string) string
- func (m *Mapping) Kept(ext ctis.Extensions) (version string, entries []Entry, ok bool)
- func (m *Mapping) Lookup(table, value string) (string, bool)
- func (m *Mapping) Match(p string) (Field, bool)
- func (m *Mapping) Owns(pointer string) bool
- func (m *Mapping) Reemit(out []byte, entries []Entry, indent string) ([]byte, int, error)
- func (m *Mapping) Reverse(table, value string) []string
- func (m *Mapping) Store(ext *ctis.Extensions, value map[string]any)
- type Passthrough
- type Predicate
- type Record
- type Set
- type Table
- type Unmapped
Constants ¶
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.
const ( MaxPathSegments = 64 MaxPathBytes = 1024 )
MaxPathSegments and MaxPathBytes bound a path or a pointer.
const APIVersion = "openctem.io/interop/v1"
APIVersion is the apiVersion of a mapping file.
const Container = "(container)"
Container is the Target of a member that only groups others.
const MaxFold = 16
MaxFold bounds the fold rules of a mapping.
const MaxSetBytes = 64 << 10
MaxSetBytes bounds a value a set rule writes.
Variables ¶
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.
var ErrMapping = errors.New("interop mapping")
ErrMapping wraps every error of Load.
var ErrPassthrough = errors.New("engine: passthrough")
ErrPassthrough is returned for an input the passthrough cannot walk.
var ErrReemit = errors.New("engine: reemit")
ErrReemit is returned by Reemit for an output that is not a JSON object or array.
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.
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 PathKey ¶
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 ¶
PointerPath turns a pointer into the path the field patterns match: array indexes become "[]" on the previous segment.
Types ¶
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.
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.
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) Kept ¶
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 ¶
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) Owns ¶
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 ¶
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.
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 ¶
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 ¶
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 NewSet ¶
NewSet groups mappings of one format and direction. The mappings must have distinct versions.
func (*Set) Select ¶
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.
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.