From 034a5405305087a77639a8510ba8e7685e9fdfc9 Mon Sep 17 00:00:00 2001 From: "M. Mert Yildiran" Date: Mon, 26 Dec 2022 07:47:26 +0300 Subject: [PATCH] :bug: Move `HubConfig` and `FrontConfig` to `TapConfig` and add `--front-proxy-port`, `--hub-proxy-port` --- cmd/check/serverConnection.go | 4 ++-- cmd/common.go | 7 +++---- cmd/openRunner.go | 7 ++++--- cmd/tap.go | 4 +++- cmd/tapRunner.go | 10 +++++----- config/config.go | 16 ++-------------- config/configStruct.go | 31 ++++++------------------------- config/configStructs/tapConfig.go | 31 ++++++++++++++++++++++--------- resources/createResources.go | 4 ++-- 9 files changed, 49 insertions(+), 65 deletions(-) diff --git a/cmd/check/serverConnection.go b/cmd/check/serverConnection.go index 2b0ecbae6..36bc08962 100644 --- a/cmd/check/serverConnection.go +++ b/cmd/check/serverConnection.go @@ -12,14 +12,14 @@ func ServerConnection(kubernetesProvider *kubernetes.Provider) bool { var connectedToHub, connectedToFront bool - if err := checkProxy(kubernetes.GetLocalhostOnPort(config.Config.Hub.PortForward.SrcPort), "/echo", kubernetesProvider); err != nil { + if err := checkProxy(kubernetes.GetLocalhostOnPort(config.Config.Tap.Hub.SrcPort), "/echo", kubernetesProvider); err != nil { log.Error().Err(err).Msg("Couldn't connect to Hub using proxy!") } else { connectedToHub = true log.Info().Msg("Connected successfully to Hub using proxy.") } - if err := checkProxy(kubernetes.GetLocalhostOnPort(config.Config.Front.PortForward.SrcPort), "", kubernetesProvider); err != nil { + if err := checkProxy(kubernetes.GetLocalhostOnPort(config.Config.Tap.Front.SrcPort), "", kubernetesProvider); err != nil { log.Error().Err(err).Msg("Couldn't connect to Front using proxy!") } else { connectedToFront = true diff --git a/cmd/common.go b/cmd/common.go index 0ff6ae147..26257b9a3 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -11,7 +11,6 @@ import ( "github.com/kubeshark/base/pkg/models" "github.com/kubeshark/kubeshark/config" - "github.com/kubeshark/kubeshark/config/configStructs" "github.com/kubeshark/kubeshark/errormessage" "github.com/kubeshark/kubeshark/internal/connect" "github.com/kubeshark/kubeshark/kubernetes" @@ -21,12 +20,12 @@ import ( "github.com/rs/zerolog/log" ) -func startProxyReportErrorIfAny(kubernetesProvider *kubernetes.Provider, ctx context.Context, cancel context.CancelFunc, serviceName string, srcPort uint16, dstPort uint16, healthCheck string) { +func startProxyReportErrorIfAny(kubernetesProvider *kubernetes.Provider, ctx context.Context, cancel context.CancelFunc, serviceName string, proxyPortLabel string, srcPort uint16, dstPort uint16, healthCheck string) { httpServer, err := kubernetes.StartProxy(kubernetesProvider, config.Config.Tap.ProxyHost, srcPort, dstPort, config.Config.ResourcesNamespace, serviceName, cancel) if err != nil { log.Error(). Err(errormessage.FormatError(err)). - Msg(fmt.Sprintf("Error occured while running k8s proxy. Try setting different port by using --%s", configStructs.ProxyPortLabel)) + Msg(fmt.Sprintf("Error occured while running k8s proxy. Try setting different port by using --%s", proxyPortLabel)) cancel() return } @@ -45,7 +44,7 @@ func startProxyReportErrorIfAny(kubernetesProvider *kubernetes.Provider, ctx con log.Error(). Str("pod-regex", podRegex.String()). Err(errormessage.FormatError(err)). - Msg(fmt.Sprintf("Error occured while running port forward. Try setting different port by using --%s", configStructs.ProxyPortLabel)) + Msg(fmt.Sprintf("Error occured while running port forward. Try setting different port by using --%s", proxyPortLabel)) cancel() return } diff --git a/cmd/openRunner.go b/cmd/openRunner.go index 38882ef51..88e8930ba 100644 --- a/cmd/openRunner.go +++ b/cmd/openRunner.go @@ -6,6 +6,7 @@ import ( "net/http" "github.com/kubeshark/kubeshark/config" + "github.com/kubeshark/kubeshark/config/configStructs" "github.com/kubeshark/kubeshark/internal/connect" "github.com/kubeshark/kubeshark/kubernetes" "github.com/kubeshark/kubeshark/utils" @@ -40,20 +41,20 @@ func runOpen() { return } - url := kubernetes.GetLocalhostOnPort(config.Config.Front.PortForward.SrcPort) + url := kubernetes.GetLocalhostOnPort(config.Config.Tap.Front.SrcPort) response, err := http.Get(fmt.Sprintf("%s/", url)) if err == nil && response.StatusCode == 200 { log.Info(). Str("service", kubernetes.FrontServiceName). - Int("port", int(config.Config.Front.PortForward.SrcPort)). + Int("port", int(config.Config.Tap.Front.SrcPort)). Msg("Found a running service.") okToOpen(url) return } log.Info().Msg("Establishing connection to K8s cluster...") - startProxyReportErrorIfAny(kubernetesProvider, ctx, cancel, kubernetes.FrontServiceName, config.Config.Front.PortForward.SrcPort, config.Config.Front.PortForward.DstPort, "") + startProxyReportErrorIfAny(kubernetesProvider, ctx, cancel, kubernetes.FrontServiceName, configStructs.ProxyPortFrontLabel, config.Config.Tap.Front.SrcPort, config.Config.Tap.Front.DstPort, "") connector := connect.NewConnector(url, connect.DefaultRetries, connect.DefaultTimeout) if err := connector.TestConnection(""); err != nil { diff --git a/cmd/tap.go b/cmd/tap.go index c667c0cc5..3ace90ff7 100644 --- a/cmd/tap.go +++ b/cmd/tap.go @@ -47,7 +47,9 @@ func init() { } tapCmd.Flags().StringP(configStructs.TagLabel, "t", defaultTapConfig.Tag, "The tag of the Docker images that are going to be pulled.") - tapCmd.Flags().Uint16P(configStructs.ProxyPortLabel, "p", defaultTapConfig.ProxyPort, "Provide a custom port for the web interface webserver.") + tapCmd.Flags().Uint16(configStructs.ProxyPortFrontLabel, defaultTapConfig.Front.SrcPort, "Provide a custom port for the front-end proxy/port-forward.") + tapCmd.Flags().Uint16(configStructs.ProxyPortHubLabel, defaultTapConfig.Hub.SrcPort, "Provide a custom port for the Hub proxy/port-forward.") + tapCmd.Flags().String(configStructs.ProxyHostLabel, defaultTapConfig.ProxyHost, "Provide a custom host for the proxy/port-forward.") tapCmd.Flags().StringSliceP(configStructs.NamespacesLabel, "n", defaultTapConfig.Namespaces, "Namespaces selector.") tapCmd.Flags().BoolP(configStructs.AllNamespacesLabel, "A", defaultTapConfig.AllNamespaces, "Tap all namespaces.") tapCmd.Flags().Bool(configStructs.EnableRedactionLabel, defaultTapConfig.EnableRedaction, "Enables redaction of potentially sensitive request/response headers and body values.") diff --git a/cmd/tapRunner.go b/cmd/tapRunner.go index c2c00e0ef..6d1cd8968 100644 --- a/cmd/tapRunner.go +++ b/cmd/tapRunner.go @@ -44,7 +44,7 @@ func tap() { state.startTime = time.Now() docker.SetTag(config.Config.Tap.Tag) - connector = connect.NewConnector(kubernetes.GetLocalhostOnPort(config.Config.Hub.PortForward.SrcPort), connect.DefaultRetries, connect.DefaultTimeout) + connector = connect.NewConnector(kubernetes.GetLocalhostOnPort(config.Config.Tap.Hub.SrcPort), connect.DefaultRetries, connect.DefaultTimeout) kubernetesProvider, err := getKubernetesProviderForCli() if err != nil { @@ -443,21 +443,21 @@ func watchHubEvents(ctx context.Context, kubernetesProvider *kubernetes.Provider } func postHubStarted(ctx context.Context, kubernetesProvider *kubernetes.Provider, cancel context.CancelFunc) { - startProxyReportErrorIfAny(kubernetesProvider, ctx, cancel, kubernetes.HubServiceName, config.Config.Hub.PortForward.SrcPort, config.Config.Hub.PortForward.DstPort, "/echo") + startProxyReportErrorIfAny(kubernetesProvider, ctx, cancel, kubernetes.HubServiceName, configStructs.ProxyPortFrontLabel, config.Config.Tap.Hub.SrcPort, config.Config.Tap.Hub.DstPort, "/echo") if err := startWorkerSyncer(ctx, cancel, kubernetesProvider, state.targetNamespaces, state.startTime); err != nil { log.Error().Err(errormessage.FormatError(err)).Msg("Error starting kubeshark worker syncer") cancel() } - url := kubernetes.GetLocalhostOnPort(config.Config.Hub.PortForward.SrcPort) + url := kubernetes.GetLocalhostOnPort(config.Config.Tap.Hub.SrcPort) log.Info().Str("url", url).Msg(fmt.Sprintf(utils.Green, "Hub is available at:")) } func postFrontStarted(ctx context.Context, kubernetesProvider *kubernetes.Provider, cancel context.CancelFunc) { - startProxyReportErrorIfAny(kubernetesProvider, ctx, cancel, kubernetes.FrontServiceName, config.Config.Front.PortForward.SrcPort, config.Config.Front.PortForward.DstPort, "") + startProxyReportErrorIfAny(kubernetesProvider, ctx, cancel, kubernetes.FrontServiceName, configStructs.ProxyPortHubLabel, config.Config.Tap.Front.SrcPort, config.Config.Tap.Front.DstPort, "") - url := kubernetes.GetLocalhostOnPort(config.Config.Front.PortForward.SrcPort) + url := kubernetes.GetLocalhostOnPort(config.Config.Tap.Front.SrcPort) log.Info().Str("url", url).Msg(fmt.Sprintf(utils.Green, "Kubeshark is available at:")) if !config.Config.HeadlessMode { diff --git a/config/config.go b/config/config.go index e8bc04156..8b06d3446 100644 --- a/config/config.go +++ b/config/config.go @@ -28,7 +28,7 @@ const ( ) var ( - Config = ConfigStruct{} + Config ConfigStruct DebugMode bool cmdName string ) @@ -50,19 +50,7 @@ func InitConfig(cmd *cobra.Command) error { go version.CheckNewerVersion() - Config.Hub = HubConfig{ - PortForward{ - 8898, - 80, - }, - } - - Config.Front = FrontConfig{ - PortForward{ - 8899, - 80, - }, - } + Config = CreateDefaultConfig() cmdName = cmd.Name() if err := defaults.Set(&Config); err != nil { diff --git a/config/configStruct.go b/config/configStruct.go index c45084451..abb7c9542 100644 --- a/config/configStruct.go +++ b/config/configStruct.go @@ -20,42 +20,23 @@ const ( KubeConfigPathConfigName = "kube-config-path" ) -type PortForward struct { - SrcPort uint16 `yaml:"src-port"` - DstPort uint16 `yaml:"dst-port"` -} - -type HubConfig struct { - PortForward PortForward `yaml:"port-forward"` -} - -type FrontConfig struct { - PortForward PortForward `yaml:"port-forward"` -} - func CreateDefaultConfig() ConfigStruct { config := ConfigStruct{} - config.Hub = HubConfig{ - PortForward{ - 8898, - 80, - }, + config.Tap.Hub = configStructs.HubConfig{ + SrcPort: 8898, + DstPort: 80, } - config.Front = FrontConfig{ - PortForward{ - 8899, - 80, - }, + config.Tap.Front = configStructs.FrontConfig{ + SrcPort: 8899, + DstPort: 80, } return config } type ConfigStruct struct { - Hub HubConfig `yaml:"hub"` - Front FrontConfig `yaml:"front"` Tap configStructs.TapConfig `yaml:"tap"` Logs configStructs.LogsConfig `yaml:"logs"` Config configStructs.ConfigConfig `yaml:"config,omitempty"` diff --git a/config/configStructs/tapConfig.go b/config/configStructs/tapConfig.go index a0bb71d02..25b90befa 100644 --- a/config/configStructs/tapConfig.go +++ b/config/configStructs/tapConfig.go @@ -14,7 +14,9 @@ import ( const ( TagLabel = "tag" - ProxyPortLabel = "proxy-port" + ProxyPortFrontLabel = "proxy-port-front" + ProxyPortHubLabel = "proxy-port-hub" + ProxyHostLabel = "proxy-host" NamespacesLabel = "namespaces" AllNamespacesLabel = "all-namespaces" EnableRedactionLabel = "redact" @@ -26,15 +28,26 @@ const ( ProfilerName = "profiler" ) +type HubConfig struct { + SrcPort uint16 `yaml:"src-port" default:"8898"` + DstPort uint16 `yaml:"dst-port" default:"80"` +} + +type FrontConfig struct { + SrcPort uint16 `yaml:"src-port" default:"8899"` + DstPort uint16 `yaml:"dst-port" default:"80"` +} + type TapConfig struct { - Tag string `yaml:"tag" default:"latest"` - PodRegexStr string `yaml:"regex" default:".*"` - ProxyPort uint16 `yaml:"proxy-port" default:"8899"` - ProxyHost string `yaml:"proxy-host" default:"127.0.0.1"` - Namespaces []string `yaml:"namespaces"` - AllNamespaces bool `yaml:"all-namespaces" default:"false"` - IgnoredUserAgents []string `yaml:"ignored-user-agents"` - EnableRedaction bool `yaml:"redact" default:"false"` + Hub HubConfig `yaml:"hub"` + Front FrontConfig `yaml:"front"` + Tag string `yaml:"tag" default:"latest"` + PodRegexStr string `yaml:"regex" default:".*"` + ProxyHost string `yaml:"proxy-host" default:"127.0.0.1"` + Namespaces []string `yaml:"namespaces"` + AllNamespaces bool `yaml:"all-namespaces" default:"false"` + IgnoredUserAgents []string `yaml:"ignored-user-agents"` + EnableRedaction bool `yaml:"redact" default:"false"` RedactPatterns struct { RequestHeaders []string `yaml:"request-headers"` ResponseHeaders []string `yaml:"response-headers"` diff --git a/resources/createResources.go b/resources/createResources.go index c670f64aa..b9d53d126 100644 --- a/resources/createResources.go +++ b/resources/createResources.go @@ -71,14 +71,14 @@ func CreateHubResources(ctx context.Context, kubernetesProvider *kubernetes.Prov return kubesharkServiceAccountExists, err } - _, err = kubernetesProvider.CreateService(ctx, kubesharkResourcesNamespace, kubernetes.HubServiceName, kubernetes.HubServiceName, 80, int32(config.Config.Hub.PortForward.DstPort), int32(config.Config.Hub.PortForward.SrcPort)) + _, err = kubernetesProvider.CreateService(ctx, kubesharkResourcesNamespace, kubernetes.HubServiceName, kubernetes.HubServiceName, 80, int32(config.Config.Tap.Hub.DstPort), int32(config.Config.Tap.Hub.SrcPort)) if err != nil { return kubesharkServiceAccountExists, err } log.Info().Str("service", kubernetes.HubServiceName).Msg("Successfully created a service.") - _, err = kubernetesProvider.CreateService(ctx, kubesharkResourcesNamespace, kubernetes.FrontServiceName, kubernetes.FrontServiceName, 80, int32(config.Config.Front.PortForward.DstPort), int32(config.Config.Front.PortForward.SrcPort)) + _, err = kubernetesProvider.CreateService(ctx, kubesharkResourcesNamespace, kubernetes.FrontServiceName, kubernetes.FrontServiceName, 80, int32(config.Config.Tap.Front.DstPort), int32(config.Config.Tap.Front.SrcPort)) if err != nil { return kubesharkServiceAccountExists, err }