Reporting and Output Formats
Relevant source files
The following files were used as context for generating this wiki page:
Reporting and Output Formats in 2ms represent the final stage of the secret detection lifecycle. This system is responsible for aggregating identified secrets from various plugins, calculating relevant metadata, and serializing this data into standardized formats for either terminal display or file-based persistence.
The module supports multiple industry-standard formats, including JSON, YAML, and SARIF, allowing for seamless integration with CI/CD pipelines, IDEs, and security dashboards. By providing structured output, the system enables automated remediation workflows and historical tracking of security findings.
Sources: lib/reporting/report.go:12-17, README.md:144-155
Report Structure and Initialization
The core of the reporting system is the Report struct. It acts as a central container for all scan results and statistics accumulated during the execution of the engine.
| Field | Type | Description |
|---|---|---|
TotalItemsScanned | int | The total number of files, messages, or pages processed across all plugins. |
TotalSecretsFound | int | The total count of unique secret instances identified. |
Results | map[string][]*secrets.Secret | A mapping where keys are source identifiers (e.g., file paths, URLs) and values are slices of discovered secrets. |
Sources: lib/reporting/report.go:21-25
Initialization Flow
The report is initialized via the Init() function, which prepares the internal map for result storage.
Output Formats
2ms supports four primary output identifiers, handled through internal constants. The specific format is determined either by a global flag for stdout or by the file extension when writing to a path.
| Format | Extension | Constant | Implementation File |
|---|---|---|---|
| JSON | .json | jsonFormat | lib/reporting/json.go |
| YAML | .yaml | longYamlFormat | lib/reporting/yaml.go |
| YML | .yml | shortYamlFormat | lib/reporting/yaml.go |
| SARIF | .sarif | sarifFormat | lib/reporting/sarif.go |
Sources: lib/reporting/report.go:12-17, cmd/config.go:42-55
SARIF Integration
The Static Analysis Results Interchange Format (SARIF) implementation is highly structured, compliant with version 2.1.0. It maps 2ms internal findings to the SARIF Object Model, including:
- Driver Metadata: Name and version of the tool (e.g.,
2ms). - Rules: Unique rule IDs and their full descriptions.
- Results: Specific locations (physical location, region, columns) and snippets of the detected secret.
- Properties: Custom fields for
validationStatusandcvssScore.
Sources: lib/reporting/sarif.go:14-25, lib/reporting/sarif.go:111-125
Data Flow and Persistence
The reporting system provides two primary methods for delivering results: displaying to the standard output (stdout) and writing to one or more files.
Output Generation Logic
The getOutput method serves as a dispatcher that selects the appropriate marshaller based on the requested format.
Sources: lib/reporting/report.go:73-86
File Persistence
The WriteFile method handles the physical creation of reports. It can accept a slice of paths, allowing the user to generate multiple reports in different formats simultaneously (e.g., a summary in YAML and a machine-readable SARIF for automation).
- Directory Creation: Uses
os.MkdirAllwith permissions0750. - Extension Parsing: Extracts the format from the file extension (e.g.,
.sarifbecomessarif). - Content Writing: Retrieves the formatted string and writes it to the file.
Sources: lib/reporting/report.go:46-71, README.md:162-167
Configuration and Validation
The command-line interface ensures that output requests are valid before the engine begins processing. The validateFormat function checks both the stdout-format flag and the report-path extensions against a regex pattern: json|yaml|sarif.
Reporting Configuration Flags
| Flag | Default | Description |
|---|---|---|
--stdout-format | yaml | Defines the format printed to the terminal. |
--report-path | [] | List of file paths for report generation. |
Sources: cmd/config.go:42-55, README.md:144-155
Summary
The reporting and output format system in 2ms ensures that secret detection results are not only accurate but also actionable. By abstracting the serialization logic into specialized drivers (JSON, YAML, SARIF) and providing flexible persistence options via the Report struct, the tool maintains high compatibility with diverse developer environments and automated security pipelines.
Sources: lib/reporting/report.go, lib/reporting/sarif.go