mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-30 13:14:22 +01:00
better ssh handling
This commit is contained in:
1 parent
e288db29a8
commit
98e6ba9378
3 files changed
+57
-73
No files matched your search
@@ -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,
|
||||||
|
|||||||
+30
-37
@@ -167,17 +167,15 @@ 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",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
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",
|
||||||
expectedErrMsg: "invalid auth method",
|
expectedErrMsg: "invalid auth method",
|
||||||
@@ -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,45 +765,24 @@ func TestOptionGuard(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
testOpts: Options{
|
testOpts: Options{
|
||||||
IncludePrivate: true,
|
GithubUser: "fakeUser",
|
||||||
GithubOrg: "fakeOrg",
|
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",
|
|
||||||
GithubOrg: "fakeOrg",
|
|
||||||
},
|
},
|
||||||
description: "double owner",
|
description: "double owner",
|
||||||
expectedErrMsg: "github user and organization set",
|
expectedErrMsg: "github user and organization set",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testOpts: Options{
|
testOpts: Options{
|
||||||
IncludePrivate: true,
|
GithubOrg: "fakeOrg",
|
||||||
GithubOrg: "fakeOrg",
|
OwnerPath: "/dev/null",
|
||||||
OwnerPath: "/dev/null",
|
|
||||||
},
|
},
|
||||||
description: "local and remote target",
|
description: "local and remote target",
|
||||||
expectedErrMsg: "github organization set and local owner path",
|
expectedErrMsg: "github organization set and local owner path",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testOpts: Options{
|
testOpts: Options{
|
||||||
IncludePrivate: true,
|
GithubUser: "fakeUser",
|
||||||
GithubUser: "fakeUser",
|
OwnerPath: "/dev/null",
|
||||||
OwnerPath: "/dev/null",
|
|
||||||
},
|
},
|
||||||
description: "local and remote target",
|
description: "local and remote target",
|
||||||
expectedErrMsg: "github user set and local owner path",
|
expectedErrMsg: "github user set and local owner path",
|
||||||
|
|||||||
@@ -60,12 +60,11 @@ type RepoDescriptor struct {
|
|||||||
// Options for gitleaks
|
// Options for gitleaks
|
||||||
type Options struct {
|
type Options struct {
|
||||||
// remote target options
|
// remote target options
|
||||||
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:"Github user to audit"`
|
GithubUser string `long:"github-user" description:"Github user to audit"`
|
||||||
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 {
|
sshAuth, err = getSSHAuth()
|
||||||
// if including private repos use ssh as authentication
|
if err != nil {
|
||||||
sshAuth, err = getSSHAuth()
|
return leaks, err
|
||||||
if err != nil {
|
|
||||||
return nil, 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 {
|
||||||
return nil, fmt.Errorf("unable to generate ssh key: %v", err)
|
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 sshAuth, err
|
return sshAuth, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (leak Leak) log() {
|
func (leak Leak) log() {
|
||||||
|
|||||||
Reference in new issue
Block a user