Compare commits

..
17 Commits
Author SHA1 Message Date
Zachary Rice 8eb28cbb4c Merge pull request #91 from zricethezav/audit_branch
test branch opt, bump changelog
2018-09-12 19:33:46 -05:00
zach rice 9dc9a8e62a bump version 2018-09-12 19:23:20 -05:00
zach rice e6caef0d94 test branch opt, bump changelog 2018-09-12 19:17:04 -05:00
Zachary Rice 5e289ed0cc Merge pull request #88 from Fabse333/master
Added Support for branches
2018-09-12 12:41:00 -05:00
Fabian Helfert d99588fe19 Fixed failure when Branch option is ommited 2018-08-23 14:07:25 +02:00
Fabian Helfert a7ccb7a9d1 Added Support for branches 2018-08-23 13:50:32 +02:00
Zachary Rice f32abb921d Merge pull request #87 from nenadstojanovikj/patch-1
Update description to reflect regex
2018-08-22 09:41:28 -05:00
Nenad Stojanovikj e8851c1f99 Update description to reflect regex 2018-08-21 21:08:44 +02:00
Zachary Rice f3a8359ac0 Merge pull request #86 from zricethezav/patch-err-check
Patch err check
2018-08-11 15:33:39 -04:00
zach rice 829e8fed8c up check to warning msg 2018-08-11 14:30:34 -05:00
zach rice c7e3605bff bump patch 2018-08-11 14:29:23 -05:00
zach rice c1774b1596 err check on patch 2018-08-11 14:27:51 -05:00
Zachary Rice 35710a7428 Merge pull request #85 from eripa/github-enterprise-support
GitHub Enterprise support + improved error handling. Fixes #84
2018-08-10 15:59:52 -04:00
Eric Ripa c7ba4d8bbf Update README and CHANGELOG with GitHub URL/Enterprise support detailed 2018-08-10 06:59:04 -07:00
Eric Ripa 5585e10933 Change error handling in getRepo functions
Instead of returning an empty list of repositories
and an error if any cloning operation fails, simply
store the error in the Repo instance and always
return all repos.

Then later on check for errors during the audit step.

By having this approach we don't prematurely exit
the scanning if there are problems during cloning,
such as if a repository is empty and fails to clone.
2018-08-09 11:31:29 -07:00
Eric Ripa 23d5bf0b51 Add simply TCP check during optsGuard to verify the given github URL 2018-08-09 11:30:55 -07:00
Eric Ripa b1c7a84fae Add initial support for GitHub Enterprise custom URLs 2018-08-09 10:04:41 -07:00
5 changed files with 110 additions and 20 deletions

No files matched your search

