getting testing there

This commit is contained in:
zach rice committed 2019-03-29 13:02:59 -05:00
1 parent 4ea0c66bec
commit 9176896742
4 files changed
+165 -161

No files matched your search

+3 -5
View File
@@ -30,6 +30,7 @@ type TomlConfig struct {
} }
Entropy struct { Entropy struct {
LineRegexes []string LineRegexes []string
Ranges []string
} }
Whitelist struct { Whitelist struct {
Files []string Files []string
@@ -37,9 +38,6 @@ type TomlConfig struct {
Commits []string Commits []string
Repos []string Repos []string
} }
Misc struct {
Entropy []string
}
} }
// Config contains gitleaks config // Config contains gitleaks config
@@ -106,8 +104,8 @@ func newConfig() (*Config, error) {
// updateConfig will update a the global config values // updateConfig will update a the global config values
func (config *Config) update(tomlConfig TomlConfig) error { func (config *Config) update(tomlConfig TomlConfig) error {
if len(tomlConfig.Misc.Entropy) != 0 { if len(tomlConfig.Entropy.Ranges) != 0 {
err := config.updateEntropyRanges(tomlConfig.Misc.Entropy) err := config.updateEntropyRanges(tomlConfig.Entropy.Ranges)
if err != nil { if err != nil {
return err return err
} }
+3 -2
View File
@@ -1,6 +1,8 @@
package gitleaks package gitleaks
import "math" import (
"math"
)
// getShannonEntropy https://en.wiktionary.org/wiki/Shannon_entropy // getShannonEntropy https://en.wiktionary.org/wiki/Shannon_entropy
func getShannonEntropy(data string) (entropy float64) { func getShannonEntropy(data string) (entropy float64) {
@@ -40,7 +42,6 @@ func highEntropyLineIsALeak(line string) bool {
if !opts.NoiseReduction { if !opts.NoiseReduction {
return true return true
} }
for _, re := range config.Entropy.regexes { for _, re := range config.Entropy.regexes {
if re.FindString(line) != "" { if re.FindString(line) != "" {
return true return true
+152 -154
View File
@@ -5,7 +5,6 @@ import (
"os" "os"
"path" "path"
"regexp" "regexp"
"strings"
"testing" "testing"
"time" "time"
@@ -59,21 +58,34 @@ repos = [
` `
const testEntropyRange = ` const testEntropyRange = `
[misc] [entropy]
entropy = [ ranges = [
"7.5-8.0", "7.5-8.0",
"3.3-3.4", "3.3-3.4",
] ]
lineregexes = [
"api",
"key",
"signature",
"secret",
"password",
"pass",
"pwd",
"token",
"curl",
"wget",
"https?",
]
` `
const testBadEntropyRange = ` const testBadEntropyRange = `
[misc] [entropy]
entropy = [ ranges = [
"8.0-3.0", "8.0-3.0",
] ]
` `
const testBadEntropyRange2 = ` const testBadEntropyRange2 = `
[misc] [entropy]
entropy = [ ranges = [
"8.0-8.9", "8.0-8.9",
] ]
` `
@@ -447,20 +459,16 @@ func testTomlLoader() string {
func TestAuditRepo(t *testing.T) { func TestAuditRepo(t *testing.T) {
var leaks []Leak var leaks []Leak
err := loadToml()
configsDir := testTomlLoader() configsDir := testTomlLoader()
defer os.RemoveAll(configsDir) defer os.RemoveAll(configsDir)
if err != nil {
panic(err)
}
leaksR, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{ leaksR, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: "https://github.com/gitleakstest/gronit.git", URL: "https://github.com/gitleakstest/gronit.git",
}) })
if err != nil { if err != nil {
panic(err) panic(err)
} }
leaksRepo := &RepoDescriptor{ leaksRepo := &RepoInfo{
repository: leaksR, repository: leaksR,
name: "gronit", name: "gronit",
} }
@@ -471,17 +479,17 @@ func TestAuditRepo(t *testing.T) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
cleanRepo := &RepoDescriptor{ cleanRepo := &RepoInfo{
repository: cleanR, repository: cleanR,
name: "h1domains", name: "h1domains",
} }
var tests = []struct { var tests = []struct {
testOpts Options testOpts *Options
description string description string
expectedErrMsg string expectedErrMsg string
numLeaks int numLeaks int
repo *RepoDescriptor repo *RepoInfo
whiteListFiles []*regexp.Regexp whiteListFiles []*regexp.Regexp
whiteListCommits map[string]bool whiteListCommits map[string]bool
whiteListRepos []*regexp.Regexp whiteListRepos []*regexp.Regexp
@@ -492,7 +500,7 @@ func TestAuditRepo(t *testing.T) {
repo: leaksRepo, repo: leaksRepo,
description: "pinned config", description: "pinned config",
numLeaks: 0, numLeaks: 0,
testOpts: Options{ testOpts: &Options{
RepoConfig: true, RepoConfig: true,
}, },
}, },
@@ -500,7 +508,7 @@ func TestAuditRepo(t *testing.T) {
repo: leaksRepo, repo: leaksRepo,
description: "commit depth = 1, one leak", description: "commit depth = 1, one leak",
numLeaks: 1, numLeaks: 1,
testOpts: Options{ testOpts: &Options{
Depth: 1, Depth: 1,
}, },
}, },
@@ -508,53 +516,58 @@ func TestAuditRepo(t *testing.T) {
repo: leaksRepo, repo: leaksRepo,
description: "two leaks present", description: "two leaks present",
numLeaks: 2, numLeaks: 2,
testOpts: &Options{},
}, },
{ {
repo: leaksRepo, repo: leaksRepo,
description: "two leaks present limit goroutines", description: "two leaks present limit goroutines",
numLeaks: 2, numLeaks: 2,
testOpts: Options{ testOpts: &Options{
Threads: 4, Threads: 4,
}, },
}, },
{ {
repo: leaksRepo, repo: leaksRepo,
description: "two leaks present whitelist AWS.. no leaks", description: "two leaks present whitelist AWS.. no leaks",
whiteListRegexes: []*regexp.Regexp{ testOpts: &Options{},
regexp.MustCompile("AKIA"), configPath: path.Join(configsDir, "regex"),
},
numLeaks: 0, numLeaks: 0,
}, },
{ {
repo: leaksRepo, repo: leaksRepo,
description: "two leaks present limit goroutines", description: "two leaks present limit goroutines",
testOpts: &Options{
Threads: 2,
},
numLeaks: 2, numLeaks: 2,
}, },
{ {
repo: cleanRepo, repo: cleanRepo,
description: "no leaks present", description: "no leaks present",
testOpts: &Options{},
numLeaks: 0, numLeaks: 0,
}, },
{ {
repo: leaksRepo, repo: leaksRepo,
description: "two leaks present whitelist go files", description: "two leaks present whitelist go files",
whiteListFiles: []*regexp.Regexp{ testOpts: &Options{},
regexp.MustCompile(".go"), configPath: path.Join(configsDir, "file"),
},
numLeaks: 0, numLeaks: 0,
}, },
{ {
repo: leaksRepo, repo: leaksRepo,
description: "two leaks present whitelist bad commit", description: "two leaks present whitelist bad commit",
whiteListCommits: map[string]bool{ configPath: path.Join(configsDir, "commit"),
"eaeffdc65b4c73ccb67e75d96bd8743be2c85973": true, testOpts: &Options{},
}, // whiteListCommits: map[string]bool{
// "eaeffdc65b4c73ccb67e75d96bd8743be2c85973": true,
// },
numLeaks: 1, numLeaks: 1,
}, },
{ {
repo: leaksRepo, repo: leaksRepo,
description: "redact", description: "redact",
testOpts: Options{ testOpts: &Options{
Redact: true, Redact: true,
}, },
numLeaks: 2, numLeaks: 2,
@@ -563,7 +576,7 @@ func TestAuditRepo(t *testing.T) {
repo: leaksRepo, repo: leaksRepo,
description: "Audit a specific commit", description: "Audit a specific commit",
numLeaks: 1, numLeaks: 1,
testOpts: Options{ testOpts: &Options{
Commit: "cb5599aeed261b2c038aa4729e2d53ca050a4988", Commit: "cb5599aeed261b2c038aa4729e2d53ca050a4988",
}, },
}, },
@@ -571,7 +584,7 @@ func TestAuditRepo(t *testing.T) {
repo: leaksRepo, repo: leaksRepo,
description: "Audit a specific commit no leaks", description: "Audit a specific commit no leaks",
numLeaks: 0, numLeaks: 0,
testOpts: Options{ testOpts: &Options{
Commit: "2b033e012eee364fc41b4ab7c5db1497399b8e67", Commit: "2b033e012eee364fc41b4ab7c5db1497399b8e67",
}, },
}, },
@@ -579,38 +592,41 @@ func TestAuditRepo(t *testing.T) {
repo: leaksRepo, repo: leaksRepo,
description: "toml whitelist regex", description: "toml whitelist regex",
configPath: path.Join(configsDir, "regex"), configPath: path.Join(configsDir, "regex"),
testOpts: &Options{},
numLeaks: 0, numLeaks: 0,
}, },
{ {
repo: leaksRepo, repo: leaksRepo,
description: "toml whitelist file", description: "toml whitelist file",
configPath: path.Join(configsDir, "file"), configPath: path.Join(configsDir, "file"),
testOpts: &Options{},
numLeaks: 0, numLeaks: 0,
}, },
{ {
repo: leaksRepo, repo: leaksRepo,
description: "toml whitelist commit", description: "toml whitelist commit",
configPath: path.Join(configsDir, "commit"), configPath: path.Join(configsDir, "commit"),
testOpts: &Options{},
numLeaks: 1, numLeaks: 1,
}, },
{ {
repo: leaksRepo, repo: leaksRepo,
description: "audit whitelist repo", description: "audit whitelist repo",
numLeaks: 0, numLeaks: 0,
whiteListRepos: []*regexp.Regexp{ testOpts: &Options{},
regexp.MustCompile("gronit"), configPath: path.Join(configsDir, "repo"),
},
}, },
{ {
repo: leaksRepo, repo: leaksRepo,
description: "toml whitelist repo", description: "toml whitelist repo",
numLeaks: 0, numLeaks: 0,
testOpts: &Options{},
configPath: path.Join(configsDir, "repo"), configPath: path.Join(configsDir, "repo"),
}, },
{ {
repo: leaksRepo, repo: leaksRepo,
description: "leaks present with entropy", description: "leaks present with entropy",
testOpts: Options{ testOpts: &Options{
Entropy: 4.7, Entropy: 4.7,
}, },
numLeaks: 6, numLeaks: 6,
@@ -618,7 +634,7 @@ func TestAuditRepo(t *testing.T) {
{ {
repo: leaksRepo, repo: leaksRepo,
description: "leaks present with entropy", description: "leaks present with entropy",
testOpts: Options{ testOpts: &Options{
Entropy: 4.7, Entropy: 4.7,
NoiseReduction: true, NoiseReduction: true,
}, },
@@ -628,7 +644,7 @@ func TestAuditRepo(t *testing.T) {
repo: leaksRepo, repo: leaksRepo,
description: "Audit until specific commit", description: "Audit until specific commit",
numLeaks: 2, numLeaks: 2,
testOpts: Options{ testOpts: &Options{
CommitStop: "f6839959b7bbdcd23008f1fb16f797f35bcd3a0c", CommitStop: "f6839959b7bbdcd23008f1fb16f797f35bcd3a0c",
}, },
}, },
@@ -636,29 +652,31 @@ func TestAuditRepo(t *testing.T) {
repo: leaksRepo, repo: leaksRepo,
description: "commit depth = 2, two leaks", description: "commit depth = 2, two leaks",
numLeaks: 2, numLeaks: 2,
testOpts: Options{ testOpts: &Options{
Depth: 2, Depth: 2,
}, },
}, },
{ {
repo: leaksRepo, repo: leaksRepo,
description: "toml entropy range", description: "toml entropy range",
numLeaks: 298, numLeaks: 296,
testOpts: &Options{},
configPath: path.Join(configsDir, "entropy"), configPath: path.Join(configsDir, "entropy"),
}, },
{ {
repo: leaksRepo, repo: leaksRepo,
testOpts: Options{ testOpts: &Options{
NoiseReduction: true, NoiseReduction: true,
}, },
description: "toml entropy range", description: "toml entropy noise reduction range",
numLeaks: 58, numLeaks: 56,
configPath: path.Join(configsDir, "entropy"), configPath: path.Join(configsDir, "entropy"),
}, },
{ {
repo: leaksRepo, repo: leaksRepo,
description: "toml bad entropy range", description: "toml bad entropy range",
numLeaks: 0, numLeaks: 0,
testOpts: &Options{},
configPath: path.Join(configsDir, "badEntropy"), configPath: path.Join(configsDir, "badEntropy"),
expectedErrMsg: "entropy range must be ascending", expectedErrMsg: "entropy range must be ascending",
}, },
@@ -666,51 +684,30 @@ func TestAuditRepo(t *testing.T) {
repo: leaksRepo, repo: leaksRepo,
description: "toml bad entropy2 range", description: "toml bad entropy2 range",
numLeaks: 0, numLeaks: 0,
testOpts: &Options{},
configPath: path.Join(configsDir, "badEntropy2"), configPath: path.Join(configsDir, "badEntropy2"),
expectedErrMsg: "invalid entropy ranges, must be within 0.0-8.0", expectedErrMsg: "invalid entropy ranges, must be within 0.0-8.0",
}, },
} }
whiteListCommits = make(map[string]bool)
g := goblin.Goblin(t) g := goblin.Goblin(t)
for _, test := range tests { for _, test := range tests {
g.Describe("TestAuditRepo", func() { g.Describe("TestAuditRepo", func() {
g.It(test.description, func() { g.It(test.description, func() {
auditDone = false auditDone = false
opts = test.testOpts opts = test.testOpts
// settin da globs
if test.whiteListFiles != nil {
whiteListFiles = test.whiteListFiles
} else {
whiteListFiles = nil
}
if test.whiteListCommits != nil {
whiteListCommits = test.whiteListCommits
} else {
whiteListCommits = nil
}
if test.whiteListRegexes != nil {
whiteListRegexes = test.whiteListRegexes
} else {
whiteListRegexes = nil
}
if test.whiteListRepos != nil {
whiteListRepos = test.whiteListRepos
} else {
whiteListRepos = nil
}
skip := false
totalCommits = 0 totalCommits = 0
config, err = newConfig()
// config paths // config paths
if test.configPath != "" { if test.configPath != "" {
os.Setenv("GITLEAKS_CONFIG", test.configPath) os.Setenv("GITLEAKS_CONFIG", test.configPath)
err := loadToml() config, err = newConfig()
if err != nil { if err != nil {
g.Assert(err.Error()).Equal(test.expectedErrMsg) g.Assert(err.Error()).Equal(test.expectedErrMsg)
skip = true goto next
} }
} }
if !skip { leaks, err = test.repo.audit()
leaks, err = auditGitRepo(test.repo)
if test.testOpts.Depth != 0 { if test.testOpts.Depth != 0 {
g.Assert(totalCommits).Equal(test.testOpts.Depth) g.Assert(totalCommits).Equal(test.testOpts.Depth)
} else { } else {
@@ -719,98 +716,99 @@ func TestAuditRepo(t *testing.T) {
} }
g.Assert(len(leaks)).Equal(test.numLeaks) g.Assert(len(leaks)).Equal(test.numLeaks)
} }
} next:
os.Setenv("GITLEAKS_CONFIG", "")
}) })
}) })
} }
} }
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 := optsGuard()
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")
+7
View File
@@ -187,6 +187,13 @@ 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...) leaks = append(repoInfo.auditSingleCommit(c, mutex), leaks...)
if opts.Commit == c.Hash.String() {
return storer.ErrStop
}
return nil
}
if opts.Commit != "" {
return nil return nil
} }