Skip to content

Commit

Permalink
rename SecretsConfig to EngineConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Baruch Odem committed Feb 20, 2024
1 parent 9fe6a3d commit 099e21b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
14 changes: 7 additions & 7 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var (
customRegexRuleVar []string
ignoreVar []string
ignoreOnExitVar = ignoreOnExitNone
secretsConfigVar secrets.SecretsConfig
engineConfigVar secrets.EngineConfig
validateVar bool
)

Expand Down Expand Up @@ -86,15 +86,15 @@ func Execute() (int, error) {
rootCmd.PersistentFlags().StringSliceVar(&reportPathVar, reportPathFlagName, []string{}, "path to generate report files. The output format will be determined by the file extension (.json, .yaml, .sarif)")
rootCmd.PersistentFlags().StringVar(&stdoutFormatVar, stdoutFormatFlagName, "yaml", "stdout output format, available formats are: json, yaml, sarif")
rootCmd.PersistentFlags().StringArrayVar(&customRegexRuleVar, customRegexRuleFlagName, []string{}, "custom regexes to apply to the scan, must be valid Go regex")
rootCmd.PersistentFlags().StringSliceVar(&secretsConfigVar.SelectedList, ruleFlagName, []string{}, "select rules by name or tag to apply to this scan")
rootCmd.PersistentFlags().StringSliceVar(&secretsConfigVar.IgnoreList, ignoreRuleFlagName, []string{}, "ignore rules by name or tag")
rootCmd.PersistentFlags().StringSliceVar(&engineConfigVar.SelectedList, ruleFlagName, []string{}, "select rules by name or tag to apply to this scan")
rootCmd.PersistentFlags().StringSliceVar(&engineConfigVar.IgnoreList, ignoreRuleFlagName, []string{}, "ignore rules by name or tag")
rootCmd.PersistentFlags().StringSliceVar(&ignoreVar, ignoreFlagName, []string{}, "ignore specific result by id")
rootCmd.PersistentFlags().StringSliceVar(&secretsConfigVar.SpecialList, specialRulesFlagName, []string{}, "special (non-default) rules to apply.\nThis list is not affected by the --rule and --ignore-rule flags.")
rootCmd.PersistentFlags().StringSliceVar(&engineConfigVar.SpecialList, specialRulesFlagName, []string{}, "special (non-default) rules to apply.\nThis list is not affected by the --rule and --ignore-rule flags.")
rootCmd.PersistentFlags().Var(&ignoreOnExitVar, ignoreOnExitFlagName, "defines which kind of non-zero exits code should be ignored\naccepts: all, results, errors, none\nexample: if 'results' is set, only engine errors will make 2ms exit code different from 0")
rootCmd.PersistentFlags().IntVar(&secretsConfigVar.MaxTargetMegabytes, maxTargetMegabytesFlagName, 0, "files larger than this will be skipped.\nOmit or set to 0 to disable this check.")
rootCmd.PersistentFlags().IntVar(&engineConfigVar.MaxTargetMegabytes, maxTargetMegabytesFlagName, 0, "files larger than this will be skipped.\nOmit or set to 0 to disable this check.")
rootCmd.PersistentFlags().BoolVar(&validateVar, validate, false, "trigger additional validation to check if discovered secrets are active or revoked")

rootCmd.AddCommand(secrets.GetRulesCommand(&secretsConfigVar))
rootCmd.AddCommand(secrets.GetRulesCommand(&engineConfigVar))

group := "Commands"
rootCmd.AddGroup(&cobra.Group{Title: group, ID: group})
Expand Down Expand Up @@ -124,7 +124,7 @@ func preRun(cmd *cobra.Command, args []string) error {
return err
}

engine, err := secrets.Init(secretsConfigVar)
engine, err := secrets.Init(engineConfigVar)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions secrets/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ type Engine struct {

const customRegexRuleIdFormat = "custom-regex-%d"

type SecretsConfig struct {
type EngineConfig struct {
SelectedList []string
IgnoreList []string
SpecialList []string

MaxTargetMegabytes int
}

func Init(secretsConfig SecretsConfig) (*Engine, error) {
selectedRules := rules.FilterRules(secretsConfig.SelectedList, secretsConfig.IgnoreList, secretsConfig.SpecialList)
func Init(engineConfig EngineConfig) (*Engine, error) {
selectedRules := rules.FilterRules(engineConfig.SelectedList, engineConfig.IgnoreList, engineConfig.SpecialList)
if len(*selectedRules) == 0 {
return nil, fmt.Errorf("no rules were selected")
}
Expand All @@ -49,7 +49,7 @@ func Init(secretsConfig SecretsConfig) (*Engine, error) {
detector := detect.NewDetector(config.Config{
Rules: rulesToBeApplied,
})
detector.MaxTargetMegaBytes = secretsConfig.MaxTargetMegabytes
detector.MaxTargetMegaBytes = engineConfig.MaxTargetMegabytes

return &Engine{
rules: rulesToBeApplied,
Expand Down Expand Up @@ -115,7 +115,7 @@ func isSecretIgnored(secret *Secret, ignoredIds *[]string) bool {
return false
}

func GetRulesCommand(secretsConfig *SecretsConfig) *cobra.Command {
func GetRulesCommand(engineConfig *EngineConfig) *cobra.Command {
canValidateDisplay := map[bool]string{
true: "V",
false: "",
Expand All @@ -127,7 +127,7 @@ func GetRulesCommand(secretsConfig *SecretsConfig) *cobra.Command {
Long: `List all rules`,
RunE: func(cmd *cobra.Command, args []string) error {

rules := rules.FilterRules(secretsConfig.SelectedList, secretsConfig.IgnoreList, secretsConfig.SpecialList)
rules := rules.FilterRules(engineConfig.SelectedList, engineConfig.IgnoreList, engineConfig.SpecialList)

tab := tabwriter.NewWriter(os.Stdout, 1, 2, 2, ' ', 0)

Expand Down
16 changes: 8 additions & 8 deletions secrets/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ func Test_Init(t *testing.T) {
specialRule := rules.HardcodedPassword()

tests := []struct {
name string
secretsConfig SecretsConfig
expectedErr error
name string
engineConfig EngineConfig
expectedErr error
}{
{
name: "selected and ignore flags used together for the same rule",
secretsConfig: SecretsConfig{
engineConfig: EngineConfig{
SelectedList: []string{allRules[0].Rule.RuleID},
IgnoreList: []string{allRules[0].Rule.RuleID},
SpecialList: []string{},
Expand All @@ -29,7 +29,7 @@ func Test_Init(t *testing.T) {
},
{
name: "non existent select flag",
secretsConfig: SecretsConfig{
engineConfig: EngineConfig{
SelectedList: []string{"non-existent-tag-name"},
IgnoreList: []string{},
SpecialList: []string{"non-existent-tag-name"},
Expand All @@ -38,7 +38,7 @@ func Test_Init(t *testing.T) {
},
{
name: "exiting special rule",
secretsConfig: SecretsConfig{
engineConfig: EngineConfig{
SelectedList: []string{"non-existent-tag-name"},
IgnoreList: []string{},
SpecialList: []string{specialRule.RuleID},
Expand All @@ -49,7 +49,7 @@ func Test_Init(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, err := Init(test.secretsConfig)
_, err := Init(test.engineConfig)
if err == nil && test.expectedErr != nil {
t.Errorf("expected error, got nil")
}
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestSecrets(t *testing.T) {
},
}

detector, err := Init(SecretsConfig{})
detector, err := Init(EngineConfig{})
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 099e21b

Please sign in to comment.