+12
View File
@@ -1,6 +1,18 @@
CHANGELOG
=========
1.3.0
-----
- Target specific branch
1.2.1
-----
- Check errors when generating commit patch
1.2.0
-----
- Added support for providing an alternate GitHub URL to support scanning GitHub Enteprise repositories
1.1.2
-----
- Added version option
+3
View File
@@ -28,7 +28,9 @@ Application Options:
-r, --repo= Repo url to audit
--github-user= User url to audit
--github-org= Organization url to audit
--github-url= GitHub API Base URL, use for GitHub Enterprise. Example: https://github.example.com/api/v3/ (default: https://api.github.com/)
-p, --private Include private repos in audit
-b, --branch= branch name to audit (defaults to HEAD)
-c, --commit= sha of commit to stop at
--repo-path= Path to repo
--owner-path= Path to owner directory (repos discovered)
@@ -42,6 +44,7 @@ Application Options:
-v, --verbose Show verbose output from gitleaks audit
--report= path to write report file
--redact redact secrets from log messages and report
--version version number
Help Options:
-h, --help Show this help message
+2 -2
View File
@@ -18,7 +18,7 @@ regex = '''-----BEGIN OPENSSH PRIVATE KEY-----'''
description = "Facebook"
regex = '''(?i)facebook.*['\"][0-9a-f]{32}['\"]'''
[[regexes]]
description = "Facebook"
description = "Twitter"
regex = '''(?i)twitter.*['\"][0-9a-zA-Z]{35,44}['\"]'''
[whitelist]
@@ -27,4 +27,4 @@ regexes = [
]
files = [
"(.*?)(jpg|gif|doc|pdf|bin|go)$"
]
]
+17
View File
@@ -139,6 +139,7 @@ func TestGetRepo(t *testing.T) {
func TestGetOwnerRepo(t *testing.T) {
var err error
dir, err = ioutil.TempDir("", "gitleaksTestOwner")
defer os.RemoveAll(dir)
if err != nil {
@@ -338,6 +339,22 @@ func TestAuditRepo(t *testing.T) {
MaxGoRoutines: 2,
},
},
{
repo: leaksRepo,
description: "audit specific bad branch",
numLeaks: 2,
testOpts: Options{
Branch: "master",
},
},
{
repo: leaksRepo,
description: "audit specific good branch",
numLeaks: 0,
testOpts: Options{
Branch: "dev",
},
},
{
repo: leaksRepo,
description: "audit all branch",
+76 -18
View File
@@ -6,7 +6,9 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"os/user"
"path"
@@ -14,6 +16,7 @@ import (
"regexp"
"strings"
"sync"
"time"
"gopkg.in/src-d/go-git.v4/plumbing"
@@ -48,6 +51,7 @@ type Repo struct {
name string
leaks []Leak
repository *git.Repository
err error
}
// Owner contains a collection of repos. This could represent an org or user.
@@ -63,6 +67,7 @@ type Options struct {
Repo string `short:"r" long:"repo" description:"Repo url to audit"`
GithubUser string `long:"github-user" description:"User url to audit"`
GithubOrg string `long:"github-org" description:"Organization url to audit"`
GithubURL string `long:"github-url" default:"https://api.github.com/" description:"GitHub API Base URL, use for GitHub Enterprise. Example: https://github.example.com/api/v3/"`
IncludePrivate bool `short:"p" long:"private" description:"Include private repos in audit"`
/*
@@ -109,7 +114,8 @@ type Config struct {
}
}
const version = "1.1.2"
const defaultGithubURL = "https://api.github.com/"
const version = "1.3.0"
const defaultConfig = `
title = "gitleaks config"
# add regexes to the regex table
@@ -227,6 +233,10 @@ func main() {
repos, err = getOwnerRepos()
}
for _, r := range repos {
if r.err != nil {
log.Warnf("skipping audit for repo %s due to cloning error: %s", r.name, r.err)
continue
}
l, err := auditRepo(r.repository)
if len(l) == 0 {
log.Infof("no leaks found for repo %s", r.name)
@@ -234,7 +244,7 @@ func main() {
log.Warnf("leaks found for repo %s", r.name)
}
if err != nil {
log.Fatal(err)
log.Fatalf("error during audit: %s", err)
}
leaks = append(leaks, l...)
}
@@ -244,7 +254,7 @@ func main() {
}
if len(leaks) != 0 {
log.Debug("leaks detected")
log.Errorf("leaks detected")
os.Exit(1)
}
}
@@ -266,8 +276,8 @@ func getRepo() (Repo, error) {
r *git.Repository
)
log.Infof("cloning %s", opts.Repo)
if opts.Disk {
log.Infof("cloning %s", opts.Repo)
cloneTarget := fmt.Sprintf("%s/%x", dir, md5.Sum([]byte(fmt.Sprintf("%s%s", opts.GithubUser, opts.Repo))))
if opts.IncludePrivate {
r, err = git.PlainClone(cloneTarget, false, &git.CloneOptions{
@@ -283,8 +293,10 @@ func getRepo() (Repo, error) {
}
} else if opts.RepoPath != "" {
// use existing repo
log.Infof("opening %s", opts.Repo)
r, err = git.PlainOpen(opts.RepoPath)
} else {
log.Infof("cloning %s", opts.Repo)
if opts.IncludePrivate {
r, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: opts.Repo,
@@ -298,14 +310,12 @@ func getRepo() (Repo, error) {
})
}
}
if err != nil {
return Repo{}, err
}
return Repo{
repository: r,
path: opts.RepoPath,
url: opts.Repo,
name: filepath.Base(opts.Repo),
err: err,
}, nil
}
@@ -343,7 +353,6 @@ func auditRef(r *git.Repository, ref *plumbing.Reference, commitWg *sync.WaitGro
if prevCommit == nil {
prevCommit = c
}
commitWg.Add(1)
go func(c *object.Commit, prevCommit *object.Commit) {
var (
@@ -351,7 +360,15 @@ func auditRef(r *git.Repository, ref *plumbing.Reference, commitWg *sync.WaitGro
filePath string
skipFile bool
)
patch, _ := c.Patch(prevCommit)
patch, err := c.Patch(prevCommit)
if err != nil {
log.Warnf("problem generating patch for commit: %s\n", c.Hash.String())
if limitGoRoutines {
<-semaphore
}
commitChan <- leaks
return
}
for _, f := range patch.FilePatches() {
skipFile = false
from, to := f.Files()
@@ -428,6 +445,22 @@ func auditRepo(r *git.Repository) ([]Leak, error) {
return nil
})
} else {
if opts.Branch != "" {
foundBranch := false
refs, _ := r.Storer.IterReferences()
branch := strings.Split(opts.Branch, "/")[len(strings.Split(opts.Branch, "/"))-1]
err = refs.ForEach(func(refBranch *plumbing.Reference) error {
if strings.Split(refBranch.Name().String(), "/")[len(strings.Split(refBranch.Name().String(), "/"))-1] == branch {
foundBranch = true
ref = refBranch
}
return nil
})
if foundBranch == false {
log.Fatalf("No branch with name", opts.Branch)
return nil, nil
}
}
auditRef(r, ref, &commitWg, commitChan)
}
@@ -510,12 +543,22 @@ func getOwnerRepos() ([]Repo, error) {
repos, err = discoverRepos(opts.OwnerPath)
} else if opts.GithubOrg != "" {
githubClient := github.NewClient(githubToken())
if opts.GithubURL != "" && opts.GithubURL != defaultGithubURL {
ghURL, _ := url.Parse(opts.GithubURL)
githubClient.BaseURL = ghURL
}
githubOptions := github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{PerPage: 10},
}
repos, err = getOrgGithubRepos(ctx, &githubOptions, githubClient)
} else if opts.GithubUser != "" {
githubClient := github.NewClient(githubToken())
if opts.GithubURL != "" && opts.GithubURL != defaultGithubURL {
ghURL, _ := url.Parse(opts.GithubURL)
githubClient.BaseURL = ghURL
}
githubOptions := github.RepositoryListOptions{
Affiliation: "owner",
ListOptions: github.ListOptions{
@@ -575,13 +618,11 @@ func getUserGithubRepos(ctx context.Context, listOpts *github.RepositoryListOpti
})
}
}
if err != nil {
return repos, fmt.Errorf("problem cloning %s -- %v", *rDesc.Name, err)
}
repos = append(repos, Repo{
name: *rDesc.Name,
url: *rDesc.SSHURL,
repository: r,
err: err,
})
}
if resp.NextPage == 0 {
@@ -639,18 +680,14 @@ func getOrgGithubRepos(ctx context.Context, listOpts *github.RepositoryListByOrg
})
}
}
if err != nil {
return nil, err
}
repos = append(repos, Repo{
url: *rDesc.SSHURL,
name: *rDesc.Name,
repository: r,
err: err,
})
}
if err != nil {
return nil, err
} else if resp.NextPage == 0 {
if resp.NextPage == 0 {
break
}
listOpts.Page = resp.NextPage
@@ -729,6 +766,27 @@ func optsGuard() error {
return fmt.Errorf("user/organization private repos require env var GITHUB_TOKEN to be set")
}
// do the URL Parse and error checking here, so we can skip it later
// empty string is OK, it will default to the public github URL.
if opts.GithubURL != "" && opts.GithubURL != defaultGithubURL {
if !strings.HasSuffix(opts.GithubURL, "/") {
opts.GithubURL += "/"
}
ghURL, err := url.Parse(opts.GithubURL)
if err != nil {
return err
}
tcpPort := "443"
if ghURL.Scheme == "http" {
tcpPort = "80"
}
timeout := time.Duration(1 * time.Second)
_, err = net.DialTimeout("tcp", ghURL.Host+":"+tcpPort, timeout)
if err != nil {
return fmt.Errorf("%s unreachable, error: %s", ghURL.Host, err)
}
}
if opts.SingleSearch != "" {
singleSearchRegex, err = regexp.Compile(opts.SingleSearch)
if err != nil {