Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add configuration parsing for custom detectors
- Loading branch information
1 parent
861ad05
commit a159c70
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/trufflesecurity/trufflehog/v3/pkg/custom_detectors" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/custom_detectorspb" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/protoyaml" | ||
) | ||
|
||
// Config holds user supplied configuration. | ||
type Config struct { | ||
Detectors []detectors.Detector | ||
} | ||
|
||
// Read parses a given filename into a Config. | ||
func Read(filename string) (*Config, error) { | ||
if filename == "" { | ||
return &Config{}, nil | ||
} | ||
input, err := os.ReadFile(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return NewYAML(input) | ||
} | ||
|
||
// NewYAML parses the given YAML data into a Config. | ||
func NewYAML(input []byte) (*Config, error) { | ||
// Parse the raw YAML into a structure. | ||
var messages custom_detectorspb.CustomDetectors | ||
if err := protoyaml.UnmarshalStrict(input, &messages); err != nil { | ||
return nil, err | ||
} | ||
// Convert the structured YAML into detectors. | ||
var detectors []detectors.Detector | ||
for _, detectorConfig := range messages.Detectors { | ||
detector, err := custom_detectors.NewWebhookCustomRegex(detectorConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
detectors = append(detectors, detector) | ||
} | ||
return &Config{ | ||
Detectors: detectors, | ||
}, nil | ||
} |