Add initial support for GitHub Enterprise custom URLs

This commit is contained in:
Eric Ripa authored and Eric Ripa committed 2018-08-09 10:04:41 -07:00
1 parent 60801b336a
commit b1c7a84fae
2 files changed
+26

No files matched your search

+1
View File
@@ -139,6 +139,7 @@ func TestGetRepo(t *testing.T) {
func TestGetOwnerRepo(t *testing.T) { func TestGetOwnerRepo(t *testing.T) {
var err error var err error
dir, err = ioutil.TempDir("", "gitleaksTestOwner") dir, err = ioutil.TempDir("", "gitleaksTestOwner")
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
if err != nil { if err != nil {
+25
View File
@@ -7,6 +7,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url"
"os" "os"
"os/user" "os/user"
"path" "path"
@@ -63,6 +64,7 @@ type Options struct {
Repo string `short:"r" long:"repo" description:"Repo url to audit"` Repo string `short:"r" long:"repo" description:"Repo url to audit"`
GithubUser string `long:"github-user" description:"User url to audit"` GithubUser string `long:"github-user" description:"User url to audit"`
GithubOrg string `long:"github-org" description:"Organization 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"` 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 version = "1.1.2"
const defaultConfig = ` const defaultConfig = `
title = "gitleaks config" title = "gitleaks config"
@@ -510,12 +513,22 @@ func getOwnerRepos() ([]Repo, error) {
repos, err = discoverRepos(opts.OwnerPath) repos, err = discoverRepos(opts.OwnerPath)
} else if opts.GithubOrg != "" { } else if opts.GithubOrg != "" {
githubClient := github.NewClient(githubToken()) githubClient := github.NewClient(githubToken())
if opts.GithubURL != "" && opts.GithubURL != defaultGithubURL {
ghURL, _ := url.Parse(opts.GithubURL)
githubClient.BaseURL = ghURL
}
githubOptions := github.RepositoryListByOrgOptions{ githubOptions := github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{PerPage: 10}, ListOptions: github.ListOptions{PerPage: 10},
} }
repos, err = getOrgGithubRepos(ctx, &githubOptions, githubClient) repos, err = getOrgGithubRepos(ctx, &githubOptions, githubClient)
} else if opts.GithubUser != "" { } else if opts.GithubUser != "" {
githubClient := github.NewClient(githubToken()) githubClient := github.NewClient(githubToken())
if opts.GithubURL != "" && opts.GithubURL != defaultGithubURL {
ghURL, _ := url.Parse(opts.GithubURL)
githubClient.BaseURL = ghURL
}
githubOptions := github.RepositoryListOptions{ githubOptions := github.RepositoryListOptions{
Affiliation: "owner", Affiliation: "owner",
ListOptions: github.ListOptions{ 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") 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 != "" { if opts.SingleSearch != "" {
singleSearchRegex, err = regexp.Compile(opts.SingleSearch) singleSearchRegex, err = regexp.Compile(opts.SingleSearch)
if err != nil { if err != nil {