Skip to content

Commit

Permalink
feat: move bearer-ignore-file flag to General Flags
Browse files Browse the repository at this point in the history
  • Loading branch information
elsapet committed Aug 28, 2023
1 parent 2d85340 commit 8313e98
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 97 deletions.
33 changes: 17 additions & 16 deletions pkg/commands/ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Examples:

func newIgnoreShowCommand() *cobra.Command {
var IgnoreShowFlags = &flag.Flags{
IgnoreFlagGroup: flag.NewIgnoreFlagGroup(),
GeneralFlagGroup: flag.NewGeneralFlagGroup(),
IgnoreShowFlagGroup: flag.NewIgnoreShowFlagGroup(),
}
cmd := &cobra.Command{
Expand All @@ -79,7 +79,7 @@ $ bearer ignore show <fingerprint>`,
return cmd.Help()
}

ignoredFingerprints, fileExists, err := ignore.GetIgnoredFingerprints(options.IgnoreOptions.BearerIgnoreFile, nil)
ignoredFingerprints, fileExists, err := ignore.GetIgnoredFingerprints(options.GeneralOptions.BearerIgnoreFile, nil)
if err != nil {
cmd.Printf("Issue loading ignored fingerprints from bearer.ignore file: %s", err)
return nil
Expand All @@ -93,7 +93,7 @@ $ bearer ignore show <fingerprint>`,
if options.IgnoreShowOptions.All {
// show all fingerprints
for fingerprintId, fingerprint := range ignoredFingerprints {
cmd.Print(ignore.DisplayIgnoredEntryTextString(fingerprintId, fingerprint))
cmd.Print(ignore.DisplayIgnoredEntryTextString(fingerprintId, fingerprint, options.GeneralOptions.NoColor))
}
} else {
// show a specific fingerprint
Expand All @@ -103,7 +103,7 @@ $ bearer ignore show <fingerprint>`,
cmd.Printf("Ignored fingerprint '%s' was not found in bearer.ignore file\n", fingerprintId)
return nil
}
cmd.Print(ignore.DisplayIgnoredEntryTextString(fingerprintId, selectedIgnoredFingerprint))
cmd.Print(ignore.DisplayIgnoredEntryTextString(fingerprintId, selectedIgnoredFingerprint, options.GeneralOptions.NoColor))
}
cmd.Print("\n\n")
return nil
Expand All @@ -119,7 +119,7 @@ $ bearer ignore show <fingerprint>`,

func newIgnoreAddCommand() *cobra.Command {
var IgnoreAddFlags = &flag.Flags{
IgnoreFlagGroup: flag.NewIgnoreFlagGroup(),
GeneralFlagGroup: flag.NewGeneralFlagGroup(),
IgnoreAddFlagGroup: flag.NewIgnoreAddFlagGroup(),
}
cmd := &cobra.Command{
Expand Down Expand Up @@ -153,7 +153,7 @@ $ bearer ignore add <fingerprint> --author Mish --comment "Possible false positi
fingerprintId: fingerprintEntry,
}

ignoredFingerprints, fileExists, err := ignore.GetIgnoredFingerprints(options.IgnoreOptions.BearerIgnoreFile, nil)
ignoredFingerprints, fileExists, err := ignore.GetIgnoredFingerprints(options.GeneralOptions.BearerIgnoreFile, nil)
if err != nil {
return fmt.Errorf("error retrieving existing ignores: %s", err)
}
Expand All @@ -168,12 +168,12 @@ $ bearer ignore add <fingerprint> --author Mish --comment "Possible false positi
return nil
}

if err := writeIgnoreFile(ignoredFingerprints, options.IgnoreOptions.BearerIgnoreFile); err != nil {
if err := writeIgnoreFile(ignoredFingerprints, options.GeneralOptions.BearerIgnoreFile); err != nil {
return err
}

cmd.Print("Fingerprint added to bearer.ignore:\n\n")
cmd.Print(ignore.DisplayIgnoredEntryTextString(fingerprintId, ignoredFingerprints[fingerprintId]))
cmd.Print(ignore.DisplayIgnoredEntryTextString(fingerprintId, ignoredFingerprints[fingerprintId], options.GeneralOptions.NoColor))
cmd.Print("\n\n")
return nil
},
Expand All @@ -188,7 +188,7 @@ $ bearer ignore add <fingerprint> --author Mish --comment "Possible false positi

func newIgnoreRemoveCommand() *cobra.Command {
var IgnoreRemoveFlags = &flag.Flags{
IgnoreFlagGroup: flag.NewIgnoreFlagGroup(),
GeneralFlagGroup: flag.NewGeneralFlagGroup(),
}
cmd := &cobra.Command{
Use: "remove <fingerprint>",
Expand All @@ -209,7 +209,7 @@ $ bearer ignore remove <fingerprint>`,
return fmt.Errorf("flag error: %s", err)
}

ignoredFingerprints, fileExists, err := ignore.GetIgnoredFingerprints(options.IgnoreOptions.BearerIgnoreFile)
ignoredFingerprints, fileExists, err := ignore.GetIgnoredFingerprints(options.GeneralOptions.BearerIgnoreFile, nil)
if err != nil {
return fmt.Errorf("error retrieving existing ignores: %s", err)
}
Expand All @@ -226,12 +226,12 @@ $ bearer ignore remove <fingerprint>`,
}

delete(ignoredFingerprints, fingerprintId)
if err := writeIgnoreFile(ignoredFingerprints, options.IgnoreOptions.BearerIgnoreFile); err != nil {
if err := writeIgnoreFile(ignoredFingerprints, options.GeneralOptions.BearerIgnoreFile); err != nil {
return err
}

cmd.Print("Fingerprint successfully removed from bearer.ignore:\n\n")
cmd.Print(ignore.DisplayIgnoredEntryTextString(fingerprintId, removedFingerprint))
cmd.Print(ignore.DisplayIgnoredEntryTextString(fingerprintId, removedFingerprint, options.GeneralOptions.NoColor))
cmd.Print("\n\n")
return nil
},
Expand All @@ -246,7 +246,7 @@ $ bearer ignore remove <fingerprint>`,

func newIgnoreMigrateCommand() *cobra.Command {
IgnoreMigrateFlags := &flag.Flags{
IgnoreFlagGroup: flag.NewIgnoreFlagGroup(),
GeneralFlagGroup: flag.NewGeneralFlagGroup(),
IgnoreMigrateFlagGroup: flag.NewIgnoreMigrateFlagGroup(),
}
cmd := &cobra.Command{
Expand All @@ -263,13 +263,14 @@ $ bearer ignore migrate`,
if err != nil {
return fmt.Errorf("flag error: %s", err)
}

configFilePath := viper.GetString(flag.ConfigFileFlag.ConfigName)
fingerprintsToMigrate, err := getIgnoredFingerprintsFromConfig(configFilePath)
if err != nil {
return fmt.Errorf("error reading config: %s\nPerhaps you need to use --config-file to specify the config path?", err.Error())
}

ignoredFingerprints, fileExists, err := ignore.GetIgnoredFingerprints(options.IgnoreOptions.BearerIgnoreFile, nil)
ignoredFingerprints, fileExists, err := ignore.GetIgnoredFingerprints(options.GeneralOptions.BearerIgnoreFile, nil)
if err != nil {
return fmt.Errorf("error retrieving existing ignores: %s", err)
}
Expand All @@ -292,7 +293,7 @@ $ bearer ignore migrate`,
}
}

cmd.Printf("Added %d ignores to:\n\t%s\n", migratedIgnoredCount, options.IgnoreOptions.BearerIgnoreFile)
cmd.Printf("Added %d ignores to:\n\t%s\n", migratedIgnoredCount, options.GeneralOptions.BearerIgnoreFile)

if skippedIgnoresToMigrate != "" {
cmd.Printf("\nThe following ignores already exist in the bearer.ignore file:\n")
Expand All @@ -302,7 +303,7 @@ $ bearer ignore migrate`,

// either no duplicate entries at this point or --force is true so we can ignore merge error
_ = ignore.MergeIgnoredFingerprints(fingerprintsToMigrate, ignoredFingerprints, options.IgnoreMigrateOptions.Force)
return writeIgnoreFile(ignoredFingerprints, options.IgnoreOptions.BearerIgnoreFile)
return writeIgnoreFile(ignoredFingerprints, options.GeneralOptions.BearerIgnoreFile)
},
SilenceErrors: false,
SilenceUsage: false,
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/process/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func FromOptions(opts flag.Options, foundLanguages []string) (Config, error) {
}
}

ignoredFingerprints, _, err := ignore.GetIgnoredFingerprints(opts.IgnoreOptions.BearerIgnoreFile, &opts.ScanOptions.Target)
ignoredFingerprints, _, err := ignore.GetIgnoredFingerprints(opts.GeneralOptions.BearerIgnoreFile, &opts.ScanOptions.Target)
if err != nil {
return Config{}, err
}
Expand Down
1 change: 0 additions & 1 deletion pkg/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ var ScanFlags = &flag.Flags{
RuleFlagGroup: flag.NewRuleFlagGroup(),
ReportFlagGroup: flag.NewReportFlagGroup(),
GeneralFlagGroup: flag.NewGeneralFlagGroup(),
IgnoreFlagGroup: flag.NewIgnoreFlagGroup(),
}

func NewScanCommand() *cobra.Command {
Expand Down
13 changes: 12 additions & 1 deletion pkg/flag/general_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ var (
Value: false,
Usage: "Disable color in output",
}
BearerIgnoreFileFlag = Flag{
Name: "bearer-ignore-file",
ConfigName: "bearer-ignore-file",
Value: "bearer.ignore",
Usage: "Load bearer.ignore file from the specified path.",
DisableInConfig: true,
}
DebugProfileFlag = Flag{
Name: "debug-profile",
ConfigName: "debug-profile",
Expand All @@ -69,7 +76,8 @@ type GeneralOptions struct {
ConfigFile string `json:"config_file" yaml:"config_file"`
Client *api.API
DisableVersionCheck bool
NoColor bool `mapstructure:"no_color" json:"no_color" yaml:"no_color"`
NoColor bool `mapstructure:"no_color" json:"no_color" yaml:"no_color"`
BearerIgnoreFile string `mapstructure:"bearer_ignore_file" json:"bearer_ignore_file" yaml:"bearer_ignore_file"`
DebugProfile bool
}

Expand All @@ -80,6 +88,7 @@ func NewGeneralFlagGroup() *GeneralFlagGroup {
Host: &HostFlag,
DisableVersionCheck: &DisableVersionCheckFlag,
NoColor: &NoColorFlag,
BearerIgnoreFile: &BearerIgnoreFileFlag,
DebugProfile: &DebugProfileFlag,
}
}
Expand All @@ -95,6 +104,7 @@ func (f *GeneralFlagGroup) Flags() []*Flag {
f.Host,
f.DisableVersionCheck,
f.NoColor,
f.BearerIgnoreFile,
f.DebugProfile,
}
}
Expand Down Expand Up @@ -122,6 +132,7 @@ func (f *GeneralFlagGroup) ToOptions() GeneralOptions {
ConfigFile: getString(f.ConfigFile),
DisableVersionCheck: getBool(f.DisableVersionCheck),
NoColor: getBool(f.NoColor),
BearerIgnoreFile: getString(f.BearerIgnoreFile),
DebugProfile: getBool(f.DebugProfile),
}
}
41 changes: 0 additions & 41 deletions pkg/flag/ignore_flags.go

This file was deleted.

29 changes: 4 additions & 25 deletions pkg/flag/ignore_migrate_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,19 @@ var (
Value: false,
Usage: "Overwrite an existing ignored finding.",
}
IgnoreMigrateBearerIgnoreFileFlag = Flag{
Name: "config-file",
ConfigName: "config-file",
Value: "bearer.yml",
Usage: "Load configuration from the specified path.",
DisableInConfig: true,
}
IgnoreMigrateConfigFileFlag = Flag{
Name: "config-file",
ConfigName: "config-file",
Value: "bearer.yml",
Usage: "Load configuration from the specified path.",
DisableInConfig: true,
}
)

