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

fix: disable rules flag #1325

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions e2e/rules/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestSecrets(t *testing.T) {
"--scanner=secrets",
"--only-rule=gitleaks",
"--format=yaml",
"--disable-version-check",
"--disable-default-rules",
"--exit-code=0",
},
Expand Down
4 changes: 2 additions & 2 deletions internal/commands/artifact/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ func Run(ctx context.Context, opts flag.Options) (err error) {
if opts.RuleOptions.DisableDefaultRules {
metaLanguageList = make([]string, 0)
}
// deal with no version check here
versionMeta, err := version_check.GetVersionMeta(ctx, metaLanguageList)

versionMeta, err := version_check.GetScanVersionMeta(ctx, opts, metaLanguageList)
if err != nil {
log.Debug().Msgf("failed: %s", err)
} else {
Expand Down
48 changes: 32 additions & 16 deletions internal/commands/process/settings/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,21 @@ func loadRules(
externalRuleDirs []string,
options flag.RuleOptions,
versionMeta *version_check.VersionMeta,
force bool) (
force bool,
) (
result LoadRulesResult,
err error,
) {
definitions := make(map[string]RuleDefinition)
builtInDefinitions := make(map[string]RuleDefinition)

log.Debug().Msg("Loading rules")

if versionMeta.Rules.Version != nil {
result.BearerRulesVersion = *versionMeta.Rules.Version
}

urls := make([]string, 0, len(versionMeta.Rules.Packages))
for _, value := range versionMeta.Rules.Packages {
log.Debug().Msgf("Added rule package URL %s", value)
urls = append(urls, value)
}
log.Debug().Msg("Loading rules")

err = LoadRuleDefinitionsFromUrls(definitions, urls)
if err != nil {
output.Fatal(fmt.Sprintf("Error loading rules: %s", err))
// sysexit
}
} else {
log.Debug().Msg("No rule packages found")
}
loadRuleDefinitionsFromRemote(definitions, options, versionMeta)

if err := loadRuleDefinitionsFromDir(builtInDefinitions, buildInRulesFs); err != nil {
return result, fmt.Errorf("error loading built-in rules: %w", err)
Expand Down Expand Up @@ -104,6 +93,33 @@ func loadRules(
return result, nil
}

func loadRuleDefinitionsFromRemote(
definitions map[string]RuleDefinition,
options flag.RuleOptions,
versionMeta *version_check.VersionMeta,
) {
if options.DisableDefaultRules {
return
}

if versionMeta.Rules.Version == nil {
log.Debug().Msg("No rule packages found")
return
}

urls := make([]string, 0, len(versionMeta.Rules.Packages))
for _, value := range versionMeta.Rules.Packages {
log.Debug().Msgf("Added rule package URL %s", value)
urls = append(urls, value)
}

err := LoadRuleDefinitionsFromUrls(definitions, urls)
if err != nil {
output.Fatal(fmt.Sprintf("Error loading rules: %s", err))
// sysexit
}
}

func loadRuleDefinitionsFromDir(definitions map[string]RuleDefinition, dir fs.FS) error {
loadedDefinitions := make(map[string]RuleDefinition)
if err := fs.WalkDir(dir, ".", func(path string, dirEntry fs.DirEntry, err error) error {
Expand Down
7 changes: 7 additions & 0 deletions internal/languages/php/pattern/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ func (*Pattern) IsLeaf(node *tree.Node) bool {
return false
}

func (*Pattern) AnonymousParentTypes() []string {
return []string{
"binary_expression",
"unary_op_expression",
}
}

func (*Pattern) LeafContentTypes() []string {
return []string{
"encapsed_string",
Expand Down
2 changes: 1 addition & 1 deletion internal/version_check/bearer_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/bearer/bearer/internal/flag"
)

func GetBearerVerionMeta(languages []string) (*VersionMeta, error) {
func GetBearerVersionMeta(languages []string) (*VersionMeta, error) {
var meta VersionMeta
client := api.New(
api.API{
Expand Down
16 changes: 15 additions & 1 deletion internal/version_check/version_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,22 @@ type BinaryVersionMeta struct {
Message string
}

func GetScanVersionMeta(ctx context.Context, options flag.Options, languages []string) (meta *VersionMeta, err error) {
if options.RuleOptions.DisableDefaultRules && options.GeneralOptions.DisableVersionCheck {
log.Debug().Msg("skipping version API call as check and default rules both disabled")

return &VersionMeta{
Binary: BinaryVersionMeta{
Latest: true,
},
}, nil
}

return GetVersionMeta(ctx, languages)
}

func GetVersionMeta(ctx context.Context, languages []string) (meta *VersionMeta, err error) {
meta, err = GetBearerVerionMeta(languages)
meta, err = GetBearerVersionMeta(languages)
if err != nil {
log.Debug().Msgf("Bearer version API failed: %s", err)

Expand Down
Loading