-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic implementation for filter expressions
- Loading branch information
Showing
7 changed files
with
221 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package formatter | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/expr-lang/expr" | ||
) | ||
|
||
// filterExpr filters NMAPRun.Hosts by given expression | ||
func filterExpr(r NMAPRun, code string) (NMAPRun, error) { | ||
program, err := expr.Compile( | ||
fmt.Sprintf("filter(Host, { %s })", code), | ||
expr.Env(r), | ||
) | ||
|
||
if err != nil { | ||
return r, err | ||
} | ||
|
||
output, err := expr.Run(program, r) | ||
if err != nil { | ||
return r, err | ||
} | ||
|
||
hosts, err := convertToHosts(output) | ||
|
||
if err != nil { | ||
return r, err | ||
} | ||
|
||
r.Host = hosts | ||
return r, nil | ||
} | ||
|
||
// convertToHosts converts output from expression engine to []Host | ||
func convertToHosts(output interface{}) ([]Host, error) { | ||
outputInterfaces, ok := output.([]interface{}) | ||
if !ok { | ||
return nil, fmt.Errorf("output is not []interface{}") | ||
} | ||
|
||
hosts := make([]Host, len(outputInterfaces)) | ||
for i, v := range outputInterfaces { | ||
host, ok := v.(Host) | ||
if !ok { | ||
return nil, fmt.Errorf("element is not Host") | ||
} | ||
hosts[i] = host | ||
} | ||
|
||
return hosts, 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,133 @@ | ||
package formatter | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test_filterExpr(t *testing.T) { | ||
type args struct { | ||
nmapRUN NMAPRun | ||
code string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want NMAPRun | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Basic test", | ||
args: args{ | ||
nmapRUN: NMAPRun{ | ||
Scanner: "", | ||
Args: "", | ||
Start: 0, | ||
StartStr: "", | ||
Version: "", | ||
ScanInfo: ScanInfo{}, | ||
Host: []Host{ | ||
{ | ||
StartTime: 0, | ||
EndTime: 0, | ||
Port: []Port{ | ||
{ | ||
Protocol: "http", | ||
PortID: 80, | ||
State: PortState{}, | ||
Service: PortService{}, | ||
Script: []Script{}, | ||
}, | ||
}, | ||
HostAddress: []HostAddress{ | ||
{ | ||
Address: "10.10.10.1", | ||
AddressType: "ipv4", | ||
Vendor: "", | ||
}, | ||
}, | ||
HostNames: HostNames{}, | ||
Status: HostStatus{ | ||
State: "up", | ||
Reason: "", | ||
}, | ||
OS: OS{}, | ||
Trace: Trace{}, | ||
Uptime: Uptime{}, | ||
Distance: Distance{}, | ||
TCPSequence: TCPSequence{}, | ||
IPIDSequence: IPIDSequence{}, | ||
TCPTSSequence: TCPTSSequence{}, | ||
}, | ||
{ | ||
StartTime: 0, | ||
EndTime: 0, | ||
Port: []Port{ | ||
{ | ||
Protocol: "", | ||
PortID: 22, | ||
State: PortState{}, | ||
Service: PortService{}, | ||
Script: []Script{}, | ||
}, | ||
}, | ||
HostAddress: []HostAddress{ | ||
{ | ||
Address: "10.10.10.20", | ||
AddressType: "ipv4", | ||
Vendor: "", | ||
}, | ||
}, | ||
HostNames: HostNames{}, | ||
Status: HostStatus{}, | ||
OS: OS{}, | ||
Trace: Trace{}, | ||
Uptime: Uptime{}, | ||
Distance: Distance{}, | ||
TCPSequence: TCPSequence{}, | ||
IPIDSequence: IPIDSequence{}, | ||
TCPTSSequence: TCPTSSequence{}, | ||
}, | ||
}, | ||
Verbose: Verbose{}, | ||
Debugging: Debugging{}, | ||
RunStats: RunStats{}, | ||
}, | ||
code: `.Status.State == "up" && any(.Port, { .PortID in [80] })`, | ||
}, | ||
want: NMAPRun{ | ||
Scanner: "", | ||
Args: "", | ||
Start: 0, | ||
StartStr: "", | ||
Version: "", | ||
ScanInfo: ScanInfo{}, | ||
Host: []Host{{StartTime: 0, EndTime: 0, Port: []Port{ | ||
{ | ||
Protocol: "http", | ||
PortID: 80, | ||
State: PortState{}, | ||
Service: PortService{}, | ||
Script: []Script{}, | ||
}, | ||
}, HostAddress: []HostAddress{{Address: "10.10.10.1", AddressType: "ipv4", Vendor: ""}}, HostNames: HostNames{}, Status: HostStatus{State: "up", Reason: ""}, OS: OS{}, Trace: Trace{}, Uptime: Uptime{}, Distance: Distance{}, TCPSequence: TCPSequence{}, IPIDSequence: IPIDSequence{}, TCPTSSequence: TCPTSSequence{}}}, | ||
Verbose: Verbose{}, | ||
Debugging: Debugging{}, | ||
RunStats: RunStats{}, | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := filterExpr(tt.args.nmapRUN, tt.args.code) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("filterExpr() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("filterExpr() = %+v, want %+v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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