Skip to content

Commit

Permalink
Added additional templates for rendering the results as an html table (
Browse files Browse the repository at this point in the history
…#15)

Co-authored-by: Britton Hayes <[email protected]>
  • Loading branch information
brittonhayes and brittonhayes authored Feb 7, 2021
1 parent 2b35e86 commit 4a9eff0
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 18 deletions.
3 changes: 3 additions & 0 deletions cmd/hunt.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var (
rulesConfig string
output string
templ string
workers int
)

// huntCmd represents the hunt command
Expand All @@ -41,6 +42,7 @@ pillager hunt ./example -r rules.toml -f custom --template "$(cat templates/simp

func init() {
rootCmd.AddCommand(huntCmd)
huntCmd.Flags().IntVarP(&workers, "workers", "w", 5, "number of concurrent workers to create")
huntCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "toggle verbose output")
huntCmd.Flags().StringVarP(&rulesConfig, "rules", "r", "", "path to gitleaks rules.toml config")
huntCmd.Flags().StringVarP(&output, "format", "f", "yaml", "set output format (json, yaml, custom)")
Expand All @@ -62,6 +64,7 @@ func StartHunt() func(cmd *cobra.Command, args []string) error {
rules.Load(rulesConfig),
hunter.StringToFormat(output),
templ,
workers,
)
h := hunter.NewHunter(c)
return h.Hunt()
Expand Down
39 changes: 28 additions & 11 deletions hunter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ Package hunter contains the types\, methods\, and interfaces for the file huntin
- [Constants](<#constants>)
- [func RenderTemplate(w io.Writer, tpl string, f scan.Report)](<#func-rendertemplate>)
- [type Config](<#type-config>)
- [func NewConfig(fs afero.Fs, path string, verbose bool, rules []gitleaks.Rule, format Format, template string) *Config](<#func-newconfig>)
- [func NewConfig(
fs afero.Fs,
path string,
verbose bool,
rules []gitleaks.Rule,
format Format,
template string,
workers int,
) *Config](<#func-newconfig>)
- [func (c *Config) Default() *Config](<#func-config-default>)
- [func (c *Config) Validate() (err error)](<#func-config-validate>)
- [type Configer](<#type-configer>)
Expand Down Expand Up @@ -51,7 +59,7 @@ func RenderTemplate(w io.Writer, tpl string, f scan.Report)

RenderTemplate renders a Hound finding in a custom go template format to the provided writer

## type [Config](<https://github.com/brittonhayes/pillager/blob/main/hunter/config.go#L16-L23>)
## type [Config](<https://github.com/brittonhayes/pillager/blob/main/hunter/config.go#L16-L24>)

Config takes all of the configurable parameters for a Hunter

Expand All @@ -60,35 +68,44 @@ type Config struct {
System afero.Fs
BasePath string
Verbose bool
Workers int
Rules []gitleaks.Rule
Format Format
Template string
}
```

### func [NewConfig](<https://github.com/brittonhayes/pillager/blob/main/hunter/config.go#L31>)
### func [NewConfig](<https://github.com/brittonhayes/pillager/blob/main/hunter/config.go#L32-L40>)

```go
func NewConfig(fs afero.Fs, path string, verbose bool, rules []gitleaks.Rule, format Format, template string) *Config
func NewConfig(
fs afero.Fs,
path string,
verbose bool,
rules []gitleaks.Rule,
format Format,
template string,
workers int,
) *Config
```

NewConfig generates a new config for the Hunter

### func \(\*Config\) [Default](<https://github.com/brittonhayes/pillager/blob/main/hunter/config.go#L45>)
### func \(\*Config\) [Default](<https://github.com/brittonhayes/pillager/blob/main/hunter/config.go#L55>)

```go
func (c *Config) Default() *Config
```

Default loads the default configuration for the Hunter

### func \(\*Config\) [Validate](<https://github.com/brittonhayes/pillager/blob/main/hunter/config.go#L58>)
### func \(\*Config\) [Validate](<https://github.com/brittonhayes/pillager/blob/main/hunter/config.go#L68>)

```go
func (c *Config) Validate() (err error)
```

## type [Configer](<https://github.com/brittonhayes/pillager/blob/main/hunter/config.go#L25-L28>)
## type [Configer](<https://github.com/brittonhayes/pillager/blob/main/hunter/config.go#L26-L29>)

```go
type Configer interface {
Expand Down Expand Up @@ -233,7 +250,7 @@ This method also accepts custom output formats using go template/html\. So if yo
panic(err)
}

config := NewConfig(fs, "./", true, rules.Load(""), CustomFormat, DefaultTemplate)
config := NewConfig(fs, "./", true, rules.Load(""), CustomFormat, DefaultTemplate, 5)
h := NewHunter(config)
_ = h.Hunt()
}
Expand Down Expand Up @@ -261,7 +278,7 @@ This is an example of how to run a scan on a single file to look for email addre
panic(err)
}

config := NewConfig(fs, "./", true, rules.Load(""), StringToFormat("yaml"), DefaultTemplate)
config := NewConfig(fs, "./", true, rules.Load(""), StringToFormat("yaml"), DefaultTemplate, 5)
h := NewHunter(config)
_ = h.Hunt()
}
Expand All @@ -288,7 +305,7 @@ This method accepts json output format as well
panic(err)
}

config := NewConfig(fs, ".", true, rules.Load(""), JSONFormat, DefaultTemplate)
config := NewConfig(fs, ".", true, rules.Load(""), JSONFormat, DefaultTemplate, 5)
h := NewHunter(config)
_ = h.Hunt()
}
Expand All @@ -315,7 +332,7 @@ Hunter will also look personally identifiable info in TOML
panic(err)
}

config := NewConfig(fs, ".", true, rules.Load(""), JSONFormat, DefaultTemplate)
config := NewConfig(fs, ".", true, rules.Load(""), JSONFormat, DefaultTemplate, 5)

h := NewHunter(config)
_ = h.Hunt()
Expand Down
12 changes: 11 additions & 1 deletion hunter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Config struct {
System afero.Fs
BasePath string
Verbose bool
Workers int
Rules []gitleaks.Rule
Format Format
Template string
Expand All @@ -28,7 +29,15 @@ type Configer interface {
}

// NewConfig generates a new config for the Hunter
func NewConfig(fs afero.Fs, path string, verbose bool, rules []gitleaks.Rule, format Format, template string) *Config {
func NewConfig(
fs afero.Fs,
path string,
verbose bool,
rules []gitleaks.Rule,
format Format,
template string,
workers int,
) *Config {
p := validate.New().Path(fs, path)
return &Config{
System: fs,
Expand All @@ -37,6 +46,7 @@ func NewConfig(fs afero.Fs, path string, verbose bool, rules []gitleaks.Rule, fo
Rules: rules,
Format: format,
Template: template,
Workers: workers,
}
}

Expand Down
3 changes: 2 additions & 1 deletion hunter/hunter.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ func (h Hunter) Hunt() error {

opt := options.Options{Path: h.Config.BasePath, Verbose: h.Config.Verbose}
conf := config.Config{Rules: h.Config.Rules}

scanner := scan.NewNoGitScanner(opt, conf)
report, err := scanner.Scan()
if err != nil {
panic(err)
return err
}

h.Hound.Howl(report)
Expand Down
8 changes: 4 additions & 4 deletions hunter/hunter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func ExampleHunter_Hunt_email() {
panic(err)
}

config := NewConfig(fs, "./", true, rules.Load(""), StringToFormat("yaml"), DefaultTemplate)
config := NewConfig(fs, "./", true, rules.Load(""), StringToFormat("yaml"), DefaultTemplate, 5)
h := NewHunter(config)
_ = h.Hunt()
}
Expand All @@ -42,7 +42,7 @@ func ExampleHunter_Hunt_custom_output() {
panic(err)
}

config := NewConfig(fs, "./", true, rules.Load(""), CustomFormat, DefaultTemplate)
config := NewConfig(fs, "./", true, rules.Load(""), CustomFormat, DefaultTemplate, 5)
h := NewHunter(config)
_ = h.Hunt()
}
Expand All @@ -61,7 +61,7 @@ func ExampleHunter_Hunt_json() {
panic(err)
}

config := NewConfig(fs, ".", true, rules.Load(""), JSONFormat, DefaultTemplate)
config := NewConfig(fs, ".", true, rules.Load(""), JSONFormat, DefaultTemplate, 5)
h := NewHunter(config)
_ = h.Hunt()
}
Expand All @@ -79,7 +79,7 @@ func ExampleHunter_Hunt_toml() {
panic(err)
}

config := NewConfig(fs, ".", true, rules.Load(""), JSONFormat, DefaultTemplate)
config := NewConfig(fs, ".", true, rules.Load(""), JSONFormat, DefaultTemplate, 5)

h := NewHunter(config)
_ = h.Hunt()
Expand Down
42 changes: 42 additions & 0 deletions templates/html.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Pillager - Scan Results</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css">
</head>
<body>
<section class="section">
<div class="container">
<h1 class="title has-text-info">
Pillager
</h1>
<p class="subtitle">
Results of your latest hunt
</p>
<div class="columns is-multiline">
{{ range .Leaks }}
<div class="column">
<div class="box shadow-md">
<p class="is-size-4">{{.File}}</p>
<p>
<span class="has-text-weight-bold">Tags:</span>
<span class="tag">{{.Tags}}</span>
</p>
<p>
<span class="has-text-weight-bold">Leak:</span>
<span class="is-family-code has-text-danger">{{.Offender}}</span>
</p>
<p>
<span class="has-text-weight-bold">Line:</span>
{{.LineNumber}}
</p>
</div>
</div>
{{end}}
</div>
</div>
</section>
</body>
</html>
2 changes: 1 addition & 1 deletion templates/markdown.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{/*markdown.tmpl*/}}
# Results
{{ range .Leaks}}
{{ range .Leaks -}}
## {{ .File }}
- Location: {{.LineNumber}}
{{end}}
55 changes: 55 additions & 0 deletions templates/table.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Pillager - Scan Results</title>
<style type="text/css">
.offender {
-webkit-text-security: disc !important;
}

.offender:hover {
-webkit-text-security: none;
}
</style>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css">
</head>
<body>
<section class="hero is-black">
<div class="hero-body">
<p class="title">
Pillager
</p>
<p class="subtitle">
Results of your latest hunt
</p>
</div>
</section>
<section class="section">
<div class="container">
<div class="table-container">
<table class="table is-fullwidth">
<thead>
<tr>
<th><abbr title="Filename">File</abbr></th>
<th><abbr title="Line Number">Line</abbr></th>
<th><abbr title="Offender">Leak</abbr></th>
</tr>
</thead>
<tbody>
{{ range .Leaks }}
<tr>
<th>{{.File}}</th>
<td>{{.LineNumber}}</td>
<td class="has-text-danger">{{.Offender}}</td>
</tr>
{{ end }}
</tbody>
</table>
</div>
</div>
</section>

</body>
</html>

0 comments on commit 4a9eff0

Please sign in to comment.