diff --git a/Makefile b/Makefile index 2627e7b2..ad15a1f9 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,8 @@ test-integration: go test github.com/zricethezav/gitleaks/hosts -v -integration build: + go fmt ./... + golint ./... go mod tidy go build $(LDFLAGS) diff --git a/audit/audit.go b/audit/audit.go index c3593afc..34f22afc 100644 --- a/audit/audit.go +++ b/audit/audit.go @@ -20,7 +20,6 @@ func Run(m *manager.Manager) error { } m.Opts.RepoPath = fmt.Sprintf("%s/%s", m.Opts.OwnerPath, f.Name()) if err := runHelper(NewRepo(m)); err != nil { - // TODO or send to errchan? return err } } @@ -31,7 +30,6 @@ func Run(m *manager.Manager) error { } func runHelper(r *Repo) error { - // Check if gitleaks will perform a local audit. if r.Manager.Opts.OpenLocal() { r.Name = path.Base(r.Manager.Opts.RepoPath) if err := r.Open(); err != nil { @@ -41,7 +39,7 @@ func runHelper(r *Repo) error { // Check if we are checking uncommitted files. This is the default behavior // for a "$gitleaks" command with no options set if r.Manager.Opts.CheckUncommitted() { - if err := r.AuditLocal(); err != nil { + if err := r.AuditUncommitted(); err != nil { return err } return nil diff --git a/audit/audit_test.go b/audit/audit_test.go index 793dc73c..92b380c3 100644 --- a/audit/audit_test.go +++ b/audit/audit_test.go @@ -151,7 +151,7 @@ func TestAudit(t *testing.T) { Report: "../test_data/test_local_repo_four_alt_config_entropy.json.got", RepoConfig: true, }, - wantPath: "../test_data/test_local_repo_four_alt_config_entropy.json.got", + wantPath: "../test_data/test_local_repo_four_alt_config_entropy.json", }, } diff --git a/audit/repo.go b/audit/repo.go index 43bdf358..cc742e08 100644 --- a/audit/repo.go +++ b/audit/repo.go @@ -17,6 +17,7 @@ import ( "gopkg.in/src-d/go-git.v4/storage/memory" "io" "os" + "path" "sync" "time" ) @@ -76,9 +77,9 @@ func (repo *Repo) Clone(cloneOption *git.CloneOptions) error { return nil } -// AuditLocal will do a `git diff` and scan changed files that are being tracked. This is useful functionality +// AuditUncommitted will do a `git diff` and scan changed files that are being tracked. This is useful functionality // for a pre-commit hook so you can make sure your code does not have any leaks before committing. -func (repo *Repo) AuditLocal() error { +func (repo *Repo) AuditUncommitted() error { auditTimeStart := time.Now() r, err := repo.Head() @@ -277,6 +278,7 @@ func (repo *Repo) Open() error { return err } repo.Repository = repository + repo.Name = path.Base(dir) } return nil } diff --git a/main.go b/main.go index b7608640..2c9bbede 100644 --- a/main.go +++ b/main.go @@ -16,9 +16,7 @@ import ( // TODO documentation for // 1. ./gitleaks --repo=https://github.com/gitleakstest/gronit -v | jq -R 'fromjson?' // 2. Dockerfile -// 3. need to add tests for --repo-config -// 4. look over comments and code -// 5. prepare release +// 3. prepare release func main() { opts, err := options.ParseOptions() @@ -55,15 +53,22 @@ func main() { metadata := m.GetMetadata() if len(m.GetLeaks()) != 0 { - log.Warnf("%d leaks detected. %d commits audited in %s", len(leaks), - metadata.Commits, durafmt.Parse(time.Duration(metadata.AuditTime)*time.Nanosecond)) + if m.Opts.CheckUncommitted() { + log.Warnf("%d leaks detected in staged changes", len(leaks)) + } else { + log.Warnf("%d leaks detected. %d commits audited in %s", len(leaks), + metadata.Commits, durafmt.Parse(time.Duration(metadata.AuditTime)*time.Nanosecond)) + } os.Exit(options.LeaksPresent) } else { - log.Infof("No leaks detected. %d commits audited in %s", - metadata.Commits, durafmt.Parse(time.Duration(metadata.AuditTime)*time.Nanosecond)) + if m.Opts.CheckUncommitted() { + log.Infof("No leaks detected in staged changes") + } else { + log.Infof("No leaks detected. %d commits audited in %s", + metadata.Commits, durafmt.Parse(time.Duration(metadata.AuditTime)*time.Nanosecond)) + } + os.Exit(options.Success) } - - os.Exit(options.Success) } // Run begins the program and contains some basic logic on how to continue with the audit. If any external git host @@ -71,7 +76,6 @@ func main() { // then Audit() and Report() will be called. Otherwise, gitleaks will create a new repo and an audit will proceed. // If no options or the uncommitted option is set then a pre-commit audit will // take place -- this is similar to running `git diff` on all the tracked files. -// TODO handle errors from errChan func Run(m *manager.Manager) error { if m.Opts.Disk { dir, err := ioutil.TempDir("", "gitleaks") diff --git a/manager/manager.go b/manager/manager.go index 92f474b5..686d49cd 100644 --- a/manager/manager.go +++ b/manager/manager.go @@ -48,7 +48,6 @@ type Leak struct { File string `json:"file"` Date time.Time `json:"date"` Tags string `json:"tags"` - Severity string `json:"severity"` } // AuditTime is a type used to determine total audit time diff --git a/options/options.go b/options/options.go index d4933f43..78516245 100644 --- a/options/options.go +++ b/options/options.go @@ -63,8 +63,10 @@ func ParseOptions() (Options, error) { _, err := parser.Parse() if err != nil { - parser.WriteHelp(os.Stdout) - os.Exit(Success) + if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type != flags.ErrHelp { + parser.WriteHelp(os.Stdout) + } + os.Exit(0) } if opts.Version { @@ -83,11 +85,32 @@ func ParseOptions() (Options, error) { // If invalid sets of options are present, a descriptive error will return // else nil is returned func (opts Options) Guard() error { - // 1. only one target option set at a time: - // repo, owner-path, repo-path + if !oneOrNoneSet(opts.Repo, opts.OwnerPath, opts.RepoPath, opts.Host) { + return fmt.Errorf("only one target option must can be set. target options: repo, owner-path, repo-path, host") + } + if !oneOrNoneSet(opts.Organization, opts.User, opts.PullRequest) { + return fmt.Errorf("only one target option must can be set. target options: repo, owner-path, repo-path, host") + } + if !oneOrNoneSet(opts.AccessToken, opts.Password) { + log.Warn("both access-token and password are set. Only password will be attempted") + } + return nil } +func oneOrNoneSet(optStr ...string) bool { + c := 0 + for _, s := range optStr { + if s != "" { + c++ + } + } + if c <= 1 { + return true + } + return false +} + // CloneOptions returns a git.cloneOptions pointer. The authentication method // is determined by what is passed in via command-Line options. If No // Username/PW or AccessToken is available and the repo target is not using the diff --git a/test_data/test_entropy.json b/test_data/test_entropy.json index 7cccf4e5..4c418cbd 100644 --- a/test_data/test_entropy.json +++ b/test_data/test_entropy.json @@ -10,7 +10,6 @@ "email": "zricer@protonmail.com", "file": "server.test.py", "date": "2019-10-24T09:29:27-04:00", - "tags": "entropy", - "severity": "" + "tags": "entropy" } ] diff --git a/test_data/test_local_owner_aws_leak.json b/test_data/test_local_owner_aws_leak.json index 6e30e5d0..261ce2b3 100644 --- a/test_data/test_local_owner_aws_leak.json +++ b/test_data/test_local_owner_aws_leak.json @@ -10,8 +10,7 @@ "email": "zricer@protonmail.com", "file": "server.test.py", "date": "2019-10-24T09:29:27-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": " const AWSKEY = \"AKIALALEMEL33243OLIBE\"", @@ -24,8 +23,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:12:32-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": " const AWSKEY = \"AKIALALEMEL33243OLIBE\"", @@ -38,8 +36,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:12:08-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "Here's an AWS secret: \"AKIALALEMEL33243OLIAE\"", @@ -52,8 +49,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:07:41-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "Here's an AWS secret: \"AKIALALEMEL33243OLIAE\"", @@ -66,8 +62,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:01:27-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "Here's an AWS secret: AKIALALEMEL33243OLIAE", @@ -80,8 +75,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:01:27-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "\nHere's an AWS secret: AKIALALEMEL33243OLIAE", @@ -94,8 +88,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T12:58:39-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "const AWSKEY = \"AKIALALEMEL33243OLIAE\"", @@ -108,8 +101,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:54:26-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "const AWSKEY = \"AKIALALEMEL33243OLIAE\"", @@ -122,8 +114,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:54:08-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "AWS secret: \"AKIALALEMEL33243OLIAE\"", @@ -136,8 +127,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:36:22-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "AWS secret: \"AKIALALEMEL33243OLIAE\"", @@ -150,8 +140,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:35:03-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": " const AWSKEY = \"AKIALALEMEL33243OLIBE\"", @@ -164,8 +153,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:12:32-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": " const AWSKEY = \"AKIALALEMEL33243OLIBE\"", @@ -178,8 +166,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:12:08-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "Here's an AWS secret: \"AKIALALEMEL33243OLIAE\"", @@ -192,8 +179,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:07:41-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "Here's an AWS secret: \"AKIALALEMEL33243OLIAE\"", @@ -206,8 +192,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:01:27-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "Here's an AWS secret: AKIALALEMEL33243OLIAE", @@ -220,8 +205,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:01:27-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "\nHere's an AWS secret: AKIALALEMEL33243OLIAE", @@ -234,8 +218,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T12:58:39-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "const AWSKEY = \"AKIALALEMEL33243OLIAE\"", @@ -248,8 +231,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:54:26-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "const AWSKEY = \"AKIALALEMEL33243OLIAE\"", @@ -262,8 +244,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:54:08-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "AWS secret: \"AKIALALEMEL33243OLIAE\"", @@ -276,8 +257,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:36:22-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "AWS secret: \"AKIALALEMEL33243OLIAE\"", @@ -290,8 +270,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:35:03-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": " const AWSKEY = \"AKIALALEMEL33243OLIBE\"", @@ -304,8 +283,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:12:32-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": " const AWSKEY = \"AKIALALEMEL33243OLIBE\"", @@ -318,8 +296,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:12:08-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "Here's an AWS secret: \"AKIALALEMEL33243OLIAE\"", @@ -332,8 +309,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:07:41-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "Here's an AWS secret: \"AKIALALEMEL33243OLIAE\"", @@ -346,8 +322,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:01:27-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "Here's an AWS secret: AKIALALEMEL33243OLIAE", @@ -360,8 +335,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:01:27-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "\nHere's an AWS secret: AKIALALEMEL33243OLIAE", @@ -374,7 +348,6 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T12:58:39-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" } ] diff --git a/test_data/test_local_repo_four_alt_config_entropy.json b/test_data/test_local_repo_four_alt_config_entropy.json index aee3ef35..2e7a661a 100644 --- a/test_data/test_local_repo_four_alt_config_entropy.json +++ b/test_data/test_local_repo_four_alt_config_entropy.json @@ -10,8 +10,7 @@ "email": "zricer@protonmail.com", "file": "secret.pem", "date": "2019-10-25T13:08:39-04:00", - "tags": "entropy", - "severity": "" + "tags": "entropy" }, { "line": " Just moments after the Cessna's inquiry, a Twin Beech piped up on frequency, in a rather superior tone, asking for his ground speed. \"I have you at one hundred and twenty-five knots of ground spe", @@ -24,7 +23,6 @@ "email": "zricer@protonmail.com", "file": "secret.pem", "date": "2019-10-25T13:07:41-04:00", - "tags": "entropy", - "severity": "" + "tags": "entropy" } ] diff --git a/test_data/test_local_repo_one_aws_leak.json b/test_data/test_local_repo_one_aws_leak.json index 2390a0c9..7f869f1e 100644 --- a/test_data/test_local_repo_one_aws_leak.json +++ b/test_data/test_local_repo_one_aws_leak.json @@ -10,7 +10,6 @@ "email": "zricer@protonmail.com", "file": "server.test.py", "date": "2019-10-24T09:29:27-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" } ] diff --git a/test_data/test_local_repo_one_aws_leak_and_file_leak.json b/test_data/test_local_repo_one_aws_leak_and_file_leak.json index 9fa1f146..c0437d42 100644 --- a/test_data/test_local_repo_one_aws_leak_and_file_leak.json +++ b/test_data/test_local_repo_one_aws_leak_and_file_leak.json @@ -10,8 +10,7 @@ "email": "zricer@protonmail.com", "file": "server.test.py", "date": "2019-10-24T10:03:38-04:00", - "tags": "", - "severity": "" + "tags": "" }, { "line": " aws_access_key_id='AKIAIO5FODNN7EXAMPLE',", @@ -24,7 +23,6 @@ "email": "zricer@protonmail.com", "file": "server.test.py", "date": "2019-10-24T09:29:27-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" } ] diff --git a/test_data/test_local_repo_one_aws_leak_commit.json b/test_data/test_local_repo_one_aws_leak_commit.json index 2390a0c9..7f869f1e 100644 --- a/test_data/test_local_repo_one_aws_leak_commit.json +++ b/test_data/test_local_repo_one_aws_leak_commit.json @@ -10,7 +10,6 @@ "email": "zricer@protonmail.com", "file": "server.test.py", "date": "2019-10-24T09:29:27-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" } ] diff --git a/test_data/test_local_repo_one_aws_leak_uncommitted.json b/test_data/test_local_repo_one_aws_leak_uncommitted.json index f3f241d2..18c60120 100644 --- a/test_data/test_local_repo_one_aws_leak_uncommitted.json +++ b/test_data/test_local_repo_one_aws_leak_uncommitted.json @@ -10,7 +10,6 @@ "email": "zricer@protonmail.com", "file": "server.test.py", "date": "2019-10-24T10:03:38-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" } ] diff --git a/test_data/test_local_repo_three_leaks.json b/test_data/test_local_repo_three_leaks.json index f275a16b..024003a4 100644 --- a/test_data/test_local_repo_three_leaks.json +++ b/test_data/test_local_repo_three_leaks.json @@ -10,8 +10,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:36:22-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "AWS secret: \"AKIALALEMEL33243OLIAE\"", @@ -24,8 +23,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:35:03-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": " const AWSKEY = \"AKIALALEMEL33243OLIBE\"", @@ -38,8 +36,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:12:32-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": " const AWSKEY = \"AKIALALEMEL33243OLIBE\"", @@ -52,8 +49,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:12:08-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "Here's an AWS secret: \"AKIALALEMEL33243OLIAE\"", @@ -66,8 +62,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:07:41-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "Here's an AWS secret: \"AKIALALEMEL33243OLIAE\"", @@ -80,8 +75,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:01:27-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "Here's an AWS secret: AKIALALEMEL33243OLIAE", @@ -94,8 +88,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:01:27-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "\nHere's an AWS secret: AKIALALEMEL33243OLIAE", @@ -108,7 +101,6 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T12:58:39-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" } ] diff --git a/test_data/test_local_repo_two_leaks.json b/test_data/test_local_repo_two_leaks.json index e10a575c..136bb6eb 100644 --- a/test_data/test_local_repo_two_leaks.json +++ b/test_data/test_local_repo_two_leaks.json @@ -10,8 +10,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:12:32-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": " const AWSKEY = \"AKIALALEMEL33243OLIBE\"", @@ -24,8 +23,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:12:08-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "Here's an AWS secret: \"AKIALALEMEL33243OLIAE\"", @@ -38,8 +36,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:07:41-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "Here's an AWS secret: \"AKIALALEMEL33243OLIAE\"", @@ -52,8 +49,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:01:27-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "Here's an AWS secret: AKIALALEMEL33243OLIAE", @@ -66,8 +62,7 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T13:01:27-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" }, { "line": "\nHere's an AWS secret: AKIALALEMEL33243OLIAE", @@ -80,7 +75,6 @@ "email": "zricer@protonmail.com", "file": "secrets.md", "date": "2019-10-25T12:58:39-04:00", - "tags": "key, AWS", - "severity": "" + "tags": "key, AWS" } ] diff --git a/test_data/test_regex_entropy.json b/test_data/test_regex_entropy.json index f8194b98..5cdc5c92 100644 --- a/test_data/test_regex_entropy.json +++ b/test_data/test_regex_entropy.json @@ -10,7 +10,6 @@ "email": "zricer@protonmail.com", "file": "server.test.py", "date": "2019-10-24T09:29:27-04:00", - "tags": "entropy", - "severity": "" + "tags": "entropy" } ]