🐛 Move HubConfig and FrontConfig to TapConfig and add --front-proxy-port, --hub-proxy-port

This commit is contained in:
M. Mert Yildiran 2022-12-26 07:47:26 +03:00
parent 57257025d2
commit 034a540530
No known key found for this signature in database
GPG Key ID: DA5D6DCBB758A461
9 changed files with 49 additions and 65 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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 {

View File

@ -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.")

View File

@ -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 {

View File

@ -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 {

View File

@ -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"`

View File

@ -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"`

View File

@ -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
}