better ssh handling

This commit is contained in:
zach rice committed 2018-10-24 15:49:53 -05:00
1 parent e288db29a8
commit 98e6ba9378
3 files changed
+39 -55

No files matched your search

+6 -12
View File
@@ -117,11 +117,11 @@ func auditGithubRepos() ([]Leak, error) {
break break
} }
if opts.GithubUser != "" { if opts.GithubUser != "" {
if opts.IncludePrivate { // if opts.IncludePrivate {
pagedGithubRepos, resp, err = githubClient.Repositories.List(ctx, "", githubOptions) // pagedGithubRepos, resp, err = githubClient.Repositories.List(ctx, "", githubOptions)
} else { // } else {
pagedGithubRepos, resp, err = githubClient.Repositories.List(ctx, opts.GithubUser, githubOptions) pagedGithubRepos, resp, err = githubClient.Repositories.List(ctx, opts.GithubUser, githubOptions)
} // }
if err != nil { if err != nil {
done = true done = true
} }
@@ -199,10 +199,7 @@ func cloneGithubRepo(githubRepo *github.Repository) (*RepoDescriptor, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("unable to generater owner temp dir: %v", err) return nil, fmt.Errorf("unable to generater owner temp dir: %v", err)
} }
if opts.IncludePrivate { if sshAuth != nil {
if sshAuth == nil {
return nil, fmt.Errorf("no ssh auth available")
}
repo, err = git.PlainClone(fmt.Sprintf("%s/%s", ownerDir, *githubRepo.Name), false, &git.CloneOptions{ repo, err = git.PlainClone(fmt.Sprintf("%s/%s", ownerDir, *githubRepo.Name), false, &git.CloneOptions{
URL: *githubRepo.SSHURL, URL: *githubRepo.SSHURL,
Auth: sshAuth, Auth: sshAuth,
@@ -213,10 +210,7 @@ func cloneGithubRepo(githubRepo *github.Repository) (*RepoDescriptor, error) {
}) })
} }
} else { } else {
if opts.IncludePrivate { if sshAuth != nil {
if sshAuth == nil {
return nil, fmt.Errorf("no ssh auth available")
}
repo, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{ repo, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: *githubRepo.SSHURL, URL: *githubRepo.SSHURL,
Auth: sshAuth, Auth: sshAuth,
+21 -28
View File
@@ -168,7 +168,6 @@ func TestGetRepo(t *testing.T) {
{ {
testOpts: Options{ testOpts: Options{
Repo: "https://github.com/gitleakstest/private", Repo: "https://github.com/gitleakstest/private",
IncludePrivate: true,
}, },
description: "test private repo", description: "test private repo",
expectedErrMsg: "invalid auth method", expectedErrMsg: "invalid auth method",
@@ -176,7 +175,6 @@ func TestGetRepo(t *testing.T) {
{ {
testOpts: Options{ testOpts: Options{
Repo: "https://github.com/gitleakstest/private", Repo: "https://github.com/gitleakstest/private",
IncludePrivate: true,
Disk: true, Disk: true,
}, },
description: "test private repo", description: "test private repo",
@@ -264,13 +262,29 @@ func TestRun(t *testing.T) {
}, },
{ {
testOpts: Options{ testOpts: Options{
GithubOrg: "gitleakstestorg", Repo: "git@github.com:gitleakstest/gronit.git",
IncludePrivate: true, SSHKey: "trash",
SSHKey: "reallyreallyreallyreallywrongpath",
}, },
description: "test private org no ssh", description: "test leak",
numLeaks: 0, numLeaks: 0,
expectedErrMsg: "unable to generate ssh key: open reallyreallyreallyreallywrongpath: no such file or directory", expectedErrMsg: "unable to generate ssh key: open trash: no such file or directory",
},
{
testOpts: Options{
Repo: "git@github.com:gitleakstest/gronit.git",
},
description: "test leak",
numLeaks: 2,
expectedErrMsg: "",
},
{
testOpts: Options{
Repo: "git@github.com:gitleakstest/gronit.git",
Disk: true,
},
description: "test leak",
numLeaks: 2,
expectedErrMsg: "",
}, },
{ {
testOpts: Options{ testOpts: Options{
@@ -751,25 +765,6 @@ func TestOptionGuard(t *testing.T) {
}, },
{ {
testOpts: Options{ testOpts: Options{
IncludePrivate: true,
GithubOrg: "fakeOrg",
},
description: "private org no githubtoken",
expectedErrMsg: "user/organization private repos require env var GITHUB_TOKEN to be set",
githubToken: false,
},
{
testOpts: Options{
IncludePrivate: true,
GithubUser: "fakeUser",
},
description: "private user no githubtoken",
expectedErrMsg: "user/organization private repos require env var GITHUB_TOKEN to be set",
githubToken: false,
},
{
testOpts: Options{
IncludePrivate: true,
GithubUser: "fakeUser", GithubUser: "fakeUser",
GithubOrg: "fakeOrg", GithubOrg: "fakeOrg",
}, },
@@ -778,7 +773,6 @@ func TestOptionGuard(t *testing.T) {
}, },
{ {
testOpts: Options{ testOpts: Options{
IncludePrivate: true,
GithubOrg: "fakeOrg", GithubOrg: "fakeOrg",
OwnerPath: "/dev/null", OwnerPath: "/dev/null",
}, },
@@ -787,7 +781,6 @@ func TestOptionGuard(t *testing.T) {
}, },
{ {
testOpts: Options{ testOpts: Options{
IncludePrivate: true,
GithubUser: "fakeUser", GithubUser: "fakeUser",
OwnerPath: "/dev/null", OwnerPath: "/dev/null",
}, },
+12 -15
View File
@@ -65,7 +65,6 @@ type Options struct {
GithubOrg string `long:"github-org" description:"Github organization to audit"` GithubOrg string `long:"github-org" description:"Github organization 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/"` 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/"`
GithubPR string `long:"github-pr" description:"Github PR url to audit. This does not clone the repo. GITHUB_TOKEN must be set"` GithubPR string `long:"github-pr" description:"Github PR url to audit. This does not clone the repo. GITHUB_TOKEN must be set"`
IncludePrivate bool `short:"p" long:"private" description:"Include private repos in audit"`
/* /*
TODO: TODO:
@@ -280,13 +279,11 @@ func run() ([]Leak, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if opts.IncludePrivate {
// if including private repos use ssh as authentication
sshAuth, err = getSSHAuth() sshAuth, err = getSSHAuth()
if err != nil { if err != nil {
return nil, err return leaks, err
}
} }
if opts.Disk { 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")
@@ -319,8 +316,7 @@ func run() ([]Leak, error) {
leaks = append(leaksFromRepo, leaks...) leaks = append(leaksFromRepo, leaks...)
} }
} else if opts.GithubOrg != "" || opts.GithubUser != "" { } else if opts.GithubOrg != "" || opts.GithubUser != "" {
// Audit a github owner -- a user or organization. If you want to include // Audit a github owner -- a user or organization.
// private repos you must pass a --private/-p option and have your ssh keys set
leaks, err = auditGithubRepos() leaks, err = auditGithubRepos()
if err != nil { if err != nil {
return leaks, err return leaks, err
@@ -355,9 +351,7 @@ func writeReport(leaks []Leak) error {
return err return err
} }
// cloneRepo clones a repo to memory(default) or to disk if the --disk option is set. If you want to // cloneRepo clones a repo to memory(default) or to disk if the --disk option is set.
// clone a private repo you must set the --private/-p option, use a ssh target, and have your ssh keys
// configured. If you want to audit a local repo, getRepo will load up a repo located at --repo-path
func cloneRepo() (*RepoDescriptor, error) { func cloneRepo() (*RepoDescriptor, error) {
var ( var (
err error err error
@@ -372,12 +366,13 @@ func cloneRepo() (*RepoDescriptor, error) {
if opts.Disk { if opts.Disk {
log.Infof("cloning %s", opts.Repo) log.Infof("cloning %s", opts.Repo)
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 strings.HasPrefix(opts.Repo, "git") {
repo, err = git.PlainClone(cloneTarget, false, &git.CloneOptions{ repo, err = git.PlainClone(cloneTarget, false, &git.CloneOptions{
URL: opts.Repo, URL: opts.Repo,
Progress: os.Stdout, Progress: os.Stdout,
Auth: sshAuth, Auth: sshAuth,
}) })
} else { } else {
repo, err = git.PlainClone(cloneTarget, false, &git.CloneOptions{ repo, err = git.PlainClone(cloneTarget, false, &git.CloneOptions{
URL: opts.Repo, URL: opts.Repo,
@@ -389,7 +384,7 @@ func cloneRepo() (*RepoDescriptor, error) {
repo, err = git.PlainOpen(opts.RepoPath) repo, err = git.PlainOpen(opts.RepoPath)
} else { } else {
log.Infof("cloning %s", opts.Repo) log.Infof("cloning %s", opts.Repo)
if opts.IncludePrivate { if strings.HasPrefix(opts.Repo, "git") {
repo, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{ repo, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: opts.Repo, URL: opts.Repo,
Progress: os.Stdout, Progress: os.Stdout,
@@ -736,8 +731,6 @@ func optsGuard() error {
return fmt.Errorf("github organization set and local owner path") return fmt.Errorf("github organization set and local owner path")
} else if opts.GithubUser != "" && opts.OwnerPath != "" { } else if opts.GithubUser != "" && opts.OwnerPath != "" {
return fmt.Errorf("github user set and local owner path") return fmt.Errorf("github user set and local owner path")
} else if opts.IncludePrivate && os.Getenv("GITHUB_TOKEN") == "" && (opts.GithubOrg != "" || opts.GithubUser != "") {
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 // do the URL Parse and error checking here, so we can skip it later
@@ -889,9 +882,13 @@ func getSSHAuth() (*ssh.PublicKeys, error) {
} }
sshAuth, err := ssh.NewPublicKeysFromFile("git", sshKeyPath, "") sshAuth, err := ssh.NewPublicKeysFromFile("git", sshKeyPath, "")
if err != nil { if err != nil {
if strings.HasPrefix(opts.Repo, "git") {
// if you are attempting to clone a git repo via ssh and supply a bad ssh key,
// the clone will fail.
return nil, fmt.Errorf("unable to generate ssh key: %v", err) return nil, fmt.Errorf("unable to generate ssh key: %v", err)
} }
return sshAuth, err }
return sshAuth, nil
} }
func (leak Leak) log() { func (leak Leak) log() {