mirror of
https://github.com/kubescape/kubescape.git
synced 2026-04-15 06:58:11 +00:00
Fixing network scanner rego
Signed-off-by: Amit Schendel <amitschendel@gmail.com>
This commit is contained in:
@@ -1,19 +1,12 @@
|
||||
package opaprocessor
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
servicediscovery "github.com/kubescape/kubescape-network-scanner/cmd"
|
||||
)
|
||||
|
||||
// Check if the service is unauthenticated using kubescape-network-scanner.
|
||||
func isUnauthenticatedService(host, port string) bool {
|
||||
portInt, err := strconv.Atoi(port)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
discoveryResults, err := servicediscovery.ScanTargets(host, portInt)
|
||||
func isUnauthenticatedService(host string, port int) bool {
|
||||
discoveryResults, err := servicediscovery.ScanTargets(host, port)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package opaprocessor
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/alicebob/miniredis/v2"
|
||||
@@ -18,11 +19,18 @@ func TestIsUnauthenticatedService(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Get the port as an integer
|
||||
port, err := strconv.Atoi(s.Port())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// rego input
|
||||
type args struct {
|
||||
host string
|
||||
port string
|
||||
port int
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
@@ -33,7 +41,7 @@ func TestIsUnauthenticatedService(t *testing.T) {
|
||||
"Unauthenticated service",
|
||||
args{
|
||||
host: s.Host(),
|
||||
port: s.Port(),
|
||||
port: port,
|
||||
},
|
||||
true,
|
||||
assert.True,
|
||||
@@ -42,7 +50,7 @@ func TestIsUnauthenticatedService(t *testing.T) {
|
||||
"Authenticated service",
|
||||
args{
|
||||
host: s.Host(),
|
||||
port: s.Port(),
|
||||
port: port,
|
||||
},
|
||||
false,
|
||||
assert.False,
|
||||
|
||||
@@ -115,18 +115,24 @@ var imageNameNormalizeDefinition = func(bctx rego.BuiltinContext, a *ast.Term) (
|
||||
|
||||
var unauthenticatedServiceDeclaration = ®o.Function{
|
||||
Name: "networkscanner.isUnauthenticatedService",
|
||||
Decl: types.NewFunction(types.Args(types.S, types.A), types.B),
|
||||
Decl: types.NewFunction(types.Args(types.S, types.N), types.B),
|
||||
Memoize: true,
|
||||
}
|
||||
|
||||
var unauthenticatedServiceDefinition = func(bctx rego.BuiltinContext, a, b *ast.Term) (*ast.Term, error) {
|
||||
aStr, err := builtins.StringOperand(a.Value, 1)
|
||||
service, err := builtins.StringOperand(a.Value, 1)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid parameter type: %v", err)
|
||||
}
|
||||
bStr, err := builtins.StringOperand(b.Value, 1)
|
||||
bNum, err := builtins.NumberOperand(b.Value, 1)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid parameter type: %v", err)
|
||||
}
|
||||
return ast.BooleanTerm(isUnauthenticatedService(string(aStr), string(bStr))), nil
|
||||
|
||||
portNumber, ok := bNum.Int()
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid parameter type: %v", err)
|
||||
}
|
||||
|
||||
return ast.BooleanTerm(isUnauthenticatedService(string(service), portNumber)), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user