Merge pull request #61 from zricethezav/feature/build_script

Feature/build script
This commit is contained in:
Zachary Rice authored and GitHub committed 2018-03-12 18:07:31 -05:00
commit f7fca41beb
9 files changed
+82 -83

No files matched your search

+1
View File
@@ -12,6 +12,7 @@
```bash ```bash
go get -u github.com/zricethezav/gitleaks go get -u github.com/zricethezav/gitleaks
``` ```
Or download from release binaries [here](https://github.com/zricethezav/gitleaks/releases)
#### Usage and Explanation #### Usage and Explanation
Executable
+21
View File
@@ -0,0 +1,21 @@
#!/usr/bin/env bash
platforms=("linux/amd64" "linux/arm" "linux/arm64" "linux/mips" "windows/amd64" "windows/386" "darwin/amd64")
for platform in "${platforms[@]}"
do
platform_split=(${platform//\// })
GOOS=${platform_split[0]}
GOARCH=${platform_split[1]}
output_name=gitleaks'-'$GOOS'-'$GOARCH
if [ $GOOS = "windows" ]; then
output_name+='.exe'
fi
env GOOS=$GOOS GOARCH=$GOARCH go build -o $output_name $package
if [ $? -ne 0 ]; then
echo 'An error has occurred! Aborting the script execution...'
exit 1
fi
done
+3 -3
View File
@@ -1,10 +1,10 @@
package main package main
import ( import (
"testing"
"os"
"bufio" "bufio"
"fmt" "fmt"
"os"
"testing"
) )
func TestCheckRegex(t *testing.T) { func TestCheckRegex(t *testing.T) {
@@ -50,7 +50,7 @@ func TestExternalRegex(t *testing.T) {
opts.RegexFile = "testregex.txt" opts.RegexFile = "testregex.txt"
opts.loadExternalRegex() opts.loadExternalRegex()
leaks := doChecks("aws=\"AKIALALEMEL33243OLIAE", leaks := doChecks("aws=\"AKIALALEMEL33243OLIAE",
Commit{}, &Repo{url:"someurl"}) Commit{}, &Repo{url: "someurl"})
if len(leaks) != 2 { if len(leaks) != 2 {
// leak from default regex, leak from external // leak from default regex, leak from external
t.Error() t.Error()
-8
View File
@@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"os" "os"
"regexp" "regexp"
) )
@@ -34,8 +33,6 @@ func init() {
stopWords = []string{"setting", "info", "env", "environment"} stopWords = []string{"setting", "info", "env", "environment"}
fileDiffRegex = regexp.MustCompile("diff --git a.+b/") fileDiffRegex = regexp.MustCompile("diff --git a.+b/")
assignRegex = regexp.MustCompile(`(=|:|:=|<-)`) assignRegex = regexp.MustCompile(`(=|:|:=|<-)`)
// TODO Externalize regex... this is tricky making it yml compliant
regexes = map[string]*regexp.Regexp{ regexes = map[string]*regexp.Regexp{
"PKCS8": regexp.MustCompile("-----BEGIN PRIVATE KEY-----"), "PKCS8": regexp.MustCompile("-----BEGIN PRIVATE KEY-----"),
"RSA": regexp.MustCompile("-----BEGIN RSA PRIVATE KEY-----"), "RSA": regexp.MustCompile("-----BEGIN RSA PRIVATE KEY-----"),
@@ -56,8 +53,3 @@ func main() {
owner := newOwner() owner := newOwner()
os.Exit(owner.auditRepos()) os.Exit(owner.auditRepos())
} }
func failF(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format, args...)
os.Exit(ExitFailure)
}
+23 -25
View File
@@ -1,13 +1,13 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"bufio"
) )
const usage = ` const usage = `
@@ -22,7 +22,7 @@ Options:
--report-path=<STR> Report output, default $GITLEAKS_HOME/report --report-path=<STR> Report output, default $GITLEAKS_HOME/report
--clone-path=<STR> Gitleaks will clone repos here, default $GITLEAKS_HOME/clones --clone-path=<STR> Gitleaks will clone repos here, default $GITLEAKS_HOME/clones
-t --temp Clone to temporary directory -t --temp Clone to temporary directory
--concurrency=<INT> Upper bound on concurrent diffs --concurrency=<INT> Upper bound on concurrent "git diff"
--since=<STR> Commit to stop at --since=<STR> Commit to stop at
--b64Entropy=<INT> Base64 entropy cutoff (default is 70) --b64Entropy=<INT> Base64 entropy cutoff (default is 70)
--hexEntropy=<INT> Hex entropy cutoff (default is 40) --hexEntropy=<INT> Hex entropy cutoff (default is 40)
@@ -33,27 +33,26 @@ Options:
` `
// Options for gitleaks. need to support remote repo/owner // Options for gitleaks
// and local repo/owner mode
type Options struct { type Options struct {
URL string URL string
RepoPath string RepoPath string
ReportPath string ReportPath string
ClonePath string ClonePath string
Concurrency int Concurrency int
B64EntropyCutoff int B64EntropyCutoff int
HexEntropyCutoff int HexEntropyCutoff int
UserMode bool UserMode bool
OrgMode bool OrgMode bool
RepoMode bool RepoMode bool
LocalMode bool LocalMode bool
Strict bool Strict bool
Entropy bool Entropy bool
SinceCommit string SinceCommit string
Tmp bool Tmp bool
Token string Token string
Verbose bool Verbose bool
RegexFile string RegexFile string
} }
// help prints the usage string and exits // help prints the usage string and exits
@@ -122,7 +121,7 @@ func newOpts(args []string) *Options {
return opts return opts
} }
// deafultOptions provides the default options // deafultOptions provides the default options used by newOpts
func defaultOptions() (*Options, error) { func defaultOptions() (*Options, error) {
return &Options{ return &Options{
Concurrency: 10, Concurrency: 10,
@@ -131,9 +130,8 @@ func defaultOptions() (*Options, error) {
}, nil }, nil
} }
// parseOptions // parseOptions will parse options supplied by the user.
func (opts *Options) parseOptions(args []string) error { func (opts *Options) parseOptions(args []string) error {
if len(args) == 0 { if len(args) == 0 {
opts.LocalMode = true opts.LocalMode = true
opts.RepoPath, _ = os.Getwd() opts.RepoPath, _ = os.Getwd()
@@ -208,7 +206,7 @@ func (opts *Options) parseOptions(args []string) error {
if opts.URL != "" { if opts.URL != "" {
opts.RepoMode = true opts.RepoMode = true
err := opts.guards() err := opts.guards()
if err != nil{ if err != nil {
return err return err
} }
return nil return nil
@@ -228,13 +226,13 @@ func (opts *Options) parseOptions(args []string) error {
} }
err := opts.guards() err := opts.guards()
if err != nil{ if err != nil {
return err return err
} }
return err return err
} }
// loadExternalRegex // loadExternalRegex loads regexes from a text file if available.
func (opts *Options) loadExternalRegex() error { func (opts *Options) loadExternalRegex() error {
file, err := os.Open(opts.RegexFile) file, err := os.Open(opts.RegexFile)
if err != nil { if err != nil {
+17 -18
View File
@@ -14,7 +14,11 @@ import (
"strings" "strings"
) )
// Owner blah blah // Owner represents the owner of a repo or group of repos.
// Owners can fall under three categories depending on how
// Gitleaks is ran; ambiguous, user, or organization.
// An ambiguous implies that gitleaks is running on a single
// repo from github or locally.
type Owner struct { type Owner struct {
name string name string
url string url string
@@ -54,7 +58,7 @@ func newOwner() *Owner {
name := ownerName() name := ownerName()
ownerPath, err := ownerPath(name) ownerPath, err := ownerPath(name)
if err != nil { if err != nil {
failF("%v", err) log.Fatal(err)
} }
owner := &Owner{ owner := &Owner{
name: name, name: name,
@@ -77,16 +81,9 @@ func newOwner() *Owner {
os.Exit(ExitFailure) os.Exit(ExitFailure)
}() }()
// if running on local repo, just go right to it.
if opts.LocalMode {
repo := newLocalRepo(opts.RepoPath)
owner.repos = append(owner.repos, *repo)
return owner
}
err = owner.fetchRepos() err = owner.fetchRepos()
if err != nil { if err != nil {
owner.failF("%v", err) log.Fatal(err)
} }
return owner return owner
} }
@@ -98,6 +95,15 @@ func newOwner() *Owner {
func (owner *Owner) fetchRepos() error { func (owner *Owner) fetchRepos() error {
var err error var err error
ctx := context.Background() ctx := context.Background()
// local mode, single repo, ambiguous account type
if opts.LocalMode {
_, repoName := path.Split(opts.RepoPath)
repo := newRepo(repoName, "", opts.RepoPath)
owner.repos = append(owner.repos, *repo)
return nil
}
if owner.accountType == "" { if owner.accountType == "" {
// single repo, ambiguous account type // single repo, ambiguous account type
_, repoName := path.Split(opts.URL) _, repoName := path.Split(opts.URL)
@@ -200,7 +206,7 @@ func (owner *Owner) auditRepos() int {
for _, repo := range owner.repos { for _, repo := range owner.repos {
leaksPst, err := repo.audit() leaksPst, err := repo.audit()
if err != nil { if err != nil {
failF("%v", err) log.Fatal(err)
} }
if leaksPst { if leaksPst {
exitCode = ExitLeaks exitCode = ExitLeaks
@@ -212,13 +218,6 @@ func (owner *Owner) auditRepos() int {
return exitCode return exitCode
} }
// failF prints a failure message out to stderr
// and exits with a exit code 2
func (owner *Owner) failF(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format, args...)
os.Exit(ExitFailure)
}
// rmTmp removes the owner's temporary repo. rmTmp will only get called if temporary // rmTmp removes the owner's temporary repo. rmTmp will only get called if temporary
// mode is set. rmTmp is called on a SIGINT and after the audits have finished // mode is set. rmTmp is called on a SIGINT and after the audits have finished
func (owner *Owner) rmTmp() { func (owner *Owner) rmTmp() {
+1 -1
View File
@@ -1,8 +1,8 @@
package main package main
import ( import (
"testing"
"os" "os"
"testing"
) )
func TestOwnerPath(t *testing.T) { func TestOwnerPath(t *testing.T) {
+8 -23
View File
@@ -8,12 +8,11 @@ import (
"log" "log"
"os" "os"
"os/exec" "os/exec"
"path"
"path/filepath" "path/filepath"
"sync" "sync"
) )
// Repo is // Repo represents a git repo
type Repo struct { type Repo struct {
name string name string
url string url string
@@ -23,7 +22,7 @@ type Repo struct {
reportPath string reportPath string
} }
// Leak is // Leak struct for reporting
type Leak struct { type Leak struct {
Line string `json:"line"` Line string `json:"line"`
Commit string `json:"commit"` Commit string `json:"commit"`
@@ -36,7 +35,7 @@ type Leak struct {
RepoURL string `json:"repoURL"` RepoURL string `json:"repoURL"`
} }
// Commit is // Commit represents a git commit
type Commit struct { type Commit struct {
Hash string Hash string
Author string Author string
@@ -44,19 +43,7 @@ type Commit struct {
Msg string Msg string
} }
// newLocalRepo will such and such // newRepo creates a new repo based on name, url, and a clone path
func newLocalRepo(repoPath string) *Repo {
_, name := path.Split(repoPath)
repo := &Repo{
name: name,
path: repoPath,
reportPath: opts.ReportPath,
}
return repo
}
// newRepo
func newRepo(name string, url string, path string) *Repo { func newRepo(name string, url string, path string) *Repo {
repo := &Repo{ repo := &Repo{
name: name, name: name,
@@ -67,7 +54,7 @@ func newRepo(name string, url string, path string) *Repo {
return repo return repo
} }
// rmTmp // rmTmp removes the temporary directory: repo.path
func (repo *Repo) rmTmp() { func (repo *Repo) rmTmp() {
log.Printf("removing tmp gitleaks repo %s\n", repo.path) log.Printf("removing tmp gitleaks repo %s\n", repo.path)
os.Remove(repo.path) os.Remove(repo.path)
@@ -212,14 +199,13 @@ func parseRevList(revList [][]byte) []Commit {
return commits return commits
} }
// reportAggregator is a go func responsible for ... // reportAggregator is will consume Leak messages from the gitLeaks channel and report them
func reportAggregator(gitLeakReceiverWG *sync.WaitGroup, gitLeaks chan Leak, leaks *[]Leak) { func reportAggregator(gitLeakReceiverWG *sync.WaitGroup, gitLeaks chan Leak, leaks *[]Leak) {
for gitLeak := range gitLeaks { for gitLeak := range gitLeaks {
*leaks = append(*leaks, gitLeak) *leaks = append(*leaks, gitLeak)
if opts.Verbose { if opts.Verbose {
b, err := json.MarshalIndent(gitLeak, "", " ") b, err := json.MarshalIndent(gitLeak, "", " ")
if err != nil { if err != nil {
// handle this?
fmt.Printf("failed to output leak: %v", err) fmt.Printf("failed to output leak: %v", err)
} }
fmt.Println(string(b)) fmt.Println(string(b))
@@ -238,8 +224,7 @@ func auditDiff(currCommit Commit, repo *Repo, commitWG *sync.WaitGroup,
defer commitWG.Done() defer commitWG.Done()
if err := os.Chdir(fmt.Sprintf(repo.path)); err != nil { if err := os.Chdir(fmt.Sprintf(repo.path)); err != nil {
// TODO handle this better log.Fatalf("unable to navigate to %s", repo.path)
os.Exit(ExitFailure)
} }
commitCmp := fmt.Sprintf("%s^!", currCommit.Hash) commitCmp := fmt.Sprintf("%s^!", currCommit.Hash)
@@ -248,7 +233,7 @@ func auditDiff(currCommit Commit, repo *Repo, commitWG *sync.WaitGroup,
<-semaphoreChan <-semaphoreChan
if err != nil { if err != nil {
os.Exit(ExitFailure) log.Fatalf("unable to diff for %s: %v", currCommit.Hash, err)
} }
leaks := doChecks(string(out), currCommit, repo) leaks := doChecks(string(out), currCommit, repo)
+8 -5
View File
@@ -5,13 +5,16 @@ import (
"testing" "testing"
) )
func TestNewLocalRepo(t *testing.T) { func TestNewRepo(t *testing.T) {
r := newLocalRepo("") // local mode
if r.path != "" { r := newRepo("repo", "", "some/repo")
if r.name != "repo" || r.path != "some/repo" {
t.Error() t.Error()
} }
r = newLocalRepo("some/path")
if r.name != "path" || r.path != "some/path" { // repo/owner mode
r = newRepo("repo", "github.com/owner/repo", "some/repo")
if r.name != "repo" || r.path != "some/repo" {
t.Error() t.Error()
} }
} }