Compare commits

..
10 Commits
Author SHA1 Message Date
zricethezav b4e69ecd37 clone and results logs, some additional comments 2018-07-21 13:29:32 -05:00
zricethezav 1c2820b4f1 Merge branch 'master' of github.com:zricethezav/gitleaks 2018-07-21 11:15:19 -05:00
zricethezav 215c3f6623 Simple slogan 2018-07-21 11:15:05 -05:00
Zachary Rice 39b162cadf Merge pull request #78 from zricethezav/default_mem
Updating Makefile, making in-memory default, adding disk option
2018-07-21 10:34:58 -05:00
zricethezav e0c717c3a3 Updating Makefile, making in-memory default, adding disk option 2018-07-21 09:09:39 -05:00
zricethezav ebb9be999f update sample docker cmd 2018-07-20 21:51:56 -05:00
Zachary Rice 5fbb15be62 Merge pull request #77 from arjunyel/patch-2
Twitter typo
2018-07-20 21:46:53 -05:00
Zachary Rice 8922546a00 Merge pull request #76 from arjunyel/patch-1
Update Go docker version
2018-07-20 21:45:59 -05:00
arjunyel 38b7fc3cfb Twitter typo 2018-07-20 21:34:00 -05:00
arjunyel 8a76e0af27 Update Go docker version 2018-07-20 21:17:49 -05:00
5 changed files with 85 additions and 74 deletions

No files matched your search

