Workers and Concurrency Model
Relevant source files
The following files were used as context for generating this wiki page:
The "Too Many Secrets" (2ms) tool employs a highly concurrent architecture designed to handle large-scale data scanning across multiple platforms efficiently. The concurrency model is built on Go's primitives, specifically goroutines and channels, to decouple data retrieval, secret detection, and post-processing tasks like validation and scoring.
The core of this model lies in a pipeline of workers that process ISourceItem objects—abstractions for scan targets like files or wiki pages—and transform them into actionable secret reports. This asynchronous approach allows the system to fetch new items from a source while simultaneously analyzing previously retrieved content for sensitive information.
Sources: cmd/workers.go:1-10, plugins/confluence.go:40-60
Pipeline Architecture
The concurrency model is structured as a multi-stage pipeline where each stage is handled by specialized workers. These stages are linked by thread-safe channels that manage the flow of data between discovery, detection, and enrichment.
Pipeline Flow Diagram
The following diagram illustrates how data flows from the initial plugin scan through the various processing workers until it reaches the final report.
Core Worker Modules
Item Processing (processItems)
The processItems worker is the entry point for content analysis. It listens on the channels.Items channel for any ISourceItem produced by the active plugin. For every item received, it increments the total items scanned in the report and spawns a new goroutine to execute engine.Detect. This ensures that multiple items are analyzed for secrets in parallel.
Sources: cmd/workers.go:10-21
Secret Routing (processSecrets)
Once the engine detects a potential secret, the processSecrets worker handles the initial categorization and routing. It is responsible for:
- Incrementing the total secrets found count.
- Forwarding the secret to the extras worker for metadata enrichment.
- Routing the secret to either the validation channel or the basic scoring channel based on the global
validateVarflag. - Organizing secrets into the report results map keyed by the secret's unique ID.
Sources: cmd/workers.go:23-39, cmd/workers_test.go:80-100
Validation and Scoring
Post-detection processing is split based on whether the user requested active validation of the discovered secrets.
| Worker | Function | Concurrency Strategy |
|---|---|---|
processValidationAndScoreWithValidation | Registers secrets for active validation and triggers scoring based on the validation result. | Uses sync.WaitGroup to track two concurrent tasks per secret: registration and scoring. |
processScoreWithoutValidation | Assigns a CVSS score to the secret based on the rule category and type without external validation. | Spawns a goroutine per secret to calculate the score using the engine. |
processSecretsExtras | Adds additional context (e.g., JWT payload decoding) to the secret. | Uses extra.AddExtraToSecret in parallel goroutines. |
Sources: cmd/workers.go:41-78, engine/engine.go:115-125
Resource Limitation and Throttling
To prevent resource exhaustion (such as hitting API rate limits or exceeding memory bounds), the project implements throttling mechanisms within individual plugins.
Request Limiting in Plugins
Plugins like the ConfluencePlugin utilize a "Limit" channel as a semaphore to control the number of concurrent outgoing requests. Before scanning a page, a worker must send an empty struct into the Limit channel, and it receives from the channel once the scan is complete.
// Example of semaphore pattern in confluence.go
p.Limit <- struct{}{}
go func(page ConfluencePage) {
p.scanPageAllVersions(wg, page, space)
<-p.Limit
}(page)
Sources: plugins/confluence.go:17-25, plugins/confluence.go:95-105
Concurrency Constants
The system defines hardcoded limits to balance performance and stability:
confluenceMaxRequests: 500 (Maximum concurrent requests for Confluence scans).confluenceDefaultWindow: 25 (Pagination size for fetching spaces and pages).
Sources: plugins/confluence.go:15-18
Synchronization and Life Cycle
The lifecycle of the worker pipeline is managed through a hierarchy of sync.WaitGroup objects. This ensures that the application does not exit or attempt to generate a report until all background processing is complete.
- Main WaitGroup: Found in
channels.WaitGroup, it tracks the top-level worker functions (processItems,processSecrets, etc.). - Local WaitGroups: Used within workers (e.g.,
wgItems,wgExtras,wgValidation) to track individual concurrent tasks spawned for specific items or secrets. - Channel Closing: Stages are cascaded; for instance,
processItemswaits for allDetectcalls to finish before closingsecretsChan, which then signalsprocessSecretsto wrap up.
Sources: cmd/workers.go:14-20, cmd/workers.go:34-38, plugins/confluence.go:53-57
Conclusion
The concurrency model of 2ms efficiently distributes the workload of secret detection across multiple specialized workers. By utilizing a pipeline of channels and goroutines, it achieves high throughput while maintaining control over external resource usage through semaphore-based throttling. This architecture allows the tool to remain responsive even when scanning large local filesystems or extensive remote wiki instances.