mirror of
https://github.com/kubeshark/kubeshark.git
synced 2026-08-25 00:47:27 +00:00
* deps: bump indirect deps to clear critical/high Dependabot alerts Bumps the vulnerable indirect dependencies flagged as critical or high severity in Dependabot: - golang.org/x/crypto v0.39.0 -> v0.54.0 (7 critical + 2 high: SSH agent constraint/key-constraint bypass, @revoked auth bypass, FIDO/U2F presence check bypass, VerifiedPublicKeyCallback permission skip, infinite loop on large channel writes, client-induced server deadlock, RSA/DSA DoS, byte arithmetic underflow panic) - google.golang.org/grpc v1.68.1 -> v1.83.0 (critical: authz bypass via missing leading slash in :path; high: xDS RBAC and HTTP/2 issues) - github.com/containerd/containerd v1.7.27 -> v1.7.34 (high: LABEL -> restart-monitor binary:// host-root RCE, runAsNonRoot evasion, local privesc via wide CRI directory permissions) - oras.land/oras-go/v2 v2.6.0 -> v2.6.2 (high: CVE-2026-50163 hardlink extract-dir escape, credential forwarding via unvalidated Location header) - github.com/moby/spdystream v0.5.0 -> v0.5.1 (high: DoS on CRI) Transitively pulls up x/net, x/sync, x/sys, x/term, x/text, x/time, x/oauth2, protobuf, filepath-securejoin, selinux and go-logr via go mod tidy. The go directive moves 1.24.0 -> 1.25.0 (required by the upgraded modules); the explicit toolchain pin is dropped. CI resolves Go from go.mod, so no workflow changes are needed. go build ./... and go test ./... pass. * ci: move golangci-lint to v2, fix resulting lint issues golangci-lint-action@v3 pins `latest` to v1.64.8, which is built with go1.24 and refuses to run now that go.mod targets 1.25.0: can't load config: the Go language version (go1.24) used to build golangci-lint is lower than the targeted Go version (1.25.0) Move the job to golangci-lint-action@v7 + v2.8.0 and add a .golangci.yml mirroring the hub repo's v2 config: govet, staticcheck, ineffassign and unused, plus gofmt/goimports as formatters. Fixes for the issues that surfaced: - ST1005: lowercase error strings, drop trailing '!' in connect/hub.go - SA4011: kubernetes/watch.go had a `break` inside a `select` default that broke the select rather than the loop, i.e. a no-op; removed - QF1008: drop the embedded ChartPathOptions selector in helm.go - QF1003: tagged switch on r.URL.Path in mcp_test.go - QF1004: strings.Replace(..., -1) -> strings.ReplaceAll - gofmt -s and goimports with a local prefix across the tree errcheck is not in the enabled set, matching hub. * cmd: clarify --time parse error in pcap dump The error neither named the offending flag/value nor separated the wrapped error from the message. Reported by Copilot on #1952. --------- Co-authored-by: Alon Girmonsky <1990761+alongir@users.noreply.github.com>
442 lines
11 KiB
Go
442 lines
11 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/creasty/defaults"
|
|
"github.com/goccy/go-yaml"
|
|
"github.com/rs/zerolog"
|
|
"github.com/rs/zerolog/log"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/pflag"
|
|
|
|
"github.com/kubeshark/kubeshark/misc"
|
|
"github.com/kubeshark/kubeshark/misc/version"
|
|
"github.com/kubeshark/kubeshark/utils"
|
|
)
|
|
|
|
const (
|
|
Separator = "="
|
|
SetCommandName = "set"
|
|
FieldNameTag = "yaml"
|
|
ReadonlyTag = "readonly"
|
|
DebugFlag = "debug"
|
|
ConfigPathFlag = "config-path"
|
|
)
|
|
|
|
var (
|
|
Config ConfigStruct
|
|
DebugMode bool
|
|
cmdName string
|
|
ConfigFilePath string
|
|
)
|
|
|
|
func InitConfig(cmd *cobra.Command) error {
|
|
var err error
|
|
DebugMode, err = cmd.Flags().GetBool(DebugFlag)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg(fmt.Sprintf("Can't receive '%s' flag", DebugFlag))
|
|
}
|
|
|
|
if DebugMode {
|
|
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
|
}
|
|
|
|
if cmd.Use == "version" {
|
|
return nil
|
|
}
|
|
|
|
if !utils.Contains([]string{
|
|
"console",
|
|
"pro",
|
|
"manifests",
|
|
"license",
|
|
"mcp",
|
|
}, cmd.Use) {
|
|
go version.CheckNewerVersion()
|
|
}
|
|
|
|
Config = CreateDefaultConfig()
|
|
Config.Tap.Debug = DebugMode
|
|
if DebugMode {
|
|
Config.LogLevel = "debug"
|
|
}
|
|
cmdName = cmd.Name()
|
|
if utils.Contains([]string{
|
|
"clean",
|
|
"console",
|
|
"pro",
|
|
"proxy",
|
|
"scripts",
|
|
"pprof",
|
|
}, cmdName) {
|
|
cmdName = "tap"
|
|
}
|
|
|
|
if err := defaults.Set(&Config); err != nil {
|
|
return err
|
|
}
|
|
|
|
ConfigFilePath = GetConfigFilePath(cmd)
|
|
if err := loadConfigFile(&Config, utils.Contains([]string{
|
|
"manifests",
|
|
"license",
|
|
}, cmd.Use)); err != nil {
|
|
if !os.IsNotExist(err) {
|
|
return fmt.Errorf("invalid config, %w\n"+
|
|
"you can regenerate the file by removing it (%v) and using `kubeshark config -r`", err, ConfigFilePath)
|
|
}
|
|
}
|
|
|
|
cmd.Flags().Visit(initFlag)
|
|
|
|
log.Debug().Interface("config", Config).Msg("Init config is finished.")
|
|
|
|
return nil
|
|
}
|
|
|
|
func GetConfigWithDefaults() (*ConfigStruct, error) {
|
|
defaultConf := ConfigStruct{}
|
|
if err := defaults.Set(&defaultConf); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
configElem := reflect.ValueOf(&defaultConf).Elem()
|
|
setZeroForReadonlyFields(configElem)
|
|
|
|
return &defaultConf, nil
|
|
}
|
|
|
|
func WriteConfig(config *ConfigStruct) error {
|
|
template, err := utils.PrettyYaml(config)
|
|
if err != nil {
|
|
return fmt.Errorf("failed converting config to yaml, err: %v", err)
|
|
}
|
|
|
|
data := []byte(template)
|
|
|
|
if _, err := os.Stat(ConfigFilePath); os.IsNotExist(err) {
|
|
err = os.MkdirAll(filepath.Dir(ConfigFilePath), 0700)
|
|
if err != nil {
|
|
return fmt.Errorf("failed creating directories, err: %v", err)
|
|
}
|
|
}
|
|
|
|
if err := os.WriteFile(ConfigFilePath, data, 0644); err != nil {
|
|
return fmt.Errorf("failed writing config, err: %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func GetConfigFilePath(cmd *cobra.Command) string {
|
|
defaultConfigPath := path.Join(misc.GetDotFolderPath(), "config.yaml")
|
|
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
return defaultConfigPath
|
|
}
|
|
|
|
if cmd != nil {
|
|
configPathOverride, err := cmd.Flags().GetString(ConfigPathFlag)
|
|
if err == nil {
|
|
if configPathOverride != "" {
|
|
resolvedConfigPath, err := filepath.Abs(configPathOverride)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("--config-path flag path cannot be resolved")
|
|
} else {
|
|
return resolvedConfigPath
|
|
}
|
|
}
|
|
} else {
|
|
log.Error().Err(err).Msg("--config-path flag parser error")
|
|
}
|
|
}
|
|
|
|
cwdConfig := filepath.Join(cwd, fmt.Sprintf("%s.yaml", misc.Program))
|
|
reader, err := os.Open(cwdConfig)
|
|
if err != nil {
|
|
return defaultConfigPath
|
|
} else {
|
|
reader.Close()
|
|
return cwdConfig
|
|
}
|
|
}
|
|
|
|
func loadConfigFile(config *ConfigStruct, silent bool) error {
|
|
reader, err := os.Open(ConfigFilePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer reader.Close()
|
|
|
|
buf, err := io.ReadAll(reader)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := yaml.Unmarshal(buf, config); err != nil {
|
|
return err
|
|
}
|
|
|
|
if !silent {
|
|
log.Info().Str("path", ConfigFilePath).Msg("Found config file!")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func initFlag(f *pflag.Flag) {
|
|
configElemValue := reflect.ValueOf(&Config).Elem()
|
|
|
|
var flagPath []string
|
|
flagPath = append(flagPath, cmdName)
|
|
|
|
flagPath = append(flagPath, strings.Split(f.Name, "-")...)
|
|
|
|
flagPathJoined := strings.Join(flagPath, ".")
|
|
if strings.HasSuffix(flagPathJoined, ".config.path") {
|
|
return
|
|
}
|
|
|
|
sliceValue, isSliceValue := f.Value.(pflag.SliceValue)
|
|
if !isSliceValue {
|
|
if err := mergeFlagValue(configElemValue, flagPath, flagPathJoined, f.Value.String()); err != nil {
|
|
log.Warn().Err(err).Send()
|
|
}
|
|
return
|
|
}
|
|
|
|
if f.Name == SetCommandName {
|
|
if err := mergeSetFlag(configElemValue, sliceValue.GetSlice()); err != nil {
|
|
log.Warn().Err(err).Send()
|
|
}
|
|
return
|
|
}
|
|
|
|
if err := mergeFlagValues(configElemValue, flagPath, flagPathJoined, sliceValue.GetSlice()); err != nil {
|
|
log.Warn().Err(err).Send()
|
|
}
|
|
}
|
|
|
|
func mergeSetFlag(configElemValue reflect.Value, setValues []string) error {
|
|
var setErrors []string
|
|
setMap := map[string][]string{}
|
|
|
|
for _, setValue := range setValues {
|
|
if !strings.Contains(setValue, Separator) {
|
|
setErrors = append(setErrors, fmt.Sprintf("Ignoring set argument %s (set argument format: <flag name>=<flag value>)", setValue))
|
|
continue
|
|
}
|
|
|
|
split := strings.SplitN(setValue, Separator, 2)
|
|
argumentKey, argumentValue := split[0], split[1]
|
|
|
|
setMap[argumentKey] = append(setMap[argumentKey], argumentValue)
|
|
}
|
|
|
|
for argumentKey, argumentValues := range setMap {
|
|
flagPath := strings.Split(argumentKey, ".")
|
|
|
|
if len(argumentValues) > 1 {
|
|
if err := mergeFlagValues(configElemValue, flagPath, argumentKey, argumentValues); err != nil {
|
|
setErrors = append(setErrors, fmt.Sprintf("%v", err))
|
|
}
|
|
} else {
|
|
if err := mergeFlagValue(configElemValue, flagPath, argumentKey, argumentValues[0]); err != nil {
|
|
setErrors = append(setErrors, fmt.Sprintf("%v", err))
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(setErrors) > 0 {
|
|
return errors.New(strings.Join(setErrors, "\n"))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func mergeFlagValue(configElemValue reflect.Value, flagPath []string, fullFlagName string, flagValue string) error {
|
|
mergeFunction := func(flagName string, currentFieldStruct reflect.StructField, currentFieldElemValue reflect.Value, currentElemValue reflect.Value) error {
|
|
currentFieldKind := currentFieldStruct.Type.Kind()
|
|
|
|
if currentFieldKind == reflect.Slice {
|
|
return mergeFlagValues(currentElemValue, []string{flagName}, fullFlagName, []string{flagValue})
|
|
}
|
|
|
|
parsedValue, err := getParsedValue(currentFieldKind, flagValue)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid value %s for flag name %s, expected %s", flagValue, flagName, currentFieldKind)
|
|
}
|
|
|
|
currentFieldElemValue.Set(parsedValue)
|
|
return nil
|
|
}
|
|
|
|
return mergeFlag(configElemValue, flagPath, fullFlagName, mergeFunction)
|
|
}
|
|
|
|
func mergeFlagValues(configElemValue reflect.Value, flagPath []string, fullFlagName string, flagValues []string) error {
|
|
mergeFunction := func(flagName string, currentFieldStruct reflect.StructField, currentFieldElemValue reflect.Value, currentElemValue reflect.Value) error {
|
|
currentFieldKind := currentFieldStruct.Type.Kind()
|
|
|
|
if currentFieldKind != reflect.Slice {
|
|
return fmt.Errorf("invalid values %s for flag name %s, expected %s", strings.Join(flagValues, ","), flagName, currentFieldKind)
|
|
}
|
|
|
|
flagValueKind := currentFieldStruct.Type.Elem().Kind()
|
|
|
|
parsedValues := reflect.MakeSlice(reflect.SliceOf(currentFieldStruct.Type.Elem()), 0, 0)
|
|
for _, flagValue := range flagValues {
|
|
parsedValue, err := getParsedValue(flagValueKind, flagValue)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid value %s for flag name %s, expected %s", flagValue, flagName, flagValueKind)
|
|
}
|
|
|
|
parsedValues = reflect.Append(parsedValues, parsedValue)
|
|
}
|
|
|
|
currentFieldElemValue.Set(parsedValues)
|
|
return nil
|
|
}
|
|
|
|
return mergeFlag(configElemValue, flagPath, fullFlagName, mergeFunction)
|
|
}
|
|
|
|
func mergeFlag(currentElemValue reflect.Value, currentFlagPath []string, fullFlagName string, mergeFunction func(flagName string, currentFieldStruct reflect.StructField, currentFieldElemValue reflect.Value, currentElemValue reflect.Value) error) error {
|
|
if len(currentFlagPath) == 0 {
|
|
return fmt.Errorf("flag \"%s\" not found", fullFlagName)
|
|
}
|
|
|
|
for i := 0; i < currentElemValue.NumField(); i++ {
|
|
currentFieldStruct := currentElemValue.Type().Field(i)
|
|
currentFieldElemValue := currentElemValue.FieldByName(currentFieldStruct.Name)
|
|
|
|
if currentFieldStruct.Type.Kind() == reflect.Struct && getFieldNameByTag(currentFieldStruct) == currentFlagPath[0] {
|
|
return mergeFlag(currentFieldElemValue, currentFlagPath[1:], fullFlagName, mergeFunction)
|
|
}
|
|
|
|
if len(currentFlagPath) > 1 || getFieldNameByTag(currentFieldStruct) != currentFlagPath[0] {
|
|
continue
|
|
}
|
|
|
|
return mergeFunction(currentFlagPath[0], currentFieldStruct, currentFieldElemValue, currentElemValue)
|
|
}
|
|
|
|
return fmt.Errorf("flag \"%s\" not found", fullFlagName)
|
|
}
|
|
|
|
func getFieldNameByTag(field reflect.StructField) string {
|
|
return strings.Split(field.Tag.Get(FieldNameTag), ",")[0]
|
|
}
|
|
|
|
func getParsedValue(kind reflect.Kind, value string) (reflect.Value, error) {
|
|
switch kind {
|
|
case reflect.String:
|
|
return reflect.ValueOf(value), nil
|
|
case reflect.Bool:
|
|
boolArgumentValue, err := strconv.ParseBool(value)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
return reflect.ValueOf(boolArgumentValue), nil
|
|
case reflect.Int:
|
|
intArgumentValue, err := strconv.ParseInt(value, 10, 64)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
return reflect.ValueOf(int(intArgumentValue)), nil
|
|
case reflect.Int8:
|
|
intArgumentValue, err := strconv.ParseInt(value, 10, 8)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
return reflect.ValueOf(int8(intArgumentValue)), nil
|
|
case reflect.Int16:
|
|
intArgumentValue, err := strconv.ParseInt(value, 10, 16)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
return reflect.ValueOf(int16(intArgumentValue)), nil
|
|
case reflect.Int32:
|
|
intArgumentValue, err := strconv.ParseInt(value, 10, 32)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
return reflect.ValueOf(int32(intArgumentValue)), nil
|
|
case reflect.Int64:
|
|
intArgumentValue, err := strconv.ParseInt(value, 10, 64)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
return reflect.ValueOf(intArgumentValue), nil
|
|
case reflect.Uint:
|
|
uintArgumentValue, err := strconv.ParseUint(value, 10, 64)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
return reflect.ValueOf(uint(uintArgumentValue)), nil
|
|
case reflect.Uint8:
|
|
uintArgumentValue, err := strconv.ParseUint(value, 10, 8)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
return reflect.ValueOf(uint8(uintArgumentValue)), nil
|
|
case reflect.Uint16:
|
|
uintArgumentValue, err := strconv.ParseUint(value, 10, 16)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
return reflect.ValueOf(uint16(uintArgumentValue)), nil
|
|
case reflect.Uint32:
|
|
uintArgumentValue, err := strconv.ParseUint(value, 10, 32)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
return reflect.ValueOf(uint32(uintArgumentValue)), nil
|
|
case reflect.Uint64:
|
|
uintArgumentValue, err := strconv.ParseUint(value, 10, 64)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
return reflect.ValueOf(uintArgumentValue), nil
|
|
}
|
|
|
|
return reflect.ValueOf(nil), errors.New("value to parse does not match type")
|
|
}
|
|
|
|
func setZeroForReadonlyFields(currentElem reflect.Value) {
|
|
for i := 0; i < currentElem.NumField(); i++ {
|
|
currentField := currentElem.Type().Field(i)
|
|
currentFieldByName := currentElem.FieldByName(currentField.Name)
|
|
|
|
if currentField.Type.Kind() == reflect.Struct {
|
|
setZeroForReadonlyFields(currentFieldByName)
|
|
continue
|
|
}
|
|
|
|
if _, ok := currentField.Tag.Lookup(ReadonlyTag); ok {
|
|
currentFieldByName.Set(reflect.Zero(currentField.Type))
|
|
}
|
|
}
|
|
}
|