+1 -1
View File
@@ -1,4 +1,4 @@
FROM golang:1.10.0 AS build FROM golang:1.10.3 AS build
WORKDIR /go/src/github.com/zricethezav/gitleaks WORKDIR /go/src/github.com/zricethezav/gitleaks
COPY . . COPY . .
RUN CGO_ENABLED=0 go build -o bin/gitleaks *.go RUN CGO_ENABLED=0 go build -o bin/gitleaks *.go
+2 -1
View File
@@ -18,4 +18,5 @@ build-all:
env GOOS="linux" GOARCH="amd64" go build -o "build/gitleaks-linux-amd64" env GOOS="linux" GOARCH="amd64" go build -o "build/gitleaks-linux-amd64"
env GOOS="linux" GOARCH="arm" go build -o "build/gitleaks-linux-arm" env GOOS="linux" GOARCH="arm" go build -o "build/gitleaks-linux-arm"
env GOOS="linux" GOARCH="mips" go build -o "build/gitleaks-linux-mips" env GOOS="linux" GOARCH="mips" go build -o "build/gitleaks-linux-mips"
env GOOS="linux" GOARCH="mips" go build -o "build/gitleaks-linux-mips" env GOOS="linux" GOARCH="mips" go build -o "build/gitleaks-linux-mips"
env GOOS="darwin" GOARCH="amd64" go build -o "build/gitleaks-darwin-amd64"
+3 -3
View File
@@ -5,7 +5,7 @@
</p> </p>
</p> </p>
## Audit git repos for secrets, keys, and whatever. ## Audit git repos for secrets
### Powered by src-d's [go-git](https://github.com/src-d/go-git) ### Powered by src-d's [go-git](https://github.com/src-d/go-git)
#### Installing #### Installing
@@ -30,7 +30,7 @@ Application Options:
--repo-path= Path to repo --repo-path= Path to repo
--owner-path= Path to owner directory (repos discovered) --owner-path= Path to owner directory (repos discovered)
--max-go= Maximum number of concurrent go-routines gitleaks spawns --max-go= Maximum number of concurrent go-routines gitleaks spawns
-m, --in-memory Run gitleaks in memory --disk Clones repo(s) to disk
--all-refs run audit on all refs --all-refs run audit on all refs
--single-search= single regular expression to search for --single-search= single regular expression to search for
--config= path to gitleaks config --config= path to gitleaks config
@@ -56,4 +56,4 @@ Check the wiki [here](https://github.com/zricethezav/gitleaks/wiki)
Please read this [Github article on removing sensitive data from a repository](https://help.github.com/articles/removing-sensitive-data-from-a-repository/) to remove the sensitive information from your history. Please read this [Github article on removing sensitive data from a repository](https://help.github.com/articles/removing-sensitive-data-from-a-repository/) to remove the sensitive information from your history.
### Run me with docker ### Run me with docker
Simply run `docker run --rm --name=gitleaks zricethezav/gitleaks https://github.com/zricethezav/gitleaks` Simply run `docker run --rm --name=gitleaks zricethezav/gitleaks --help`
+10 -10
View File
@@ -85,10 +85,10 @@ func TestGetRepo(t *testing.T) {
}, },
{ {
testOpts: Options{ testOpts: Options{
Repo: "https://github.com/gitleakstest/gronit", Repo: "https://github.com/gitleakstest/gronit",
InMem: true, Disk: true,
}, },
description: "test inmem clone remote repo", description: "test on disk clone remote repo",
expectedErrMsg: "", expectedErrMsg: "",
}, },
{ {
@@ -117,7 +117,7 @@ func TestGetRepo(t *testing.T) {
testOpts: Options{ testOpts: Options{
Repo: "https://github.com/gitleakstest/private", Repo: "https://github.com/gitleakstest/private",
IncludePrivate: true, IncludePrivate: true,
InMem: true, Disk: true,
}, },
description: "test private repo", description: "test private repo",
expectedErrMsg: "invalid auth method", expectedErrMsg: "invalid auth method",
@@ -167,9 +167,9 @@ func TestGetOwnerRepo(t *testing.T) {
{ {
testOpts: Options{ testOpts: Options{
GithubUser: "gitleakstest", GithubUser: "gitleakstest",
InMem: true, Disk: true,
}, },
description: "test github user in mem", description: "test github user on disk ",
numRepos: 2, numRepos: 2,
expectedErrMsg: "", expectedErrMsg: "",
}, },
@@ -201,9 +201,9 @@ func TestGetOwnerRepo(t *testing.T) {
{ {
testOpts: Options{ testOpts: Options{
GithubOrg: "gitleakstestorg", GithubOrg: "gitleakstestorg",
InMem: true, Disk: true,
}, },
description: "test org in mem", description: "test org on disk",
numRepos: 2, numRepos: 2,
expectedErrMsg: "", expectedErrMsg: "",
}, },
@@ -211,9 +211,9 @@ func TestGetOwnerRepo(t *testing.T) {
testOpts: Options{ testOpts: Options{
GithubOrg: "gitleakstestorg", GithubOrg: "gitleakstestorg",
IncludePrivate: true, IncludePrivate: true,
InMem: true, Disk: true,
}, },
description: "test private org in mem no ssh", description: "test private org on disk no ssh",
numRepos: 0, numRepos: 0,
expectedErrMsg: "no ssh auth available", expectedErrMsg: "no ssh auth available",
}, },
+69 -59
View File
@@ -10,6 +10,7 @@ import (
"os" "os"
"os/user" "os/user"
"path" "path"
"path/filepath"
"regexp" "regexp"
"strings" "strings"
"sync" "sync"
@@ -79,7 +80,7 @@ type Options struct {
// Process options // Process options
MaxGoRoutines int `long:"max-go" description:"Maximum number of concurrent go-routines gitleaks spawns"` MaxGoRoutines int `long:"max-go" description:"Maximum number of concurrent go-routines gitleaks spawns"`
InMem bool `short:"m" long:"in-memory" description:"Run gitleaks in memory"` Disk bool `long:"disk" description:"Clones repo(s) to disk"`
AuditAllRefs bool `long:"all-refs" description:"run audit on all refs"` AuditAllRefs bool `long:"all-refs" description:"run audit on all refs"`
SingleSearch string `long:"single-search" description:"single regular expression to search for"` SingleSearch string `long:"single-search" description:"single regular expression to search for"`
ConfigPath string `long:"config" description:"path to gitleaks config"` ConfigPath string `long:"config" description:"path to gitleaks config"`
@@ -129,7 +130,7 @@ regex = '''-----BEGIN OPENSSH PRIVATE KEY-----'''
description = "Facebook" description = "Facebook"
regex = '''(?i)facebook.*['\"][0-9a-f]{32}['\"]''' regex = '''(?i)facebook.*['\"][0-9a-f]{32}['\"]'''
[[regexes]] [[regexes]]
description = "Facebook" description = "Twitter"
regex = '''(?i)twitter.*['\"][0-9a-zA-Z]{35,44}['\"]''' regex = '''(?i)twitter.*['\"][0-9a-zA-Z]{35,44}['\"]'''
[whitelist] [whitelist]
@@ -180,7 +181,7 @@ func main() {
if err != nil { if err != nil {
os.Exit(1) os.Exit(1)
} }
setLogLevel() setLogs()
err = optsGuard() err = optsGuard()
if err != nil { if err != nil {
@@ -200,7 +201,7 @@ func main() {
} }
} }
if !opts.InMem { 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")
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
@@ -221,6 +222,11 @@ func main() {
} }
for _, r := range repos { for _, r := range repos {
l, err := auditRepo(r.repository) l, err := auditRepo(r.repository)
if len(l) == 0 {
log.Infof("\x1b[32;2m%s\x1b[0m found for repo %s", "no leaks", r.name)
} else {
log.Warnf("\x1b[31;2m%s\x1b[0m found for repo %s", "leaks", r.name)
}
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@@ -254,23 +260,8 @@ func getRepo() (Repo, error) {
r *git.Repository r *git.Repository
) )
if opts.InMem { log.Infof("cloning %s", opts.Repo)
if opts.IncludePrivate { if opts.Disk {
r, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: opts.Repo,
Progress: os.Stdout,
Auth: sshAuth,
})
} else {
r, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: opts.Repo,
Progress: os.Stdout,
})
}
} else if opts.RepoPath != "" {
// use existing repo
r, err = git.PlainOpen(opts.RepoPath)
} else {
cloneTarget := fmt.Sprintf("%s/%x", dir, md5.Sum([]byte(fmt.Sprintf("%s%s", opts.GithubUser, opts.Repo)))) cloneTarget := fmt.Sprintf("%s/%x", dir, md5.Sum([]byte(fmt.Sprintf("%s%s", opts.GithubUser, opts.Repo))))
if opts.IncludePrivate { if opts.IncludePrivate {
r, err = git.PlainClone(cloneTarget, false, &git.CloneOptions{ r, err = git.PlainClone(cloneTarget, false, &git.CloneOptions{
@@ -284,6 +275,22 @@ func getRepo() (Repo, error) {
Progress: os.Stdout, Progress: os.Stdout,
}) })
} }
} else if opts.RepoPath != "" {
// use existing repo
r, err = git.PlainOpen(opts.RepoPath)
} else {
if opts.IncludePrivate {
r, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: opts.Repo,
Progress: os.Stdout,
Auth: sshAuth,
})
} else {
r, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: opts.Repo,
Progress: os.Stdout,
})
}
} }
if err != nil { if err != nil {
return Repo{}, err return Repo{}, err
@@ -292,6 +299,7 @@ func getRepo() (Repo, error) {
repository: r, repository: r,
path: opts.RepoPath, path: opts.RepoPath,
url: opts.Repo, url: opts.Repo,
name: filepath.Base(opts.Repo),
}, nil }, nil
} }
@@ -300,8 +308,7 @@ func getRepo() (Repo, error) {
// double dip // double dip
func auditRef(r *git.Repository, ref *plumbing.Reference, commitWg *sync.WaitGroup, commitChan chan []Leak) error { func auditRef(r *git.Repository, ref *plumbing.Reference, commitWg *sync.WaitGroup, commitChan chan []Leak) error {
var ( var (
err error err error
// prevTree *object.Tree
prevCommit *object.Commit prevCommit *object.Commit
limitGoRoutines bool limitGoRoutines bool
semaphore chan bool semaphore chan bool
@@ -431,7 +438,6 @@ func auditRepo(r *git.Repository) ([]Leak, error) {
}() }()
commitWg.Wait() commitWg.Wait()
return leaks, err return leaks, err
} }
@@ -452,7 +458,7 @@ func checkDiff(diff string, commit *object.Commit, filePath string, branch strin
continue continue
} }
// whitelists // if offender matches whitelist regex, ignore it
for _, wRe := range whiteListRegexes { for _, wRe := range whiteListRegexes {
whitelistMatch := wRe.FindString(line) whitelistMatch := wRe.FindString(line)
if whitelistMatch != "" { if whitelistMatch != "" {
@@ -516,7 +522,7 @@ func getOwnerRepos() ([]Repo, error) {
return repos, err return repos, err
} }
// getUserGithubRepos // getUserGithubRepos grabs all github user's public and/or private repos
func getUserGithubRepos(ctx context.Context, listOpts *github.RepositoryListOptions, client *github.Client) ([]Repo, error) { func getUserGithubRepos(ctx context.Context, listOpts *github.RepositoryListOptions, client *github.Client) ([]Repo, error) {
var ( var (
err error err error
@@ -534,19 +540,8 @@ func getUserGithubRepos(ctx context.Context, listOpts *github.RepositoryListOpti
} }
for _, rDesc := range rs { for _, rDesc := range rs {
log.Debugf("Cloning: %s from %s", *rDesc.Name, *rDesc.SSHURL) log.Infof("cloning: %s", *rDesc.Name)
if opts.InMem { if opts.Disk {
if opts.IncludePrivate {
r, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: *rDesc.SSHURL,
Auth: sshAuth,
})
} else {
r, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: *rDesc.CloneURL,
})
}
} else {
ownerDir, err := ioutil.TempDir(dir, opts.GithubUser) ownerDir, err := ioutil.TempDir(dir, opts.GithubUser)
if err != nil { if err != nil {
return repos, fmt.Errorf("unable to generater owner temp dir: %v", err) return repos, fmt.Errorf("unable to generater owner temp dir: %v", err)
@@ -562,6 +557,17 @@ func getUserGithubRepos(ctx context.Context, listOpts *github.RepositoryListOpti
}) })
} }
} else {
if opts.IncludePrivate {
r, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: *rDesc.SSHURL,
Auth: sshAuth,
})
} else {
r, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: *rDesc.CloneURL,
})
}
} }
if err != nil { if err != nil {
return repos, fmt.Errorf("problem cloning %s -- %v", *rDesc.Name, err) return repos, fmt.Errorf("problem cloning %s -- %v", *rDesc.Name, err)
@@ -580,7 +586,7 @@ func getUserGithubRepos(ctx context.Context, listOpts *github.RepositoryListOpti
return repos, err return repos, err
} }
// getOrgGithubRepos // getOrgGithubRepos grabs all of org's public and/or private repos
func getOrgGithubRepos(ctx context.Context, listOpts *github.RepositoryListByOrgOptions, client *github.Client) ([]Repo, error) { func getOrgGithubRepos(ctx context.Context, listOpts *github.RepositoryListByOrgOptions, client *github.Client) ([]Repo, error) {
var ( var (
err error err error
@@ -592,22 +598,8 @@ func getOrgGithubRepos(ctx context.Context, listOpts *github.RepositoryListByOrg
for { for {
rs, resp, err := client.Repositories.ListByOrg(ctx, opts.GithubOrg, listOpts) rs, resp, err := client.Repositories.ListByOrg(ctx, opts.GithubOrg, listOpts)
for _, rDesc := range rs { for _, rDesc := range rs {
log.Debugf("Cloning: %s from %s", *rDesc.Name, *rDesc.SSHURL) log.Infof("cloning: %s", *rDesc.Name)
if opts.InMem { if opts.Disk {
if opts.IncludePrivate {
if sshAuth == nil {
return nil, fmt.Errorf("no ssh auth available")
}
r, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: *rDesc.SSHURL,
Auth: sshAuth,
})
} else {
r, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: *rDesc.CloneURL,
})
}
} else {
ownerDir, err = ioutil.TempDir(dir, opts.GithubUser) ownerDir, err = ioutil.TempDir(dir, opts.GithubUser)
if err != nil { if err != nil {
return repos, fmt.Errorf("unable to generater owner temp dir: %v", err) return repos, fmt.Errorf("unable to generater owner temp dir: %v", err)
@@ -626,6 +618,20 @@ func getOrgGithubRepos(ctx context.Context, listOpts *github.RepositoryListByOrg
}) })
} }
} else {
if opts.IncludePrivate {
if sshAuth == nil {
return nil, fmt.Errorf("no ssh auth available")
}
r, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: *rDesc.SSHURL,
Auth: sshAuth,
})
} else {
r, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: *rDesc.CloneURL,
})
}
} }
if err != nil { if err != nil {
return nil, err return nil, err
@@ -647,6 +653,7 @@ func getOrgGithubRepos(ctx context.Context, listOpts *github.RepositoryListByOrg
return repos, err return repos, err
} }
// githubToken returns a oauth2 client for the github api to consume
func githubToken() *http.Client { func githubToken() *http.Client {
githubToken := os.Getenv("GITHUB_TOKEN") githubToken := os.Getenv("GITHUB_TOKEN")
if githubToken == "" { if githubToken == "" {
@@ -687,7 +694,7 @@ func discoverRepos(ownerPath string) ([]Repo, error) {
} }
// setLogLevel sets log level for gitleaks. Default is Warning // setLogLevel sets log level for gitleaks. Default is Warning
func setLogLevel() { func setLogs() {
switch opts.Log { switch opts.Log {
case "info": case "info":
log.SetLevel(log.InfoLevel) log.SetLevel(log.InfoLevel)
@@ -696,8 +703,11 @@ func setLogLevel() {
case "warn": case "warn":
log.SetLevel(log.WarnLevel) log.SetLevel(log.WarnLevel)
default: default:
log.SetLevel(log.WarnLevel) log.SetLevel(log.InfoLevel)
} }
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
} }
// optsGuard prevents invalid options // optsGuard prevents invalid options
@@ -723,7 +733,7 @@ func optsGuard() error {
return nil return nil
} }
// loadToml loads of the toml config containing regexes. // loadToml loads of the toml config containing regexes and whitelists
// 1. look for config path // 1. look for config path
// 2. two, look for gitleaks config env var // 2. two, look for gitleaks config env var
func loadToml() error { func loadToml() error {