mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-29 20:54:22 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b4e69ecd37 | ||
|
|
1c2820b4f1 | ||
|
|
215c3f6623 |
No files matched your search
@@ -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
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -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 {
|
||||||
@@ -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,6 +260,7 @@ func getRepo() (Repo, error) {
|
|||||||
r *git.Repository
|
r *git.Repository
|
||||||
)
|
)
|
||||||
|
|
||||||
|
log.Infof("cloning %s", opts.Repo)
|
||||||
if opts.Disk {
|
if opts.Disk {
|
||||||
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 {
|
||||||
@@ -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,7 +540,7 @@ 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.Disk {
|
if opts.Disk {
|
||||||
ownerDir, err := ioutil.TempDir(dir, opts.GithubUser)
|
ownerDir, err := ioutil.TempDir(dir, opts.GithubUser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -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,7 +598,7 @@ 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.Disk {
|
if opts.Disk {
|
||||||
ownerDir, err = ioutil.TempDir(dir, opts.GithubUser)
|
ownerDir, err = ioutil.TempDir(dir, opts.GithubUser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -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 {
|
||||||
|
|||||||
Reference in new issue
Block a user