Explorar el Código

making good progress on tests

zach rice hace 6 años
padre
commit
4ea0c66bec
Se han modificado 4 ficheros con 149 adiciones y 129 borrados
  1. 17 2
      main.go
  2. 2 2
      src/constants.go
  3. 17 11
      src/core.go
  4. 113 114
      src/gitleaks_test.go

+ 17 - 2
main.go

@@ -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
src/constants.go

@@ -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 leakExit = 1
+const ErrExit = 2
+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
src/core.go

@@ -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 {
-		log.Warnf("%d leaks detected. %d commits inspected in %s", len(leaks), totalCommits, durafmt.Parse(time.Now().Sub(now)).String())
-		os.Exit(leakExit)
-	} else {
-		log.Infof("%d leaks detected. %d commits inspected in %s", len(leaks), totalCommits, durafmt.Parse(time.Now().Sub(now)).String())
-	}
+	return &Report{
+		Leaks:    leaks,
+		Duration: durafmt.Parse(time.Now().Sub(now)).String(),
+		Commits:  totalCommits,
+	}, err
 }
 }

+ 113 - 114
src/gitleaks_test.go

@@ -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{
-				regexp.MustCompile("main.go"),
-			},
+			configPath:     path.Join(configsDir, "file"),
 		},
 		},
 		{
 		{
-			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 {
-					whiteListFiles = test.whiteListFiles
-				} else {
-					whiteListFiles = nil
-				}
-				opts = test.testOpts
-				leaks, err := run()
+				report, err := Run(test.testOpts)
 				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
-				err := optsGuard()
+				opts = &(test.testOpts)
+				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) {
-	tmpDir, _ := ioutil.TempDir("", "gitleaksTestConfigDir")
-	defer os.RemoveAll(tmpDir)
-	err := ioutil.WriteFile(path.Join(tmpDir, "gitleaksConfig"), []byte(defaultConfig), 0644)
-	if err != nil {
-		panic(err)
-	}
+// func TestLoadToml(t *testing.T) {
+// 	tmpDir, _ := ioutil.TempDir("", "gitleaksTestConfigDir")
+// 	defer os.RemoveAll(tmpDir)
+// 	err := ioutil.WriteFile(path.Join(tmpDir, "gitleaksConfig"), []byte(defaultConfig), 0644)
+// 	if err != nil {
+// 		panic(err)
+// 	}
 
 
-	configPath := path.Join(tmpDir, "gitleaksConfig")
-	noConfigPath := path.Join(tmpDir, "gitleaksConfigNope")
+// 	configPath := path.Join(tmpDir, "gitleaksConfig")
+// 	noConfigPath := path.Join(tmpDir, "gitleaksConfigNope")
 
 
-	var tests = []struct {
-		testOpts       Options
-		description    string
-		configPath     string
-		expectedErrMsg string
-		singleSearch   bool
-	}{
-		{
-			testOpts: Options{
-				ConfigPath: configPath,
-			},
-			description: "path to config",
-		},
-		{
-			testOpts:     Options{},
-			description:  "env var path to no config",
-			singleSearch: true,
-		},
-		{
-			testOpts: Options{
-				ConfigPath: noConfigPath,
-			},
-			description:    "no path to config",
-			expectedErrMsg: fmt.Sprintf("no gitleaks config at %s", noConfigPath),
-		},
-		{
-			testOpts:       Options{},
-			description:    "env var path to config",
-			configPath:     configPath,
-			expectedErrMsg: "",
-		},
-		{
-			testOpts:       Options{},
-			description:    "env var path to no config",
-			configPath:     noConfigPath,
-			expectedErrMsg: fmt.Sprintf("problem loading config: open %s: no such file or directory", noConfigPath),
-		},
-	}
+// 	var tests = []struct {
+// 		testOpts       Options
+// 		description    string
+// 		configPath     string
+// 		expectedErrMsg string
+// 		singleSearch   bool
+// 	}{
+// 		{
+// 			testOpts: Options{
+// 				ConfigPath: configPath,
+// 			},
+// 			description: "path to config",
+// 		},
+// 		{
+// 			testOpts:     Options{},
+// 			description:  "env var path to no config",
+// 			singleSearch: true,
+// 		},
+// 		{
+// 			testOpts: Options{
+// 				ConfigPath: noConfigPath,
+// 			},
+// 			description:    "no path to config",
+// 			expectedErrMsg: fmt.Sprintf("no gitleaks config at %s", noConfigPath),
+// 		},
+// 		{
+// 			testOpts:       Options{},
+// 			description:    "env var path to config",
+// 			configPath:     configPath,
+// 			expectedErrMsg: "",
+// 		},
+// 		{
+// 			testOpts:       Options{},
+// 			description:    "env var path to no config",
+// 			configPath:     noConfigPath,
+// 			expectedErrMsg: fmt.Sprintf("problem loading config: open %s: no such file or directory", noConfigPath),
+// 		},
+// 	}
 
 
-	g := goblin.Goblin(t)
-	for _, test := range tests {
-		g.Describe("TestLoadToml", func() {
-			g.It(test.description, func() {
-				opts = test.testOpts
-				if test.singleSearch {
-					singleSearchRegex = regexp.MustCompile("test")
-				} else {
-					singleSearchRegex = nil
-				}
-				if test.configPath != "" {
-					os.Setenv("GITLEAKS_CONFIG", test.configPath)
-				} else {
-					os.Clearenv()
-				}
-				err := loadToml()
-				if err != nil {
-					g.Assert(err.Error()).Equal(test.expectedErrMsg)
-				} else {
-					g.Assert("").Equal(test.expectedErrMsg)
-				}
-			})
-		})
-	}
-}
+// 	g := goblin.Goblin(t)
+// 	for _, test := range tests {
+// 		g.Describe("TestLoadToml", func() {
+// 			g.It(test.description, func() {
+// 				opts = test.testOpts
+// 				if test.singleSearch {
+// 					singleSearchRegex = regexp.MustCompile("test")
+// 				} else {
+// 					singleSearchRegex = nil
+// 				}
+// 				if test.configPath != "" {
+// 					os.Setenv("GITLEAKS_CONFIG", test.configPath)
+// 				} else {
+// 					os.Clearenv()
+// 				}
+// 				err := loadToml()
+// 				if err != nil {
+// 					g.Assert(err.Error()).Equal(test.expectedErrMsg)
+// 				} else {
+// 					g.Assert("").Equal(test.expectedErrMsg)
+// 				}
+// 			})
+// 		})
+// 	}
+// }