-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Large refactor of pillager application. This refactor prioritizes code readability and modularity. * fix: fixes lint error * fix: more consistent logging * fix: merged go.mod * fix: updated goreleaser build path * fix: update reviewdog * fix: github action and docker build Co-authored-by: brittonhayes <[email protected]>
- Loading branch information
1 parent
7aaa846
commit 39a7587
Showing
39 changed files
with
837 additions
and
873 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,4 @@ updates: | |
- package-ecosystem: "gomod" | ||
directory: "/" | ||
schedule: | ||
interval: "weekly" | ||
interval: "monthly" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
name: test | ||
|
||
on: [ pull_request ] | ||
on: [pull_request] | ||
|
||
jobs: | ||
test: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
linters: | ||
enable: | ||
- gofumpt | ||
- gofmt | ||
- gosimple | ||
- godot | ||
- godox | ||
- dupl | ||
- exhaustive | ||
- funlen | ||
- gocritic | ||
- goprintffuncname | ||
- ifshort | ||
presets: | ||
- unused |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.PHONY: | ||
lint: | ||
golangci-lint run ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"runtime" | ||
"os" | ||
|
||
"github.com/brittonhayes/pillager" | ||
"github.com/brittonhayes/pillager/pkg/hunter" | ||
"github.com/brittonhayes/pillager/pkg/rules" | ||
"github.com/spf13/afero" | ||
"github.com/rs/zerolog" | ||
) | ||
|
||
func main() { | ||
// Create a new hunter config | ||
c := hunter.NewConfig(afero.NewOsFs(), ".", true, rules.Load(""), hunter.StringToFormat("JSON"), "", runtime.NumCPU()) | ||
err := example() | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func example() error { | ||
opts := []pillager.ConfigOption{ | ||
pillager.WithLogLevel(zerolog.DebugLevel), | ||
} | ||
|
||
// Create a new hunter from the config | ||
h := hunter.NewHunter(c) | ||
// Create a new hunter config | ||
p, err := hunter.New(opts...) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Start hunting | ||
_ = h.Hunt() | ||
results, err := p.Hunt() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Report results | ||
err = p.Report(os.Stdout, results) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
AKIAIO5FODNN7EXAMPLE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package main | ||
|
||
import ( | ||
pillager "github.com/brittonhayes/pillager/internal/commands" | ||
) | ||
|
||
func main() { | ||
pillager.Execute() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
//go:generate golangci-lint run ./... | ||
//go:generate gomarkdoc ./pkg/hunter/... | ||
//go:generate gomarkdoc ./pkg/rules/... | ||
//go:generate gomarkdoc ./pkg/format/... | ||
package pillager | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"runtime" | ||
|
||
"github.com/rs/zerolog" | ||
"github.com/rs/zerolog/log" | ||
|
||
"github.com/brittonhayes/pillager/internal/validate" | ||
"github.com/brittonhayes/pillager/pkg/format" | ||
"github.com/brittonhayes/pillager/pkg/rules" | ||
"github.com/spf13/afero" | ||
gitleaks "github.com/zricethezav/gitleaks/v7/config" | ||
) | ||
|
||
// Config takes all of the configurable | ||
// parameters for a Hunter. | ||
type Config struct { | ||
Filesystem afero.Fs | ||
Style format.Style | ||
Gitleaks gitleaks.Config | ||
|
||
ScanPath string | ||
Verbose bool | ||
Debug bool | ||
Workers int | ||
Template string | ||
} | ||
|
||
type ConfigOption func(*Config) | ||
|
||
func NewConfig(opts ...ConfigOption) *Config { | ||
var ( | ||
defaultFS = afero.NewOsFs() | ||
defaultVerbose = false | ||
defaultScanPath = "." | ||
defaultStyle = format.StyleJSON | ||
defaultWorkers = runtime.NumCPU() | ||
defaultGitleaks = rules.NewLoader().Load() | ||
defaultTemplate = "" | ||
defaultLogLevel = zerolog.ErrorLevel | ||
) | ||
|
||
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}) | ||
zerolog.SetGlobalLevel(defaultLogLevel) | ||
config := &Config{ | ||
ScanPath: defaultScanPath, | ||
Filesystem: defaultFS, | ||
Style: defaultStyle, | ||
Workers: defaultWorkers, | ||
Gitleaks: defaultGitleaks, | ||
Verbose: defaultVerbose, | ||
Template: defaultTemplate, | ||
} | ||
|
||
for _, opt := range opts { | ||
opt(config) | ||
} | ||
|
||
if err := config.validate(); err != nil { | ||
log.Fatal().Err(err).Send() | ||
} | ||
|
||
return config | ||
} | ||
|
||
func WithFS(fs afero.Fs) ConfigOption { | ||
return func(c *Config) { | ||
c.Filesystem = fs | ||
} | ||
} | ||
|
||
func WithScanPath(path string) ConfigOption { | ||
return func(c *Config) { | ||
c.ScanPath = validate.Path(c.Filesystem, c.ScanPath) | ||
} | ||
} | ||
|
||
func WithLogLevel(level string) ConfigOption { | ||
return func(c *Config) { | ||
lvl, err := zerolog.ParseLevel(level) | ||
if err != nil { | ||
log.Fatal().Err(err).Send() | ||
} | ||
zerolog.SetGlobalLevel(lvl) | ||
} | ||
} | ||
|
||
func WithVerbose(verbose bool) ConfigOption { | ||
return func(c *Config) { | ||
c.Verbose = verbose | ||
} | ||
} | ||
|
||
func WithWorkers(count int) ConfigOption { | ||
return func(c *Config) { | ||
c.Workers = count | ||
} | ||
} | ||
|
||
func WithStyle(style format.Style) ConfigOption { | ||
return func(c *Config) { | ||
if c.Template != "" { | ||
c.Style = format.StyleCustom | ||
return | ||
} | ||
|
||
c.Style = style | ||
} | ||
} | ||
|
||
func WithTemplate(template string) ConfigOption { | ||
return func(c *Config) { | ||
c.Style = format.StyleCustom | ||
c.Template = template | ||
} | ||
} | ||
|
||
func WithGitleaksConfig(g gitleaks.Config) ConfigOption { | ||
return func(c *Config) { | ||
c.Gitleaks = g | ||
} | ||
} | ||
|
||
func (c *Config) validate() error { | ||
if c.Filesystem == nil { | ||
return errors.New("missing filesystem in Config") | ||
} | ||
|
||
if c.Gitleaks.Rules == nil { | ||
return errors.New("no gitleaks rules provided") | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.