Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(secret): Secret analyzer initialization using config object #2748

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions pkg/fanal/analyzer/secret/secret.go
Expand Up @@ -39,11 +39,12 @@ var (

type ScannerOption struct {
ConfigPath string
knqyf263 marked this conversation as resolved.
Show resolved Hide resolved
Config *secret.Config
}

// SecretAnalyzer is an analyzer for secrets
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can just export scanner in SecretAnalyzer. You can initialize SecretAnalyzer with your scanner rather than calling RegisterSecretAnalyzer.

type SecretAnalyzer struct {
scanner secret.Scanner
Scanner secret.Scanner
configPath string
}

Expand All @@ -62,7 +63,7 @@ func newSecretAnalyzer(configPath string) (SecretAnalyzer, error) {
return SecretAnalyzer{}, xerrors.Errorf("secret scanner error: %w", err)
}
return SecretAnalyzer{
scanner: s,
Scanner: s,
configPath: configPath,
}, nil
}
Expand All @@ -87,7 +88,7 @@ func (a SecretAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInput)
filePath = fmt.Sprintf("/%s", filePath)
}

result := a.scanner.Scan(secret.ScanArgs{
result := a.Scanner.Scan(secret.ScanArgs{
FilePath: filePath,
Content: content,
})
Expand Down Expand Up @@ -154,7 +155,7 @@ func (a SecretAnalyzer) Required(filePath string, fi os.FileInfo) bool {
return false
}

if a.scanner.AllowPath(filePath) {
if a.Scanner.AllowPath(filePath) {
return false
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/fanal/analyzer/secret/secret_test.go
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
"github.com/aquasecurity/trivy/pkg/fanal/secret"
"github.com/aquasecurity/trivy/pkg/fanal/types"
)

Expand Down Expand Up @@ -97,12 +98,13 @@ func TestSecretAnalyzer(t *testing.T) {
tests := []struct {
name string
configPath string
config *secret.Config
filePath string
dir string
want *analyzer.AnalysisResult
}{
{
name: "return results",
name: "return results with config file",
configPath: "testdata/config.yaml",
filePath: "testdata/secret.txt",
dir: ".",
Expand Down
11 changes: 7 additions & 4 deletions pkg/fanal/secret/scanner.go
Expand Up @@ -287,14 +287,17 @@ func NewScanner(configPath string) (Scanner, error) {

log.Logger.Infof("Loading %s for secret scanning...", configPath)

// reset global
global = Global{}

var config Config
if err = yaml.NewDecoder(f).Decode(&config); err != nil {
return Scanner{}, xerrors.Errorf("secrets config decode error: %w", err)
}

return NewScannerByConfig(config)
}

func NewScannerByConfig(config Config) (Scanner, error) {
global := &Global{}

enabledRules := builtinRules
if len(config.EnableBuiltinRuleIDs) != 0 {
// Enable only specified built-in rules
Expand All @@ -319,7 +322,7 @@ func NewScanner(configPath string) (Scanner, error) {

global.ExcludeBlock = config.ExcludeBlock

return Scanner{Global: &global}, nil
return Scanner{Global: global}, nil
}

type ScanArgs struct {
Expand Down