Skip to content

Commit

Permalink
chore: improve trace output
Browse files Browse the repository at this point in the history
  • Loading branch information
didroe committed Aug 25, 2023
1 parent b89dd90 commit fa21245
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 32 deletions.
2 changes: 1 addition & 1 deletion internal/languages/java/java.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Get() language.Language {
return &implementation{}
}

func (*implementation) Name() string {
func (*implementation) ID() string {
return "java"
}

Expand Down
2 changes: 1 addition & 1 deletion internal/languages/javascript/javascript.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Get() language.Language {
return &implementation{}
}

func (*implementation) Name() string {
func (*implementation) ID() string {
return "javascript"
}

Expand Down
2 changes: 1 addition & 1 deletion internal/languages/ruby/ruby.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func Get() language.Language {
return &implementation{}
}

func (*implementation) Name() string {
func (*implementation) ID() string {
return "ruby"
}

Expand Down
31 changes: 30 additions & 1 deletion internal/scanner/ast/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import (
"errors"
"strings"

"github.com/rs/zerolog/log"
sitter "github.com/smacker/go-tree-sitter"
"gopkg.in/yaml.v3"

"github.com/bearer/bearer/internal/scanner/ast/tree"
)

type Set struct {
languageID string
sitterLanguage *sitter.Language
queries []Query
queryByInput map[string]*Query
Expand All @@ -24,8 +27,9 @@ type Query struct {
input string
}

func NewSet(sitterLanguage *sitter.Language) *Set {
func NewSet(languageID string, sitterLanguage *sitter.Language) *Set {
return &Set{
languageID: languageID,
sitterLanguage: sitterLanguage,
sitterCursor: sitter.NewQueryCursor(),
queryByInput: make(map[string]*Query),
Expand Down Expand Up @@ -109,6 +113,10 @@ func (querySet *Set) Compile() error {
s.WriteString("\n")
}

if log.Trace().Enabled() {
log.Trace().Msgf("%s queries:\n%s", querySet.languageID, querySet.dump())
}

sitterQuery, err := sitter.NewQuery([]byte(s.String()), querySet.sitterLanguage)
if err != nil {
return err
Expand All @@ -119,6 +127,27 @@ func (querySet *Set) Compile() error {
return nil
}

type dumpValue struct {
ID int
Input string
}

func (querySet *Set) dump() string {
queries := make([]dumpValue, len(querySet.queries))

for i, query := range querySet.queries {
queries[i].ID = query.id
queries[i].Input = query.input
}

yamlQueries, err := yaml.Marshal(queries)
if err != nil {
return err.Error()
}

return string(yamlQueries)
}

func (querySet *Set) Close() {
querySet.sitterCursor.Close()
querySet.freeSitterQuery()
Expand Down
14 changes: 7 additions & 7 deletions internal/scanner/ast/tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ func (node *Node) QueryResults(queryID int) []QueryResult {
}

type nodeDump struct {
Type string
ID int
Content string `yaml:",omitempty"`
DataflowSources,
AliasOf []int `yaml:",omitempty"`
Children []nodeDump `yaml:",omitempty"`
Queries []int `yaml:",omitempty"`
Type string
ID int
Content string `yaml:",omitempty"`
DataflowSources []int `yaml:"dataflow_sources,omitempty"`
AliasOf []int `yaml:"alias_of,omitempty"`
Children []nodeDump `yaml:",omitempty"`
Queries []int `yaml:",omitempty"`
}

func (node *Node) Dump() string {
Expand Down
4 changes: 2 additions & 2 deletions internal/scanner/detectors/customrule/customrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func New(
patterns []settings.RulePattern,
) (types.Detector, error) {
var compiledPatterns []Pattern
for _, pattern := range patterns {
patternQuery, err := patternquery.Compile(language, querySet, pattern.Pattern, pattern.Focus)
for i, pattern := range patterns {
patternQuery, err := patternquery.Compile(language, querySet, ruleID, i, pattern.Pattern, pattern.Focus)
if err != nil {
return nil, fmt.Errorf("error compiling pattern: %s", err)
}
Expand Down
51 changes: 45 additions & 6 deletions internal/scanner/detectors/customrule/patternquery/patternquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"

"github.com/rs/zerolog/log"

Expand All @@ -20,6 +21,9 @@ type Query interface {
}

type query struct {
ruleID string
patternIndex int
input string
treeQuery *astquery.Query
paramToVariable map[string]string
equalParams [][]string
Expand All @@ -38,6 +42,8 @@ type Result struct {
func Compile(
language language.Language,
querySet *astquery.Set,
ruleID string,
patternIndex int,
input string,
focusedVariable string,
) (Query, error) {
Expand All @@ -51,15 +57,48 @@ func Compile(
return &rootVariableQuery{variable: builderResult.RootVariable}, nil
}

treeQuery := querySet.Add(builderResult.Query)
log.Trace().Msgf("compiled pattern to query %d: %s -> %s", treeQuery.ID(), input, builderResult.Query)

return &query{
treeQuery: treeQuery,
query := &query{
ruleID: ruleID,
patternIndex: patternIndex,
input: input,
treeQuery: querySet.Add(builderResult.Query),
paramToVariable: builderResult.ParamToVariable,
equalParams: builderResult.EqualParams,
paramToContent: builderResult.ParamToContent,
}, nil
}

if log.Trace().Enabled() {
log.Trace().Msgf("compiled pattern:\n%s", query.dump())
}

return query, nil
}

type dumpValue struct {
RuleID string `yaml:"rule_id"`
PatternIndex int `yaml:"pattern_index"`
Pattern string
TreeQueryID int `yaml:"tree_query_id"`
ParamToVariable map[string]string `yaml:"param_to_variable,omitempty"`
ParamToContent map[string]map[string]string `yaml:"param_to_content,omitempty"`
EqualParams [][]string `yaml:"equal_params,omitempty"`
}

func (query *query) dump() string {
yamlQuery, err := yaml.Marshal(&dumpValue{
RuleID: query.ruleID,
PatternIndex: query.patternIndex,
Pattern: query.input,
TreeQueryID: query.treeQuery.ID(),
ParamToVariable: query.paramToVariable,
ParamToContent: query.paramToContent,
EqualParams: query.equalParams,
})
if err != nil {
return err.Error()
}

return string(yamlQuery)
}

func (query *query) MatchAt(node *tree.Node) ([]*Result, error) {
Expand Down
6 changes: 3 additions & 3 deletions internal/scanner/detectorset/detectorset.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func New(
rules map[string]*settings.Rule,
language language.Language,
) (Set, error) {
relevantRules, presenceRules := getRelevantRules(rules, language.Name())
relevantRules, presenceRules := getRelevantRules(rules, language.ID())
builtinAndSharedRuleIDs, topLevelRuleIDs := findNotableRuleIDs(builtinDetectors, relevantRules, presenceRules)

detectors, err := createDetectors(language, querySet, builtinDetectors, relevantRules)
Expand Down Expand Up @@ -103,13 +103,13 @@ func (set *detectorSet) lookupDetector(detectorType string) (detectortypes.Detec

func getRelevantRules(
rules map[string]*settings.Rule,
languageName string,
languageID string,
) (map[string]*settings.Rule, set.Set[string]) {
relevantRules := make(map[string]*settings.Rule)
presenceRules := set.New[string]()

for ruleID, rule := range rules {
if !slices.Contains(rule.Languages, languageName) {
if !slices.Contains(rule.Languages, languageID) {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion internal/scanner/language/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type Language interface {
Name() string
ID() string
EnryLanguages() []string
NewBuiltInDetectors(schemaClassifier *schema.Classifier, querySet *query.Set) []detectortypes.Detector
SitterLanguage() *sitter.Language
Expand Down
6 changes: 3 additions & 3 deletions internal/scanner/languagescanner/languagescanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func New(
schemaClassifier *schema.Classifier,
rules map[string]*settings.Rule,
) (*Scanner, error) {
querySet := query.NewSet(language.SitterLanguage())
querySet := query.NewSet(language.ID(), language.SitterLanguage())

detectorSet, err := detectorset.New(
querySet,
Expand All @@ -58,8 +58,8 @@ func New(
}, nil
}

func (scanner *Scanner) LanguageName() string {
return scanner.language.Name()
func (scanner *Scanner) LanguageID() string {
return scanner.language.ID()
}

func (scanner *Scanner) Scan(
Expand Down
12 changes: 6 additions & 6 deletions internal/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ type Scanner struct {
}

func New(schemaClassifier *schemaclassifier.Classifier, rules map[string]*settings.Rule) (*Scanner, error) {
langImplementations := []language.Language{
languages := []language.Language{
java.Get(),
javascript.Get(),
ruby.Get(),
}

languageScanners := make([]*languagescanner.Scanner, len(langImplementations))
languageScanners := make([]*languagescanner.Scanner, len(languages))

for i, langImplementation := range langImplementations {
languageScanner, err := languagescanner.New(langImplementation, schemaClassifier, rules)
for i, language := range languages {
languageScanner, err := languagescanner.New(language, schemaClassifier, rules)
if err != nil {
return nil, fmt.Errorf("error creating %s language scanner: %w", langImplementation.Name(), err)
return nil, fmt.Errorf("error creating %s language scanner: %w", language.ID(), err)
}

languageScanners[i] = languageScanner
Expand All @@ -64,7 +64,7 @@ func (scanner *Scanner) Scan(
for _, languageScanner := range scanner.languageScanners {
detections, err := languageScanner.Scan(ctx, fileStats, file)
if err != nil {
return fmt.Errorf("%s scan failed: %w", languageScanner.LanguageName(), err)
return fmt.Errorf("%s scan failed: %w", languageScanner.LanguageID(), err)
}

for _, detection := range detections {
Expand Down

0 comments on commit fa21245

Please sign in to comment.