Skip to content

Commit

Permalink
Modify plugin tests aded
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksejsv committed Sep 26, 2023
1 parent d78bc71 commit 420ed90
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 67 deletions.
38 changes: 21 additions & 17 deletions plugins/src/modify/modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,39 @@ func (p *plugin) Setup(processor *pdk.Processor) error {
p.processor = processor

// Convert string regexs into actual regexps
for i, entry := range p.processor.Data["modify"].([]interface{}) {
p.processor.Data["modify"].([]interface{})[i].(map[string]interface{})["regex"] = regexp.MustCompile(entry.(map[string]interface{})["regex"].(string))
if p.processor.Data["modify"] != nil {
for i, entry := range p.processor.Data["modify"].([]interface{}) {
p.processor.Data["modify"].([]interface{})[i].(map[string]interface{})["regex"] = regexp.MustCompile(entry.(map[string]interface{})["regex"].(string))
}
}

return nil
}

func (p *plugin) Process(relations []map[string]interface{}) ([]map[string]interface{}, error) {

for _, relation := range relations {
for _, m := range p.processor.Data["modify"].([]interface{}) {
for _, part := range []string{"from", "to", "edge"} {
rp := relation[part]
if p.processor.Data["modify"] != nil {
for _, relation := range relations {
for _, m := range p.processor.Data["modify"].([]interface{}) {
for _, part := range []string{"from", "to", "edge"} {
rp := relation[part]

if rp != nil {
rpt := relation[part].(map[string]interface{})
if rp != nil {
rpt := relation[part].(map[string]interface{})

if p.processor.Data["group"] != nil && rpt["group"] != p.processor.Data["group"].(string) {
continue
}
if p.processor.Data["group"] != nil && rpt["group"] != p.processor.Data["group"].(string) {
continue
}

mt := m.(map[string]interface{})
mt := m.(map[string]interface{})

if mt["field"].(string) == "id" {
rpt["id"] = mt["regex"].(*regexp.Regexp).ReplaceAllString(rpt["id"].(string), fmt.Sprint(mt["replacement"]))
}
if mt["field"].(string) == "id" {
rpt["id"] = mt["regex"].(*regexp.Regexp).ReplaceAllString(rpt["id"].(string), fmt.Sprint(mt["replacement"]))
}

if rpt["attributes"] != nil && rpt["attributes"].(map[string]interface{})[mt["field"].(string)] != nil {
rpt["attributes"].(map[string]interface{})[mt["field"].(string)] = mt["regex"].(*regexp.Regexp).ReplaceAllString(rpt["attributes"].(map[string]interface{})[mt["field"].(string)].(string), fmt.Sprint(mt["replacement"]))
if rpt["attributes"] != nil && rpt["attributes"].(map[string]interface{})[mt["field"].(string)] != nil {
rpt["attributes"].(map[string]interface{})[mt["field"].(string)] = mt["regex"].(*regexp.Regexp).ReplaceAllString(rpt["attributes"].(map[string]interface{})[mt["field"].(string)].(string), fmt.Sprint(mt["replacement"]))
}
}
}
}
Expand Down
97 changes: 48 additions & 49 deletions plugins/src/modify/modify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

/*
* Test processing the data source's response
* Test attributes modifications
*/
func TestProcess(t *testing.T) {

Expand All @@ -16,71 +16,69 @@ func TestProcess(t *testing.T) {

processor := &pdk.Processor{
Data: map[string]interface{}{
"field": "id",
"group": "type",
"group": "name",

"taxonomy": map[string]interface{}{
"brute-force": "intrusion-attempts",
"modify": []interface{}{
map[string]interface{}{
"field": "age",
"regex": "\\d*",
"replacement": "***",
},
},
},
}

err := c.Setup(processor)
if err != nil {
t.Errorf("Can't setup a taxonomy plugin: %s", err.Error())
t.Errorf("Can't setup a modify plugin: %s", err.Error())
}

// Pairs of data source plugins responses and the expected processing results
table := []struct {
entry map[string]interface{}
inserted map[string]interface{}
modified map[string]interface{}
}{
// New relation should be added because "id" == "brute-force"
// No modifications should be made, because group is different
{map[string]interface{}{
"from": map[string]interface{}{
"id": "brute-force",
"group": "type",
"search": "type",
"id": "John",
"group": "neighbor",
"search": "name",
"attributes": map[string]interface{}{
"age": "25",
},
},
}, map[string]interface{}{
"id": "intrusion-attempts",
"group": "taxonomy",
"search": "taxonomy",
}},

// New relation should be added because "attributes.id" == "brute-force"
{map[string]interface{}{
"from": map[string]interface{}{
"id": "malware",
"group": "type",
"search": "type",
"id": "John",
"group": "name",
"search": "name",
"attributes": map[string]interface{}{
"id": "brute-force",
"age": "25",
},
},
}, map[string]interface{}{
"id": "intrusion-attempts",
"group": "taxonomy",
"search": "taxonomy",
}},

// New relation should NOT be added because "id" value is not mentioned in processor.Data["taxonomy"]
// Age should be anonymized
{map[string]interface{}{
"from": map[string]interface{}{
"id": "ddos",
"group": "type",
"search": "type",
"id": "John",
"group": "name",
"search": "name",
"attributes": map[string]interface{}{
"age": "25",
},
},
}, nil},

// New relation should NOT be added because "group" value is not equal to processor.Data["group"]
{map[string]interface{}{
}, map[string]interface{}{
"from": map[string]interface{}{
"id": "brute-force",
"group": "address",
"search": "type",
"id": "John",
"group": "name",
"search": "name",
"attributes": map[string]interface{}{
"age": "***",
},
},
}, nil},
}},
}

for _, row := range table {
Expand All @@ -90,19 +88,20 @@ func TestProcess(t *testing.T) {
continue
}

if row.inserted == nil && len(result) > 1 {
t.Errorf("Unwanted relation added for \"%s\": \"%s\"", row.entry["from"], result[1])
modified := row.modified["from"].(map[string]interface{})
from := result[0]["from"].(map[string]interface{})

} else if len(result) == 1 && row.inserted != nil {
t.Errorf("No new relations added for \"%s\": \"%s\", expected: \"%s\"", row.entry["from"], result, row.inserted)
if from["id"] != modified["id"] {
t.Errorf("Invalid modification of ID in \"%v\": \"%v\", expected: \"%v\"",
row.entry["from"], from, modified)
break
}

} else if len(result) > 1 && row.inserted != nil {
for k, v := range result[1]["to"].(map[string]interface{}) {
if row.inserted[k] != v {
t.Errorf("Invalid taxonomy added for \"%s\": \"%s\", expected: \"%s\"",
row.entry["from"], result[1]["to"], row.inserted)
break
}
for k, v := range from["attributes"].(map[string]interface{}) {
if modified["attributes"].(map[string]interface{})[k] != v {
t.Errorf("Invalid modification of \"%v\": \"%v\", expected: \"%v\"",
row.entry["from"], from, modified)
break
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/src/taxonomy/taxonomy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

/*
* Test processing the data source's response
* Test taxonomy nodes creating
*/
func TestProcess(t *testing.T) {

Expand Down

0 comments on commit 420ed90

Please sign in to comment.