Configuration Management
Relevant source files
The following files were used as context for generating this wiki page:
Configuration Management in 2ms provides a flexible system for controlling secret detection behavior across various platforms. It allows users to define scanning parameters, authentication credentials, and rule filtering through multiple input methods, including Command Line Interface (CLI) flags, environment variables, and structured configuration files.
The system is built on a hybrid model that merges explicit CLI arguments with persistent settings from YAML or JSON files. This ensures that global preferences can be maintained in a file while specific scan requirements can be overridden on-the-fly via the terminal.
Sources: README.md:144-150, cmd/main.go:68-110
Configuration Architecture
The configuration lifecycle begins during command initialization. The application uses cobra for CLI command definition and viper for managing configuration files and environment variables. The initialize() function in cmd/config.go triggers the loading of the configuration file and binds it to the CLI flags.
Configuration Loading Flow
The following diagram illustrates how the system aggregates configuration from different sources before execution.
Sources: cmd/main.go:68-75, cmd/config.go:17-23
Key Data Structures
Configuration is primarily represented through the EngineConfig struct, which dictates the rules and filtering logic applied during the detection phase.
| Structure | Description | Source File |
|---|---|---|
EngineConfig | Contains lists for selected, ignored, and special rules, as well as file size limits. | engine/engine.go:44-53 |
ConfluencePlugin | Stores Confluence-specific configuration like spaces and history scanning. | plugins/confluence.go:30-38 |
Config | Library-level configuration structure used for reporting and global state. | lib/reporting/report.go:48-60 |
Input Methods
1. Configuration Files
Users can specify a file path using the --config flag. Supported formats include YAML and JSON. When a file is provided, 2ms merges these values with any provided CLI arguments.
Sources: README.md:144-150, cmd/main.go:76-77
2. Environment Variables
The application automatically checks for environment variables prefixed with 2MS_. This allows for secure credential injection (e.g., 2MS_TOKEN) without exposing secrets in shell history or configuration files.
Sources: cmd/main.go:72-73
3. CLI Global Flags
Global flags are applicable across all scan commands (e.g., git, filesystem, confluence).
| Flag | Type | Description |
|---|---|---|
--log-level | string | Log verbosity (trace, debug, info, warn, error, fatal). |
--report-path | strings | Target paths for results (.json, .yaml, .sarif). |
--rule | strings | Filter rules by name or tag. |
--ignore-rule | strings | Exclude specific rules. |
--validate | boolean | Enables remote API checks for secret validity. |
--max-target-megabytes | int | Skips files exceeding the specified size. |
| Sources: cmd/main.go:79-92, README.md:255-274 |
Engine Configuration Logic
The Engine is initialized using an EngineConfig object. During initialization, the system filters the total rule set based on user-provided inclusion and exclusion lists.
Rule Selection Process
The engine logic prioritizes rule sets in the following order:
- Special Rules: Explicitly added via
--add-special-rule(e.g.,hardcoded-password). - Selected Rules: Filtered by name or tag.
- Ignored Rules: Explicitly removed from the active set.
Sources: engine/engine.go:55-65, cmd/main.go:136-140
Plugin-Specific Configuration
Each plugin defines its own set of arguments and flags relevant to its platform. For instance, the Confluence plugin manages URL parsing and authentication tokens separately from the core engine.
Confluence Configuration
The Confluence plugin uses an initialize method to prepare its internal client. It handles the trimming of trailing slashes from URLs and warns the user if credentials (username/token) are missing, defaulting to anonymous scanning for public pages.
Sources: plugins/confluence.go:81-93
Filesystem Configuration
The filesystem plugin configures local paths and ignore patterns. It maintains a hardcoded ignoredFolders list (containing .git) while allowing users to add custom patterns via CLI.
Sources: plugins/filesystem.go:21-27, plugins/filesystem.go:58-75
Conclusion
The configuration management system in 2ms enables a highly customizable scanning environment. By decoupling the core engine configuration from plugin-specific settings and allowing for a tiered input priority (CLI > Config File > Environment Variables), the tool remains adaptable for both local developer use and automated CI/CD pipeline integration.
Sources: cmd/main.go:126-160, README.md:82-120