mirror of
https://github.com/gitleaks/gitleaks.git
synced 2026-11-28 20:24:22 +01:00
refactoring after actually learning some go patterns
This commit is contained in:
1 parent
c8880ed911
commit
faca0e14d9
6 files changed
+549
-350
No files matched your search
@@ -1,20 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/google/go-github/github"
|
||||
_ "fmt"
|
||||
"github.com/mitchellh/go-homedir"
|
||||
"golang.org/x/oauth2"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
_"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"go.uber.org/zap"
|
||||
_"time"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
const EXIT_CLEAN = 0
|
||||
const EXIT_FAILURE = 1
|
||||
const EXIT_LEAKS = 2
|
||||
|
||||
var (
|
||||
regexes map[string]*regexp.Regexp
|
||||
stopWords []string
|
||||
@@ -25,29 +27,13 @@ var (
|
||||
gitLeaksPath string
|
||||
gitLeaksClonePath string
|
||||
gitLeaksReportPath string
|
||||
logger *zap.Logger
|
||||
)
|
||||
|
||||
type RepoDesc struct {
|
||||
name string
|
||||
url string
|
||||
path string
|
||||
owner *Owner
|
||||
}
|
||||
|
||||
type Owner struct {
|
||||
name string
|
||||
url string
|
||||
accountType string
|
||||
path string
|
||||
reportPath string
|
||||
}
|
||||
|
||||
func init() {
|
||||
base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
|
||||
hexChars = "1234567890abcdefABCDEF"
|
||||
|
||||
stopWords = []string{"setting", "info", "env", "environment"}
|
||||
|
||||
regexes = map[string]*regexp.Regexp{
|
||||
"PKCS8": regexp.MustCompile("-----BEGIN PRIVATE KEY-----"),
|
||||
"RSA": regexp.MustCompile("-----BEGIN RSA PRIVATE KEY-----"),
|
||||
@@ -60,14 +46,19 @@ func init() {
|
||||
"Heroku": regexp.MustCompile("(?i)heroku.*[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}"),
|
||||
// "Custom": regexp.MustCompile(".*")
|
||||
}
|
||||
assignRegex = regexp.MustCompile(`(=|:|:=|<-)`)
|
||||
fileDiffRegex = regexp.MustCompile("diff --git a.+b/")
|
||||
homeDir, err := homedir.Dir()
|
||||
if err != nil {
|
||||
log.Fatal("Cant find home dir")
|
||||
assignRegex = regexp.MustCompile(`(=|:|:=|<-)`)
|
||||
|
||||
// gitleaks dir defaults to $HOME/.gitleaks if no env var GITLEAKS_HOME is present.
|
||||
gitLeaksPath = os.Getenv("GITLEAKS_HOME")
|
||||
if gitLeaksPath == "" {
|
||||
homeDir, err := homedir.Dir()
|
||||
if err != nil {
|
||||
log.Fatal("Cant find home dir")
|
||||
}
|
||||
gitLeaksPath = filepath.Join(homeDir, ".gitleaks")
|
||||
}
|
||||
|
||||
gitLeaksPath = filepath.Join(homeDir, ".gitleaks")
|
||||
if _, err := os.Stat(gitLeaksPath); os.IsNotExist(err) {
|
||||
os.Mkdir(gitLeaksPath, os.ModePerm)
|
||||
}
|
||||
@@ -81,141 +72,25 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
// getOwner
|
||||
func getOwner(opts *Options) *Owner {
|
||||
var owner Owner
|
||||
if opts.RepoURL != "" {
|
||||
splitSlashes := strings.Split(opts.RepoURL, "/")
|
||||
owner = Owner{
|
||||
name: splitSlashes[len(splitSlashes)-2],
|
||||
url: opts.RepoURL,
|
||||
accountType: "users",
|
||||
}
|
||||
|
||||
} else if opts.UserURL != "" {
|
||||
_, ownerName := path.Split(opts.UserURL)
|
||||
owner = Owner{
|
||||
name: ownerName,
|
||||
url: opts.UserURL,
|
||||
accountType: "user",
|
||||
}
|
||||
} else if opts.OrgURL != "" {
|
||||
_, ownerName := path.Split(opts.OrgURL)
|
||||
owner = Owner{
|
||||
name: ownerName,
|
||||
url: opts.OrgURL,
|
||||
accountType: "org",
|
||||
}
|
||||
}
|
||||
|
||||
if opts.Tmp {
|
||||
dir, err := ioutil.TempDir("", owner.name)
|
||||
if err != nil {
|
||||
log.Fatal("Cant make temp dir")
|
||||
}
|
||||
owner.path = dir
|
||||
} else {
|
||||
owner.path = filepath.Join(gitLeaksClonePath, owner.name)
|
||||
if _, err := os.Stat(owner.path); os.IsNotExist(err) {
|
||||
os.Mkdir(owner.path, os.ModePerm)
|
||||
}
|
||||
}
|
||||
owner.reportPath = filepath.Join(gitLeaksPath, "report", owner.name)
|
||||
return &owner
|
||||
}
|
||||
|
||||
// getRepos
|
||||
func getRepos(opts *Options, owner *Owner) []RepoDesc {
|
||||
var (
|
||||
allRepos []*github.Repository
|
||||
repos []*github.Repository
|
||||
repoDescs []RepoDesc
|
||||
resp *github.Response
|
||||
ctx = context.Background()
|
||||
err error
|
||||
)
|
||||
if opts.RepoURL != "" {
|
||||
_, repoName := path.Split(opts.RepoURL)
|
||||
if strings.HasSuffix(repoName, ".git") {
|
||||
repoName = repoName[:len(repoName)-4]
|
||||
}
|
||||
ownerPath := filepath.Join(owner.path, repoName)
|
||||
repo := RepoDesc{
|
||||
name: repoName,
|
||||
url: opts.RepoURL,
|
||||
owner: owner,
|
||||
path: ownerPath}
|
||||
repoDescs = append(repoDescs, repo)
|
||||
return repoDescs
|
||||
}
|
||||
|
||||
tokenClient := getAccessToken(opts)
|
||||
gitClient := github.NewClient(tokenClient)
|
||||
|
||||
// TODO include fork check
|
||||
orgOpt := &github.RepositoryListByOrgOptions{
|
||||
ListOptions: github.ListOptions{PerPage: 10},
|
||||
}
|
||||
userOpt := &github.RepositoryListOptions{
|
||||
ListOptions: github.ListOptions{PerPage: 10},
|
||||
}
|
||||
|
||||
for {
|
||||
if opts.UserURL != "" {
|
||||
repos, resp, err = gitClient.Repositories.List(
|
||||
ctx, owner.name, userOpt)
|
||||
} else if opts.OrgURL != "" {
|
||||
repos, resp, err = gitClient.Repositories.ListByOrg(
|
||||
ctx, owner.name, orgOpt)
|
||||
}
|
||||
allRepos = append(allRepos, repos...)
|
||||
if resp.NextPage == 0 || err != nil {
|
||||
break
|
||||
}
|
||||
|
||||
for _, repo := range repos {
|
||||
repoPath := filepath.Join(owner.path, *repo.Name)
|
||||
repoDescs = append(repoDescs,
|
||||
RepoDesc{
|
||||
name: *repo.Name,
|
||||
url: *repo.CloneURL,
|
||||
owner: owner,
|
||||
path: repoPath})
|
||||
}
|
||||
|
||||
orgOpt.Page = resp.NextPage
|
||||
userOpt.Page = resp.NextPage
|
||||
}
|
||||
|
||||
return repoDescs
|
||||
}
|
||||
|
||||
// getAccessToken checks
|
||||
// 1. option
|
||||
// 2. env var
|
||||
// TODO. $HOME/.gitleaks/.creds
|
||||
func getAccessToken(opts *Options) *http.Client {
|
||||
var token string
|
||||
if opts.Token != "" {
|
||||
token = opts.Token
|
||||
} else {
|
||||
token = os.Getenv("GITHUB_TOKEN")
|
||||
}
|
||||
if token == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
tokenService := oauth2.StaticTokenSource(
|
||||
&oauth2.Token{AccessToken: token},
|
||||
)
|
||||
tokenClient := oauth2.NewClient(context.Background(), tokenService)
|
||||
return tokenClient
|
||||
}
|
||||
|
||||
func main() {
|
||||
// TODO abstract logging
|
||||
atom := zap.NewAtomicLevel()
|
||||
encoderCfg := zap.NewProductionEncoderConfig()
|
||||
encoderCfg.TimeKey = ""
|
||||
logger = zap.New(zapcore.NewCore(
|
||||
zapcore.NewJSONEncoder(encoderCfg),
|
||||
zapcore.Lock(os.Stdout),
|
||||
atom,
|
||||
))
|
||||
logger.Info("HEY")
|
||||
atom.SetLevel(zap.InfoLevel)
|
||||
logger.Info("HEY")
|
||||
|
||||
|
||||
args := os.Args[1:]
|
||||
opts := parseOptions(args)
|
||||
owner := getOwner(opts)
|
||||
repos := getRepos(opts, owner)
|
||||
start(repos, owner, opts)
|
||||
owner := newOwner(opts)
|
||||
owner.auditRepos(opts)
|
||||
// repos := getRepos(opts, owner)
|
||||
// start(repos, owner, opts)
|
||||
}
|
||||
Reference in new issue
Block a user