From 8313e98d8833e6b7bb25b43829e4b81fa0ecab52 Mon Sep 17 00:00:00 2001 From: elsapet Date: Mon, 28 Aug 2023 15:37:51 +0200 Subject: [PATCH] feat: move bearer-ignore-file flag to General Flags --- pkg/commands/ignore.go | 33 +++++++++-------- pkg/commands/process/settings/settings.go | 2 +- pkg/commands/scan.go | 1 - pkg/flag/general_flags.go | 13 ++++++- pkg/flag/ignore_flags.go | 41 --------------------- pkg/flag/ignore_migrate_flags.go | 29 ++------------- pkg/flag/options.go | 9 ----- pkg/report/output/privacy/privacy_test.go | 1 - pkg/report/output/security/security_test.go | 1 - pkg/util/ignore/ignore.go | 8 +++- 10 files changed, 41 insertions(+), 97 deletions(-) delete mode 100644 pkg/flag/ignore_flags.go diff --git a/pkg/commands/ignore.go b/pkg/commands/ignore.go index 1636c16c2..954f08ff4 100644 --- a/pkg/commands/ignore.go +++ b/pkg/commands/ignore.go @@ -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{ @@ -79,7 +79,7 @@ $ bearer ignore show `, 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 @@ -93,7 +93,7 @@ $ bearer ignore show `, 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 @@ -103,7 +103,7 @@ $ bearer ignore show `, 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 @@ -119,7 +119,7 @@ $ bearer ignore show `, func newIgnoreAddCommand() *cobra.Command { var IgnoreAddFlags = &flag.Flags{ - IgnoreFlagGroup: flag.NewIgnoreFlagGroup(), + GeneralFlagGroup: flag.NewGeneralFlagGroup(), IgnoreAddFlagGroup: flag.NewIgnoreAddFlagGroup(), } cmd := &cobra.Command{ @@ -153,7 +153,7 @@ $ bearer ignore add --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) } @@ -168,12 +168,12 @@ $ bearer ignore add --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 }, @@ -188,7 +188,7 @@ $ bearer ignore add --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 ", @@ -209,7 +209,7 @@ $ bearer ignore remove `, 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) } @@ -226,12 +226,12 @@ $ bearer ignore remove `, } 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 }, @@ -246,7 +246,7 @@ $ bearer ignore remove `, func newIgnoreMigrateCommand() *cobra.Command { IgnoreMigrateFlags := &flag.Flags{ - IgnoreFlagGroup: flag.NewIgnoreFlagGroup(), + GeneralFlagGroup: flag.NewGeneralFlagGroup(), IgnoreMigrateFlagGroup: flag.NewIgnoreMigrateFlagGroup(), } cmd := &cobra.Command{ @@ -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) } @@ -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") @@ -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, diff --git a/pkg/commands/process/settings/settings.go b/pkg/commands/process/settings/settings.go index c46ec7d3e..a755dfe7b 100644 --- a/pkg/commands/process/settings/settings.go +++ b/pkg/commands/process/settings/settings.go @@ -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 } diff --git a/pkg/commands/scan.go b/pkg/commands/scan.go index dbff6e5a9..fbf945a55 100644 --- a/pkg/commands/scan.go +++ b/pkg/commands/scan.go @@ -35,7 +35,6 @@ var ScanFlags = &flag.Flags{ RuleFlagGroup: flag.NewRuleFlagGroup(), ReportFlagGroup: flag.NewReportFlagGroup(), GeneralFlagGroup: flag.NewGeneralFlagGroup(), - IgnoreFlagGroup: flag.NewIgnoreFlagGroup(), } func NewScanCommand() *cobra.Command { diff --git a/pkg/flag/general_flags.go b/pkg/flag/general_flags.go index 2add45248..b8efb245c 100644 --- a/pkg/flag/general_flags.go +++ b/pkg/flag/general_flags.go @@ -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", @@ -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 } @@ -80,6 +88,7 @@ func NewGeneralFlagGroup() *GeneralFlagGroup { Host: &HostFlag, DisableVersionCheck: &DisableVersionCheckFlag, NoColor: &NoColorFlag, + BearerIgnoreFile: &BearerIgnoreFileFlag, DebugProfile: &DebugProfileFlag, } } @@ -95,6 +104,7 @@ func (f *GeneralFlagGroup) Flags() []*Flag { f.Host, f.DisableVersionCheck, f.NoColor, + f.BearerIgnoreFile, f.DebugProfile, } } @@ -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), } } diff --git a/pkg/flag/ignore_flags.go b/pkg/flag/ignore_flags.go deleted file mode 100644 index 8dab84a97..000000000 --- a/pkg/flag/ignore_flags.go +++ /dev/null @@ -1,41 +0,0 @@ -package flag - -var ( - BearerIgnoreFileFlag = Flag{ - Name: "bearer-ignore-file", - ConfigName: "bearer-ignore-file", - Value: "bearer.ignore", - Usage: "Load bearer.ignore file from the specified path.", - DisableInConfig: true, - } -) - -type IgnoreFlagGroup struct { - BearerIgnoreFileFlag *Flag -} - -type IgnoreOptions struct { - BearerIgnoreFile string `mapstructure:"ignore_bearer_ignore_file" json:"ignore_bearer_ignore_file" yaml:"ignore_bearer_ignore_file"` -} - -func NewIgnoreFlagGroup() *IgnoreFlagGroup { - return &IgnoreFlagGroup{ - BearerIgnoreFileFlag: &BearerIgnoreFileFlag, - } -} - -func (f *IgnoreFlagGroup) Name() string { - return "Ignore" -} - -func (f *IgnoreFlagGroup) Flags() []*Flag { - return []*Flag{ - f.BearerIgnoreFileFlag, - } -} - -func (f *IgnoreFlagGroup) ToOptions() IgnoreOptions { - return IgnoreOptions{ - BearerIgnoreFile: getString(f.BearerIgnoreFileFlag), - } -} diff --git a/pkg/flag/ignore_migrate_flags.go b/pkg/flag/ignore_migrate_flags.go index ec57527c0..006235ab2 100644 --- a/pkg/flag/ignore_migrate_flags.go +++ b/pkg/flag/ignore_migrate_flags.go @@ -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, } } @@ -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), } } diff --git a/pkg/flag/options.go b/pkg/flag/options.go index b9cdad93a..daaa4dea1 100644 --- a/pkg/flag/options.go +++ b/pkg/flag/options.go @@ -53,7 +53,6 @@ type Flags struct { ProcessFlagGroup *ProcessFlagGroup ScanFlagGroup *ScanFlagGroup GeneralFlagGroup *GeneralFlagGroup - IgnoreFlagGroup *IgnoreFlagGroup IgnoreAddFlagGroup *IgnoreAddFlagGroup IgnoreShowFlagGroup *IgnoreShowFlagGroup IgnoreMigrateFlagGroup *IgnoreMigrateFlagGroup @@ -66,7 +65,6 @@ type Options struct { RuleOptions ScanOptions GeneralOptions - IgnoreOptions IgnoreAddOptions IgnoreShowOptions IgnoreMigrateOptions @@ -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) } @@ -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() } diff --git a/pkg/report/output/privacy/privacy_test.go b/pkg/report/output/privacy/privacy_test.go index 0dddb0860..fdf5b25f5 100644 --- a/pkg/report/output/privacy/privacy_test.go +++ b/pkg/report/output/privacy/privacy_test.go @@ -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"}) diff --git a/pkg/report/output/security/security_test.go b/pkg/report/output/security/security_test.go index 705ae1521..45a7fa15a 100644 --- a/pkg/report/output/security/security_test.go +++ b/pkg/report/output/security/security_test.go @@ -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"}) diff --git a/pkg/util/ignore/ignore.go b/pkg/util/ignore/ignore.go index 0e8d3a636..082ae679c 100644 --- a/pkg/util/ignore/ignore.go +++ b/pkg/util/ignore/ignore.go @@ -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) @@ -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 }