-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from owenrumney/add-output-formats
add sarif and json output formats
- Loading branch information
Showing
51 changed files
with
12,810 additions
and
377 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
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,32 @@ | ||
package formatters | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/owenrumney/squealer/internal/app/squealer/match" | ||
) | ||
|
||
type DefaultFormatter struct { | ||
} | ||
|
||
func (d DefaultFormatter) PrintTransgressions(transgressions []match.Transgression, redacted bool) (string, error) { | ||
builder := strings.Builder{} | ||
|
||
for _, t := range transgressions { | ||
var content = t.LineContent | ||
if redacted { | ||
content = t.RedactedContent | ||
} | ||
builder.Write([]byte(fmt.Sprintf(` | ||
content: | %s | ||
Filename: | %s | ||
secret Hash: | %s | ||
commit: | %s | ||
Committer: | %s (%s) | ||
Committed: | %s | ||
exclude rule: | %s | ||
`, content, t.Filename, t.Hash, t.CommitHash, t.Committer, t.CommitterEmail, t.Committed, t.ExcludeRule))) | ||
} | ||
return builder.String(), nil | ||
} |
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,35 @@ | ||
package formatters | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
|
||
"github.com/owenrumney/squealer/internal/app/squealer/match" | ||
) | ||
|
||
func TestDefaultFormatterOutput(t *testing.T) { | ||
trans := []match.Transgression{createTestTransgression("Joe Bloggs", "[email protected]", "2001-01-01", "abcd123456efg")} | ||
|
||
plainText, _ := DefaultFormatter{}.PrintTransgressions(trans, false) | ||
assert.Equal(t, ` | ||
content: | password=Password1234 | ||
Filename: | /config.yml | ||
secret Hash: | sdjn34rf32fds | ||
commit: | abcd123456efg | ||
Committer: | Joe Bloggs ([email protected]) | ||
Committed: | 2001-01-01 | ||
exclude rule: | | ||
`, plainText) | ||
|
||
redacted, _ := DefaultFormatter{}.PrintTransgressions(trans, true) | ||
assert.Equal(t, ` | ||
content: | password=REDACTED | ||
Filename: | /config.yml | ||
secret Hash: | sdjn34rf32fds | ||
commit: | abcd123456efg | ||
Committer: | Joe Bloggs ([email protected]) | ||
Committed: | 2001-01-01 | ||
exclude rule: | | ||
`, redacted) | ||
|
||
} |
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,20 @@ | ||
package formatters | ||
|
||
import ( | ||
"github.com/owenrumney/squealer/internal/app/squealer/match" | ||
) | ||
|
||
type Formatter interface { | ||
PrintTransgressions([]match.Transgression, bool) (string, error) | ||
} | ||
|
||
func GetFormatter(format string) Formatter { | ||
switch format { | ||
case "sarif": | ||
return &SarifFormatter{} | ||
case "json": | ||
return &JsonFormatter{} | ||
default: | ||
return &DefaultFormatter{} | ||
} | ||
} |
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,29 @@ | ||
package formatters | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
|
||
"github.com/owenrumney/squealer/internal/app/squealer/match" | ||
) | ||
|
||
func TestGetFormatter(t *testing.T) { | ||
assert.IsType(t, GetFormatter("json"), &JsonFormatter{}) | ||
assert.IsType(t, GetFormatter("sarif"), &SarifFormatter{}) | ||
assert.IsType(t, GetFormatter("default"), &DefaultFormatter{}) | ||
assert.IsType(t, GetFormatter("text"), &DefaultFormatter{}) | ||
} | ||
|
||
func createTestTransgression(committer, committerEmail, committed, commitHash string) match.Transgression { | ||
return match.Transgression{ | ||
LineContent: "password=Password1234", | ||
Filename: "/config.yml", | ||
Hash: "sdjn34rf32fds", | ||
Match: "Password1234", | ||
RedactedContent: "password=REDACTED", | ||
CommitterEmail: committerEmail, | ||
Committer: committer, | ||
CommitHash: commitHash, | ||
Committed: committed, | ||
} | ||
} |
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,65 @@ | ||
package formatters | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/owenrumney/squealer/internal/app/squealer/match" | ||
) | ||
|
||
type JsonFormatter struct { | ||
} | ||
|
||
type transgressionsBlock struct { | ||
Transgressions []transgressionBlock `json:"transgressions"` | ||
} | ||
|
||
type committer struct { | ||
Name string `json:"name"` | ||
Email string `json:"email"` | ||
} | ||
|
||
type transgressionBlock struct { | ||
Content string `json:"content"` | ||
Filename string `json:"filename"` | ||
Hash string `json:"secret_hash"` | ||
Match string `json:"match_string"` | ||
Committer committer `json:"committer"` | ||
CommitHash string `json:"commit_hash"` | ||
Committed string `json:"committed"` | ||
ExcludeRule string `json:"exclude_rule"` | ||
} | ||
|
||
func (j JsonFormatter) PrintTransgressions(transgressions []match.Transgression, redacted bool) (string, error) { | ||
var tb []transgressionBlock | ||
|
||
for _, t := range transgressions { | ||
var content = t.LineContent | ||
if redacted { | ||
content = t.RedactedContent | ||
} | ||
|
||
tb = append(tb, transgressionBlock{ | ||
Content: content, | ||
Filename: t.Filename, | ||
Hash: t.Hash, | ||
Match: t.Match, | ||
Committer: committer{ | ||
Name: t.Committer, | ||
Email: t.CommitterEmail, | ||
}, | ||
CommitHash: t.CommitHash, | ||
Committed: t.Committed, | ||
ExcludeRule: t.ExcludeRule, | ||
}) | ||
} | ||
|
||
tBlock := &transgressionsBlock{ | ||
Transgressions: tb, | ||
} | ||
|
||
outBytes, err := json.MarshalIndent(tBlock, "", " ") | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(outBytes), err | ||
} |
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,50 @@ | ||
package formatters | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
|
||
"github.com/owenrumney/squealer/internal/app/squealer/match" | ||
) | ||
|
||
func TestJsonFormatterOutput(t *testing.T) { | ||
trans := []match.Transgression{createTestTransgression("Joe Bloggs", "[email protected]", "2001-01-01", "abcd123456efg")} | ||
|
||
plainText, _ := JsonFormatter{}.PrintTransgressions(trans, false) | ||
assert.Equal(t, `{ | ||
"transgressions": [ | ||
{ | ||
"content": "password=Password1234", | ||
"filename": "/config.yml", | ||
"secret_hash": "sdjn34rf32fds", | ||
"match_string": "Password1234", | ||
"committer": { | ||
"name": "Joe Bloggs", | ||
"email": "[email protected]" | ||
}, | ||
"commit_hash": "abcd123456efg", | ||
"committed": "2001-01-01", | ||
"exclude_rule": "" | ||
} | ||
] | ||
}`, plainText) | ||
|
||
redacted, _ := JsonFormatter{}.PrintTransgressions(trans, true) | ||
assert.Equal(t, `{ | ||
"transgressions": [ | ||
{ | ||
"content": "password=REDACTED", | ||
"filename": "/config.yml", | ||
"secret_hash": "sdjn34rf32fds", | ||
"match_string": "Password1234", | ||
"committer": { | ||
"name": "Joe Bloggs", | ||
"email": "[email protected]" | ||
}, | ||
"commit_hash": "abcd123456efg", | ||
"committed": "2001-01-01", | ||
"exclude_rule": "" | ||
} | ||
] | ||
}`, redacted) | ||
} |
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,44 @@ | ||
package formatters | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/owenrumney/go-sarif/sarif" | ||
|
||
"github.com/owenrumney/squealer/internal/app/squealer/match" | ||
) | ||
|
||
type SarifFormatter struct { | ||
} | ||
|
||
func (s SarifFormatter) PrintTransgressions(transgressions []match.Transgression, redacted bool) (string, error) { | ||
report, err := sarif.New(sarif.Version210) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
run := report.AddRun("squealer", "https://github.com/owenrumney/squealer") | ||
|
||
for _, t := range transgressions { | ||
var content = t.LineContent | ||
if redacted { | ||
content = t.RedactedContent | ||
} | ||
rule := run.AddRule(t.Hash). | ||
WithDescription("There should be no sensitive data stored in the repository"). | ||
WithHelp("Add exclude rules to the config for squealer to ignore. Exclude rules take the format filename:hash") | ||
|
||
result := run.AddResult(rule.Id). | ||
WithMessage(fmt.Sprintf("found transgression [%s], secret hashs [%s]", content, t.Hash)). | ||
WithLevel("error"). | ||
WithLocationDetails(t.Filename, 1, 1) | ||
|
||
run.AddResultDetails(rule, result, t.Filename) | ||
} | ||
|
||
var buf bytes.Buffer | ||
if err = report.PrettyWrite(&buf); err != nil { | ||
return "", err | ||
} | ||
return buf.String(), nil | ||
} |
Oops, something went wrong.