Skip to content

Commit

Permalink
feat: Adding more rules
Browse files Browse the repository at this point in the history
  • Loading branch information
owenrumney committed Apr 22, 2022
1 parent 9e2cf3e commit b215911
Show file tree
Hide file tree
Showing 27 changed files with 564 additions and 152 deletions.
131 changes: 3 additions & 128 deletions cmd/squealer/main.go
Original file line number Diff line number Diff line change
@@ -1,139 +1,14 @@
package main

import (
"fmt"
"math"
"os"

"github.com/owenrumney/squealer/internal/app/squealer/cmd"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/owenrumney/squealer/internal/app/squealer/formatters"
"github.com/owenrumney/squealer/internal/app/squealer/mertics"
"github.com/owenrumney/squealer/internal/app/squealer/scan"
"github.com/owenrumney/squealer/pkg/config"
)

var rootcmd = &cobra.Command{
Use: "squealer",
Short: "Search for secrets and squeal about them",
Long: `Telling tales on your secret leaking`,
Run: squeal,
}

var (
redacted = false
concise = false
noGit = false
debug = false
everything = false
configFilePath string
fromHash string
toHash string
commitListFile string
format string
"os"
)

func init() {
log.SetFormatter(&log.TextFormatter{})
log.SetOutput(os.Stderr)
log.SetLevel(log.InfoLevel)
}

func squeal(_ *cobra.Command, args []string) {
if concise {
log.SetLevel(log.FatalLevel)
}

if debug {
log.SetLevel(log.DebugLevel)
}

var basePath = "./"
if len(args) > 0 {
basePath = args[0]
}
cfg, err := config.LoadConfig(configFilePath)
if err != nil {
fail(err)
}

scanner := getScanner(cfg, basePath)
transgressions, err := scanner.Scan()
if err != nil {
fail(err)
}

output, err := formatters.GetFormatter(format).PrintTransgressions(transgressions, redacted)
if err != nil {
log.WithError(err).Error(err.Error())
}

fmt.Printf(output)

metrics := scanner.GetMetrics()
if !concise {
_, _ = fmt.Fprint(os.Stderr, printMetrics(metrics))
}

exitCode := int(math.Min(float64(metrics.TransgressionsReported), 1))

log.Infof("Exit code: %d", exitCode)
os.Exit(exitCode)
}

func getScanner(cfg *config.Config, basePath string) scan.Scanner {
scanner, err := scan.NewScanner(scan.ScannerConfig{
Cfg: cfg,
Basepath: basePath,
Redacted: redacted,
NoGit: noGit,
FromHash: fromHash,
ToHash: toHash,
Everything: everything,
CommitListFile: commitListFile,
})
if err != nil {
fail(err)
}
return scanner
}

func printMetrics(metrics *mertics.Metrics) string {
duration, _ := metrics.Duration()
return fmt.Sprintf(`
Processing:
duration: %4.2fs
commits: %d
commit files: %d
transgressionMap:
identified: %d
ignored: %d
reported: %d
`,
duration,
metrics.CommitsProcessed,
metrics.FilesProcessed,
metrics.TransgressionsFound,
metrics.TransgressionsIgnored,
metrics.TransgressionsReported)
}

