Skip to content

Commit

Permalink
fix: engine refactor (#1637)
Browse files Browse the repository at this point in the history
* fix: use engine in worker logic

* test: use specific language in testhelper
  • Loading branch information
didroe authored Jun 17, 2024
1 parent 9e7769a commit 6cbab1c
Show file tree
Hide file tree
Showing 48 changed files with 171 additions and 115 deletions.
2 changes: 1 addition & 1 deletion pkg/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
14 changes: 11 additions & 3 deletions pkg/commands/process/orchestrator/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions pkg/commands/processing_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions pkg/languages/golang/golang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
_ "embed"
"testing"

"github.com/bearer/bearer/pkg/languages/golang"
"github.com/bearer/bearer/pkg/languages/testhelper"
)

Expand All @@ -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/")
}
4 changes: 2 additions & 2 deletions pkg/languages/java/java_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 7 additions & 6 deletions pkg/languages/javascript/javascript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
_ "embed"
"testing"

"github.com/bearer/bearer/pkg/languages/javascript"
"github.com/bearer/bearer/pkg/languages/testhelper"
)

Expand All @@ -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/")
}
6 changes: 3 additions & 3 deletions pkg/languages/php/php_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 7 additions & 6 deletions pkg/languages/python/python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
_ "embed"
"testing"

"github.com/bearer/bearer/pkg/languages/python"
"github.com/bearer/bearer/pkg/languages/testhelper"
)

Expand All @@ -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/")
}
7 changes: 4 additions & 3 deletions pkg/languages/ruby/ruby_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
_ "embed"
"testing"

"github.com/bearer/bearer/pkg/languages/ruby"
"github.com/bearer/bearer/pkg/languages/testhelper"
)

Expand All @@ -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/")
}
Loading

0 comments on commit 6cbab1c

Please sign in to comment.