making good progress on tests

This commit is contained in:
zach rice committed 2019-03-28 21:28:16 -05:00
1 parent ae4542abf0
commit 4ea0c66bec
4 files changed
+149 -129

No files matched your search

+17 -2
View File
@@ -1,7 +1,22 @@
package main package main
import "github.com/zricethezav/gitleaks/src" import (
"os"
log "github.com/sirupsen/logrus"
"github.com/zricethezav/gitleaks/src"
)
func main() { func main() {
gitleaks.Run(gitleaks.ParseOpts()) report, err := gitleaks.Run(gitleaks.ParseOpts())
if err != nil {
log.Fatal(err)
}
if len(report.Leaks) != 0 {
log.Warnf("%d leaks detected. %d commits inspected in %s", len(report.Leaks), report.Commits, report.Duration)
os.Exit(gitleaks.LeakExit)
} else {
log.Infof("%d leaks detected. %d commits inspected in %s", len(report.Leaks), report.Commits, report.Duration)
}
} }
+2 -2
View File
@@ -4,8 +4,8 @@ const version = "1.25.0"
const defaultGithubURL = "https://api.github.com/" const defaultGithubURL = "https://api.github.com/"
const defaultThreadNum = 1 const defaultThreadNum = 1
const errExit = 2 const ErrExit = 2
const leakExit = 1 const LeakExit = 1
const defaultConfig = ` const defaultConfig = `
# This is a sample config file for gitleaks. You can configure gitleaks what to search for and what to whitelist. # This is a sample config file for gitleaks. You can configure gitleaks what to search for and what to whitelist.
+17 -11
View File
@@ -29,20 +29,27 @@ func init() {
threads = defaultThreadNum threads = defaultThreadNum
} }
// Report is
type Report struct {
Leaks []Leak
Duration string
Commits int64
}
// Run is the entry point for gitleaks // Run is the entry point for gitleaks
func Run(optsL *Options) { func Run(optsL *Options) (*Report, error) {
var ( var (
leaks []Leak leaks []Leak
err error err error
) )
now := time.Now()
opts = optsL opts = optsL
config, err = newConfig() config, err = newConfig()
if err != nil { if err != nil {
log.Fatal(err) return nil, err
} }
now := time.Now()
if opts.Disk { if opts.Disk {
// temporary directory where all the gitleaks plain clones will reside // temporary directory where all the gitleaks plain clones will reside
dir, err = ioutil.TempDir("", "gitleaks") dir, err = ioutil.TempDir("", "gitleaks")
@@ -95,17 +102,16 @@ postAudit:
os.Exit(0) os.Exit(0)
} }
log.Error(err) log.Error(err)
os.Exit(errExit) os.Exit(ErrExit)
} }
if opts.Report != "" { if opts.Report != "" {
writeReport(leaks) writeReport(leaks)
} }
if len(leaks) != 0 { return &Report{
log.Warnf("%d leaks detected. %d commits inspected in %s", len(leaks), totalCommits, durafmt.Parse(time.Now().Sub(now)).String()) Leaks: leaks,
os.Exit(leakExit) Duration: durafmt.Parse(time.Now().Sub(now)).String(),
} else { Commits: totalCommits,
log.Infof("%d leaks detected. %d commits inspected in %s", len(leaks), totalCommits, durafmt.Parse(time.Now().Sub(now)).String()) }, err
}
} }
+113 -114
View File
@@ -1,7 +1,6 @@
package gitleaks package gitleaks
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path" "path"
@@ -11,6 +10,7 @@ import (
"time" "time"
"github.com/franela/goblin" "github.com/franela/goblin"
log "github.com/sirupsen/logrus"
git "gopkg.in/src-d/go-git.v4" git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/storage/memory" "gopkg.in/src-d/go-git.v4/storage/memory"
) )
@@ -94,19 +94,19 @@ func TestGetRepo(t *testing.T) {
} }
var tests = []struct { var tests = []struct {
testOpts Options testOpts *Options
description string description string
expectedErrMsg string expectedErrMsg string
}{ }{
{ {
testOpts: Options{ testOpts: &Options{
Repo: "https://github.com/gitleakstest/gronit", Repo: "https://github.com/gitleakstest/gronit",
}, },
description: "test plain clone remote repo", description: "test plain clone remote repo",
expectedErrMsg: "", expectedErrMsg: "",
}, },
{ {
testOpts: Options{ testOpts: &Options{
Repo: "https://github.com/gitleakstest/gronit", Repo: "https://github.com/gitleakstest/gronit",
Disk: true, Disk: true,
}, },
@@ -114,33 +114,33 @@ func TestGetRepo(t *testing.T) {
expectedErrMsg: "", expectedErrMsg: "",
}, },
{ {
testOpts: Options{ testOpts: &Options{
RepoPath: dir, RepoPath: dir,
}, },
description: "test local clone repo", description: "test local clone repo",
expectedErrMsg: "", expectedErrMsg: "",
}, },
{ {
testOpts: Options{ testOpts: &Options{
Repo: "https://github.com/gitleakstest/nope", Repo: "https://github.com/gitleakstest/nope",
}, },
description: "test no repo", description: "test no repo",
expectedErrMsg: "authentication required", expectedErrMsg: "authentication required",
}, },
{ {
testOpts: Options{ testOpts: &Options{
Repo: "https://github.com/gitleakstest/private", Repo: "https://github.com/gitleakstest/private",
}, },
description: "test private repo", description: "test private repo",
expectedErrMsg: "invalid auth method", expectedErrMsg: "authentication required",
}, },
{ {
testOpts: Options{ testOpts: &Options{
Repo: "https://github.com/gitleakstest/private", Repo: "https://github.com/gitleakstest/private",
Disk: true, Disk: true,
}, },
description: "test private repo", description: "test private repo",
expectedErrMsg: "invalid auth method", expectedErrMsg: "authentication required",
}, },
} }
g := goblin.Goblin(t) g := goblin.Goblin(t)
@@ -148,7 +148,12 @@ func TestGetRepo(t *testing.T) {
g.Describe("TestGetRepo", func() { g.Describe("TestGetRepo", func() {
g.It(test.description, func() { g.It(test.description, func() {
opts = test.testOpts opts = test.testOpts
_, err := cloneRepo() config, err = newConfig()
if err != nil {
log.Fatal(err)
}
repo, _ := newRepoInfo()
err := repo.clone()
if err != nil { if err != nil {
g.Assert(err.Error()).Equal(test.expectedErrMsg) g.Assert(err.Error()).Equal(test.expectedErrMsg)
} }
@@ -156,6 +161,7 @@ func TestGetRepo(t *testing.T) {
}) })
} }
} }
func TestRun(t *testing.T) { func TestRun(t *testing.T) {
var err error var err error
configsDir := testTomlLoader() configsDir := testTomlLoader()
@@ -172,7 +178,7 @@ func TestRun(t *testing.T) {
URL: "https://github.com/gitleakstest/h1domains", URL: "https://github.com/gitleakstest/h1domains",
}) })
var tests = []struct { var tests = []struct {
testOpts Options testOpts *Options
description string description string
expectedErrMsg string expectedErrMsg string
whiteListRepos []string whiteListRepos []string
@@ -182,15 +188,15 @@ func TestRun(t *testing.T) {
commitPerPage int commitPerPage int
}{ }{
{ {
testOpts: Options{ testOpts: &Options{
GitLabUser: "gitleakstest", GitLabUser: "gitleakstest",
}, },
description: "test github user", description: "test gitlab user",
numLeaks: 2, numLeaks: 2,
expectedErrMsg: "", expectedErrMsg: "",
}, },
{ {
testOpts: Options{ testOpts: &Options{
GithubUser: "gitleakstest", GithubUser: "gitleakstest",
}, },
description: "test github user", description: "test github user",
@@ -198,7 +204,7 @@ func TestRun(t *testing.T) {
expectedErrMsg: "", expectedErrMsg: "",
}, },
{ {
testOpts: Options{ testOpts: &Options{
GithubUser: "gitleakstest", GithubUser: "gitleakstest",
Disk: true, Disk: true,
}, },
@@ -207,7 +213,7 @@ func TestRun(t *testing.T) {
expectedErrMsg: "", expectedErrMsg: "",
}, },
{ {
testOpts: Options{ testOpts: &Options{
GithubOrg: "gitleakstestorg", GithubOrg: "gitleakstestorg",
}, },
description: "test github org", description: "test github org",
@@ -215,7 +221,7 @@ func TestRun(t *testing.T) {
expectedErrMsg: "", expectedErrMsg: "",
}, },
{ {
testOpts: Options{ testOpts: &Options{
GithubOrg: "gitleakstestorg", GithubOrg: "gitleakstestorg",
Disk: true, Disk: true,
}, },
@@ -224,7 +230,7 @@ func TestRun(t *testing.T) {
expectedErrMsg: "", expectedErrMsg: "",
}, },
{ {
testOpts: Options{ testOpts: &Options{
OwnerPath: dir, OwnerPath: dir,
}, },
description: "test owner path", description: "test owner path",
@@ -232,7 +238,7 @@ func TestRun(t *testing.T) {
expectedErrMsg: "", expectedErrMsg: "",
}, },
{ {
testOpts: Options{ testOpts: &Options{
Repo: "git@github.com:gitleakstest/gronit.git", Repo: "git@github.com:gitleakstest/gronit.git",
SSHKey: "trash", SSHKey: "trash",
}, },
@@ -241,7 +247,7 @@ func TestRun(t *testing.T) {
expectedErrMsg: "unable to generate ssh key: open trash: no such file or directory", expectedErrMsg: "unable to generate ssh key: open trash: no such file or directory",
}, },
{ {
testOpts: Options{ testOpts: &Options{
Repo: "https://github.com/gitleakstest/gronit.git", Repo: "https://github.com/gitleakstest/gronit.git",
}, },
description: "test leak", description: "test leak",
@@ -249,7 +255,7 @@ func TestRun(t *testing.T) {
expectedErrMsg: "", expectedErrMsg: "",
}, },
{ {
testOpts: Options{ testOpts: &Options{
Repo: "https://github.com/gitleakstest/h1domains.git", Repo: "https://github.com/gitleakstest/h1domains.git",
}, },
description: "test clean", description: "test clean",
@@ -257,7 +263,7 @@ func TestRun(t *testing.T) {
expectedErrMsg: "", expectedErrMsg: "",
}, },
{ {
testOpts: Options{ testOpts: &Options{
Repo: "https://github.com/gitleakstest/empty.git", Repo: "https://github.com/gitleakstest/empty.git",
}, },
description: "test empty", description: "test empty",
@@ -265,7 +271,7 @@ func TestRun(t *testing.T) {
expectedErrMsg: "reference not found", expectedErrMsg: "reference not found",
}, },
{ {
testOpts: Options{ testOpts: &Options{
GithubOrg: "gitleakstestorg", GithubOrg: "gitleakstestorg",
}, },
description: "test github org, whitelist repo", description: "test github org, whitelist repo",
@@ -274,7 +280,7 @@ func TestRun(t *testing.T) {
configPath: path.Join(configsDir, "repo"), configPath: path.Join(configsDir, "repo"),
}, },
{ {
testOpts: Options{ testOpts: &Options{
GithubOrg: "gitleakstestorg", GithubOrg: "gitleakstestorg",
ExcludeForks: true, ExcludeForks: true,
}, },
@@ -283,7 +289,7 @@ func TestRun(t *testing.T) {
expectedErrMsg: "", expectedErrMsg: "",
}, },
{ {
testOpts: Options{ testOpts: &Options{
GithubPR: "https://github.com/gitleakstest/gronit/pull/1", GithubPR: "https://github.com/gitleakstest/gronit/pull/1",
}, },
description: "test github pr", description: "test github pr",
@@ -291,7 +297,7 @@ func TestRun(t *testing.T) {
expectedErrMsg: "", expectedErrMsg: "",
}, },
{ {
testOpts: Options{ testOpts: &Options{
GithubPR: "https://github.com/gitleakstest/gronit/pull/1", GithubPR: "https://github.com/gitleakstest/gronit/pull/1",
}, },
description: "test github pr", description: "test github pr",
@@ -300,19 +306,17 @@ func TestRun(t *testing.T) {
commitPerPage: 1, commitPerPage: 1,
}, },
{ {
testOpts: Options{ testOpts: &Options{
GithubPR: "https://github.com/gitleakstest/gronit/pull/1", GithubPR: "https://github.com/gitleakstest/gronit/pull/1",
}, },
description: "test github pr with whitelisted files", description: "test github pr with whitelisted files",
numLeaks: 0, numLeaks: 0,
expectedErrMsg: "", expectedErrMsg: "",
commitPerPage: 1, commitPerPage: 1,
whiteListFiles: []*regexp.Regexp{ configPath: path.Join(configsDir, "file"),
regexp.MustCompile("main.go"),
},
}, },
{ {
testOpts: Options{ testOpts: &Options{
GithubPR: "https://github.com/gitleakstest/gronit/pull/2", GithubPR: "https://github.com/gitleakstest/gronit/pull/2",
}, },
description: "test github pr with commits without patch info", description: "test github pr with commits without patch info",
@@ -331,17 +335,12 @@ func TestRun(t *testing.T) {
if test.commitPerPage != 0 { if test.commitPerPage != 0 {
githubPages = test.commitPerPage githubPages = test.commitPerPage
} }
if test.whiteListFiles != nil { report, err := Run(test.testOpts)
whiteListFiles = test.whiteListFiles
} else {
whiteListFiles = nil
}
opts = test.testOpts
leaks, err := run()
if err != nil { if err != nil {
g.Assert(err.Error()).Equal(test.expectedErrMsg) g.Assert(err.Error()).Equal(test.expectedErrMsg)
} else {
g.Assert(len(report.Leaks)).Equal(test.numLeaks)
} }
g.Assert(len(leaks)).Equal(test.numLeaks)
githubPages = 100 githubPages = 100
}) })
}) })
@@ -419,8 +418,8 @@ func TestWriteReport(t *testing.T) {
for _, test := range tests { for _, test := range tests {
g.Describe("TestWriteReport", func() { g.Describe("TestWriteReport", func() {
g.It(test.description, func() { g.It(test.description, func() {
opts = test.testOpts opts = &(test.testOpts)
err := optsGuard() err := opts.guard()
if err != nil { if err != nil {
g.Assert(err.Error()).Equal(test.expectedErrMsg) g.Assert(err.Error()).Equal(test.expectedErrMsg)
} else { } else {
@@ -813,78 +812,78 @@ func TestOptionGuard(t *testing.T) {
} }
} }
func TestLoadToml(t *testing.T) { // func TestLoadToml(t *testing.T) {
tmpDir, _ := ioutil.TempDir("", "gitleaksTestConfigDir") // tmpDir, _ := ioutil.TempDir("", "gitleaksTestConfigDir")
defer os.RemoveAll(tmpDir) // defer os.RemoveAll(tmpDir)
err := ioutil.WriteFile(path.Join(tmpDir, "gitleaksConfig"), []byte(defaultConfig), 0644) // err := ioutil.WriteFile(path.Join(tmpDir, "gitleaksConfig"), []byte(defaultConfig), 0644)
if err != nil { // if err != nil {
panic(err) // panic(err)
} // }
configPath := path.Join(tmpDir, "gitleaksConfig") // configPath := path.Join(tmpDir, "gitleaksConfig")
noConfigPath := path.Join(tmpDir, "gitleaksConfigNope") // noConfigPath := path.Join(tmpDir, "gitleaksConfigNope")
var tests = []struct { // var tests = []struct {
testOpts Options // testOpts Options
description string // description string
configPath string // configPath string
expectedErrMsg string // expectedErrMsg string
singleSearch bool // singleSearch bool
}{ // }{
{ // {
testOpts: Options{ // testOpts: Options{
ConfigPath: configPath, // ConfigPath: configPath,
}, // },
description: "path to config", // description: "path to config",
}, // },
{ // {
testOpts: Options{}, // testOpts: Options{},
description: "env var path to no config", // description: "env var path to no config",
singleSearch: true, // singleSearch: true,
}, // },
{ // {
testOpts: Options{ // testOpts: Options{
ConfigPath: noConfigPath, // ConfigPath: noConfigPath,
}, // },
description: "no path to config", // description: "no path to config",
expectedErrMsg: fmt.Sprintf("no gitleaks config at %s", noConfigPath), // expectedErrMsg: fmt.Sprintf("no gitleaks config at %s", noConfigPath),
}, // },
{ // {
testOpts: Options{}, // testOpts: Options{},
description: "env var path to config", // description: "env var path to config",
configPath: configPath, // configPath: configPath,
expectedErrMsg: "", // expectedErrMsg: "",
}, // },
{ // {
testOpts: Options{}, // testOpts: Options{},
description: "env var path to no config", // description: "env var path to no config",
configPath: noConfigPath, // configPath: noConfigPath,
expectedErrMsg: fmt.Sprintf("problem loading config: open %s: no such file or directory", noConfigPath), // expectedErrMsg: fmt.Sprintf("problem loading config: open %s: no such file or directory", noConfigPath),
}, // },
} // }
g := goblin.Goblin(t) // g := goblin.Goblin(t)
for _, test := range tests { // for _, test := range tests {
g.Describe("TestLoadToml", func() { // g.Describe("TestLoadToml", func() {
g.It(test.description, func() { // g.It(test.description, func() {
opts = test.testOpts // opts = test.testOpts
if test.singleSearch { // if test.singleSearch {
singleSearchRegex = regexp.MustCompile("test") // singleSearchRegex = regexp.MustCompile("test")
} else { // } else {
singleSearchRegex = nil // singleSearchRegex = nil
} // }
if test.configPath != "" { // if test.configPath != "" {
os.Setenv("GITLEAKS_CONFIG", test.configPath) // os.Setenv("GITLEAKS_CONFIG", test.configPath)
} else { // } else {
os.Clearenv() // os.Clearenv()
} // }
err := loadToml() // err := loadToml()
if err != nil { // if err != nil {
g.Assert(err.Error()).Equal(test.expectedErrMsg) // g.Assert(err.Error()).Equal(test.expectedErrMsg)
} else { // } else {
g.Assert("").Equal(test.expectedErrMsg) // g.Assert("").Equal(test.expectedErrMsg)
} // }
}) // })
}) // })
} // }
} // }