Skip to content

Commit

Permalink
fix: tracee-ebpf, tracee-rules dowsnt support new flag format
Browse files Browse the repository at this point in the history
change tracee --grpc-listen-addr and --http-listen-addr flag to --server
flag

tracee-ebpf and tracee-rulse supported the old foramt, made them support
the new flag format
  • Loading branch information
ShohamBit committed Jan 9, 2025
1 parent 9179f50 commit 4f0accb
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 60 deletions.
53 changes: 26 additions & 27 deletions cmd/tracee-ebpf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (

"github.com/aquasecurity/tracee/pkg/cmd"
"github.com/aquasecurity/tracee/pkg/cmd/flags"

// "github.com/aquasecurity/tracee/pkg/cmd/flags/server"
"github.com/aquasecurity/tracee/pkg/cmd/flags/server"
"github.com/aquasecurity/tracee/pkg/cmd/initialize"
"github.com/aquasecurity/tracee/pkg/cmd/urfave"
"github.com/aquasecurity/tracee/pkg/logger"
Expand Down Expand Up @@ -129,31 +128,31 @@ func main() {
Value: "/tmp/tracee",
Usage: "path where tracee will install or lookup it's resources",
},
// &cli.BoolFlag{
// Name: server.MetricsEndpointFlag,
// Usage: "enable metrics endpoint",
// Value: false,
// },
// &cli.BoolFlag{
// Name: server.HealthzEndpointFlag,
// Usage: "enable healthz endpoint",
// Value: false,
// },
// &cli.BoolFlag{
// Name: server.PProfEndpointFlag,
// Usage: "enable pprof endpoints",
// Value: false,
// },
// &cli.BoolFlag{
// Name: server.PyroscopeAgentFlag,
// Usage: "enable pyroscope agent",
// Value: false,
// },
// &cli.StringFlag{
// Name: server.HTTPListenEndpointFlag,
// Usage: "listening address of the metrics endpoint server",
// Value: ":3366",
// },
&cli.BoolFlag{
Name: server.HTTPServer + "." + server.MetricsEndpointFlag,
Usage: "enable metrics endpoint",
Value: false,
},
&cli.BoolFlag{
Name: server.HTTPServer + "." + server.HealthzEndpointFlag,
Usage: "enable healthz endpoint",
Value: false,
},
&cli.BoolFlag{
Name: server.HTTPServer + "." + server.PProfEndpointFlag,
Usage: "enable pprof endpoints",
Value: false,
},
&cli.BoolFlag{
Name: server.HTTPServer + "." + server.PyroscopeAgentEndpointFlag,
Usage: "enable pyroscope agent",
Value: false,
},
&cli.StringFlag{
Name: server.HTTPServer + "." + server.ListenEndpointFlag,
Usage: "listening address of the metrics endpoint server",
Value: ":3366",
},
&cli.BoolFlag{
Name: "no-containers",
Usage: "disable container info enrichment to events. safeguard option.",
Expand Down
34 changes: 17 additions & 17 deletions cmd/tracee-rules/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ func main() {
Usage: "configure output format via templates. Usage: --output-template=path/to/my.tmpl",
},
&cli.BoolFlag{
Name: server.PProfEndpointFlag,
Name: server.HTTPServer + "." + server.PProfEndpointFlag,
Usage: "enable pprof endpoints",
Value: false,
},
&cli.BoolFlag{
Name: server.PyroscopeAgentFlag,
Name: server.HTTPServer + "." + server.PyroscopeAgentEndpointFlag,
Usage: "enable pyroscope agent",
Value: false,
},
Expand All @@ -218,21 +218,21 @@ func main() {
Usage: "size of the event channel's buffer consumed by signatures",
Value: 1000,
},
// &cli.BoolFlag{
// Name: server.MetricsEndpointFlag,
// Usage: "enable metrics endpoint",
// Value: false,
// },
// &cli.BoolFlag{
// Name: server.HealthzEndpointFlag,
// Usage: "enable healthz endpoint",
// Value: false,
// },
// &cli.StringFlag{
// Name: server.HTTPListenEndpointFlag,
// Usage: "listening address of the metrics endpoint server",
// Value: ":4466",
// },
&cli.BoolFlag{
Name: server.HTTPServer + "." + server.MetricsEndpointFlag,
Usage: "enable metrics endpoint",
Value: false,
},
&cli.BoolFlag{
Name: server.HTTPServer + "." + server.HealthzEndpointFlag,
Usage: "enable healthz endpoint",
Value: false,
},
&cli.StringFlag{
Name: server.HTTPServer + "." + server.ListenEndpointFlag,
Usage: "listening address of the metrics endpoint server",
Value: ":4466",
},
&cli.BoolFlag{
Name: "allcaps",
Value: false,
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/cobra/cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,13 @@ func GetTraceeRunner(c *cobra.Command, version string) (cmd.Runner, error) {
if err != nil {
return runner, err
}
server, err := server.PrepareServer(serverFlag)
serverRunner, err := server.PrepareServer(serverFlag)
if err != nil {
return runner, err
}

runner.HTTPServer = server.HTTPServer
runner.GRPCServer = server.GRPCServer
runner.HTTPServer = serverRunner.HTTPServer
runner.GRPCServer = serverRunner.GRPCServer
runner.TraceeConfig = cfg
runner.Printer = p
runner.InstallPath = traceeInstallPath
Expand Down
33 changes: 25 additions & 8 deletions pkg/cmd/flags/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"net/url"
"os"
"strconv"
"strings"

"github.com/aquasecurity/tracee/pkg/errfmt"
Expand Down Expand Up @@ -50,7 +51,7 @@ func PrepareServer(serverSlice []string) (*Server, error) {
return nil, fmt.Errorf("cannot process the flag: try grpc.Xxx or http.Xxx instead")
}
switch serverParts[0] {
//flag http.Xxx
// flag http.Xxx
case HTTPServer:
httpParts := strings.SplitN(serverParts[1], "=", 2)
switch httpParts[0] {
Expand Down Expand Up @@ -79,7 +80,7 @@ func PrepareServer(serverSlice []string) (*Server, error) {
default:
return nil, errors.New("invalid http flag, consider using one of the following commands: address, metrics, healthz, pprof, pyroscope")
}
//flag grpc.Xxx
// flag grpc.Xxx
case GRPCServer:
grpcParts := strings.SplitN(serverParts[1], "=", 2)
switch grpcParts[0] {
Expand Down Expand Up @@ -138,23 +139,39 @@ func PrepareServer(serverSlice []string) (*Server, error) {
}

func isValidAddr(addr string) bool {
// Check if the address is a valid URL.
_, err := url.ParseRequestURI("http://" + addr)
if err != nil {
return false
}

host, port, err := net.SplitHostPort(addr)
if err != nil {
// Check if the address contains a port.
if !strings.Contains(addr, ":") {
return false
}

ip := net.ParseIP(host)
if ip == nil && host != "localhost" && host != "0.0.0.0" {
// Split the address into host and port.
host, portStr, err := net.SplitHostPort(addr)
if err != nil {
return false
}

_, err = net.LookupPort("tcp", port)
if err != nil {
// If a host is specified, check if it's a valid IP address or hostname.
if host != "" {
ip := net.ParseIP(host)
if ip == nil {
_, err := net.LookupHost(host)
if err != nil {
return false
}
}
}
// Check if the port is a valid integer and within the allowed range.
port, err := strconv.Atoi(portStr)
if err != nil || port < 0 || port > 65535 {
return false
}
if port == 0 {
return false
}

Expand Down
14 changes: 11 additions & 3 deletions pkg/cmd/flags/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"fmt"
"testing"

"github.com/aquasecurity/tracee/pkg/server/grpc"
"github.com/aquasecurity/tracee/pkg/server/http"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/aquasecurity/tracee/pkg/server/grpc"
"github.com/aquasecurity/tracee/pkg/server/http"
)

func TestPrepareServer(t *testing.T) {
Expand All @@ -26,6 +27,14 @@ func TestPrepareServer(t *testing.T) {
},
expectedError: nil,
},
{
testName: "http server only just port",
serverFlags: []string{"http.address=:8080"},
expectedServer: &Server{
HTTPServer: http.New(":8080"),
},
expectedError: nil,
},
{
testName: "grpc server only",
serverFlags: []string{"grpc.address=unix:/tmp/tracee.sock"},
Expand Down Expand Up @@ -122,7 +131,6 @@ func TestPrepareServer(t *testing.T) {
} else {
assert.Equal(t, testcase.expectedServer, server)
}

})
}
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/cmd/urfave/urfave.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (

"github.com/aquasecurity/tracee/pkg/cmd"
"github.com/aquasecurity/tracee/pkg/cmd/flags"

// "github.com/aquasecurity/tracee/pkg/cmd/flags/server"
"github.com/aquasecurity/tracee/pkg/cmd/initialize"
"github.com/aquasecurity/tracee/pkg/cmd/printer"
"github.com/aquasecurity/tracee/pkg/config"
Expand Down

0 comments on commit 4f0accb

Please sign in to comment.