diff --git a/pkg/commands/app.go b/pkg/commands/app.go index eb780f57b..4fe416482 100644 --- a/pkg/commands/app.go +++ b/pkg/commands/app.go @@ -14,7 +14,7 @@ func NewApp(version string, commitSHA string, engine engine.Engine) *cobra.Comma rootCmd := NewRootCommand() rootCmd.AddCommand( NewCompletionCommand(), - NewProcessingWorkerCommand(), + NewProcessingWorkerCommand(engine), NewInitCommand(), NewScanCommand(engine), NewIgnoreCommand(), diff --git a/pkg/commands/process/orchestrator/worker/worker.go b/pkg/commands/process/orchestrator/worker/worker.go index 675a226eb..bb0b8d398 100644 --- a/pkg/commands/process/orchestrator/worker/worker.go +++ b/pkg/commands/process/orchestrator/worker/worker.go @@ -20,6 +20,7 @@ import ( "github.com/bearer/bearer/pkg/commands/debugprofile" config "github.com/bearer/bearer/pkg/commands/process/settings" "github.com/bearer/bearer/pkg/detectors" + "github.com/bearer/bearer/pkg/engine" "github.com/bearer/bearer/pkg/report/writer" "github.com/bearer/bearer/pkg/scanner" "github.com/bearer/bearer/pkg/scanner/stats" @@ -32,6 +33,7 @@ var ErrorTimeoutReached = errors.New("file processing time exceeded") type Worker struct { debug bool classifer *classification.Classifier + engine engine.Engine enabledScanners []string sastScanner *scanner.Scanner skipTest bool @@ -43,6 +45,10 @@ func (worker *Worker) Setup(config config.Config) error { worker.skipTest = config.Scan.SkipTest if slices.Contains(worker.enabledScanners, "sast") { + if err := worker.engine.Initialize(config.LogLevel); err != nil { + return err + } + classifier, err := classification.NewClassifier(&classification.Config{Config: config}) if err != nil { return err @@ -53,7 +59,7 @@ func (worker *Worker) Setup(config config.Config) error { return err } - sastScanner, err := scanner.New(classifier.Schema, config.Rules) + sastScanner, err := scanner.New(worker.engine, classifier.Schema, config.Rules) if err != nil { return err } @@ -102,10 +108,12 @@ func (worker *Worker) Close() { if worker.sastScanner != nil { worker.sastScanner.Close() } + + worker.engine.Close() } -func Start(parentProcessID int, port string) error { - worker := Worker{} +func Start(parentProcessID int, port string, engine engine.Engine) error { + worker := Worker{engine: engine} ctx, cancelProcess := signal.NotifyContext(context.Background(), os.Interrupt) go monitorParentProcess(ctx, parentProcessID, cancelProcess) diff --git a/pkg/commands/processing_worker.go b/pkg/commands/processing_worker.go index 56875338b..f918c276e 100644 --- a/pkg/commands/processing_worker.go +++ b/pkg/commands/processing_worker.go @@ -9,11 +9,12 @@ import ( "github.com/bearer/bearer/pkg/commands/debugprofile" "github.com/bearer/bearer/pkg/commands/process/orchestrator/worker" + "github.com/bearer/bearer/pkg/engine" "github.com/bearer/bearer/pkg/flag" "github.com/bearer/bearer/pkg/util/output" ) -func NewProcessingWorkerCommand() *cobra.Command { +func NewProcessingWorkerCommand(engine engine.Engine) *cobra.Command { flags := flag.Flags{flag.WorkerFlagGroup} cmd := &cobra.Command{ @@ -43,7 +44,7 @@ func NewProcessingWorkerCommand() *cobra.Command { } log.Debug().Msgf("running scan worker on port `%s`", options.WorkerOptions.Port) - return worker.Start(options.WorkerOptions.ParentProcessID, options.WorkerOptions.Port) + return worker.Start(options.WorkerOptions.ParentProcessID, options.WorkerOptions.Port, engine) }, Hidden: true, SilenceErrors: true, diff --git a/pkg/languages/golang/.snapshots/TestImport--main.yml b/pkg/languages/golang/.snapshots/TestImport-main.yml similarity index 100% rename from pkg/languages/golang/.snapshots/TestImport--main.yml rename to pkg/languages/golang/.snapshots/TestImport-main.yml diff --git a/pkg/languages/golang/.snapshots/TestScope--scope.yml b/pkg/languages/golang/.snapshots/TestScope-scope.yml similarity index 100% rename from pkg/languages/golang/.snapshots/TestScope--scope.yml rename to pkg/languages/golang/.snapshots/TestScope-scope.yml diff --git a/pkg/languages/golang/.snapshots/flow/TestFlow--different-line.yml b/pkg/languages/golang/.snapshots/flow/TestFlow-different-line.yml similarity index 100% rename from pkg/languages/golang/.snapshots/flow/TestFlow--different-line.yml rename to pkg/languages/golang/.snapshots/flow/TestFlow-different-line.yml diff --git a/pkg/languages/golang/.snapshots/flow/TestFlow--same-line.yml b/pkg/languages/golang/.snapshots/flow/TestFlow-same-line.yml similarity index 100% rename from pkg/languages/golang/.snapshots/flow/TestFlow--same-line.yml rename to pkg/languages/golang/.snapshots/flow/TestFlow-same-line.yml diff --git a/pkg/languages/golang/golang_test.go b/pkg/languages/golang/golang_test.go index 877f36af3..c27bec307 100644 --- a/pkg/languages/golang/golang_test.go +++ b/pkg/languages/golang/golang_test.go @@ -4,6 +4,7 @@ import ( _ "embed" "testing" + "github.com/bearer/bearer/pkg/languages/golang" "github.com/bearer/bearer/pkg/languages/testhelper" ) @@ -17,13 +18,13 @@ var scopeRule []byte var importRule []byte func TestFlow(t *testing.T) { - testhelper.GetRunner(t, loggerRule, "Go").RunTest(t, "./testdata/testcases/flow", ".snapshots/flow/") + testhelper.GetRunner(t, loggerRule, golang.Get()).RunTest(t, "./testdata/testcases/flow", ".snapshots/flow/") } func TestScope(t *testing.T) { - testhelper.GetRunner(t, scopeRule, "Go").RunTest(t, "./testdata/scope", ".snapshots/") + testhelper.GetRunner(t, scopeRule, golang.Get()).RunTest(t, "./testdata/scope", ".snapshots/") } func TestImport(t *testing.T) { - testhelper.GetRunner(t, importRule, "Go").RunTest(t, "./testdata/import", ".snapshots/") + testhelper.GetRunner(t, importRule, golang.Get()).RunTest(t, "./testdata/import", ".snapshots/") } diff --git a/pkg/languages/java/.snapshots/TestScope--scope.yml b/pkg/languages/java/.snapshots/TestScope-scope.yml similarity index 100% rename from pkg/languages/java/.snapshots/TestScope--scope.yml rename to pkg/languages/java/.snapshots/TestScope-scope.yml diff --git a/pkg/languages/java/.snapshots/flow/TestFlow--different-line.yml b/pkg/languages/java/.snapshots/flow/TestFlow-different-line.yml similarity index 100% rename from pkg/languages/java/.snapshots/flow/TestFlow--different-line.yml rename to pkg/languages/java/.snapshots/flow/TestFlow-different-line.yml diff --git a/pkg/languages/java/.snapshots/flow/TestFlow--same-line.yml b/pkg/languages/java/.snapshots/flow/TestFlow-same-line.yml similarity index 100% rename from pkg/languages/java/.snapshots/flow/TestFlow--same-line.yml rename to pkg/languages/java/.snapshots/flow/TestFlow-same-line.yml diff --git a/pkg/languages/java/java_test.go b/pkg/languages/java/java_test.go index 084921d7d..b3f48bc05 100644 --- a/pkg/languages/java/java_test.go +++ b/pkg/languages/java/java_test.go @@ -18,11 +18,11 @@ var loggerRule []byte var scopeRule []byte func TestFlow(t *testing.T) { - testhelper.GetRunner(t, loggerRule, "Java").RunTest(t, "./testdata/testcases/flow", ".snapshots/flow/") + testhelper.GetRunner(t, loggerRule, java.Get()).RunTest(t, "./testdata/testcases/flow", ".snapshots/flow/") } func TestScope(t *testing.T) { - testhelper.GetRunner(t, scopeRule, "Java").RunTest(t, "./testdata/scope", ".snapshots/") + testhelper.GetRunner(t, scopeRule, java.Get()).RunTest(t, "./testdata/scope", ".snapshots/") } func TestPattern(t *testing.T) { diff --git a/pkg/languages/javascript/.snapshots/TestPatternVariables--main.yml b/pkg/languages/javascript/.snapshots/TestPatternVariables-main.yml similarity index 100% rename from pkg/languages/javascript/.snapshots/TestPatternVariables--main.yml rename to pkg/languages/javascript/.snapshots/TestPatternVariables-main.yml diff --git a/pkg/languages/javascript/.snapshots/TestScope--scope.yml b/pkg/languages/javascript/.snapshots/TestScope-scope.yml similarity index 100% rename from pkg/languages/javascript/.snapshots/TestScope--scope.yml rename to pkg/languages/javascript/.snapshots/TestScope-scope.yml diff --git a/pkg/languages/javascript/.snapshots/flow/TestFlow--assigment-expression.yml b/pkg/languages/javascript/.snapshots/flow/TestFlow-assigment-expression.yml similarity index 100% rename from pkg/languages/javascript/.snapshots/flow/TestFlow--assigment-expression.yml rename to pkg/languages/javascript/.snapshots/flow/TestFlow-assigment-expression.yml diff --git a/pkg/languages/javascript/.snapshots/flow/TestFlow--variable-declarator.yml b/pkg/languages/javascript/.snapshots/flow/TestFlow-variable-declarator.yml similarity index 100% rename from pkg/languages/javascript/.snapshots/flow/TestFlow--variable-declarator.yml rename to pkg/languages/javascript/.snapshots/flow/TestFlow-variable-declarator.yml diff --git a/pkg/languages/javascript/.snapshots/import/TestImport--import.yml b/pkg/languages/javascript/.snapshots/import/TestImport-import.yml similarity index 100% rename from pkg/languages/javascript/.snapshots/import/TestImport--import.yml rename to pkg/languages/javascript/.snapshots/import/TestImport-import.yml diff --git a/pkg/languages/javascript/.snapshots/object-deconstructing/TestObjectDeconstructing--deconstructing.yml b/pkg/languages/javascript/.snapshots/object-deconstructing/TestObjectDeconstructing-deconstructing.yml similarity index 100% rename from pkg/languages/javascript/.snapshots/object-deconstructing/TestObjectDeconstructing--deconstructing.yml rename to pkg/languages/javascript/.snapshots/object-deconstructing/TestObjectDeconstructing-deconstructing.yml diff --git a/pkg/languages/javascript/.snapshots/object-deconstructing/TestObjectDeconstructing--multiple_objects.yml b/pkg/languages/javascript/.snapshots/object-deconstructing/TestObjectDeconstructing-multiple_objects.yml similarity index 100% rename from pkg/languages/javascript/.snapshots/object-deconstructing/TestObjectDeconstructing--multiple_objects.yml rename to pkg/languages/javascript/.snapshots/object-deconstructing/TestObjectDeconstructing-multiple_objects.yml diff --git a/pkg/languages/javascript/.snapshots/string/TestString--concatanation.yml b/pkg/languages/javascript/.snapshots/string/TestString-concatanation.yml similarity index 100% rename from pkg/languages/javascript/.snapshots/string/TestString--concatanation.yml rename to pkg/languages/javascript/.snapshots/string/TestString-concatanation.yml diff --git a/pkg/languages/javascript/.snapshots/string/TestString--simple.yml b/pkg/languages/javascript/.snapshots/string/TestString-simple.yml similarity index 100% rename from pkg/languages/javascript/.snapshots/string/TestString--simple.yml rename to pkg/languages/javascript/.snapshots/string/TestString-simple.yml diff --git a/pkg/languages/javascript/.snapshots/string/TestString--single-quotes.yml b/pkg/languages/javascript/.snapshots/string/TestString-single-quotes.yml similarity index 100% rename from pkg/languages/javascript/.snapshots/string/TestString--single-quotes.yml rename to pkg/languages/javascript/.snapshots/string/TestString-single-quotes.yml diff --git a/pkg/languages/javascript/.snapshots/string/TestString--template-variable-reconciliation.yml b/pkg/languages/javascript/.snapshots/string/TestString-template-variable-reconciliation.yml similarity index 100% rename from pkg/languages/javascript/.snapshots/string/TestString--template-variable-reconciliation.yml rename to pkg/languages/javascript/.snapshots/string/TestString-template-variable-reconciliation.yml diff --git a/pkg/languages/javascript/.snapshots/string/TestString--template.yml b/pkg/languages/javascript/.snapshots/string/TestString-template.yml similarity index 100% rename from pkg/languages/javascript/.snapshots/string/TestString--template.yml rename to pkg/languages/javascript/.snapshots/string/TestString-template.yml diff --git a/pkg/languages/javascript/javascript_test.go b/pkg/languages/javascript/javascript_test.go index 2d47dbb84..f65edcc8f 100644 --- a/pkg/languages/javascript/javascript_test.go +++ b/pkg/languages/javascript/javascript_test.go @@ -4,6 +4,7 @@ import ( _ "embed" "testing" + "github.com/bearer/bearer/pkg/languages/javascript" "github.com/bearer/bearer/pkg/languages/testhelper" ) @@ -26,25 +27,25 @@ var patternVariablesRule []byte var scopeRule []byte func TestFlow(t *testing.T) { - testhelper.GetRunner(t, datatypeRule, "Javascript").RunTest(t, "./testdata/testcases/flow", ".snapshots/flow/") + testhelper.GetRunner(t, datatypeRule, javascript.Get()).RunTest(t, "./testdata/testcases/flow", ".snapshots/flow/") } func TestObjectDeconstructing(t *testing.T) { - testhelper.GetRunner(t, deconstructingRule, "Javascript").RunTest(t, "./testdata/testcases/object-deconstructing", ".snapshots/object-deconstructing/") + testhelper.GetRunner(t, deconstructingRule, javascript.Get()).RunTest(t, "./testdata/testcases/object-deconstructing", ".snapshots/object-deconstructing/") } func TestImport(t *testing.T) { - testhelper.GetRunner(t, importRule, "Javascript").RunTest(t, "./testdata/import", ".snapshots/import/") + testhelper.GetRunner(t, importRule, javascript.Get()).RunTest(t, "./testdata/import", ".snapshots/import/") } func TestString(t *testing.T) { - testhelper.GetRunner(t, insecureURLRule, "Javascript").RunTest(t, "./testdata/testcases/string", ".snapshots/string/") + testhelper.GetRunner(t, insecureURLRule, javascript.Get()).RunTest(t, "./testdata/testcases/string", ".snapshots/string/") } func TestPatternVariables(t *testing.T) { - testhelper.GetRunner(t, patternVariablesRule, "Javascript").RunTest(t, "./testdata/pattern_variables", ".snapshots/") + testhelper.GetRunner(t, patternVariablesRule, javascript.Get()).RunTest(t, "./testdata/pattern_variables", ".snapshots/") } func TestScope(t *testing.T) { - testhelper.GetRunner(t, scopeRule, "Javascript").RunTest(t, "./testdata/scope", ".snapshots/") + testhelper.GetRunner(t, scopeRule, javascript.Get()).RunTest(t, "./testdata/scope", ".snapshots/") } diff --git a/pkg/languages/php/.snapshots/TestConst--main.yml b/pkg/languages/php/.snapshots/TestConst-main.yml similarity index 100% rename from pkg/languages/php/.snapshots/TestConst--main.yml rename to pkg/languages/php/.snapshots/TestConst-main.yml diff --git a/pkg/languages/php/.snapshots/TestScope--scope.yml b/pkg/languages/php/.snapshots/TestScope-scope.yml similarity index 100% rename from pkg/languages/php/.snapshots/TestScope--scope.yml rename to pkg/languages/php/.snapshots/TestScope-scope.yml diff --git a/pkg/languages/php/.snapshots/flow/TestFlow--different-line.yml b/pkg/languages/php/.snapshots/flow/TestFlow-different-line.yml similarity index 100% rename from pkg/languages/php/.snapshots/flow/TestFlow--different-line.yml rename to pkg/languages/php/.snapshots/flow/TestFlow-different-line.yml diff --git a/pkg/languages/php/.snapshots/flow/TestFlow--same-line.yml b/pkg/languages/php/.snapshots/flow/TestFlow-same-line.yml similarity index 100% rename from pkg/languages/php/.snapshots/flow/TestFlow--same-line.yml rename to pkg/languages/php/.snapshots/flow/TestFlow-same-line.yml diff --git a/pkg/languages/php/php_test.go b/pkg/languages/php/php_test.go index a18dce93e..3dab31158 100644 --- a/pkg/languages/php/php_test.go +++ b/pkg/languages/php/php_test.go @@ -26,15 +26,15 @@ var scopeRule []byte var mdRule []byte func TestFlow(t *testing.T) { - testhelper.GetRunner(t, loggerRule, "PHP").RunTest(t, "./testdata/testcases/flow", ".snapshots/flow/") + testhelper.GetRunner(t, loggerRule, php.Get()).RunTest(t, "./testdata/testcases/flow", ".snapshots/flow/") } func TestScope(t *testing.T) { - testhelper.GetRunner(t, scopeRule, "PHP").RunTest(t, "./testdata/scope", ".snapshots/") + testhelper.GetRunner(t, scopeRule, php.Get()).RunTest(t, "./testdata/scope", ".snapshots/") } func TestConst(t *testing.T) { - testhelper.GetRunner(t, mdRule, "PHP").RunTest(t, "./testdata/md", ".snapshots/") + testhelper.GetRunner(t, mdRule, php.Get()).RunTest(t, "./testdata/md", ".snapshots/") } func TestAnalyzer(t *testing.T) { diff --git a/pkg/languages/python/.snapshots/TestDatatypes--different-line.yml b/pkg/languages/python/.snapshots/TestDatatypes-different-line.yml similarity index 100% rename from pkg/languages/python/.snapshots/TestDatatypes--different-line.yml rename to pkg/languages/python/.snapshots/TestDatatypes-different-line.yml diff --git a/pkg/languages/python/.snapshots/TestDatatypes--same-line.yml b/pkg/languages/python/.snapshots/TestDatatypes-same-line.yml similarity index 100% rename from pkg/languages/python/.snapshots/TestDatatypes--same-line.yml rename to pkg/languages/python/.snapshots/TestDatatypes-same-line.yml diff --git a/pkg/languages/python/.snapshots/TestFlow--flow.yml b/pkg/languages/python/.snapshots/TestFlow-flow.yml similarity index 100% rename from pkg/languages/python/.snapshots/TestFlow--flow.yml rename to pkg/languages/python/.snapshots/TestFlow-flow.yml diff --git a/pkg/languages/python/.snapshots/TestImport--import.yml b/pkg/languages/python/.snapshots/TestImport-import.yml similarity index 100% rename from pkg/languages/python/.snapshots/TestImport--import.yml rename to pkg/languages/python/.snapshots/TestImport-import.yml diff --git a/pkg/languages/python/.snapshots/TestPair--pair.yml b/pkg/languages/python/.snapshots/TestPair-pair.yml similarity index 100% rename from pkg/languages/python/.snapshots/TestPair--pair.yml rename to pkg/languages/python/.snapshots/TestPair-pair.yml diff --git a/pkg/languages/python/.snapshots/TestScope--scope.yml b/pkg/languages/python/.snapshots/TestScope-scope.yml similarity index 100% rename from pkg/languages/python/.snapshots/TestScope--scope.yml rename to pkg/languages/python/.snapshots/TestScope-scope.yml diff --git a/pkg/languages/python/.snapshots/TestSubscript--subscript.yml b/pkg/languages/python/.snapshots/TestSubscript-subscript.yml similarity index 100% rename from pkg/languages/python/.snapshots/TestSubscript--subscript.yml rename to pkg/languages/python/.snapshots/TestSubscript-subscript.yml diff --git a/pkg/languages/python/python_test.go b/pkg/languages/python/python_test.go index d88197e79..bce26213d 100644 --- a/pkg/languages/python/python_test.go +++ b/pkg/languages/python/python_test.go @@ -4,6 +4,7 @@ import ( _ "embed" "testing" + "github.com/bearer/bearer/pkg/languages/python" "github.com/bearer/bearer/pkg/languages/testhelper" ) @@ -26,25 +27,25 @@ var subscriptRule []byte var pairRule []byte func TestDatatypes(t *testing.T) { - testhelper.GetRunner(t, datatypesRule, "python").RunTest(t, "./testdata/datatypes", ".snapshots/") + testhelper.GetRunner(t, datatypesRule, python.Get()).RunTest(t, "./testdata/datatypes", ".snapshots/") } func TestScope(t *testing.T) { - testhelper.GetRunner(t, scopeRule, "python").RunTest(t, "./testdata/scope", ".snapshots/") + testhelper.GetRunner(t, scopeRule, python.Get()).RunTest(t, "./testdata/scope", ".snapshots/") } func TestFlow(t *testing.T) { - testhelper.GetRunner(t, flowRule, "python").RunTest(t, "./testdata/flow", ".snapshots/") + testhelper.GetRunner(t, flowRule, python.Get()).RunTest(t, "./testdata/flow", ".snapshots/") } func TestImport(t *testing.T) { - testhelper.GetRunner(t, importRule, "python").RunTest(t, "./testdata/import", ".snapshots/") + testhelper.GetRunner(t, importRule, python.Get()).RunTest(t, "./testdata/import", ".snapshots/") } func TestSubscript(t *testing.T) { - testhelper.GetRunner(t, subscriptRule, "python").RunTest(t, "./testdata/subscript", ".snapshots/") + testhelper.GetRunner(t, subscriptRule, python.Get()).RunTest(t, "./testdata/subscript", ".snapshots/") } func TestPair(t *testing.T) { - testhelper.GetRunner(t, pairRule, "python").RunTest(t, "./testdata/pair", ".snapshots/") + testhelper.GetRunner(t, pairRule, python.Get()).RunTest(t, "./testdata/pair", ".snapshots/") } diff --git a/pkg/languages/ruby/.snapshots/TestPatternVariables--main.yml b/pkg/languages/ruby/.snapshots/TestPatternVariables-main.yml similarity index 100% rename from pkg/languages/ruby/.snapshots/TestPatternVariables--main.yml rename to pkg/languages/ruby/.snapshots/TestPatternVariables-main.yml diff --git a/pkg/languages/ruby/.snapshots/TestRuby--call.yml b/pkg/languages/ruby/.snapshots/TestRuby-call.yml similarity index 100% rename from pkg/languages/ruby/.snapshots/TestRuby--call.yml rename to pkg/languages/ruby/.snapshots/TestRuby-call.yml diff --git a/pkg/languages/ruby/.snapshots/TestRuby--insecure-url.yml b/pkg/languages/ruby/.snapshots/TestRuby-insecure-url.yml similarity index 100% rename from pkg/languages/ruby/.snapshots/TestRuby--insecure-url.yml rename to pkg/languages/ruby/.snapshots/TestRuby-insecure-url.yml diff --git a/pkg/languages/ruby/.snapshots/TestRuby--object-variable-reconciliation.yml b/pkg/languages/ruby/.snapshots/TestRuby-object-variable-reconciliation.yml similarity index 100% rename from pkg/languages/ruby/.snapshots/TestRuby--object-variable-reconciliation.yml rename to pkg/languages/ruby/.snapshots/TestRuby-object-variable-reconciliation.yml diff --git a/pkg/languages/ruby/.snapshots/TestRuby--object.yml b/pkg/languages/ruby/.snapshots/TestRuby-object.yml similarity index 100% rename from pkg/languages/ruby/.snapshots/TestRuby--object.yml rename to pkg/languages/ruby/.snapshots/TestRuby-object.yml diff --git a/pkg/languages/ruby/.snapshots/TestScope--scope.yml b/pkg/languages/ruby/.snapshots/TestScope-scope.yml similarity index 100% rename from pkg/languages/ruby/.snapshots/TestScope--scope.yml rename to pkg/languages/ruby/.snapshots/TestScope-scope.yml diff --git a/pkg/languages/ruby/ruby_test.go b/pkg/languages/ruby/ruby_test.go index 03a04f0f8..1bd952319 100644 --- a/pkg/languages/ruby/ruby_test.go +++ b/pkg/languages/ruby/ruby_test.go @@ -4,6 +4,7 @@ import ( _ "embed" "testing" + "github.com/bearer/bearer/pkg/languages/ruby" "github.com/bearer/bearer/pkg/languages/testhelper" ) @@ -17,13 +18,13 @@ var patternVariablesRule []byte var scopeRule []byte func TestRuby(t *testing.T) { - testhelper.GetRunner(t, loggerRule, "Ruby").RunTest(t, "./testdata/testcases", ".snapshots/") + testhelper.GetRunner(t, loggerRule, ruby.Get()).RunTest(t, "./testdata/testcases", ".snapshots/") } func TestPatternVariables(t *testing.T) { - testhelper.GetRunner(t, patternVariablesRule, "Ruby").RunTest(t, "./testdata/pattern_variables", ".snapshots/") + testhelper.GetRunner(t, patternVariablesRule, ruby.Get()).RunTest(t, "./testdata/pattern_variables", ".snapshots/") } func TestScope(t *testing.T) { - testhelper.GetRunner(t, scopeRule, "Ruby").RunTest(t, "./testdata/scope", ".snapshots/") + testhelper.GetRunner(t, scopeRule, ruby.Get()).RunTest(t, "./testdata/scope", ".snapshots/") } diff --git a/pkg/languages/testhelper/testhelper.go b/pkg/languages/testhelper/testhelper.go index 550c1919d..ee67e6eda 100644 --- a/pkg/languages/testhelper/testhelper.go +++ b/pkg/languages/testhelper/testhelper.go @@ -4,101 +4,85 @@ import ( "context" "os" "path/filepath" + "runtime" "strings" "testing" + "time" "github.com/bradleyjkemp/cupaloy" "github.com/hhatto/gocloc" "github.com/rs/zerolog" + "github.com/rs/zerolog/log" "gopkg.in/yaml.v3" + "github.com/bearer/bearer/pkg/classification" "github.com/bearer/bearer/pkg/commands" "github.com/bearer/bearer/pkg/commands/process/filelist" "github.com/bearer/bearer/pkg/commands/process/filelist/files" - "github.com/bearer/bearer/pkg/commands/process/orchestrator/work" - "github.com/bearer/bearer/pkg/commands/process/orchestrator/worker" "github.com/bearer/bearer/pkg/commands/process/settings" settingsloader "github.com/bearer/bearer/pkg/commands/process/settings/loader" "github.com/bearer/bearer/pkg/commands/process/settings/rules" - engine "github.com/bearer/bearer/pkg/engine/implementation" + "github.com/bearer/bearer/pkg/detectors" + engine "github.com/bearer/bearer/pkg/engine" + engineimpl "github.com/bearer/bearer/pkg/engine/implementation" "github.com/bearer/bearer/pkg/flag" - "github.com/bearer/bearer/pkg/languages" "github.com/bearer/bearer/pkg/report/output" + "github.com/bearer/bearer/pkg/report/writer" + "github.com/bearer/bearer/pkg/scanner" + "github.com/bearer/bearer/pkg/scanner/language" "github.com/bearer/bearer/pkg/types" util "github.com/bearer/bearer/pkg/util/output" "github.com/bearer/bearer/pkg/version_check" ) type Runner struct { - config settings.Config - worker worker.Worker + engine engine.Engine + config settings.Config + classifier *classification.Classifier + scanner *scanner.Scanner } -func GetRunner(t *testing.T, ruleBytes []byte, lang string) *Runner { +func GetRunner(t *testing.T, ruleBytes []byte, lang language.Language) *Runner { zerolog.SetGlobalLevel(zerolog.InfoLevel) + log.Logger = log.Output(zerolog.ConsoleWriter{ + Out: os.Stderr, + FormatTimestamp: func(i interface{}) string { + timestamp, _ := time.Parse(time.RFC3339, i.(string)) + return timestamp.Format("2006-01-02 15:04:05") + }, + }) - err := commands.ScanFlags.BindForConfigInit(commands.NewScanCommand(nil)) - if err != nil { - t.Fatalf("failed to bind flags: %s", err) - } + engine := engineimpl.New([]language.Language{lang}) + config := buildConfig(t, engine, ruleBytes) - configFlags, err := commands.ScanFlags.ToOptions([]string{}) - if err != nil { - t.Fatalf("failed to generate default flags: %s", err) + if err := engine.Initialize("trace"); err != nil { + t.Fatalf("failed to initialize engine: %s", err) } - configFlags.Format = flag.FormatYAML - configFlags.Report = flag.ReportSecurity - configFlags.Quiet = true - meta := &version_check.VersionMeta{ - Rules: version_check.RuleVersionMeta{ - Packages: make(map[string]string), - }, - Binary: version_check.BinaryVersionMeta{ - Latest: true, - Message: "", - }, - } - - engine := engine.New(languages.Default()) - config, err := settingsloader.FromOptions(configFlags, meta, engine) + classifier, err := classification.NewClassifier(&classification.Config{Config: config}) if err != nil { - t.Fatalf("failed to generate default scan settings: %s", err) + t.Fatalf("failed to create classifier: %s", err) } - config.Rules = getRulesFromYaml(t, ruleBytes) - - worker := worker.Worker{} - err = worker.Setup(config) + scanner, err := scanner.New(engine, classifier.Schema, config.Rules) if err != nil { - t.Fatalf("failed to setup scan worker: %s", err) + t.Fatalf("failed to create scanner: %s", err) } runner := &Runner{ - worker: worker, - config: config, - } + engine: engine, + config: config, + classifier: classifier, + scanner: scanner, + } + runtime.SetFinalizer(runner, func(runner *Runner) { + runner.scanner.Close() + runner.engine.Close() + }) return runner } -func getRulesFromYaml(t *testing.T, ruleBytes []byte) map[string]*settings.Rule { - var ruleDefinition settings.RuleDefinition - err := yaml.Unmarshal(ruleBytes, &ruleDefinition) - if err != nil { - t.Fatalf("failed to unmarshal rule %s", err) - } - - definitions := map[string]settings.RuleDefinition{ - ruleDefinition.Metadata.ID: ruleDefinition, - } - enabledRules := map[string]struct{}{ - ruleDefinition.Metadata.ID: {}, - } - - return rules.BuildRules(definitions, enabledRules) -} - func (runner *Runner) RunTest(t *testing.T, testdataPath string, snapshotPath string) { dummyGoclocLanguage := gocloc.Language{} dummyGoclocResult := gocloc.Result{ @@ -125,11 +109,9 @@ func (runner *Runner) RunTest(t *testing.T, testdataPath string, snapshotPath st } for _, file := range fileList.Files { - myfile := file - ext := filepath.Ext(file.FilePath) - testName := "/" + strings.TrimSuffix(file.FilePath, ext) + ".yml" - t.Run(testName, func(t *testing.T) { - runner.scanSingleFile(t, testdataPath, myfile, snapshotPath) + testName := strings.TrimSuffix(file.FilePath, filepath.Ext(file.FilePath)) + t.Run(testName, func(tt *testing.T) { + runner.scanSingleFile(tt, testdataPath, file, snapshotPath) }) } } @@ -146,15 +128,19 @@ func (runner *Runner) scanSingleFile(t *testing.T, testDataPath string, fileRela t.Fatalf("failed to get absolute path of report file: %s", err) } - _, err = runner.worker.Scan(context.Background(), work.ProcessRequest{ - File: fileRelativePath, - ReportPath: detectorsReportPath, - Repository: work.Repository{ - Dir: testDataPath, + if err = detectors.Extract( + context.Background(), + testDataPath, + fileRelativePath.FilePath, + &writer.Detectors{ + Classifier: runner.classifier, + File: detectorsReportFile, }, - }) - - if err != nil { + nil, + []string{"sast"}, + runner.scanner, + false, + ); err != nil { t.Fatalf("failed to do scan %s", err) } @@ -177,6 +163,63 @@ func (runner *Runner) scanSingleFile(t *testing.T, testDataPath string, fileRela t.Fatalf("failed to encoded to yaml: %s", err) } - cupaloyCopy := cupaloy.NewDefaultConfig().WithOptions(cupaloy.SnapshotSubdirectory(snapshotsPath)) - cupaloyCopy.SnapshotT(t, report) + cupaloy.NewDefaultConfig().WithOptions( + cupaloy.SnapshotSubdirectory(snapshotsPath), + cupaloy.SnapshotFileExtension(".yml"), + ).SnapshotT(t, report) +} + +func buildConfig(t *testing.T, engine engine.Engine, ruleBytes []byte) settings.Config { + err := commands.ScanFlags.BindForConfigInit(commands.NewScanCommand(nil)) + if err != nil { + t.Fatalf("failed to bind flags: %s", err) + } + + configFlags, err := commands.ScanFlags.ToOptions([]string{}) + if err != nil { + t.Fatalf("failed to generate default flags: %s", err) + } + configFlags.Format = flag.FormatYAML + configFlags.Report = flag.ReportSecurity + configFlags.Quiet = true + configFlags.DisableDefaultRules = true + configFlags.ExternalRuleDir = []string{} + configFlags.DisableVersionCheck = true + configFlags.IgnoreGit = true + + meta := &version_check.VersionMeta{ + Rules: version_check.RuleVersionMeta{ + Packages: make(map[string]string), + }, + Binary: version_check.BinaryVersionMeta{ + Latest: true, + Message: "", + }, + } + + config, err := settingsloader.FromOptions(configFlags, meta, engine) + if err != nil { + t.Fatalf("failed to generate default scan settings: %s", err) + } + + config.Rules = getRulesFromYaml(t, ruleBytes) + + return config +} + +func getRulesFromYaml(t *testing.T, ruleBytes []byte) map[string]*settings.Rule { + var ruleDefinition settings.RuleDefinition + err := yaml.Unmarshal(ruleBytes, &ruleDefinition) + if err != nil { + t.Fatalf("failed to unmarshal rule %s", err) + } + + definitions := map[string]settings.RuleDefinition{ + ruleDefinition.Metadata.ID: ruleDefinition, + } + enabledRules := map[string]struct{}{ + ruleDefinition.Metadata.ID: {}, + } + + return rules.BuildRules(definitions, enabledRules) } diff --git a/pkg/scanner/detectors/testhelper/testhelper.go b/pkg/scanner/detectors/testhelper/testhelper.go index 34b0d0101..605dcecef 100644 --- a/pkg/scanner/detectors/testhelper/testhelper.go +++ b/pkg/scanner/detectors/testhelper/testhelper.go @@ -4,9 +4,11 @@ import ( "context" "os" "testing" + "time" "github.com/bradleyjkemp/cupaloy" "github.com/rs/zerolog" + "github.com/rs/zerolog/log" "gopkg.in/yaml.v3" "github.com/bearer/bearer/pkg/classification" @@ -37,6 +39,13 @@ func RunTest( fileName string, ) { zerolog.SetGlobalLevel(zerolog.InfoLevel) + log.Logger = log.Output(zerolog.ConsoleWriter{ + Out: os.Stderr, + FormatTimestamp: func(i interface{}) string { + timestamp, _ := time.Parse(time.RFC3339, i.(string)) + return timestamp.Format("2006-01-02 15:04:05") + }, + }) t.Run(name, func(tt *testing.T) { classifier, err := classification.NewClassifier(&classification.Config{ diff --git a/pkg/scanner/scanner.go b/pkg/scanner/scanner.go index c418a8fab..56d3c40e0 100644 --- a/pkg/scanner/scanner.go +++ b/pkg/scanner/scanner.go @@ -7,12 +7,7 @@ import ( schemaclassifier "github.com/bearer/bearer/pkg/classification/schema" "github.com/bearer/bearer/pkg/commands/process/settings" - "github.com/bearer/bearer/pkg/languages/golang" - "github.com/bearer/bearer/pkg/languages/java" - "github.com/bearer/bearer/pkg/languages/javascript" - "github.com/bearer/bearer/pkg/languages/php" - "github.com/bearer/bearer/pkg/languages/python" - "github.com/bearer/bearer/pkg/languages/ruby" + "github.com/bearer/bearer/pkg/engine" "github.com/bearer/bearer/pkg/report" reportdetections "github.com/bearer/bearer/pkg/report/detections" "github.com/bearer/bearer/pkg/report/detectors" @@ -21,7 +16,6 @@ import ( customruletypes "github.com/bearer/bearer/pkg/scanner/detectors/customrule/types" "github.com/bearer/bearer/pkg/scanner/detectors/datatype" detectortypes "github.com/bearer/bearer/pkg/scanner/detectors/types" - "github.com/bearer/bearer/pkg/scanner/language" "github.com/bearer/bearer/pkg/util/file" "github.com/bearer/bearer/pkg/util/pluralize" @@ -33,15 +27,12 @@ type Scanner struct { languageScanners []*languagescanner.Scanner } -func New(schemaClassifier *schemaclassifier.Classifier, rules map[string]*settings.Rule) (*Scanner, error) { - languages := []language.Language{ - java.Get(), - javascript.Get(), - ruby.Get(), - php.Get(), - golang.Get(), - python.Get(), - } +func New( + engine engine.Engine, + schemaClassifier *schemaclassifier.Classifier, + rules map[string]*settings.Rule, +) (*Scanner, error) { + languages := engine.GetLanguages() languageScanners := make([]*languagescanner.Scanner, len(languages))