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 2 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
16 changes: 12 additions & 4 deletions pkg/fanal/analyzer/secret/secret.go
Expand Up @@ -39,6 +39,7 @@ 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.

Expand All @@ -48,22 +49,29 @@ type SecretAnalyzer struct {
}

func RegisterSecretAnalyzer(opt ScannerOption) error {
a, err := newSecretAnalyzer(opt.ConfigPath)
a, err := newSecretAnalyzer(opt)
if err != nil {
return xerrors.Errorf("secret scanner init error: %w", err)
}
analyzer.RegisterAnalyzer(a)
return nil
}

func newSecretAnalyzer(configPath string) (SecretAnalyzer, error) {
s, err := secret.NewScanner(configPath)
func newSecretAnalyzer(opt ScannerOption) (SecretAnalyzer, error) {
if opt.Config != nil {
s, err := secret.NewScannerByConfig(*opt.Config)
if err != nil {
return SecretAnalyzer{}, xerrors.Errorf("secret scanner error: %w", err)
}
return SecretAnalyzer{scanner: s}, nil
}
s, err := secret.NewScanner(opt.ConfigPath)
if err != nil {
return SecretAnalyzer{}, xerrors.Errorf("secret scanner error: %w", err)
}
return SecretAnalyzer{
scanner: s,
configPath: configPath,
configPath: opt.ConfigPath,
}, nil
}

Expand Down
34 changes: 31 additions & 3 deletions 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 All @@ -115,6 +117,32 @@ func TestSecretAnalyzer(t *testing.T) {
},
},
},
{
name: "return results with config",
configPath: "",
config: &secret.Config{
CustomRules: []secret.Rule{
{
ID: "rule1",
Category: "general",
Title: "Generic Rule",
Severity: "HIGH",
Regex: secret.MustCompile("(?i)(?P<key>(secret))(=|:).{0,5}['\"](?P<secret>[0-9a-zA-Z\\-_=]{8,64})['\"]"),
SecretGroupName: "secret",
},
},
},
filePath: "testdata/secret.txt",
dir: ".",
want: &analyzer.AnalysisResult{
Secrets: []types.Secret{
{
FilePath: "testdata/secret.txt",
Findings: []types.SecretFinding{wantFinding1, wantFinding2},
},
},
},
},
{
name: "image scan return result",
configPath: "testdata/image-config.yaml",
Expand Down Expand Up @@ -150,7 +178,7 @@ func TestSecretAnalyzer(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a, err := newSecretAnalyzer(tt.configPath)
a, err := newSecretAnalyzer(ScannerOption{tt.configPath, tt.config})
require.NoError(t, err)
content, err := os.Open(tt.filePath)
require.NoError(t, err)
Expand Down Expand Up @@ -205,7 +233,7 @@ func TestSecretRequire(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a, err := newSecretAnalyzer("")
a, err := newSecretAnalyzer(ScannerOption{"", nil})
require.NoError(t, err)

fi, err := os.Stat(tt.filePath)
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