From b1c7a84faeadd64d263aede22f241157751c7401 Mon Sep 17 00:00:00 2001 From: Eric Ripa Date: Thu, 9 Aug 2018 07:11:02 -0700 Subject: [PATCH 1/4] Add initial support for GitHub Enterprise custom URLs --- gitleaks_test.go | 1 + main.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/gitleaks_test.go b/gitleaks_test.go index c044cae4..8cdfeeeb 100644 --- a/gitleaks_test.go +++ b/gitleaks_test.go @@ -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 { diff --git a/main.go b/main.go index 87c3303e..23df5875 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "fmt" "io/ioutil" "net/http" + "net/url" "os" "os/user" "path" @@ -63,6 +64,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,6 +111,7 @@ type Config struct { } } +const defaultGithubURL = "https://api.github.com/" const version = "1.1.2" const defaultConfig = ` title = "gitleaks config" @@ -510,12 +513,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{ @@ -729,6 +742,18 @@ 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 += "/" + } + _, err := url.Parse(opts.GithubURL) + if err != nil { + return err + } + } + if opts.SingleSearch != "" { singleSearchRegex, err = regexp.Compile(opts.SingleSearch) if err != nil { From 23d5bf0b51aac91201f1a1ab933d7c856e80357c Mon Sep 17 00:00:00 2001 From: Eric Ripa Date: Thu, 9 Aug 2018 10:05:47 -0700 Subject: [PATCH 2/4] Add simply TCP check during optsGuard to verify the given github URL --- main.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 23df5875..eb675856 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "io/ioutil" + "net" "net/http" "net/url" "os" @@ -15,6 +16,7 @@ import ( "regexp" "strings" "sync" + "time" "gopkg.in/src-d/go-git.v4/plumbing" @@ -748,10 +750,19 @@ func optsGuard() error { if !strings.HasSuffix(opts.GithubURL, "/") { opts.GithubURL += "/" } - _, err := url.Parse(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 != "" { From 5585e109330aa6fa6bf7078ea4bf015daab29cff Mon Sep 17 00:00:00 2001 From: Eric Ripa Date: Thu, 9 Aug 2018 10:06:10 -0700 Subject: [PATCH 3/4] 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. --- main.go | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index eb675856..a99cd11b 100644 --- a/main.go +++ b/main.go @@ -51,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. @@ -232,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) @@ -239,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...) } @@ -249,7 +254,7 @@ func main() { } if len(leaks) != 0 { - log.Debug("leaks detected") + log.Errorf("leaks detected") os.Exit(1) } } @@ -303,14 +308,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 } @@ -590,13 +593,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 { @@ -654,18 +655,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 From c7ba4d8bbf174b9f44f88317dfdc3ef6880ad6f1 Mon Sep 17 00:00:00 2001 From: Eric Ripa Date: Fri, 10 Aug 2018 06:59:04 -0700 Subject: [PATCH 4/4] Update README and CHANGELOG with GitHub URL/Enterprise support detailed --- CHANGELOG.md | 4 ++++ README.md | 3 +++ 2 files changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 664d1888..be5a49f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ CHANGELOG ========= +1.2.0 +----- +- Added support for providing an alternate GitHub URL to support scanning GitHub Enteprise repositories + 1.1.2 ----- - Added version option diff --git a/README.md b/README.md index 9b7276af..1f200e77 100644 --- a/README.md +++ b/README.md @@ -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