func main() {
rootcmd.PersistentFlags().BoolVar(&redacted, "redacted", redacted, "Display the results redacted.")
rootcmd.PersistentFlags().BoolVar(&concise, "concise", concise, "Reduced output.")
rootcmd.PersistentFlags().BoolVar(&noGit, "no-git", noGit, "Scan as a directory rather than a git history.")
rootcmd.PersistentFlags().BoolVar(&debug, "debug", debug, "Include debug output.")
rootcmd.PersistentFlags().BoolVar(&everything, "everything", everything, "Scan all commits.... everywhere.")
rootcmd.PersistentFlags().StringVar(&configFilePath, "config-file", configFilePath, "Path to the config file with the rules.")
rootcmd.PersistentFlags().StringVar(&fromHash, "from-hash", fromHash, "The hash to work back to from the starting hash.")
rootcmd.PersistentFlags().StringVar(&toHash, "to-hash", toHash, "The most recent hash to start with.")
rootcmd.PersistentFlags().StringVar(&format, "output-format", format, "The format that the output should come in (default, json, sarif.")
rootcmd.PersistentFlags().StringVar(&commitListFile, "commits-file", commitListFile, "Provide a file with the commits to check per line (git rev-list master..HEAD)")

if err := rootcmd.Execute(); err != nil {
if err := cmd.Root().Execute(); err != nil {
fail(err)
}
}
Expand Down
142 changes: 142 additions & 0 deletions internal/app/squealer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package cmd

import (
"fmt"
"math"
"os"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/owenrumney/squealer/internal/pkg/formatters"
"github.com/owenrumney/squealer/internal/pkg/mertics"
"github.com/owenrumney/squealer/internal/pkg/scan"
"github.com/owenrumney/squealer/pkg/config"
)

var (
redacted = false
concise = false
noGit = false
debug = false
everything = false
configFilePath string
fromHash string
toHash string
commitListFile string
format string
)

func Root() *cobra.Command {
rootCommand := &cobra.Command{
Use: "squealer",
Short: "Search for secrets and squeal about them",
Long: `Telling tales on your secret leaking`,
RunE: squeal,
}
return rootCommand
}

func init() {

log.SetFormatter(&log.TextFormatter{})
log.SetOutput(os.Stderr)
log.SetLevel(log.InfoLevel)
}

func configureFlags(command *cobra.Command) {
command.PersistentFlags().BoolVar(&redacted, "redacted", redacted, "Display the results redacted.")
command.PersistentFlags().BoolVar(&concise, "concise", concise, "Reduced output.")
command.PersistentFlags().BoolVar(&noGit, "no-git", noGit, "Scan as a directory rather than a git history.")
command.PersistentFlags().BoolVar(&debug, "debug", debug, "Include debug output.")
command.PersistentFlags().BoolVar(&everything, "everything", everything, "Scan all commits.... everywhere.")
command.PersistentFlags().StringVar(&configFilePath, "config-file", configFilePath, "Path to the config file with the rules.")
command.PersistentFlags().StringVar(&fromHash, "from-hash", fromHash, "The hash to work back to from the starting hash.")
command.PersistentFlags().StringVar(&toHash, "to-hash", toHash, "The most recent hash to start with.")
command.PersistentFlags().StringVar(&format, "output-format", format, "The format that the output should come in (default, json, sarif.")
command.PersistentFlags().StringVar(&commitListFile, "commits-file", commitListFile, "Provide a file with the commits to check per line (git rev-list master..HEAD)")

}

func squeal(_ *cobra.Command, args []string) error {
if concise {
log.SetLevel(log.FatalLevel)
}

if debug {
log.SetLevel(log.DebugLevel)
}

var basePath = "./"
if len(args) > 0 {
basePath = args[0]
}
cfg, err := config.LoadConfig(configFilePath)
if err != nil {
return err
}

scanner, err := getScanner(cfg, basePath)
if err != nil {
return err
}
transgressions, err := scanner.Scan()
if err != nil {
return err
}

output, err := formatters.GetFormatter(format).PrintTransgressions(transgressions, redacted)
if err != nil {
log.WithError(err).Error(err.Error())
}

fmt.Printf(output)

metrics := scanner.GetMetrics()
if !concise {
_, _ = fmt.Fprint(os.Stderr, printMetrics(metrics))
}

exitCode := int(math.Min(float64(metrics.TransgressionsReported), 1))
os.Exit(exitCode)
return nil
}

func getScanner(cfg *config.Config, basePath string) (scan.Scanner, error) {
scanner, err := scan.NewScanner(scan.ScannerConfig{
Cfg: cfg,
Basepath: basePath,
Redacted: redacted,
NoGit: noGit,
FromHash: fromHash,
ToHash: toHash,
Everything: everything,
CommitListFile: commitListFile,
})
if err != nil {
return nil, err
}
return scanner, nil
}

func printMetrics(metrics *mertics.Metrics) string {
duration, _ := metrics.Duration()
return fmt.Sprintf(`
Processing:
duration: %4.2fs
commits: %d
commit files: %d
transgressionMap:
identified: %d
ignored: %d
reported: %d
`,
duration,
metrics.CommitsProcessed,
metrics.FilesProcessed,
metrics.TransgressionsFound,
metrics.TransgressionsIgnored,
metrics.TransgressionsReported)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strings"

"github.com/owenrumney/squealer/internal/app/squealer/match"
"github.com/owenrumney/squealer/internal/pkg/match"
)

type DefaultFormatter struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package formatters

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/owenrumney/squealer/internal/app/squealer/match"
"github.com/stretchr/testify/assert"

"github.com/owenrumney/squealer/internal/pkg/match"
)

func TestDefaultFormatterOutput(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package formatters

import (
"github.com/owenrumney/squealer/internal/app/squealer/match"
"github.com/owenrumney/squealer/internal/pkg/match"
)

type Formatter interface {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package formatters

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/owenrumney/squealer/internal/app/squealer/match"
"github.com/stretchr/testify/assert"

"github.com/owenrumney/squealer/internal/pkg/match"
)

func TestGetFormatter(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package formatters
import (
"encoding/json"

"github.com/owenrumney/squealer/internal/app/squealer/match"
"github.com/owenrumney/squealer/internal/pkg/match"
)

type JsonFormatter struct {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package formatters

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/owenrumney/squealer/internal/app/squealer/match"
"github.com/stretchr/testify/assert"

"github.com/owenrumney/squealer/internal/pkg/match"
)

func TestJsonFormatterOutput(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/owenrumney/go-sarif/sarif"

"github.com/owenrumney/squealer/internal/app/squealer/match"
"github.com/owenrumney/squealer/internal/pkg/match"
)

type SarifFormatter struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/stretchr/testify/assert"

"github.com/owenrumney/squealer/internal/app/squealer/match"
"github.com/owenrumney/squealer/internal/pkg/match"
)

func TestSarifFormmaterOutput(t *testing.T) {
Expand Down
Loading

0 comments on commit b215911

Please sign in to comment.