mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-29 20:54:22 +01:00
working on coverage
This commit is contained in:
1 parent
042b82e2af
commit
f9be4e543a
3 files changed
+175
-167
No files matched your search
+1
-1
@@ -20,8 +20,8 @@ var (
|
|||||||
threads int
|
threads int
|
||||||
totalCommits int64
|
totalCommits int64
|
||||||
commitMap = make(map[string]bool)
|
commitMap = make(map[string]bool)
|
||||||
cMutex = &sync.Mutex{}
|
|
||||||
auditDone bool
|
auditDone bool
|
||||||
|
mutex = &sync.Mutex{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|||||||
+169
-163
@@ -1,10 +1,12 @@
|
|||||||
package gitleaks
|
package gitleaks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -656,10 +658,18 @@ func TestAuditRepo(t *testing.T) {
|
|||||||
Depth: 2,
|
Depth: 2,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
repo: leaksRepo,
|
||||||
|
description: "toml entropy range from opts",
|
||||||
|
numLeaks: 454,
|
||||||
|
testOpts: &Options{
|
||||||
|
ConfigPath: path.Join(configsDir, "entropy"),
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
repo: leaksRepo,
|
repo: leaksRepo,
|
||||||
description: "toml entropy range",
|
description: "toml entropy range",
|
||||||
numLeaks: 430,
|
numLeaks: 454,
|
||||||
testOpts: &Options{},
|
testOpts: &Options{},
|
||||||
configPath: path.Join(configsDir, "entropy"),
|
configPath: path.Join(configsDir, "entropy"),
|
||||||
},
|
},
|
||||||
@@ -714,11 +724,7 @@ func TestAuditRepo(t *testing.T) {
|
|||||||
if opts.Redact {
|
if opts.Redact {
|
||||||
g.Assert(leaks[0].Offender).Equal("REDACTED")
|
g.Assert(leaks[0].Offender).Equal("REDACTED")
|
||||||
}
|
}
|
||||||
if test.description == "toml entropy range" {
|
g.Assert(len(leaks)).Equal(test.numLeaks)
|
||||||
g.Assert(len(leaks) > test.numLeaks).Equal(true)
|
|
||||||
} else {
|
|
||||||
g.Assert(len(leaks)).Equal(test.numLeaks)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
next:
|
next:
|
||||||
os.Setenv("GITLEAKS_CONFIG", "")
|
os.Setenv("GITLEAKS_CONFIG", "")
|
||||||
@@ -727,165 +733,165 @@ func TestAuditRepo(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// func TestOptionGuard(t *testing.T) {
|
func TestOptionGuard(t *testing.T) {
|
||||||
// var tests = []struct {
|
var tests = []struct {
|
||||||
// testOpts Options
|
testOpts *Options
|
||||||
// githubToken bool
|
githubToken bool
|
||||||
// description string
|
description string
|
||||||
// expectedErrMsg string
|
expectedErrMsg string
|
||||||
// expectedErrMsgFuzzy string
|
expectedErrMsgFuzzy string
|
||||||
// }{
|
}{
|
||||||
// {
|
{
|
||||||
// testOpts: Options{},
|
testOpts: &Options{},
|
||||||
// description: "default no opts",
|
description: "default no opts",
|
||||||
// expectedErrMsg: "",
|
expectedErrMsg: "",
|
||||||
// },
|
},
|
||||||
// {
|
{
|
||||||
// testOpts: Options{
|
testOpts: &Options{
|
||||||
// GithubUser: "fakeUser",
|
GithubUser: "fakeUser",
|
||||||
// GithubOrg: "fakeOrg",
|
GithubOrg: "fakeOrg",
|
||||||
// },
|
},
|
||||||
// description: "double owner",
|
description: "double owner",
|
||||||
// expectedErrMsg: "github user and organization set",
|
expectedErrMsg: "github user and organization set",
|
||||||
// },
|
},
|
||||||
// {
|
{
|
||||||
// testOpts: Options{
|
testOpts: &Options{
|
||||||
// GithubOrg: "fakeOrg",
|
GithubOrg: "fakeOrg",
|
||||||
// OwnerPath: "/dev/null",
|
OwnerPath: "/dev/null",
|
||||||
// },
|
},
|
||||||
// description: "local and remote target",
|
description: "local and remote target",
|
||||||
// expectedErrMsg: "github organization set and local owner path",
|
expectedErrMsg: "github organization set and local owner path",
|
||||||
// },
|
},
|
||||||
// {
|
{
|
||||||
// testOpts: Options{
|
testOpts: &Options{
|
||||||
// GithubUser: "fakeUser",
|
GithubUser: "fakeUser",
|
||||||
// OwnerPath: "/dev/null",
|
OwnerPath: "/dev/null",
|
||||||
// },
|
},
|
||||||
// description: "local and remote target",
|
description: "local and remote target",
|
||||||
// expectedErrMsg: "github user set and local owner path",
|
expectedErrMsg: "github user set and local owner path",
|
||||||
// },
|
},
|
||||||
// {
|
{
|
||||||
// testOpts: Options{
|
testOpts: &Options{
|
||||||
// GithubUser: "fakeUser",
|
GithubUser: "fakeUser",
|
||||||
// SingleSearch: "*/./....",
|
SingleSearch: "*/./....",
|
||||||
// },
|
},
|
||||||
// description: "single search invalid regex gaurd",
|
description: "single search invalid regex gaurd",
|
||||||
// expectedErrMsgFuzzy: "unable to compile regex: */./...., ",
|
expectedErrMsgFuzzy: "unable to compile regex: */./...., ",
|
||||||
// },
|
},
|
||||||
// {
|
{
|
||||||
// testOpts: Options{
|
testOpts: &Options{
|
||||||
// GithubUser: "fakeUser",
|
GithubUser: "fakeUser",
|
||||||
// SingleSearch: "mystring",
|
SingleSearch: "mystring",
|
||||||
// },
|
},
|
||||||
// description: "single search regex gaurd",
|
description: "single search regex gaurd",
|
||||||
// expectedErrMsg: "",
|
expectedErrMsg: "",
|
||||||
// },
|
},
|
||||||
// {
|
{
|
||||||
// testOpts: Options{
|
testOpts: &Options{
|
||||||
// GithubOrg: "fakeOrg",
|
GithubOrg: "fakeOrg",
|
||||||
// Entropy: 9,
|
Entropy: 9,
|
||||||
// },
|
},
|
||||||
// description: "Invalid entropy level guard",
|
description: "Invalid entropy level guard",
|
||||||
// expectedErrMsg: "The maximum level of entropy is 8",
|
expectedErrMsg: "The maximum level of entropy is 8",
|
||||||
// },
|
},
|
||||||
// }
|
}
|
||||||
// g := goblin.Goblin(t)
|
g := goblin.Goblin(t)
|
||||||
// for _, test := range tests {
|
for _, test := range tests {
|
||||||
// g.Describe("Test Option Gaurd", func() {
|
g.Describe("Test Option Gaurd", func() {
|
||||||
// g.It(test.description, func() {
|
g.It(test.description, func() {
|
||||||
// os.Clearenv()
|
os.Clearenv()
|
||||||
// opts = test.testOpts
|
opts = test.testOpts
|
||||||
// if test.githubToken {
|
if test.githubToken {
|
||||||
// os.Setenv("GITHUB_TOKEN", "fakeToken")
|
os.Setenv("GITHUB_TOKEN", "fakeToken")
|
||||||
// }
|
}
|
||||||
// err := optsGuard()
|
err := opts.guard()
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// if test.expectedErrMsgFuzzy != "" {
|
if test.expectedErrMsgFuzzy != "" {
|
||||||
// g.Assert(strings.Contains(err.Error(), test.expectedErrMsgFuzzy)).Equal(true)
|
g.Assert(strings.Contains(err.Error(), test.expectedErrMsgFuzzy)).Equal(true)
|
||||||
// } else {
|
} else {
|
||||||
// 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)
|
||||||
// }
|
}
|
||||||
|
|
||||||
// })
|
})
|
||||||
// })
|
})
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
|
||||||
// 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 = newConfig()
|
||||||
// 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)
|
||||||
// }
|
}
|
||||||
// })
|
})
|
||||||
// })
|
})
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
+5
-3
@@ -109,7 +109,6 @@ func (repoInfo *RepoInfo) audit() ([]Leak, error) {
|
|||||||
leaks []Leak
|
leaks []Leak
|
||||||
commitCount int64
|
commitCount int64
|
||||||
commitWg sync.WaitGroup
|
commitWg sync.WaitGroup
|
||||||
mutex = &sync.Mutex{}
|
|
||||||
semaphore chan bool
|
semaphore chan bool
|
||||||
logOpts git.LogOptions
|
logOpts git.LogOptions
|
||||||
)
|
)
|
||||||
@@ -186,7 +185,10 @@ func (repoInfo *RepoInfo) audit() ([]Leak, error) {
|
|||||||
|
|
||||||
// commits w/o parent (root of git the git ref) or option for single commit is not empty str
|
// commits w/o parent (root of git the git ref) or option for single commit is not empty str
|
||||||
if len(c.ParentHashes) == 0 || opts.Commit == c.Hash.String() {
|
if len(c.ParentHashes) == 0 || opts.Commit == c.Hash.String() {
|
||||||
leaks = append(repoInfo.auditSingleCommit(c, mutex), leaks...)
|
leaksFromSingleCommit := repoInfo.auditSingleCommit(c)
|
||||||
|
mutex.Lock()
|
||||||
|
leaks = append(leaksFromSingleCommit, leaks...)
|
||||||
|
mutex.Unlock()
|
||||||
if opts.Commit == c.Hash.String() {
|
if opts.Commit == c.Hash.String() {
|
||||||
return storer.ErrStop
|
return storer.ErrStop
|
||||||
}
|
}
|
||||||
@@ -273,7 +275,7 @@ func (repoInfo *RepoInfo) audit() ([]Leak, error) {
|
|||||||
return leaks, nil
|
return leaks, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (repoInfo *RepoInfo) auditSingleCommit(c *object.Commit, mutex *sync.Mutex) []Leak {
|
func (repoInfo *RepoInfo) auditSingleCommit(c *object.Commit) []Leak {
|
||||||
var leaks []Leak
|
var leaks []Leak
|
||||||
fIter, err := c.Files()
|
fIter, err := c.Files()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in new issue
Block a user