Documentation
¶
Overview ¶
Package mapping turns the JSON, JSON Lines, CSV or XML output of any tool into a CTIS report through a declarative mapping file, so a tool or an export needs no parser code.
A mapping is a JSON document (apiVersion "openctem.io/mapping/v1"). Each rule selects input records with closed predicates and sets CTIS members from source paths with a closed set of transforms. The language has no expressions, loops, code or I/O: it cannot read files, reach the network or run for longer than the input it walks. A tool that keeps its mapping in YAML converts it to JSON before calling Load; the strict decoding here is the authority.
The full language is in docs/mapping.md.
Index ¶
Constants ¶
const ( DefaultMaxInputBytes = 64 << 20 DefaultMaxRecordBytes = 1 << 20 DefaultMaxRecords = 200_000 DefaultMaxOutputs = 200_000 DefaultMaxIssues = 200 MaxInputDepth = 64 )
Default bounds of Apply.
const ( // MaxCSVColumns is the most columns a header row may have. MaxCSVColumns = 1024 // DefaultMaxCellBytes is the largest cell; a row with a longer cell is // skipped. DefaultMaxCellBytes = 64 << 10 )
Bounds of a CSV input.
const ( MaxMappingBytes = 256 << 10 MaxRules = 200 MaxSetsPerRule = 100 MaxPredicates = 16 MaxRegexLen = 256 MaxMapEntries = 256 MaxInValues = 256 MaxTemplateBytes = 1024 MaxIdentity = 8 MaxSkipRows = 100 )
Bounds on a mapping file.
const ( SourceJSON = "json" SourceJSONL = "jsonl" SourceCSV = "csv" SourceXML = "xml" )
Source formats.
const ( KindAsset = "asset" KindFinding = "finding" KindDependency = "dependency" )
Record kinds.
const APIVersion = "openctem.io/mapping/v1"
APIVersion is the only mapping format version this package reads.
const (
// DefaultMaxXMLElements is the most elements in one document.
DefaultMaxXMLElements = 4_000_000
)
Bounds of an XML input. The reader is importer.SafeXMLTokenReader: no document type declaration of any kind, so no entity can be declared or expanded and no external resource is named or read.
const MaxObservedFields = 512
MaxObservedFields bounds the distinct input fields Options.Observe records.
Variables ¶
var ( ErrInputTooLarge = errors.New("mapping: input is larger than the limit") ErrNotAList = errors.New("mapping: the each path does not name a list") )
Errors of Apply.
var ErrEncoding = errors.New("mapping: the input is UTF-16; save it as UTF-8")
ErrEncoding is returned for an input that is not UTF-8 (a UTF-16 byte order mark).
Functions ¶
This section is empty.
Types ¶
type CSVSpec ¶ added in v1.6.2
type CSVSpec struct {
// Delimiter is ",", ";", "\t", "|" or "auto" (the default): the one
// of these that occurs most often, outside quotes, in the header row.
Delimiter string `json:"delimiter,omitempty"`
// SkipRows are lines before the header row (a report title, an export
// date), 0 to MaxSkipRows.
SkipRows int `json:"skip_rows,omitempty"`
}
CSVSpec reads a CSV input (source "csv").
type Issue ¶
type Issue struct {
// Record is the 1-based input record (the line for JSON Lines).
Record int `json:"record"`
// Rule is the index of the rule, or -1 for the record itself.
Rule int `json:"rule"`
Message string `json:"message"`
}
Issue is a record that was skipped or a rule output that was dropped.
type Mapping ¶
type Mapping struct {
// contains filtered or unexported fields
}
Mapping is a loaded, validated mapping file.
func Load ¶
Load parses and validates a mapping file. Every error names the rule and member it is about.
func (*Mapping) Apply ¶
func (m *Mapping) Apply(ctx context.Context, r io.Reader, opts Options) (*ctis.Report, Stats, error)
Apply reads the tool output from r and returns the CTIS report the mapping makes of it. Every rule whose predicates hold emits one record; a record that does not decode into its CTIS type or does not validate is skipped and counted. The output is deterministic for the same input, mapping and Options.Now.
A record limit or output limit ends the read early with Stats.Truncated set; an input larger than MaxInputBytes, a malformed json document or a canceled ctx returns an error and no report.
func (*Mapping) Digest ¶
Digest is the SHA-256 of the mapping's canonical JSON form ("sha256:<hex>"): object members sorted, insignificant whitespace removed. Reformatting or reordering a file keeps it; any change of content does not. A tool descriptor records it, so the mapping is covered by the descriptor's own digest.
func (*Mapping) Identity ¶ added in v1.6.2
Identity is the list of finding members the mapping declares as the identity of a finding (empty when it declares none). A receiver compares it, and the value specs of these members (Rules), across versions of a mapping: a change of identity makes earlier findings and new ones keyed apart.
func (*Mapping) Rules ¶ added in v1.6.2
func (m *Mapping) Rules() []RuleSummary
Rules summarizes the rules in order.
type Options ¶
type Options struct {
// Now is the report clock; nil means time.Now. Inject it for
// reproducible output.
Now func() time.Time
// Tool is copied into report.tool. A mapping never sets it from the
// input, so a tool's output cannot claim to be another tool.
Tool *ctis.Tool
MaxInputBytes int64 // whole input; default 64 MiB
MaxRecordBytes int // one JSON Lines line; default 1 MiB
MaxRecords int // input records read; default 200 000
MaxOutputs int // assets + findings + dependencies; default 200 000
MaxIssues int // Stats.Issues kept; default 200
MaxCellBytes int // one CSV cell; default 64 KiB
MaxXMLElements int // elements of an XML document; default 4 000 000
// Observe records the input's fields in Stats.Fields and the ones no
// rule reads in Stats.Unmapped (a mapping editor's preview).
Observe bool
}
Options bound and stamp one Apply.
type Predicate ¶
type Predicate struct {
Path string `json:"path"`
Exists *bool `json:"exists,omitempty"`
Equals any `json:"equals,omitempty"`
In []any `json:"in,omitempty"`
Matches string `json:"matches,omitempty"`
}
Predicate is one closed condition on an input record. Exactly one of Exists, Equals, In and Matches is set.
type Predicates ¶
type Predicates []Predicate
Predicates is "when": one predicate object or a list that must all hold.
func (*Predicates) UnmarshalJSON ¶
func (p *Predicates) UnmarshalJSON(b []byte) error
UnmarshalJSON accepts an object or an array of objects.
type RuleSpec ¶
type RuleSpec struct {
When Predicates `json:"when,omitempty"`
Kind string `json:"kind"`
Set map[string]ValueSpec `json:"set"`
}
RuleSpec is one rule as written in the file.
type RuleSummary ¶ added in v1.6.2
type RuleSummary struct {
Kind string `json:"kind"`
// Targets are the members the rule sets, sorted.
Targets []string `json:"targets"`
// Specs is the canonical JSON of each target's value spec.
Specs map[string]string `json:"specs"`
// Const is true for a target whose value is a constant.
Const map[string]bool `json:"const,omitempty"`
}
RuleSummary describes one rule for a receiver that checks a mapping against its own policy (members it reserves) or compares two versions.
type Stats ¶
type Stats struct {
Records int `json:"records"`
Assets int `json:"assets"`
Findings int `json:"findings"`
Dependencies int `json:"dependencies"`
Skipped int `json:"skipped"`
Truncated bool `json:"truncated"`
Issues []Issue `json:"issues,omitempty"`
// With Options.Observe: the input's fields as source paths (CSV: the
// header columns in order; JSON and XML: leaf paths without list
// indexes, sorted), at most MaxObservedFields, FieldsTruncated when
// there were more; and those no predicate or value reads.
Fields []string `json:"fields,omitempty"`
FieldsTruncated bool `json:"fields_truncated,omitempty"`
Unmapped []string `json:"unmapped,omitempty"`
}
Stats count what Apply did.
type ValueSpec ¶
type ValueSpec struct {
Path string `json:"path,omitempty"`
Const any `json:"const,omitempty"`
Template string `json:"template,omitempty"`
Vars map[string]string `json:"vars,omitempty"`
Default any `json:"default,omitempty"`
First bool `json:"first,omitempty"`
Trim bool `json:"trim,omitempty"`
Lower bool `json:"lower,omitempty"`
Upper bool `json:"upper,omitempty"`
Split string `json:"split,omitempty"`
Map map[string]string `json:"map,omitempty"`
Join *string `json:"join,omitempty"`
As string `json:"as,omitempty"`
MaxBytes int `json:"max_bytes,omitempty"`
// contains filtered or unexported fields
}
ValueSpec is how one CTIS member gets its value: a bare string is a source path; an object takes exactly one of Path, Const and Template, then the transforms in this order: First, Trim, Lower, Upper, Split, Map, Join, As, MaxBytes. Default applies when the result is missing or empty.
func (ValueSpec) MarshalJSON ¶
MarshalJSON writes a bare path back as a string, so the digest of a mapping does not depend on which spelling its author used.
func (*ValueSpec) UnmarshalJSON ¶
UnmarshalJSON accepts a bare path string or an object.