options.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "strconv"
  6. "strings"
  7. "regexp"
  8. "github.com/mitchellh/go-homedir"
  9. "path/filepath"
  10. "go.uber.org/zap"
  11. "go.uber.org/zap/zapcore"
  12. )
  13. const DEBUG = 0
  14. const INFO = 1
  15. const ERROR = 2
  16. const usage = `usage: gitleaks [options] <URL>/<path_to_repo>
  17. Options:
  18. Modes
  19. -u --user Git user mode
  20. -r --repo Git repo mode
  21. -o --org Git organization mode
  22. -l --local Local mode, gitleaks will look for local repo in <path>
  23. Logging
  24. -ll <INT> --log=<INT> 0: Debug, 1: Info, 3: Error
  25. -v --verbose Verbose mode, will output leaks as gitleaks finds them
  26. Locations
  27. --report_path=<STR> Report output, default $GITLEAKS_HOME/report
  28. --clone_path=<STR> Gitleaks will clone repos here, default $GITLEAKS_HOME/clones
  29. Other
  30. -t --temp Clone to temporary directory
  31. -c <INT> Upper bound on concurrent diffs
  32. --since=<STR> Commit to stop at
  33. --b64Entropy=<INT> Base64 entropy cutoff (default is 70)
  34. --hexEntropy=<INT> Hex entropy cutoff (default is 40)
  35. -e --entropy Enable entropy
  36. -h --help Display this message
  37. --token=<STR> Github API token
  38. --stopwords Enables stopwords
  39. `
  40. // Options for gitleaks. need to support remote repo/owner
  41. // and local repo/owner mode
  42. type Options struct {
  43. URL string
  44. RepoPath string
  45. ClonePath string
  46. ReportPath string
  47. Concurrency int
  48. B64EntropyCutoff int
  49. HexEntropyCutoff int
  50. // MODES
  51. UserMode bool
  52. OrgMode bool
  53. RepoMode bool
  54. LocalMode bool
  55. // OPTS
  56. Strict bool
  57. Entropy bool
  58. SinceCommit string
  59. Persist bool
  60. IncludeForks bool
  61. Tmp bool
  62. ReportOut bool
  63. Token string
  64. // LOGS/REPORT
  65. Verbose bool
  66. LogLevel int
  67. }
  68. // help prints the usage string and exits
  69. func help() {
  70. os.Stderr.WriteString(usage)
  71. }
  72. // optionsNextInt is a parseOptions helper that returns the value (int) of an option if valid
  73. func (opts *Options) nextInt(args []string, i *int) int {
  74. if len(args) > *i+1 {
  75. *i++
  76. } else {
  77. help()
  78. }
  79. argInt, err := strconv.Atoi(args[*i])
  80. if err != nil {
  81. opts.failF("Invalid %s option: %s\n", args[*i-1], args[*i])
  82. }
  83. return argInt
  84. }
  85. // optionsNextString is a parseOptions helper that returns the value (string) of an option if valid
  86. func (opts *Options) nextString(args []string, i *int) string {
  87. if len(args) > *i+1 {
  88. *i++
  89. } else {
  90. opts.failF("Invalid %s option: %s\n", args[*i-1], args[*i])
  91. }
  92. return args[*i]
  93. }
  94. // optInt grabs the string ...
  95. func (opts *Options) optString(arg string, prefixes ...string) (bool, string) {
  96. for _, prefix := range prefixes {
  97. if strings.HasPrefix(arg, prefix) {
  98. return true, arg[len(prefix):]
  99. }
  100. }
  101. return false, ""
  102. }
  103. // optInt grabs the int ...
  104. func (opts *Options) optInt(arg string, prefixes ...string) (bool, int) {
  105. for _, prefix := range prefixes {
  106. if strings.HasPrefix(arg, prefix) {
  107. i, err := strconv.Atoi(arg[len(prefix):])
  108. if err != nil {
  109. opts.failF("Invalid %s int option\n", prefix)
  110. }
  111. return true, i
  112. }
  113. }
  114. return false, 0
  115. }
  116. // newOpts generates opts and parses arguments
  117. func newOpts(args []string) (*Options) {
  118. opts, err := defaultOptions()
  119. if err != nil{
  120. opts.failF("%v", err)
  121. }
  122. err = opts.parseOptions(args)
  123. if err != nil{
  124. opts.failF("%v", err)
  125. }
  126. opts.setupLogger()
  127. return opts
  128. }
  129. // deafultOptions provides the default options
  130. func defaultOptions() (*Options, error) {
  131. // default GITLEAKS_HOME is $HOME/.gitleaks
  132. // gitleaks will use this location for clones if
  133. // no clone-path is provided
  134. gitleaksHome := os.Getenv("GITLEAKS_HOME")
  135. if gitleaksHome == "" {
  136. homeDir, err := homedir.Dir()
  137. if err != nil {
  138. return nil, fmt.Errorf("could not find system home dir")
  139. }
  140. gitleaksHome = filepath.Join(homeDir, ".gitleaks")
  141. }
  142. // make sure gitleaks home exists
  143. if _, err := os.Stat(gitleaksHome); os.IsNotExist(err) {
  144. os.Mkdir(gitleaksHome, os.ModePerm)
  145. }
  146. return &Options{
  147. Concurrency: 10,
  148. B64EntropyCutoff: 70,
  149. HexEntropyCutoff: 40,
  150. LogLevel: INFO,
  151. ClonePath: filepath.Join(gitleaksHome, "clone"),
  152. ReportPath: filepath.Join(gitleaksHome, "report"),
  153. }, nil
  154. }
  155. // parseOptions
  156. func (opts *Options) parseOptions(args []string) error {
  157. if len(args) == 0 {
  158. help()
  159. }
  160. for i := 0; i < len(args); i++ {
  161. arg := args[i]
  162. switch arg {
  163. case "-s":
  164. opts.SinceCommit = opts.nextString(args, &i)
  165. case "--strict":
  166. opts.Strict = true
  167. case "-b", "--b64Entropy":
  168. opts.B64EntropyCutoff = opts.nextInt(args, &i)
  169. case "-x", "--hexEntropy":
  170. opts.HexEntropyCutoff = opts.nextInt(args, &i)
  171. case "-e", "--entropy":
  172. opts.Entropy = true
  173. case "-c":
  174. opts.Concurrency = opts.nextInt(args, &i)
  175. case "-o", "--org":
  176. opts.OrgMode = true
  177. case "-u", "--user":
  178. opts.UserMode = true
  179. case "-r", "--repo":
  180. opts.RepoMode = true
  181. case "-l", "--local":
  182. opts.LocalMode = true
  183. case "--report-out":
  184. opts.ReportOut = true
  185. case "-t", "--temp":
  186. opts.Tmp = true
  187. case "-ll":
  188. opts.LogLevel = opts.nextInt(args, &i)
  189. case "-h", "--help":
  190. help()
  191. os.Exit(EXIT_CLEAN)
  192. default:
  193. // TARGETS
  194. if i == len(args)-1 {
  195. fmt.Println(arg[i])
  196. if opts.LocalMode {
  197. opts.RepoPath = args[i]
  198. } else {
  199. opts.URL = args[i]
  200. }
  201. } else if match, value := opts.optString(arg, "--token="); match {
  202. opts.Token = value
  203. } else if match, value := opts.optString(arg, "--since="); match {
  204. opts.SinceCommit = value
  205. } else if match, value := opts.optString(arg, "--report-path="); match {
  206. opts.ReportPath = value
  207. } else if match, value := opts.optString(arg, "--clone-path="); match {
  208. opts.ClonePath = value
  209. } else if match, value := opts.optInt(arg, "--log="); match {
  210. opts.LogLevel = value
  211. } else if match, value := opts.optInt(arg, "--b64Entropy="); match {
  212. opts.B64EntropyCutoff = value
  213. } else if match, value := opts.optInt(arg, "--hexEntropy="); match {
  214. opts.HexEntropyCutoff = value
  215. } else {
  216. fmt.Printf("Unknown option %s\n\n", arg)
  217. help()
  218. return fmt.Errorf("Unknown option %s\n\n", arg)
  219. }
  220. }
  221. }
  222. err := opts.guards()
  223. if err != nil{
  224. fmt.Printf("%v", err)
  225. }
  226. return err
  227. }
  228. // failF prints a failure message out to stderr, displays help
  229. // and exits with a exit code 2
  230. func (opts *Options) failF(format string, args ...interface{}) {
  231. fmt.Fprintf(os.Stderr, format, args...)
  232. help()
  233. os.Exit(EXIT_FAILURE)
  234. }
  235. // guards will prevent gitleaks from continuing if any invalid options
  236. // are found.
  237. func (opts *Options) guards() error {
  238. if (opts.RepoMode || opts.OrgMode || opts.UserMode) && !isGithubTarget(opts.URL) {
  239. return fmt.Errorf("Not valid github target %s\n", opts.URL)
  240. } else if (opts.RepoMode || opts.OrgMode || opts.UserMode) && opts.LocalMode {
  241. return fmt.Errorf("Cannot run Gitleaks on repo/user/org mode and local mode\n")
  242. } else if (opts.RepoMode || opts.UserMode) && opts.OrgMode {
  243. return fmt.Errorf("Cannot run Gitleaks on more than one mode\n")
  244. } else if (opts.OrgMode || opts.UserMode) && opts.RepoMode {
  245. return fmt.Errorf("Cannot run Gitleaks on more than one mode\n")
  246. } else if (opts.OrgMode || opts.RepoMode) && opts.UserMode{
  247. return fmt.Errorf("Cannot run Gitleaks on more than one mode\n")
  248. } else if opts.LocalMode && opts.Tmp {
  249. return fmt.Errorf("Cannot run Gitleaks with temp settings and local mode\n")
  250. } else if opts.SinceCommit != "" && (opts.OrgMode || opts.UserMode) {
  251. return fmt.Errorf("Cannot run Gitleaks with since commit flag and a owner mode\n")
  252. }
  253. return nil
  254. }
  255. // setupLogger initiates the logger and sets the logging level
  256. // based on what is set in arguments. Default logging level is
  257. // INFO
  258. func (opts *Options) setupLogger() {
  259. atom := zap.NewAtomicLevel()
  260. encoderCfg := zap.NewProductionEncoderConfig()
  261. encoderCfg.TimeKey = ""
  262. logger = zap.New(zapcore.NewCore(
  263. zapcore.NewJSONEncoder(encoderCfg),
  264. zapcore.Lock(os.Stdout),
  265. atom,
  266. ))
  267. switch opts.LogLevel {
  268. case DEBUG:
  269. atom.SetLevel(zap.DebugLevel)
  270. case INFO:
  271. atom.SetLevel(zap.InfoLevel)
  272. case ERROR:
  273. atom.SetLevel(zap.ErrorLevel)
  274. }
  275. }
  276. // isGithubTarget checks if url is a valid github target
  277. func isGithubTarget(url string) bool {
  278. re := regexp.MustCompile("github\\.com\\/")
  279. return re.MatchString(url)
  280. }