type IgnoreMigrateFlagGroup struct {
IgnoreMigrateForceFlag *Flag
IgnoreMigrateConfigFileFlag *Flag
IgnoreMigrateBearerIgnoreFileFlag *Flag
IgnoreMigrateForceFlag *Flag
}

type IgnoreMigrateOptions struct {
Force bool `mapstructure:"ignore_migrate_force" json:"ignore_migrate_force" yaml:"ignore_migrate_force"`
ConfigFile string `mapstructure:"ignore_migrate_config_file" json:"ignore_migrate_config_file" yaml:"ignore_migrate_config_file"`
Force bool `mapstructure:"ignore_migrate_force" json:"ignore_migrate_force" yaml:"ignore_migrate_force"`
}

func NewIgnoreMigrateFlagGroup() *IgnoreMigrateFlagGroup {
return &IgnoreMigrateFlagGroup{
IgnoreMigrateForceFlag: &IgnoreMigrateForceFlag,
IgnoreMigrateBearerIgnoreFileFlag: &IgnoreMigrateBearerIgnoreFileFlag,
IgnoreMigrateConfigFileFlag: &IgnoreMigrateConfigFileFlag,
IgnoreMigrateForceFlag: &IgnoreMigrateForceFlag,
}
}

Expand All @@ -49,13 +30,11 @@ func (f *IgnoreMigrateFlagGroup) Name() string {
func (f *IgnoreMigrateFlagGroup) Flags() []*Flag {
return []*Flag{
f.IgnoreMigrateForceFlag,
f.IgnoreMigrateConfigFileFlag,
}
}

func (f *IgnoreMigrateFlagGroup) ToOptions() IgnoreMigrateOptions {
return IgnoreMigrateOptions{
Force: getBool(f.IgnoreMigrateForceFlag),
ConfigFile: getString(f.IgnoreMigrateConfigFileFlag),
Force: getBool(f.IgnoreMigrateForceFlag),
}
}
9 changes: 0 additions & 9 deletions pkg/flag/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ type Flags struct {
ProcessFlagGroup *ProcessFlagGroup
ScanFlagGroup *ScanFlagGroup
GeneralFlagGroup *GeneralFlagGroup
IgnoreFlagGroup *IgnoreFlagGroup
IgnoreAddFlagGroup *IgnoreAddFlagGroup
IgnoreShowFlagGroup *IgnoreShowFlagGroup
IgnoreMigrateFlagGroup *IgnoreMigrateFlagGroup
Expand All @@ -66,7 +65,6 @@ type Options struct {
RuleOptions
ScanOptions
GeneralOptions
IgnoreOptions
IgnoreAddOptions
IgnoreShowOptions
IgnoreMigrateOptions
Expand Down Expand Up @@ -184,9 +182,6 @@ func (f *Flags) groups() []FlagGroup {
if f.RepoFlagGroup != nil {
groups = append(groups, f.RepoFlagGroup)
}
if f.IgnoreFlagGroup != nil {
groups = append(groups, f.IgnoreFlagGroup)
}
if f.IgnoreAddFlagGroup != nil {
groups = append(groups, f.IgnoreAddFlagGroup)
}
Expand Down Expand Up @@ -294,10 +289,6 @@ func (f *Flags) ToOptions(args []string) (Options, error) {
opts.GeneralOptions = f.GeneralFlagGroup.ToOptions()
}

if f.IgnoreFlagGroup != nil {
opts.IgnoreOptions = f.IgnoreFlagGroup.ToOptions()
}

if f.IgnoreAddFlagGroup != nil {
opts.IgnoreAddOptions = f.IgnoreAddFlagGroup.ToOptions()
}
Expand Down
1 change: 0 additions & 1 deletion pkg/report/output/privacy/privacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func generateConfig(reportOptions flag.ReportOptions) (settings.Config, error) {
RepoOptions: flag.RepoOptions{},
ReportOptions: reportOptions,
GeneralOptions: flag.GeneralOptions{},
IgnoreOptions: flag.IgnoreOptions{},
}

return settings.FromOptions(opts, []string{"ruby"})
Expand Down
1 change: 0 additions & 1 deletion pkg/report/output/security/security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ func generateConfig(reportOptions flag.ReportOptions) (settings.Config, error) {
RepoOptions: flag.RepoOptions{},
ReportOptions: reportOptions,
GeneralOptions: flag.GeneralOptions{},
IgnoreOptions: flag.IgnoreOptions{},
}

return settings.FromOptions(opts, []string{"ruby"})
Expand Down
8 changes: 7 additions & 1 deletion pkg/util/ignore/ignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ var bold = color.New(color.Bold).SprintFunc()
var morePrefix = color.HiBlackString("├─ ")
var lastPrefix = color.HiBlackString("└─ ")

func DisplayIgnoredEntryTextString(fingerprintId string, entry IgnoredFingerprint) string {
func DisplayIgnoredEntryTextString(fingerprintId string, entry IgnoredFingerprint, noColor bool) string {
initialColorSetting := color.NoColor
if noColor && !initialColorSetting {
color.NoColor = true
}
prefix := morePrefix
result := fmt.Sprintf(bold(color.HiBlueString("%s \n")), fingerprintId)

Expand All @@ -83,5 +87,7 @@ func DisplayIgnoredEntryTextString(fingerprintId string, entry IgnoredFingerprin
result += fmt.Sprintf("\n%sComment: %s", lastPrefix, bold(*entry.Comment))
}

color.NoColor = initialColorSetting

return result
}

0 comments on commit 8313e98

Please sign in to comment.