Developing Custom Target Plugins
Relevant source files
The following files were used as context for generating this wiki page:
Too Many Secrets (2ms) is designed with an extensible architecture that utilizes plugins to support diverse scanning targets such as internal communication platforms, content management systems, and source code repositories. Developing a custom target plugin allows the tool to fetch content from new sources and present it to the core engine for secret detection.
Plugins act as the bridge between external data sources and the 2ms engine. They are responsible for implementing command-line interfaces, handling authentication, navigating remote or local resource structures, and converting specific data formats into a standardized ISourceItem that the engine can process.
Sources: README.md:14-18, CONTRIBUTING.md:28-28
Plugin Architecture
All plugins must adhere to a standard interface and data structure to ensure compatibility with the 2ms core. The system relies on Go interfaces to define the behavior of a plugin and the items it produces.
The IPlugin Interface
Every plugin must implement the IPlugin interface, which defines the basic identity and entry point for the plugin's execution logic.
| Method | Return Type | Description |
|---|---|---|
GetName() | string | Returns the unique name of the plugin (e.g., "confluence", "git"). |
DefineCommand(chan ISourceItem, chan error) | (*cobra.Command, error) | Sets up the plugin's CLI command, flags, and the execution loop. |
Sources: plugins/plugins.go:44-47
Data Structures and Channels
Plugins interact with the engine using specific data structures to represent content and channels to facilitate asynchronous processing.
ISourceItemInterface: Defines the requirements for any piece of content to be scanned. It must provide methods to retrieve the content as a string, a unique identifier, and a user-friendly source description or link.itemStruct: The standard implementation ofISourceItemused by existing plugins.ChannelsStruct: A helper structure commonly used within plugins to manage the flow of items, errors, and synchronization via aWaitGroup.
Sources: plugins/plugins.go:10-42
The diagram above illustrates the relationship between the plugin interface, the data items produced, and the communication channels.
Sources: plugins/plugins.go:10-42
Implementing a Plugin
Developing a plugin involves three primary phases: defining the CLI command, implementing the data fetching logic, and converting the data into ISourceItem objects.
1. Defining the Command and Flags
The DefineCommand function uses the cobra library to integrate the plugin into the 2ms CLI. This is where flags for authentication (tokens, usernames) and target specifications (URLs, paths) are defined.
func (p *CustomPlugin) DefineCommand(items chan ISourceItem, errors chan error) (*cobra.Command, error) {
var cmd = &cobra.Command{
Use: fmt.Sprintf("%s <ARGS>", p.GetName()),
Run: func(cmd *cobra.Command, args []string) {
// Implementation logic
},
}
// Define flags here
return cmd, nil
}
Sources: plugins/confluence.go:50-74, plugins/filesystem.go:34-58
2. Data Fetching and Iteration
Plugins must implement logic to traverse the target source. For example:
- Filesystem: Uses
filepath.Walkto iterate through local directories. - Confluence/Paligo: Implements API clients to paginate through spaces, folders, and documents.
- Git: Uses
git logcommands to read file diffs and commit history.
Sources: plugins/filesystem.go:67-99, plugins/confluence.go:153-176, plugins/git.go:94-106
3. Producing Items
As content is retrieved, it must be wrapped in an item struct and sent to the items channel. Each item should have a unique ID, typically constructed by concatenating the plugin name, project/space name, and a unique resource identifier.
Sources: plugins/confluence.go:136-151, plugins/git.go:81-89, plugins/filesystem.go:121-131
This flowchart represents the high-level execution loop within a plugin's Run function.
Sources: plugins/confluence.go:59-71, plugins/paligo.go:86-103
Standard Plugin Components
Existing plugins demonstrate common patterns that should be followed for consistency:
| Component | Description | Examples |
|---|---|---|
| Request Limiting | Using a semaphore channel (e.g., make(chan struct{}, max)) to prevent overwhelming remote APIs. | confluenceMaxRequests (500) |
| Authentication | Support for Basic Auth (username/token) or Bearer tokens, often using utils.CreateBasicAuthCredentials. | ConfluencePlugin, PaligoPlugin |
| Rate Limiting | Explicit handling of API rate limits, including Retry-After headers. | PaligoClient using golang.org/x/time/rate |
| Filtering/Ignoring | Allowing users to skip specific patterns or directories. | flagIgnored in FileSystemPlugin |
Sources: plugins/confluence.go:21, plugins/paligo.go:185-195, plugins/filesystem.go:73-89
Conclusion
Developing custom target plugins is the primary mechanism for extending the reach of 2ms. By implementing the IPlugin interface and adhering to the asynchronous channel-based communication model, developers can enable 2ms to scan any data source for potential secrets. Success in plugin development relies on efficient resource traversal, robust error handling via the provided channels, and proper transformation of source-specific data into the standardized item format.
Sources: README.md:394-396, CONTRIBUTING.md:28-28