mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-27 19:54:24 +01:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
923dcf0311 | ||
|
|
8896cf66cd | ||
|
|
df133ccaf8 | ||
|
|
23cd693d71 | ||
|
|
524d81fde9 | ||
|
|
d387f528c4 | ||
|
|
dcda26578b | ||
|
|
28639d3a49 | ||
|
|
19e23c9cbe | ||
|
|
160015b872 | ||
|
|
abdf2c47f0 | ||
|
|
8536f16ceb | ||
|
|
6862c13bf7 | ||
|
|
9939c638ec | ||
|
|
ed3a512b4b | ||
|
|
7c988c927d | ||
|
|
bde9af44ea | ||
|
|
eee4d05642 | ||
|
|
9144594d77 | ||
|
|
1cb15242fd | ||
|
|
08498600d1 | ||
|
|
88c0c7779b |
No files matched your search
@@ -1,5 +1,31 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
1.12.1
|
||||
----
|
||||
- Show program usage when no arguments are provided
|
||||
- Exit program after the -h or --help options are used
|
||||
1.12.0
|
||||
----
|
||||
- removing --csv option
|
||||
- --report option now requires .json or .csv in filename
|
||||
- adding total time to audit in logs
|
||||
|
||||
1.11.1
|
||||
----
|
||||
- fix commit whitelist logic
|
||||
|
||||
1.11.0
|
||||
-----
|
||||
- Commit depth option
|
||||
- Commit stats output
|
||||
|
||||
1.10.0
|
||||
-----
|
||||
- Add entropy option
|
||||
|
||||
1.9.0
|
||||
-----
|
||||
- exclude fork option
|
||||
|
||||
1.8.0
|
||||
-----
|
||||
|
||||
Generated
+7
-1
@@ -44,6 +44,12 @@
|
||||
revision = "44c6ddd0a2342c386950e880b658017258da92fc"
|
||||
version = "v1.0.0"
|
||||
|
||||
[[projects]]
|
||||
name = "github.com/hako/durafmt"
|
||||
packages = ["."]
|
||||
revision = "7b7ae1e72eade09dbc9c2cfba3e6c4bae7b8bcac"
|
||||
version = "1.0.0"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
name = "github.com/jbenet/go-context"
|
||||
@@ -249,6 +255,6 @@
|
||||
[solve-meta]
|
||||
analyzer-name = "dep"
|
||||
analyzer-version = 1
|
||||
inputs-digest = "8185a08afcb3ad854f58b49061fb70c57e7b64244abb43a08714d55cac7a97f9"
|
||||
inputs-digest = "ad1b6855b3c8b3e1303f29123160cde693d39d6724682ce1cd246cd1a2343c7c"
|
||||
solver-name = "gps-cdcl"
|
||||
solver-version = 1
|
||||
@@ -1,7 +1,7 @@
|
||||
.PHONY: test build-all deploy
|
||||
|
||||
test:
|
||||
go get github.com/golang/lint/golint
|
||||
go get golang.org/x/lint/golint
|
||||
go fmt
|
||||
golint
|
||||
go test --race --cover -run=Test$
|
||||
|
||||
@@ -26,12 +26,13 @@ Usage:
|
||||
|
||||
Application Options:
|
||||
-r, --repo= Repo url to audit
|
||||
--github-user= User url to audit
|
||||
--github-org= Organization url to audit
|
||||
--github-user= Github user to audit
|
||||
--github-org= Github organization to audit
|
||||
--github-url= GitHub API Base URL, use for GitHub Enterprise. Example: https://github.example.com/api/v3/ (default: https://api.github.com/)
|
||||
-p, --private Include private repos in audit
|
||||
-b, --branch= branch name to audit (defaults to HEAD)
|
||||
-c, --commit= sha of commit to stop at
|
||||
--depth= maximum commit depth
|
||||
--repo-path= Path to repo
|
||||
--owner-path= Path to owner directory (repos discovered)
|
||||
--max-go= Maximum number of concurrent go-routines gitleaks spawns
|
||||
@@ -40,10 +41,11 @@ Application Options:
|
||||
--single-search= single regular expression to search for
|
||||
--config= path to gitleaks config
|
||||
--ssh-key= path to ssh key
|
||||
--exclude-forks exclude forks for organization/user audits
|
||||
-e, --entropy= Include entropy checks during audit. Entropy scale: 0.0(no entropy) - 8.0(max entropy)
|
||||
-l, --log= log level
|
||||
-v, --verbose Show verbose output from gitleaks audit
|
||||
--report= path to write report file
|
||||
--csv report output to csv
|
||||
--redact redact secrets from log messages and report
|
||||
--version version number
|
||||
--sample-config prints a sample config file
|
||||
|
||||
+90
-52
@@ -279,11 +279,20 @@ func TestRun(t *testing.T) {
|
||||
testOpts: Options{
|
||||
GithubOrg: "gitleakstestorg",
|
||||
},
|
||||
description: "test github org",
|
||||
description: "test github org, whitelist repo",
|
||||
numLeaks: 0,
|
||||
expectedErrMsg: "",
|
||||
configPath: path.Join(configsDir, "repo"),
|
||||
},
|
||||
{
|
||||
testOpts: Options{
|
||||
GithubOrg: "gitleakstestorg",
|
||||
ExcludeForks: true,
|
||||
},
|
||||
description: "test github org, exclude forks",
|
||||
numLeaks: 0,
|
||||
expectedErrMsg: "",
|
||||
},
|
||||
}
|
||||
g := goblin.Goblin(t)
|
||||
for _, test := range tests {
|
||||
@@ -306,6 +315,8 @@ func TestRun(t *testing.T) {
|
||||
func TestWriteReport(t *testing.T) {
|
||||
tmpDir, _ := ioutil.TempDir("", "reportDir")
|
||||
reportJSON := path.Join(tmpDir, "report.json")
|
||||
reportJASON := path.Join(tmpDir, "report.jason")
|
||||
reportVOID := path.Join("thereIsNoWay", "thisReportWillGetWritten.json")
|
||||
reportCSV := path.Join(tmpDir, "report.csv")
|
||||
defer os.RemoveAll(tmpDir)
|
||||
leaks := []Leak{
|
||||
@@ -322,17 +333,18 @@ func TestWriteReport(t *testing.T) {
|
||||
}
|
||||
|
||||
var tests = []struct {
|
||||
leaks []Leak
|
||||
reportFile string
|
||||
fileName string
|
||||
description string
|
||||
testOpts Options
|
||||
leaks []Leak
|
||||
reportFile string
|
||||
fileName string
|
||||
description string
|
||||
testOpts Options
|
||||
expectedErrMsg string
|
||||
}{
|
||||
{
|
||||
leaks: leaks,
|
||||
reportFile: reportJSON,
|
||||
fileName: "report.json",
|
||||
description: "can we write a file",
|
||||
description: "can we write a json file",
|
||||
testOpts: Options{
|
||||
Report: reportJSON,
|
||||
},
|
||||
@@ -341,10 +353,29 @@ func TestWriteReport(t *testing.T) {
|
||||
leaks: leaks,
|
||||
reportFile: reportCSV,
|
||||
fileName: "report.csv",
|
||||
description: "can we write a file",
|
||||
description: "can we write a csv file",
|
||||
testOpts: Options{
|
||||
Report: reportCSV,
|
||||
CSV: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
leaks: leaks,
|
||||
reportFile: reportJASON,
|
||||
fileName: "report.jason",
|
||||
description: "bad file",
|
||||
expectedErrMsg: "Report should be a .json or .csv file",
|
||||
testOpts: Options{
|
||||
Report: reportJASON,
|
||||
},
|
||||
},
|
||||
{
|
||||
leaks: leaks,
|
||||
reportFile: reportVOID,
|
||||
fileName: "report.jason",
|
||||
description: "bad dir",
|
||||
expectedErrMsg: "thereIsNoWay does not exist",
|
||||
testOpts: Options{
|
||||
Report: reportVOID,
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -353,9 +384,14 @@ func TestWriteReport(t *testing.T) {
|
||||
g.Describe("TestWriteReport", func() {
|
||||
g.It(test.description, func() {
|
||||
opts = test.testOpts
|
||||
writeReport(test.leaks)
|
||||
f, _ := os.Stat(test.reportFile)
|
||||
g.Assert(f.Name()).Equal(test.fileName)
|
||||
err := optsGuard()
|
||||
if err != nil {
|
||||
g.Assert(err.Error()).Equal(test.expectedErrMsg)
|
||||
} else {
|
||||
writeReport(test.leaks)
|
||||
f, _ := os.Stat(test.reportFile)
|
||||
g.Assert(f.Name()).Equal(test.fileName)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -491,14 +527,12 @@ func TestAuditRepo(t *testing.T) {
|
||||
numLeaks: 0,
|
||||
},
|
||||
{
|
||||
// note this double counts the first commit since we are whitelisting
|
||||
// a "bad" first commit
|
||||
repo: leaksRepo,
|
||||
description: "two leaks present whitelist bad commit",
|
||||
whiteListCommits: map[string]bool{
|
||||
"eaeffdc65b4c73ccb67e75d96bd8743be2c85973": true,
|
||||
},
|
||||
numLeaks: 2,
|
||||
numLeaks: 1,
|
||||
},
|
||||
{
|
||||
repo: leaksRepo,
|
||||
@@ -530,12 +564,10 @@ func TestAuditRepo(t *testing.T) {
|
||||
numLeaks: 0,
|
||||
},
|
||||
{
|
||||
// note this double counts the first commit since we are whitelisting
|
||||
// a "bad" first commit
|
||||
repo: leaksRepo,
|
||||
description: "toml whitelist commit",
|
||||
configPath: path.Join(configsDir, "commit"),
|
||||
numLeaks: 2,
|
||||
numLeaks: 1,
|
||||
},
|
||||
{
|
||||
repo: leaksRepo,
|
||||
@@ -551,6 +583,38 @@ func TestAuditRepo(t *testing.T) {
|
||||
numLeaks: 0,
|
||||
configPath: path.Join(configsDir, "repo"),
|
||||
},
|
||||
{
|
||||
repo: leaksRepo,
|
||||
description: "leaks present with entropy",
|
||||
testOpts: Options{
|
||||
Entropy: 4.7,
|
||||
},
|
||||
numLeaks: 7,
|
||||
},
|
||||
{
|
||||
repo: leaksRepo,
|
||||
description: "Audit until specific commit",
|
||||
numLeaks: 1,
|
||||
testOpts: Options{
|
||||
Commit: "f6839959b7bbdcd23008f1fb16f797f35bcd3a0c",
|
||||
},
|
||||
},
|
||||
{
|
||||
repo: leaksRepo,
|
||||
description: "commit depth = 1, no leaks",
|
||||
numLeaks: 0,
|
||||
testOpts: Options{
|
||||
Depth: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
repo: leaksRepo,
|
||||
description: "commit depth = 2, one leak",
|
||||
numLeaks: 1,
|
||||
testOpts: Options{
|
||||
Depth: 2,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
whiteListCommits = make(map[string]bool)
|
||||
@@ -677,6 +741,14 @@ func TestOptionGuard(t *testing.T) {
|
||||
description: "single search regex gaurd",
|
||||
expectedErrMsg: "",
|
||||
},
|
||||
{
|
||||
testOpts: Options{
|
||||
GithubOrg: "fakeOrg",
|
||||
Entropy: 9,
|
||||
},
|
||||
description: "Invalid entropy level guard",
|
||||
expectedErrMsg: "The maximum level of entropy is 8",
|
||||
},
|
||||
}
|
||||
g := goblin.Goblin(t)
|
||||
for _, test := range tests {
|
||||
@@ -841,22 +913,6 @@ func BenchmarkAuditRepo1000Proc(b *testing.B) {
|
||||
auditGitRepo(benchmarkRepo)
|
||||
}
|
||||
}
|
||||
func BenchmarkAuditRepo10000Proc(b *testing.B) {
|
||||
loadToml()
|
||||
opts.MaxGoRoutines = 10000
|
||||
benchmarkRepo = getBenchmarkRepo()
|
||||
for n := 0; n < b.N; n++ {
|
||||
auditGitRepo(benchmarkRepo)
|
||||
}
|
||||
}
|
||||
func BenchmarkAuditRepo100000Proc(b *testing.B) {
|
||||
loadToml()
|
||||
opts.MaxGoRoutines = 100000
|
||||
benchmarkRepo = getBenchmarkRepo()
|
||||
for n := 0; n < b.N; n++ {
|
||||
auditGitRepo(benchmarkRepo)
|
||||
}
|
||||
}
|
||||
func BenchmarkAuditLeakRepo1Proc(b *testing.B) {
|
||||
loadToml()
|
||||
opts.MaxGoRoutines = 1
|
||||
@@ -917,21 +973,3 @@ func BenchmarkAuditLeakRepo1000Proc(b *testing.B) {
|
||||
auditGitRepo(benchmarkRepo)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAuditLeakRepo10000Proc(b *testing.B) {
|
||||
loadToml()
|
||||
opts.MaxGoRoutines = 10000
|
||||
benchmarkLeaksRepo = getBenchmarkLeaksRepo()
|
||||
for n := 0; n < b.N; n++ {
|
||||
auditGitRepo(benchmarkRepo)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAuditLeakRepo100000Proc(b *testing.B) {
|
||||
loadToml()
|
||||
opts.MaxGoRoutines = 100000
|
||||
benchmarkLeaksRepo = getBenchmarkLeaksRepo()
|
||||
for n := 0; n < b.N; n++ {
|
||||
auditGitRepo(benchmarkRepo)
|
||||
}
|
||||
}
|
||||
@@ -5,8 +5,10 @@ import (
|
||||
"crypto/md5"
|
||||
"encoding/csv"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -29,14 +31,13 @@ import (
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/google/go-github/github"
|
||||
"github.com/hako/durafmt"
|
||||
flags "github.com/jessevdk/go-flags"
|
||||
log "github.com/sirupsen/logrus"
|
||||
git "gopkg.in/src-d/go-git.v4"
|
||||
)
|
||||
|
||||
// Leak represents a leaked secret or regex match.
|
||||
// Output to stdout as json if the --verbose option is set or
|
||||
// as a csv if the --csv and --report options are set.
|
||||
type Leak struct {
|
||||
Line string `json:"line"`
|
||||
Commit string `json:"commit"`
|
||||
@@ -75,25 +76,27 @@ type Options struct {
|
||||
|
||||
Branch string `short:"b" long:"branch" description:"branch name to audit (defaults to HEAD)"`
|
||||
Commit string `short:"c" long:"commit" description:"sha of commit to stop at"`
|
||||
Depth int `long:"depth" description:"maximum commit depth"`
|
||||
|
||||
// local target option
|
||||
RepoPath string `long:"repo-path" description:"Path to repo"`
|
||||
OwnerPath string `long:"owner-path" description:"Path to owner directory (repos discovered)"`
|
||||
|
||||
// Process options
|
||||
MaxGoRoutines int `long:"max-go" description:"Maximum number of concurrent go-routines gitleaks spawns"`
|
||||
Disk bool `long:"disk" description:"Clones repo(s) to disk"`
|
||||
AuditAllRefs bool `long:"all-refs" description:"run audit on all refs"`
|
||||
SingleSearch string `long:"single-search" description:"single regular expression to search for"`
|
||||
ConfigPath string `long:"config" description:"path to gitleaks config"`
|
||||
SSHKey string `long:"ssh-key" description:"path to ssh key"`
|
||||
MaxGoRoutines int `long:"max-go" description:"Maximum number of concurrent go-routines gitleaks spawns"`
|
||||
Disk bool `long:"disk" description:"Clones repo(s) to disk"`
|
||||
AuditAllRefs bool `long:"all-refs" description:"run audit on all refs"`
|
||||
SingleSearch string `long:"single-search" description:"single regular expression to search for"`
|
||||
ConfigPath string `long:"config" description:"path to gitleaks config"`
|
||||
SSHKey string `long:"ssh-key" description:"path to ssh key"`
|
||||
ExcludeForks bool `long:"exclude-forks" description:"exclude forks for organization/user audits"`
|
||||
Entropy float64 `long:"entropy" short:"e" description:"Include entropy checks during audit. Entropy scale: 0.0(no entropy) - 8.0(max entropy)"`
|
||||
// TODO: IncludeMessages string `long:"messages" description:"include commit messages in audit"`
|
||||
|
||||
// Output options
|
||||
Log string `short:"l" long:"log" description:"log level"`
|
||||
Verbose bool `short:"v" long:"verbose" description:"Show verbose output from gitleaks audit"`
|
||||
Report string `long:"report" description:"path to write report file"`
|
||||
CSV bool `long:"csv" description:"report output to csv"`
|
||||
Report string `long:"report" description:"path to write report file. Needs to be csv or json"`
|
||||
Redact bool `long:"redact" description:"redact secrets from log messages and report"`
|
||||
Version bool `long:"version" description:"version number"`
|
||||
SampleConfig bool `long:"sample-config" description:"prints a sample config file"`
|
||||
@@ -123,7 +126,7 @@ type gitDiff struct {
|
||||
}
|
||||
|
||||
const defaultGithubURL = "https://api.github.com/"
|
||||
const version = "1.8.0"
|
||||
const version = "1.12.1"
|
||||
const errExit = 2
|
||||
const leakExit = 1
|
||||
const defaultConfig = `
|
||||
@@ -193,6 +196,7 @@ var (
|
||||
sshAuth *ssh.PublicKeys
|
||||
dir string
|
||||
maxGo int
|
||||
totalCommits int64
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -203,7 +207,20 @@ func init() {
|
||||
}
|
||||
|
||||
func main() {
|
||||
_, err := flags.Parse(&opts)
|
||||
parser := flags.NewParser(&opts, flags.Default)
|
||||
_, err := parser.Parse()
|
||||
|
||||
if err != nil {
|
||||
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
if len(os.Args) == 1 {
|
||||
parser.WriteHelp(os.Stdout)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if opts.Version {
|
||||
fmt.Println(version)
|
||||
os.Exit(0)
|
||||
@@ -212,6 +229,7 @@ func main() {
|
||||
fmt.Println(defaultConfig)
|
||||
os.Exit(0)
|
||||
}
|
||||
now := time.Now()
|
||||
leaks, err := run()
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
@@ -221,8 +239,9 @@ func main() {
|
||||
writeReport(leaks)
|
||||
}
|
||||
|
||||
log.Infof("%d commits inspected in %s", totalCommits, durafmt.Parse(time.Now().Sub(now)).String())
|
||||
if len(leaks) != 0 {
|
||||
log.Warnf("leaks detected")
|
||||
log.Warnf("%d leaks detected", len(leaks))
|
||||
os.Exit(leakExit)
|
||||
}
|
||||
}
|
||||
@@ -293,7 +312,7 @@ func run() ([]Leak, error) {
|
||||
func writeReport(leaks []Leak) error {
|
||||
var err error
|
||||
log.Infof("writing report to %s", opts.Report)
|
||||
if opts.CSV {
|
||||
if strings.HasSuffix(opts.Report, ".csv") {
|
||||
f, err := os.Create(opts.Report)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -428,13 +447,14 @@ func auditGitRepo(repo *RepoDescriptor) ([]Leak, error) {
|
||||
// the --max-go option (default is set to the number of cores on your cpu).
|
||||
func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
|
||||
var (
|
||||
err error
|
||||
prevCommit *object.Commit
|
||||
semaphore chan bool
|
||||
repoName string
|
||||
leaks []Leak
|
||||
commitWg sync.WaitGroup
|
||||
mutex = &sync.Mutex{}
|
||||
err error
|
||||
prevCommit *object.Commit
|
||||
semaphore chan bool
|
||||
repoName string
|
||||
leaks []Leak
|
||||
commitWg sync.WaitGroup
|
||||
mutex = &sync.Mutex{}
|
||||
commitCount int
|
||||
)
|
||||
repoName = repo.name
|
||||
if opts.MaxGoRoutines != 0 {
|
||||
@@ -447,13 +467,25 @@ func auditGitReference(repo *RepoDescriptor, ref *plumbing.Reference) []Leak {
|
||||
return nil
|
||||
}
|
||||
err = cIter.ForEach(func(c *object.Commit) error {
|
||||
if c.Hash.String() == opts.Commit {
|
||||
if c.Hash.String() == opts.Commit || (opts.Depth != 0 && commitCount == opts.Depth) {
|
||||
cIter.Close()
|
||||
return errors.New("ErrStop")
|
||||
}
|
||||
commitCount = commitCount + 1
|
||||
totalCommits = totalCommits + 1
|
||||
if whiteListCommits[c.Hash.String()] {
|
||||
prevCommit = c
|
||||
log.Infof("skipping commit: %s\n", c.Hash.String())
|
||||
return nil
|
||||
}
|
||||
if prevCommit != nil {
|
||||
if whiteListCommits[prevCommit.Hash.String()] {
|
||||
prevCommit = c
|
||||
log.Infof("skipping commit: %s\n", c.Hash.String())
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
commitWg.Add(1)
|
||||
semaphore <- true
|
||||
go func(c *object.Commit, prevCommit *object.Commit) {
|
||||
@@ -575,30 +607,68 @@ func inspect(diff gitDiff) []Leak {
|
||||
break
|
||||
}
|
||||
|
||||
leak := Leak{
|
||||
Line: line,
|
||||
Commit: diff.commit.Hash.String(),
|
||||
Offender: match,
|
||||
Type: leakType,
|
||||
Message: diff.commit.Message,
|
||||
Author: diff.commit.Author.String(),
|
||||
File: diff.filePath,
|
||||
Branch: diff.branchName,
|
||||
Repo: diff.repoName,
|
||||
leaks = addLeak(leaks, line, match, leakType, diff)
|
||||
}
|
||||
|
||||
if opts.Entropy > 0 {
|
||||
words := strings.Fields(line)
|
||||
for _, word := range words {
|
||||
if getShannonEntropy(word) >= opts.Entropy {
|
||||
leaks = addLeak(leaks, line, word, "High Entropy", diff)
|
||||
}
|
||||
}
|
||||
if opts.Redact {
|
||||
leak.Offender = "REDACTED"
|
||||
leak.Line = "REDACTED"
|
||||
}
|
||||
if opts.Verbose {
|
||||
leak.log()
|
||||
}
|
||||
leaks = append(leaks, leak)
|
||||
}
|
||||
}
|
||||
return leaks
|
||||
}
|
||||
|
||||
// getShannonEntropy https://en.wiktionary.org/wiki/Shannon_entropy
|
||||
func getShannonEntropy(data string) (entropy float64) {
|
||||
if data == "" {
|
||||
return 0
|
||||
}
|
||||
|
||||
charCounts := make(map[rune]int)
|
||||
for _, char := range data {
|
||||
charCounts[char]++
|
||||
}
|
||||
|
||||
invLength := 1.0 / float64(len(data))
|
||||
for _, count := range charCounts {
|
||||
freq := float64(count) * invLength
|
||||
entropy -= freq * math.Log2(freq)
|
||||
}
|
||||
|
||||
return entropy
|
||||
}
|
||||
|
||||
// addLeak is helper for func inspect() to append leaks if found during a diff check.
|
||||
func addLeak(leaks []Leak, line string, offender string, leakType string, diff gitDiff) []Leak {
|
||||
leak := Leak{
|
||||
Line: line,
|
||||
Commit: diff.commit.Hash.String(),
|
||||
Offender: offender,
|
||||
Type: leakType,
|
||||
Message: diff.commit.Message,
|
||||
Author: diff.commit.Author.String(),
|
||||
File: diff.filePath,
|
||||
Branch: diff.branchName,
|
||||
Repo: diff.repoName,
|
||||
}
|
||||
|
||||
if opts.Redact {
|
||||
leak.Offender = "REDACTED"
|
||||
leak.Line = "REDACTED"
|
||||
}
|
||||
|
||||
if opts.Verbose {
|
||||
leak.log()
|
||||
}
|
||||
|
||||
leaks = append(leaks, leak)
|
||||
return leaks
|
||||
}
|
||||
|
||||
// auditGithubRepos kicks off audits if --github-user or --github-org options are set.
|
||||
// First, we gather all the github repositories from the github api (this doesnt actually clone the repo).
|
||||
// After all the repos have been pulled from github's api we proceed to audit the repos by calling auditGithubRepo.
|
||||
@@ -715,6 +785,9 @@ func cloneGithubRepo(githubRepo *github.Repository) (*RepoDescriptor, error) {
|
||||
repo *git.Repository
|
||||
err error
|
||||
)
|
||||
if opts.ExcludeForks && githubRepo.GetFork() {
|
||||
return nil, fmt.Errorf("skipping %s, excluding forks", *githubRepo.Name)
|
||||
}
|
||||
for _, repoName := range whiteListRepos {
|
||||
if repoName == *githubRepo.Name {
|
||||
return nil, fmt.Errorf("skipping %s, whitelisted", repoName)
|
||||
@@ -862,6 +935,19 @@ func optsGuard() error {
|
||||
}
|
||||
}
|
||||
|
||||
if opts.Entropy > 8 {
|
||||
return fmt.Errorf("The maximum level of entropy is 8")
|
||||
}
|
||||
if opts.Report != "" {
|
||||
if !strings.HasSuffix(opts.Report, ".json") && !strings.HasSuffix(opts.Report, ".csv") {
|
||||
return fmt.Errorf("Report should be a .json or .csv file")
|
||||
}
|
||||
dirPath := filepath.Dir(opts.Report)
|
||||
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
|
||||
return fmt.Errorf("%s does not exist", dirPath)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
[568vq].out
|
||||
|
||||
*.cgo1.go
|
||||
*.cgo2.c
|
||||
_cgo_defun.c
|
||||
_cgo_gotypes.go
|
||||
_cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
||||
*.test
|
||||
*.prof
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.5
|
||||
- 1.6
|
||||
- 1.7
|
||||
- 1.8
|
||||
- 1.9
|
||||
- tip
|
||||
|
||||
before_install:
|
||||
- go get golang.org/x/tools/cmd/cover
|
||||
|
||||
script:
|
||||
- GOARCH=386 go test # test 32bit architectures.
|
||||
- go test -coverprofile=coverage.txt -covermode=atomic
|
||||
|
||||
after_success:
|
||||
- bash <(curl -s https://codecov.io/bash)
|
||||
|
||||
sudo: false
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
# Contributor Covenant Code of Conduct
|
||||
|
||||
## Our Pledge
|
||||
|
||||
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
||||
|
||||
## Our Standards
|
||||
|
||||
Examples of behavior that contributes to creating a positive environment include:
|
||||
|
||||
* Using welcoming and inclusive language
|
||||
* Being respectful of differing viewpoints and experiences
|
||||
* Gracefully accepting constructive criticism
|
||||
* Focusing on what is best for the community
|
||||
* Showing empathy towards other community members
|
||||
|
||||
Examples of unacceptable behavior by participants include:
|
||||
|
||||
* The use of sexualized language or imagery and unwelcome sexual attention or advances
|
||||
* Trolling, insulting/derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or electronic address, without explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a professional setting
|
||||
|
||||
## Our Responsibilities
|
||||
|
||||
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
|
||||
|
||||
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at wesley@hakobaito.co.uk. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
|
||||
|
||||
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
|
||||
|
||||
[homepage]: http://contributor-covenant.org
|
||||
[version]: http://contributor-covenant.org/version/1/4/
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
# Contributing
|
||||
|
||||
Contributions are welcome! Fork this repo and add your changes and submit a PR.
|
||||
|
||||
If you would like to fix a bug, add a feature or provide feedback you can do so in the issues section.
|
||||
|
||||
You can run tests by runnning `go test`. Running `go test; go vet; golint` is recommended.
|
||||
|
||||
durafmt is also tested against `gometalinter`.
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Wesley Hill
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
# durafmt
|
||||
|
||||
[](https://travis-ci.org/hako/durafmt) [](https://goreportcard.com/report/github.com/hako/durafmt) [](https://codecov.io/gh/hako/durafmt) [](https://godoc.org/github.com/hako/durafmt)
|
||||
[](https://www.codetriage.com/hako/durafmt)
|
||||
|
||||
|
||||
|
||||
durafmt is a tiny Go library that formats `time.Duration` strings into a human readable format.
|
||||
|
||||
```
|
||||
go get github.com/hako/durafmt
|
||||
```
|
||||
|
||||
# Why
|
||||
|
||||
If you've worked with `time.Duration` in Go, you most likely have come across this:
|
||||
|
||||
```
|
||||
53m28.587093086s // :)
|
||||
```
|
||||
|
||||
The above seems very easy to read, unless your duration looks like this:
|
||||
|
||||
```
|
||||
354h22m3.24s // :S
|
||||
```
|
||||
|
||||
# Usage
|
||||
|
||||
### durafmt.ParseString()
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hako/durafmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
duration, err := durafmt.ParseString("354h22m3.24s")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(duration) // 2 weeks 18 hours 22 minutes 3 seconds
|
||||
// duration.String() // String representation. "2 weeks 18 hours 22 minutes 3 seconds"
|
||||
}
|
||||
```
|
||||
|
||||
### durafmt.ParseStringShort()
|
||||
|
||||
Version of `durafmt.ParseString()` that only returns the first part of the duration string.
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/hako/durafmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
duration, err := durafmt.ParseStringShort("354h22m3.24s")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(duration) // 2 weeks
|
||||
// duration.String() // String short representation. "2 weeks"
|
||||
}
|
||||
```
|
||||
|
||||
### durafmt.Parse()
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"github.com/hako/durafmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
timeduration := (354 * time.Hour) + (22 * time.Minute) + (3 * time.Second)
|
||||
duration := durafmt.Parse(timeduration).String()
|
||||
fmt.Println(duration) // 2 weeks 18 hours 22 minutes 3 seconds
|
||||
}
|
||||
```
|
||||
|
||||
### durafmt.ParseShort()
|
||||
|
||||
Version of `durafmt.Parse()` that only returns the first part of the duration string.
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"github.com/hako/durafmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
timeduration := (354 * time.Hour) + (22 * time.Minute) + (3 * time.Second)
|
||||
duration := durafmt.ParseShort(timeduration).String()
|
||||
fmt.Println(duration) // 2 weeks
|
||||
}
|
||||
```
|
||||
|
||||
# Contributing
|
||||
|
||||
Contributions are welcome! Fork this repo and add your changes and submit a PR.
|
||||
|
||||
If you would like to fix a bug, add a feature or provide feedback you can do so in the issues section.
|
||||
|
||||
You can run tests by runnning `go test`. Running `go test; go vet; golint` is recommended.
|
||||
|
||||
durafmt is also tested against `gometalinter`.
|
||||
|
||||
# License
|
||||
|
||||
MIT
|
||||
+147
@@ -0,0 +1,147 @@
|
||||
// Package durafmt formats time.Duration into a human readable format.
|
||||
package durafmt
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
units = []string{"years", "weeks", "days", "hours", "minutes", "seconds", "milliseconds"}
|
||||
)
|
||||
|
||||
// Durafmt holds the parsed duration and the original input duration.
|
||||
type Durafmt struct {
|
||||
duration time.Duration
|
||||
input string // Used as reference.
|
||||
short bool
|
||||
}
|
||||
|
||||
// Parse creates a new *Durafmt struct, returns error if input is invalid.
|
||||
func Parse(dinput time.Duration) *Durafmt {
|
||||
input := dinput.String()
|
||||
return &Durafmt{dinput, input, false}
|
||||
}
|
||||
|
||||
// ParseShort creates a new *Durafmt struct, short form, returns error if input is invalid.
|
||||
func ParseShort(dinput time.Duration) *Durafmt {
|
||||
input := dinput.String()
|
||||
return &Durafmt{dinput, input, true}
|
||||
}
|
||||
|
||||
// ParseString creates a new *Durafmt struct from a string.
|
||||
// returns an error if input is invalid.
|
||||
func ParseString(input string) (*Durafmt, error) {
|
||||
if input == "0" || input == "-0" {
|
||||
return nil, errors.New("durafmt: missing unit in duration " + input)
|
||||
}
|
||||
duration, err := time.ParseDuration(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Durafmt{duration, input, false}, nil
|
||||
}
|
||||
|
||||
// ParseStringShort creates a new *Durafmt struct from a string, short form
|
||||
// returns an error if input is invalid.
|
||||
func ParseStringShort(input string) (*Durafmt, error) {
|
||||
if input == "0" || input == "-0" {
|
||||
return nil, errors.New("durafmt: missing unit in duration " + input)
|
||||
}
|
||||
duration, err := time.ParseDuration(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Durafmt{duration, input, true}, nil
|
||||
}
|
||||
|
||||
// String parses d *Durafmt into a human readable duration.
|
||||
func (d *Durafmt) String() string {
|
||||
var duration string
|
||||
|
||||
// Check for minus durations.
|
||||
if string(d.input[0]) == "-" {
|
||||
duration += "-"
|
||||
d.duration = -d.duration
|
||||
}
|
||||
|
||||
// Convert duration.
|
||||
seconds := int64(d.duration.Seconds()) % 60
|
||||
minutes := int64(d.duration.Minutes()) % 60
|
||||
hours := int64(d.duration.Hours()) % 24
|
||||
days := int64(d.duration/(24*time.Hour)) % 365 % 7
|
||||
|
||||
// Edge case between 364 and 365 days.
|
||||
// We need to calculate weeks from what is left from years
|
||||
leftYearDays := int64(d.duration/(24*time.Hour)) % 365
|
||||
weeks := leftYearDays / 7
|
||||
if leftYearDays >= 364 && leftYearDays < 365 {
|
||||
weeks = 52
|
||||
}
|
||||
|
||||
years := int64(d.duration/(24*time.Hour)) / 365
|
||||
milliseconds := int64(d.duration/time.Millisecond) -
|
||||
(seconds * 1000) - (minutes * 60000) - (hours * 3600000) -
|
||||
(days * 86400000) - (weeks * 604800000) - (years * 31536000000)
|
||||
|
||||
// Create a map of the converted duration time.
|
||||
durationMap := map[string]int64{
|
||||
"milliseconds": milliseconds,
|
||||
"seconds": seconds,
|
||||
"minutes": minutes,
|
||||
"hours": hours,
|
||||
"days": days,
|
||||
"weeks": weeks,
|
||||
"years": years,
|
||||
}
|
||||
|
||||
// Construct duration string.
|
||||
for _, u := range units {
|
||||
v := durationMap[u]
|
||||
strval := strconv.FormatInt(v, 10)
|
||||
switch {
|
||||
// add to the duration string if v > 1.
|
||||
case v > 1:
|
||||
duration += strval + " " + u + " "
|
||||
// remove the plural 's', if v is 1.
|
||||
case v == 1:
|
||||
duration += strval + " " + strings.TrimRight(u, "s") + " "
|
||||
// omit any value with 0s or 0.
|
||||
case d.duration.String() == "0" || d.duration.String() == "0s":
|
||||
// note: milliseconds and minutes have the same suffix (m)
|
||||
// so we have to check if the units match with the suffix.
|
||||
|
||||
// check for a suffix that is NOT the milliseconds suffix.
|
||||
if strings.HasSuffix(d.input, string(u[0])) && !strings.Contains(d.input, "ms") {
|
||||
// if it happens that the units are milliseconds, skip.
|
||||
if u == "milliseconds" {
|
||||
continue
|
||||
}
|
||||
duration += strval + " " + u
|
||||
}
|
||||
// process milliseconds here.
|
||||
if u == "milliseconds" {
|
||||
if strings.Contains(d.input, "ms") {
|
||||
duration += strval + " " + u
|
||||
break
|
||||
}
|
||||
}
|
||||
break
|
||||
// omit any value with 0.
|
||||
case v == 0:
|
||||
continue
|
||||
}
|
||||
}
|
||||
// trim any remaining spaces.
|
||||
duration = strings.TrimSpace(duration)
|
||||
|
||||
// if more than 2 spaces present return the first 2 strings
|
||||
// if short version is requested
|
||||
if d.short {
|
||||
duration = strings.Join(strings.Split(duration, " ")[:2], " ")
|
||||
}
|
||||
|
||||
return duration
|
||||
}
|
||||
Reference in new issue
Block a user