Skip to content

Commit

Permalink
remove writing default config, introduce GITLEAKS_CONFIG (gitleaks#746)
Browse files Browse the repository at this point in the history
* remove writing default config, introduce GITLEAKS_CONFIG

* setting report format default to json, update readme
  • Loading branch information
zricethezav authored Dec 9, 2021
1 parent 6f6ebd4 commit 3fedf6f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ Flags:
-c, --config string config file path
order of precedence:
1. --config/-c
2. (--source/-s)/.gitleaks.toml
if --config/-c is not set and no (--source/-s)/.gitleaks.toml present
then .gitleaks.toml will be written to (--source/-s)/.gitleaks.toml for future use
2. env var GITLEAKS_CONFIG
3. (--source/-s)/.gitleaks.toml
If none of the three options are used, then gitleaks will use the default config
--exit-code string exit code when leaks have been encountered (default: 1)
-h, --help help for gitleaks
-l, --log-level string log level (debug, info, warn, error, fatal) (default "info")
Expand Down
31 changes: 16 additions & 15 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ const banner = `
const configDescription = `config file path
order of precedence:
1. --config/-c
2. (--source/-s)/.gitleaks.toml
if --config/-c is not set and no (--source/s)/.gitleaks.toml is present
then .gitleaks.toml will be written to (--source/-s)/.gitleaks.toml for future use`
2. env var GITLEAKS_CONFIG
3. (--source/-s)/.gitleaks.toml
If none of the three options are used, then gitleaks will use the default config`

var rootCmd = &cobra.Command{
Use: "gitleaks",
Expand All @@ -38,10 +38,10 @@ var rootCmd = &cobra.Command{
func init() {
cobra.OnInitialize(initLog)
rootCmd.PersistentFlags().StringP("config", "c", "", configDescription)
rootCmd.PersistentFlags().Int("exit-code", 1, "exit code when leaks have been encountered (default: 1)")
rootCmd.PersistentFlags().Int("exit-code", 1, "exit code when leaks have been encountered")
rootCmd.PersistentFlags().StringP("source", "s", ".", "path to source (default: $PWD)")
rootCmd.PersistentFlags().StringP("report-path", "r", "", "report file")
rootCmd.PersistentFlags().StringP("report-format", "f", "", "output format (json, csv, sarif)")
rootCmd.PersistentFlags().StringP("report-format", "f", "json", "output format (json, csv, sarif)")
rootCmd.PersistentFlags().StringP("log-level", "l", "info", "log level (debug, info, warn, error, fatal)")
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "show verbose output from scan")
rootCmd.PersistentFlags().Bool("redact", false, "redact secrets from logs and stdout")
Expand Down Expand Up @@ -78,7 +78,11 @@ func initConfig() {
}
if cfgPath != "" {
viper.SetConfigFile(cfgPath)
log.Debug().Msgf("Using gitleaks config %s", cfgPath)
log.Debug().Msgf("Using gitleaks config %s from `--config`", cfgPath)
} else if os.Getenv("GITLEAKS_CONFIG") != "" {
envPath := os.Getenv("GITLEAKS_CONFIG")
viper.SetConfigFile(envPath)
log.Debug().Msgf("Using gitleaks config from GITLEAKS_CONFIG env var: %s", envPath)
} else {
source, err := rootCmd.Flags().GetString("source")
if err != nil {
Expand All @@ -90,23 +94,20 @@ func initConfig() {
}

if !fileInfo.IsDir() {
log.Debug().Msgf("Unable to write default gitleaks config to %s since --source=%s is a file, using default config",
log.Debug().Msgf("Unable to load gitleaks config from %s since --source=%s is a file, using default config",
filepath.Join(source, ".gitleaks.toml"), source)
viper.SetConfigType("toml")
viper.ReadConfig(strings.NewReader(config.DefaultConfig))
return
}

if _, err := os.Stat(filepath.Join(source, ".gitleaks.toml")); os.IsNotExist(err) {
log.Debug().Msgf("No gitleaks config found, writing default gitleaks config to %s", filepath.Join(source, ".gitleaks.toml"))
if err := os.WriteFile(filepath.Join(source, ".gitleaks.toml"), []byte(config.DefaultConfig), os.ModePerm); err != nil {
log.Debug().Msgf("Unable to write default gitleaks config to %s, using default config", filepath.Join(source, ".gitleaks.toml"))
viper.SetConfigType("toml")
viper.ReadConfig(strings.NewReader(config.DefaultConfig))
return
}
log.Debug().Msgf("No gitleaks config found in path %s, using default gitleaks config", filepath.Join(source, ".gitleaks.toml"))
viper.SetConfigType("toml")
viper.ReadConfig(strings.NewReader(config.DefaultConfig))
return
} else {
log.Debug().Msgf("Using existing gitleaks config %s", filepath.Join(source, ".gitleaks.toml"))
log.Debug().Msgf("Using existing gitleaks config %s from `(--source)/.gitleaks.toml`", filepath.Join(source, ".gitleaks.toml"))
}

viper.AddConfigPath(source)
Expand Down

0 comments on commit 3fedf6f

Please sign in to comment.