mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-12-01 05:24:22 +01:00
working on tests
This commit is contained in:
1 parent
d85c8d8abd
commit
36c31bf181
4 files changed
+76
-78
No files matched your search
+35
-28
@@ -3,10 +3,10 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"path/filepath"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const usage = `usage: gitleaks [options] <URL>/<path_to_repo>
|
const usage = `usage: gitleaks [options] <URL>/<path_to_repo>
|
||||||
@@ -47,6 +47,7 @@ type Options struct {
|
|||||||
RepoPath string
|
RepoPath string
|
||||||
|
|
||||||
ReportPath string
|
ReportPath string
|
||||||
|
ClonePath string
|
||||||
|
|
||||||
Concurrency int
|
Concurrency int
|
||||||
B64EntropyCutoff int
|
B64EntropyCutoff int
|
||||||
@@ -141,25 +142,6 @@ func newOpts(args []string) *Options {
|
|||||||
|
|
||||||
// deafultOptions provides the default options
|
// deafultOptions provides the default options
|
||||||
func defaultOptions() (*Options, error) {
|
func defaultOptions() (*Options, error) {
|
||||||
/*
|
|
||||||
// default GITLEAKS_HOME is $HOME/.gitleaks
|
|
||||||
// gitleaks will use this location for clones if
|
|
||||||
// no clone-path is provided
|
|
||||||
gitleaksHome := os.Getenv("GITLEAKS_HOME")
|
|
||||||
if gitleaksHome == "" {
|
|
||||||
homeDir, err := homedir.Dir()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("could not find system home dir")
|
|
||||||
}
|
|
||||||
gitleaksHome = filepath.Join(homeDir, ".gitleaks")
|
|
||||||
}
|
|
||||||
|
|
||||||
// make sure gitleaks home exists
|
|
||||||
if _, err := os.Stat(gitleaksHome); os.IsNotExist(err) {
|
|
||||||
os.Mkdir(gitleaksHome, os.ModePerm)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
return &Options{
|
return &Options{
|
||||||
Concurrency: 10,
|
Concurrency: 10,
|
||||||
B64EntropyCutoff: 70,
|
B64EntropyCutoff: 70,
|
||||||
@@ -213,25 +195,33 @@ func (opts *Options) parseOptions(args []string) error {
|
|||||||
help()
|
help()
|
||||||
os.Exit(EXIT_CLEAN)
|
os.Exit(EXIT_CLEAN)
|
||||||
default:
|
default:
|
||||||
// TARGETS
|
if match, value := opts.optString(arg, "--token="); match {
|
||||||
if i == len(args)-1 {
|
|
||||||
if opts.LocalMode {
|
|
||||||
opts.RepoPath = filepath.Clean(args[i])
|
|
||||||
} else {
|
|
||||||
opts.URL = args[i]
|
|
||||||
}
|
|
||||||
} else if match, value := opts.optString(arg, "--token="); match {
|
|
||||||
opts.Token = value
|
opts.Token = value
|
||||||
} else if match, value := opts.optString(arg, "--since="); match {
|
} else if match, value := opts.optString(arg, "--since="); match {
|
||||||
opts.SinceCommit = value
|
opts.SinceCommit = value
|
||||||
} else if match, value := opts.optString(arg, "--report-path="); match {
|
} else if match, value := opts.optString(arg, "--report-path="); match {
|
||||||
opts.ReportPath = value
|
opts.ReportPath = value
|
||||||
|
} else if match, value := opts.optString(arg, "--clone-path="); match {
|
||||||
|
opts.ClonePath = value
|
||||||
} else if match, value := opts.optInt(arg, "--log="); match {
|
} else if match, value := opts.optInt(arg, "--log="); match {
|
||||||
opts.LogLevel = value
|
opts.LogLevel = value
|
||||||
} else if match, value := opts.optInt(arg, "--b64Entropy="); match {
|
} else if match, value := opts.optInt(arg, "--b64Entropy="); match {
|
||||||
opts.B64EntropyCutoff = value
|
opts.B64EntropyCutoff = value
|
||||||
} else if match, value := opts.optInt(arg, "--hexEntropy="); match {
|
} else if match, value := opts.optInt(arg, "--hexEntropy="); match {
|
||||||
opts.HexEntropyCutoff = value
|
opts.HexEntropyCutoff = value
|
||||||
|
} else if i == len(args)-1 {
|
||||||
|
fmt.Println(args[i])
|
||||||
|
if opts.LocalMode {
|
||||||
|
opts.RepoPath = filepath.Clean(args[i])
|
||||||
|
} else {
|
||||||
|
if isGithubTarget(args[i]) {
|
||||||
|
opts.URL = args[i]
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Unknown option %s\n\n", arg)
|
||||||
|
help()
|
||||||
|
os.Exit(EXIT_CLEAN)
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("Unknown option %s\n\n", arg)
|
fmt.Printf("Unknown option %s\n\n", arg)
|
||||||
help()
|
help()
|
||||||
@@ -239,9 +229,26 @@ func (opts *Options) parseOptions(args []string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err := opts.guards()
|
err := opts.guards()
|
||||||
if !opts.RepoMode && !opts.UserMode && !opts.OrgMode && !opts.LocalMode {
|
if !opts.RepoMode && !opts.UserMode && !opts.OrgMode && !opts.LocalMode {
|
||||||
|
if opts.URL != "" {
|
||||||
opts.RepoMode = true
|
opts.RepoMode = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
pwd, _ = os.Getwd()
|
||||||
|
// check if pwd contains a .git, if it does, run local mode
|
||||||
|
dotGitPath := filepath.Join(pwd, ".git")
|
||||||
|
|
||||||
|
if _, err := os.Stat(dotGitPath); os.IsNotExist(err) {
|
||||||
|
fmt.Printf("gitleaks has no target")
|
||||||
|
os.Exit(EXIT_CLEAN)
|
||||||
|
} else {
|
||||||
|
opts.LocalMode = true
|
||||||
|
opts.RepoPath = pwd
|
||||||
|
opts.RepoMode = false
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,14 +7,12 @@ import (
|
|||||||
"github.com/google/go-github/github"
|
"github.com/google/go-github/github"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
"github.com/mitchellh/go-homedir"
|
|
||||||
"path/filepath"
|
|
||||||
"log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Owner struct {
|
type Owner struct {
|
||||||
@@ -31,22 +29,15 @@ func ownerPath(ownerName string) (string, error){
|
|||||||
fmt.Println("creating tmp")
|
fmt.Println("creating tmp")
|
||||||
dir, err := ioutil.TempDir("", ownerName)
|
dir, err := ioutil.TempDir("", ownerName)
|
||||||
return dir, err
|
return dir, err
|
||||||
|
} else if opts.ClonePath != "" {
|
||||||
|
if _, err := os.Stat(opts.ClonePath); os.IsNotExist(err) {
|
||||||
|
os.Mkdir(opts.ClonePath, os.ModePerm)
|
||||||
|
}
|
||||||
|
return opts.ClonePath, nil
|
||||||
|
} else {
|
||||||
|
return os.Getwd()
|
||||||
}
|
}
|
||||||
|
|
||||||
gitleaksHome := os.Getenv("GITLEAKS_HOME")
|
|
||||||
if gitleaksHome == "" {
|
|
||||||
homeDir, err := homedir.Dir()
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("could not find system home dir")
|
|
||||||
}
|
|
||||||
gitleaksHome = filepath.Join(homeDir, ".gitleaks")
|
|
||||||
}
|
|
||||||
|
|
||||||
// make sure gitleaks home exists
|
|
||||||
if _, err := os.Stat(gitleaksHome); os.IsNotExist(err) {
|
|
||||||
os.Mkdir(gitleaksHome, os.ModePerm)
|
|
||||||
}
|
|
||||||
return gitleaksHome + "/" + ownerName, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// newOwner instantiates an owner and creates any necessary resources for said owner.
|
// newOwner instantiates an owner and creates any necessary resources for said owner.
|
||||||
@@ -195,7 +186,7 @@ func (owner *Owner) addRepos(githubRepos []*github.Repository) {
|
|||||||
func (owner *Owner) auditRepos() int {
|
func (owner *Owner) auditRepos() int {
|
||||||
exitCode := EXIT_CLEAN
|
exitCode := EXIT_CLEAN
|
||||||
for _, repo := range owner.repos {
|
for _, repo := range owner.repos {
|
||||||
leaksPst, err := repo.audit(owner)
|
leaksPst, err := repo.audit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
failF("%v\n", err)
|
failF("%v\n", err)
|
||||||
}
|
}
|
||||||
@@ -216,25 +207,6 @@ func (owner *Owner) failF(format string, args ...interface{}) {
|
|||||||
os.Exit(EXIT_FAILURE)
|
os.Exit(EXIT_FAILURE)
|
||||||
}
|
}
|
||||||
|
|
||||||
// setupDir sets up the owner's directory for clones and reports.
|
|
||||||
// If the temporary option is set then a temporary directory will be
|
|
||||||
// used for the owner repo clones.
|
|
||||||
func (owner *Owner) setupDir() error {
|
|
||||||
if opts.Tmp {
|
|
||||||
fmt.Println("creating tmp")
|
|
||||||
dir, err := ioutil.TempDir("", owner.name)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Errorf("unable to create temp directories for cloning")
|
|
||||||
}
|
|
||||||
owner.path = dir
|
|
||||||
} else {
|
|
||||||
if _, err := os.Stat(owner.path); os.IsNotExist(err) {
|
|
||||||
os.Mkdir(owner.path, os.ModePerm)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// rmTmp removes the temporary repo
|
// rmTmp removes the temporary repo
|
||||||
func (owner *Owner) rmTmp() {
|
func (owner *Owner) rmTmp() {
|
||||||
log.Printf("removing tmp gitleaks repo for %s\n", owner.name)
|
log.Printf("removing tmp gitleaks repo for %s\n", owner.name)
|
||||||
|
|||||||
@@ -5,12 +5,12 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
"sync"
|
||||||
"log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Repo struct {
|
type Repo struct {
|
||||||
@@ -70,7 +70,7 @@ func newRepo(name string, url string, path string) *Repo {
|
|||||||
// commands so that users could opt for doing all clones/diffs in memory.
|
// commands so that users could opt for doing all clones/diffs in memory.
|
||||||
// Audit also declares two WaitGroups, one for distributing regex/entropy checks, and one for receiving
|
// Audit also declares two WaitGroups, one for distributing regex/entropy checks, and one for receiving
|
||||||
// the leaks if there are any. This could be done a little more elegantly in the future.
|
// the leaks if there are any. This could be done a little more elegantly in the future.
|
||||||
func (repo *Repo) audit(owner *Owner) (bool, error) {
|
func (repo *Repo) audit() (bool, error) {
|
||||||
var (
|
var (
|
||||||
out []byte
|
out []byte
|
||||||
err error
|
err error
|
||||||
@@ -140,7 +140,7 @@ func (repo *Repo) audit(owner *Owner) (bool, error) {
|
|||||||
leaksPst = true
|
leaksPst = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if opts.ReportPath != "" && len(leaks) != 0 {
|
if (opts.ReportPath != "" || opts.ReportOut) && len(leaks) != 0 {
|
||||||
err = repo.writeReport()
|
err = repo.writeReport()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return leaksPst, fmt.Errorf("could not write report to %s", opts.ReportPath)
|
return leaksPst, fmt.Errorf("could not write report to %s", opts.ReportPath)
|
||||||
@@ -154,7 +154,6 @@ func (repo *Repo) audit(owner *Owner) (bool, error) {
|
|||||||
// no leaks have been found
|
// no leaks have been found
|
||||||
func (repo *Repo) writeReport() error {
|
func (repo *Repo) writeReport() error {
|
||||||
reportJSON, _ := json.MarshalIndent(repo.leaks, "", "\t")
|
reportJSON, _ := json.MarshalIndent(repo.leaks, "", "\t")
|
||||||
|
|
||||||
if _, err := os.Stat(opts.ReportPath); os.IsNotExist(err) {
|
if _, err := os.Stat(opts.ReportPath); os.IsNotExist(err) {
|
||||||
os.Mkdir(opts.ReportPath, os.ModePerm)
|
os.Mkdir(opts.ReportPath, os.ModePerm)
|
||||||
}
|
}
|
||||||
|
|||||||
+29
-9
@@ -1,6 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
"os"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
func TestNewRepo(t *testing.T) {
|
func TestNewRepo(t *testing.T) {
|
||||||
// TODO
|
// TODO
|
||||||
@@ -11,11 +15,30 @@ func TestNewLocalRepo(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestWriteReport(t *testing.T) {
|
func TestWriteReport(t *testing.T) {
|
||||||
// TODO
|
opts, _ = defaultOptions()
|
||||||
/*
|
r := newRepo("fakerepo", "github.com", "")
|
||||||
opts, err := defaultOptions()
|
r.leaks = []Leak{*sampleLeak(), *sampleLeak()}
|
||||||
r := newRepo("fakerepo", "github.com")
|
r.writeReport()
|
||||||
sampleLeak := Leak{
|
if _, err := os.Stat("fakerepo_leaks.json"); os.IsNotExist(err) {
|
||||||
|
t.Error()
|
||||||
|
} else {
|
||||||
|
os.Remove("fakerepo_leaks.json")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAudit(t *testing.T) {
|
||||||
|
opts, _ = defaultOptions()
|
||||||
|
opts.RepoMode = true
|
||||||
|
opts.Tmp = true
|
||||||
|
opts.URL = "https://github.com/zricethezav/gronit"
|
||||||
|
owner := newOwner()
|
||||||
|
r := newRepo("gronit", "https://github.com/zricethezav/gronit", owner.path)
|
||||||
|
r.audit()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func sampleLeak() *Leak {
|
||||||
|
return &Leak{
|
||||||
Line: "yoo",
|
Line: "yoo",
|
||||||
Commit: "mycommit",
|
Commit: "mycommit",
|
||||||
Offender: "oh boy",
|
Offender: "oh boy",
|
||||||
@@ -25,7 +48,4 @@ func TestWriteReport(t *testing.T) {
|
|||||||
Author: "lol",
|
Author: "lol",
|
||||||
RepoURL: "yooo",
|
RepoURL: "yooo",
|
||||||
}
|
}
|
||||||
r.leaks = []Leak{sampleLeak, sampleLeak}
|
|
||||||
r.writeReport()
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user