Skip to content

Commit

Permalink
Add README.md, refactor tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Caroline Russell <[email protected]>
  • Loading branch information
cerrussell committed Jul 31, 2024
1 parent 21a1c5f commit bd3b130
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 37 deletions.
8 changes: 8 additions & 0 deletions components/enrichers/reachability/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Reachability Enricher

This enricher takes findings and checks if the target is reachable with a
reachables slice produced by
[appthreat/atom](https://github.com/appthreat/atom).

For each finding, it adds the following annotation.
"Reachable:<true/false>"
4 changes: 2 additions & 2 deletions components/enrichers/reachability/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func parsePurl(purl string, patterns regexes) []string {
// removeDuplicates removes duplicate strings from a slice.
func removeDuplicates(elements []string) []string {
encountered := map[string]bool{}
result := []string{}
var result []string

for v := range elements {
if encountered[elements[v]] == true {
Expand Down Expand Up @@ -312,7 +312,7 @@ func run() {
}

for _, r := range res {
enrichedIssues := []*v1.EnrichedIssue{}
var enrichedIssues []*v1.EnrichedIssue
for _, i := range r.GetIssues() {
eI, err := enrichIssue(i, data, patterns)
if err != nil {
Expand Down
75 changes: 40 additions & 35 deletions components/enrichers/reachability/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,59 @@ import (
"testing"
)

func countResults(res []*v1.EnrichedIssue) (int, int) {
r := 0
f := 0
for _, finding := range res {
if strings.Contains(fmt.Sprintf("%#v\n", finding.Annotations), "\"reachable\":\"false\"") {
f++
}
if strings.Contains(fmt.Sprintf("%#v\n", finding.Annotations), "\"reachable\":\"true\"") {
r++
}
}
return r, f
}

func readPb(t *testing.T, err error, outFile string) []*v1.EnrichedIssue {
pbBytes, err := os.ReadFile(outFile)
require.NoError(t, err)

res := v1.EnrichedLaunchToolResponse{}
require.NoError(t, proto.Unmarshal(pbBytes, &res))
return res.GetIssues()
}

func TestReachableEnricher(t *testing.T) {
// prepare
//dir, err := os.MkdirTemp("/tmp", "")
//require.NoError(t, err)
//prepare
outDir, err := os.MkdirTemp("/tmp", "")
require.NoError(t, err)

dir := "testdata"
readPath = dir
writePath = dir
writePath = outDir
sliceFile = dir + "/sampleReachables.json"

run()
assert.FileExists(t, outDir+"/reachability.reachability.enriched.pb", "file was not created")
assert.FileExists(t, outDir+"/bandit.reachability.enriched.pb", "file was not created")
assert.FileExists(t, outDir+"/pip-safety.reachability.enriched.pb", "file was not created")

pbBytes, err := os.ReadFile(dir + "/reachability.reachability.enriched.pb")
require.NoError(t, err)
res := readPb(t, err, outDir+"/reachability.reachability.enriched.pb")

res := v1.EnrichedLaunchToolResponse{}
require.NoError(t, proto.Unmarshal(pbBytes, &res))

for _, finding := range res.Issues {
assert.Contains(t, fmt.Sprintf("%#v\n", finding.Annotations), "\"reachable\":\"false\"")
}

pbBytes, err = os.ReadFile(dir + "/bandit.reachability.enriched.pb")
require.NoError(t, err)
r, f := countResults(res)
assert.Equal(t, 0, r)
assert.Equal(t, 1, f)

res = v1.EnrichedLaunchToolResponse{}
require.NoError(t, proto.Unmarshal(pbBytes, &res))
res = readPb(t, err, outDir+"/bandit.reachability.enriched.pb")

for _, finding := range res.Issues {
assert.Contains(t, fmt.Sprintf("%#v\n", finding.Annotations), "\"reachable\":\"true\"")
}
r, f = countResults(res)
assert.Equal(t, 2, r)
assert.Equal(t, 0, f)

pbBytes, err = os.ReadFile(dir + "/pip-safety.reachability.enriched.pb")
require.NoError(t, err)

res = v1.EnrichedLaunchToolResponse{}
require.NoError(t, proto.Unmarshal(pbBytes, &res))
res = readPb(t, err, outDir+"/pip-safety.reachability.enriched.pb")

r := 0
f := 0
for _, finding := range res.Issues {
if strings.Contains(fmt.Sprintf("%#v\n", finding.Annotations), "\"reachable\":\"false\"") {
f++
}
if strings.Contains(fmt.Sprintf("%#v\n", finding.Annotations), "\"reachable\":\"true\"") {
r++
}
}
r, f = countResults(res)
assert.Equal(t, 14, r)
assert.Equal(t, 9, f)

Expand Down

0 comments on commit bd3b130

Please sign in to comment.