mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-29 04:34:46 +01:00
Deprecate detect and protect. Add git, dir, stdin (#1504)
* init backwards compatible cmd * update readme, todos * ... * moar comments, sprucing up root * readme change
This commit is contained in:
1 parent
e93a7c0d26
commit
44ad62e0b1
7 files changed
+270
-64
No files matched your search
+30
-3
@@ -1,3 +1,21 @@
|
||||
// The `detect` and `protect` command is now deprecated. Here are some equivalent commands
|
||||
// to help guide you.
|
||||
|
||||
// OLD CMD: gitleaks detect --source={repo}
|
||||
// NEW CMD: gitleaks git {repo}
|
||||
|
||||
// OLD CMD: gitleaks protect --source={repo}
|
||||
// NEW CMD: gitleaks git --pre-commit {repo}
|
||||
|
||||
// OLD CMD: gitleaks protect --staged --source={repo}
|
||||
// NEW CMD: gitleaks git --pre-commit --staged {repo}
|
||||
|
||||
// OLD CMD: gitleaks detect --no-git --source={repo}
|
||||
// NEW CMD: gitleaks directory {directory/file}
|
||||
|
||||
// OLD CMD: gitleaks detect --no-git --pipe
|
||||
// NEW CMD: gitleaks stdin
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
@@ -15,12 +33,16 @@ func init() {
|
||||
rootCmd.AddCommand(detectCmd)
|
||||
detectCmd.Flags().Bool("no-git", false, "treat git repo as a regular directory and scan those files, --log-opts has no effect on the scan when --no-git is set")
|
||||
detectCmd.Flags().Bool("pipe", false, "scan input from stdin, ex: `cat some_file | gitleaks detect --pipe`")
|
||||
detectCmd.Flags().Bool("follow-symlinks", false, "scan files that are symlinks to other files")
|
||||
detectCmd.Flags().StringP("source", "s", ".", "path to source")
|
||||
detectCmd.Flags().String("log-opts", "", "git log options")
|
||||
}
|
||||
|
||||
var detectCmd = &cobra.Command{
|
||||
Use: "detect",
|
||||
Short: "detect secrets in code",
|
||||
Run: runDetect,
|
||||
Use: "detect",
|
||||
Short: "detect secrets in code",
|
||||
Run: runDetect,
|
||||
Hidden: true,
|
||||
}
|
||||
|
||||
func runDetect(cmd *cobra.Command, args []string) {
|
||||
@@ -101,5 +123,10 @@ func runDetect(cmd *cobra.Command, args []string) {
|
||||
}
|
||||
}
|
||||
|
||||
// set follow symlinks flag
|
||||
if detector.FollowSymlinks, err = cmd.Flags().GetBool("follow-symlinks"); err != nil {
|
||||
log.Fatal().Err(err).Msg("")
|
||||
}
|
||||
|
||||
findingSummaryAndExit(findings, cmd, cfg, exitCode, start, err)
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/zricethezav/gitleaks/v8/report"
|
||||
"github.com/zricethezav/gitleaks/v8/sources"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(directoryCmd)
|
||||
directoryCmd.Flags().Bool("follow-symlinks", false, "scan files that are symlinks to other files")
|
||||
}
|
||||
|
||||
var directoryCmd = &cobra.Command{
|
||||
Use: "dir [flags] [path]",
|
||||
Aliases: []string{"file", "directory"},
|
||||
Short: "scan directories or files for secrets",
|
||||
Run: runDirectory,
|
||||
}
|
||||
|
||||
func runDirectory(cmd *cobra.Command, args []string) {
|
||||
initConfig()
|
||||
var (
|
||||
findings []report.Finding
|
||||
err error
|
||||
)
|
||||
|
||||
// setup config (aka, the thing that defines rules)
|
||||
cfg := Config(cmd)
|
||||
|
||||
// start timer
|
||||
start := time.Now()
|
||||
|
||||
// grab source
|
||||
source, err := cmd.Flags().GetString("source")
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("could not get source")
|
||||
}
|
||||
detector := Detector(cmd, cfg, source)
|
||||
|
||||
// set exit code
|
||||
exitCode, err := cmd.Flags().GetInt("exit-code")
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("could not get exit code")
|
||||
}
|
||||
|
||||
var paths <-chan sources.ScanTarget
|
||||
paths, err = sources.DirectoryTargets(source, detector.Sema, detector.FollowSymlinks)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err)
|
||||
}
|
||||
|
||||
findings, err = detector.DetectFiles(paths)
|
||||
if err != nil {
|
||||
// don't exit on error, just log it
|
||||
log.Error().Err(err).Msg("failed scan directory")
|
||||
}
|
||||
|
||||
findingSummaryAndExit(findings, cmd, cfg, exitCode, start, err)
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/zricethezav/gitleaks/v8/report"
|
||||
"github.com/zricethezav/gitleaks/v8/sources"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(gitCmd)
|
||||
gitCmd.Flags().Bool("staged", false, "scan staged commits (good for pre-commit)")
|
||||
gitCmd.Flags().Bool("pre-commit", false, "scan using git diff")
|
||||
gitCmd.Flags().String("log-opts", "", "git log options")
|
||||
}
|
||||
|
||||
var gitCmd = &cobra.Command{
|
||||
Use: "git [flags] [repo]",
|
||||
Short: "scan git repositories for secrets",
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
Run: runGit,
|
||||
}
|
||||
|
||||
func runGit(cmd *cobra.Command, args []string) {
|
||||
initConfig()
|
||||
var (
|
||||
findings []report.Finding
|
||||
err error
|
||||
)
|
||||
|
||||
// setup config (aka, the thing that defines rules)
|
||||
cfg := Config(cmd)
|
||||
|
||||
// start timer
|
||||
start := time.Now()
|
||||
|
||||
// grab source
|
||||
source, err := cmd.Flags().GetString("source")
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("could not get source")
|
||||
}
|
||||
detector := Detector(cmd, cfg, source)
|
||||
|
||||
// set exit code
|
||||
exitCode, err := cmd.Flags().GetInt("exit-code")
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("could not get exit code")
|
||||
}
|
||||
|
||||
var (
|
||||
gitCmd *sources.GitCmd
|
||||
logOpts string
|
||||
)
|
||||
logOpts, err = cmd.Flags().GetString("log-opts")
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("could not call GetString() for log-opts")
|
||||
}
|
||||
gitCmd, err = sources.NewGitLogCmd(source, logOpts)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("could not create Git cmd")
|
||||
}
|
||||
findings, err = detector.DetectGit(gitCmd)
|
||||
if err != nil {
|
||||
// don't exit on error, just log it
|
||||
log.Error().Err(err).Msg("failed to scan Git repository")
|
||||
}
|
||||
|
||||
findingSummaryAndExit(findings, cmd, cfg, exitCode, start, err)
|
||||
}
|
||||
+6
-3
@@ -12,13 +12,16 @@ import (
|
||||
|
||||
func init() {
|
||||
protectCmd.Flags().Bool("staged", false, "detect secrets in a --staged state")
|
||||
protectCmd.Flags().String("log-opts", "", "git log options")
|
||||
protectCmd.Flags().StringP("source", "s", ".", "path to source")
|
||||
rootCmd.AddCommand(protectCmd)
|
||||
}
|
||||
|
||||
var protectCmd = &cobra.Command{
|
||||
Use: "protect",
|
||||
Short: "protect secrets in code",
|
||||
Run: runProtect,
|
||||
Use: "protect",
|
||||
Short: "protect secrets in code",
|
||||
Run: runProtect,
|
||||
Hidden: true,
|
||||
}
|
||||
|
||||
func runProtect(cmd *cobra.Command, args []string) {
|
||||
|
||||
+2
-9
@@ -30,7 +30,7 @@ const configDescription = `config file path
|
||||
order of precedence:
|
||||
1. --config/-c
|
||||
2. env var GITLEAKS_CONFIG
|
||||
3. (--source/-s)/.gitleaks.toml
|
||||
3. (target path)/.gitleaks.toml
|
||||
If none of the three options are used, then gitleaks will use the default config`
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
@@ -43,7 +43,6 @@ func init() {
|
||||
cobra.OnInitialize(initLog)
|
||||
rootCmd.PersistentFlags().StringP("config", "c", "", configDescription)
|
||||
rootCmd.PersistentFlags().Int("exit-code", 1, "exit code when leaks have been encountered")
|
||||
rootCmd.PersistentFlags().StringP("source", "s", ".", "path to source")
|
||||
rootCmd.PersistentFlags().StringP("report-path", "r", "", "report file")
|
||||
rootCmd.PersistentFlags().StringP("report-format", "f", "json", "output format (json, csv, junit, sarif)")
|
||||
rootCmd.PersistentFlags().StringP("baseline-path", "b", "", "path to baseline with issues that can be ignored")
|
||||
@@ -55,10 +54,8 @@ func init() {
|
||||
rootCmd.PersistentFlags().Uint("redact", 0, "redact secrets from logs and stdout. To redact only parts of the secret just apply a percent value from 0..100. For example --redact=20 (default 100%)")
|
||||
rootCmd.Flag("redact").NoOptDefVal = "100"
|
||||
rootCmd.PersistentFlags().Bool("no-banner", false, "suppress banner")
|
||||
rootCmd.PersistentFlags().String("log-opts", "", "git log options")
|
||||
rootCmd.PersistentFlags().StringSlice("enable-rule", []string{}, "only enable specific rules by id, ex: `gitleaks detect --enable-rule=atlassian-api-token --enable-rule=slack-access-token`")
|
||||
rootCmd.PersistentFlags().StringSlice("enable-rule", []string{}, "only enable specific rules by id")
|
||||
rootCmd.PersistentFlags().StringP("gitleaks-ignore-path", "i", ".", "path to .gitleaksignore file or folder containing one")
|
||||
rootCmd.PersistentFlags().Bool("follow-symlinks", false, "scan files that are symlinks to other files")
|
||||
err := viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config"))
|
||||
if err != nil {
|
||||
log.Fatal().Msgf("err binding config %s", err.Error())
|
||||
@@ -261,10 +258,6 @@ func Detector(cmd *cobra.Command, cfg config.Config, source string) *detect.Dete
|
||||
detector.Config.Rules = ruleOverride
|
||||
}
|
||||
|
||||
// set follow symlinks flag
|
||||
if detector.FollowSymlinks, err = cmd.Flags().GetBool("follow-symlinks"); err != nil {
|
||||
log.Fatal().Err(err).Msg("")
|
||||
}
|
||||
return detector
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/zricethezav/gitleaks/v8/report"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(stdInCmd)
|
||||
}
|
||||
|
||||
var stdInCmd = &cobra.Command{
|
||||
Use: "stdin",
|
||||
Short: "detect secrets from stdin",
|
||||
Run: runStdIn,
|
||||
}
|
||||
|
||||
func runStdIn(cmd *cobra.Command, args []string) {
|
||||
initConfig()
|
||||
var (
|
||||
findings []report.Finding
|
||||
err error
|
||||
)
|
||||
|
||||
// setup config (aka, the thing that defines rules)
|
||||
cfg := Config(cmd)
|
||||
|
||||
// start timer
|
||||
start := time.Now()
|
||||
detector := Detector(cmd, cfg, "")
|
||||
|
||||
// set exit code
|
||||
exitCode, err := cmd.Flags().GetInt("exit-code")
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("could not get exit code")
|
||||
}
|
||||
|
||||
findings, err = detector.DetectReader(os.Stdin, 10)
|
||||
if err != nil {
|
||||
// log fatal to exit, no need to continue since a report
|
||||
// will not be generated when scanning from a pipe...for now
|
||||
log.Fatal().Err(err).Msg("failed scan input from stdin")
|
||||
}
|
||||
|
||||
findingSummaryAndExit(findings, cmd, cfg, exitCode, start, err)
|
||||
}
|
||||
Reference in new issue
Block a user