Skip to content

Commit

Permalink
Merge pull request #57 from owenrumney/owenr-add-run-as-library
Browse files Browse the repository at this point in the history
feat: Add support for running as a library
  • Loading branch information
owenrumney authored Apr 22, 2022
2 parents 9e2cf3e + 0a3c92b commit 9181f43
Show file tree
Hide file tree
Showing 1,100 changed files with 809 additions and 365,823 deletions.
File renamed without changes
8 changes: 5 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
branches:
- master
pull_request:

jobs:
build:
name: building squealer
Expand All @@ -16,9 +16,11 @@ jobs:
- uses: actions/setup-go@v2
with:
stable: 'false'
go-version: '1.16.2' # The Go version to download (if necessary) and use.
go-version: '1.17.0' # The Go version to download (if necessary) and use.
- run: go version

- name: Run test
run: make test


- name: Check quality
run: make quality
2 changes: 1 addition & 1 deletion .github/workflows/releases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: actions/setup-go@v2
with:
stable: 'false'
go-version: '1.16.2' # The Go version to download (if necessary) and use.
go-version: '1.17.0' # The Go version to download (if necessary) and use.
- run: go version

- name: Login to Docker Hub
Expand Down
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,11 @@ test:
push-image:
./scripts/publish-image.sh

.PHONY: image
image:
docker build --build-arg squealer_version=$(TRAVIS_TAG) -t $(IMAGE) .
docker build --build-arg squealer_version=$(TRAVIS_TAG) -t $(IMAGE) .

.PHONY: quality
quality:
which golangci-lint || go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.43.0
golangci-lint run --timeout 3m --verbose
49 changes: 46 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![Sqealer](squealer.png)
![Squealer](.github/image/ssquealer.png)

# Squealer

Expand Down Expand Up @@ -164,9 +164,52 @@ Squealer can be used for scanning a specific string using either the default con
go get -u github.com/owenrumney/squealer
```
### Using the code
### Using as a library
#### Git and Directory Scanning
```go
package main
import (
"fmt"
"github.com/owenrumney/squealer/pkg/squealer"
)
func main() {
// create a new scanner (optionally load your own config in)
scanner, err := squealer.New(
squealer.OptionWithConfig(*cfg), // if not supplied , config.DefaultConfig() used
squealer.OptionRedactedSecrets(redacted), // defaults to true, secrets in output redacted
squealer.OptionNoGitScan(noGit), // Treat Directories with .git in them as Directories, defaults to false
squealer.OptionWithBasePath(basePath), // The path to scan, default is '.'
squealer.OptionWithFromHash(fromHash), // Specify the starting hash for the scan, useful for PRs
squealer.OptionWithToHash(toHash), // Specify the hash to stop scanning, useful for PRs scanning
squealer.OptionWithScanEverything(everything), // Scan everything in every branch, defaults to only the current branch
squealer.OptionWithCommitListFile(commitListFile), // a file of commits that you want to explicitly scan in a text file.
)
transgressions, err := scanner.Scan()
if err != nil {
panic(err)
}
for _, t := range transgressions {
fmt.Printf("%s[%d]\n", t.Filename, t.LineNo)
}
}
```
#### String Scanning
```go
package main
```golang
import (
"fmt"
Expand Down
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
27 changes: 24 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
module github.com/owenrumney/squealer

go 1.16
go 1.17

require (
github.com/go-git/go-git/v5 v5.4.2
github.com/google/go-cmp v0.5.6 // indirect
github.com/owenrumney/go-sarif v1.1.1
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.4.0
github.com/stretchr/testify v1.7.1
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/Microsoft/go-winio v0.4.16 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
github.com/zclconf/go-cty v1.10.0 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/yaml.v2 v2.4.0
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
Loading

0 comments on commit 9181f43

Please sign in to comment.