Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for running as a library #57

Merged
merged 2 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
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