added allowed set flag (#169)

This commit is contained in:
RoyUP9
2021-08-05 14:23:16 +03:00
committed by GitHub
parent 90f0f603c7
commit 60533a9591
6 changed files with 47 additions and 9 deletions

View File

@@ -70,5 +70,5 @@ func init() {
tapCmd.Flags().String(configStructs.HumanMaxEntriesDBSizeTapName, defaultTapConfig.HumanMaxEntriesDBSize, "override the default max entries db size of 200mb") tapCmd.Flags().String(configStructs.HumanMaxEntriesDBSizeTapName, defaultTapConfig.HumanMaxEntriesDBSize, "override the default max entries db size of 200mb")
tapCmd.Flags().String(configStructs.DirectionTapName, defaultTapConfig.Direction, "Record traffic that goes in this direction (relative to the tapped pod): in/any") tapCmd.Flags().String(configStructs.DirectionTapName, defaultTapConfig.Direction, "Record traffic that goes in this direction (relative to the tapped pod): in/any")
tapCmd.Flags().Bool(configStructs.DryRunTapName, defaultTapConfig.DryRun, "Preview of all pods matching the regex, without tapping them") tapCmd.Flags().Bool(configStructs.DryRunTapName, defaultTapConfig.DryRun, "Preview of all pods matching the regex, without tapping them")
tapCmd.Flags().String(configStructs.EnforcePolicyFile, "", "Yaml file with policy rules") tapCmd.Flags().String(configStructs.EnforcePolicyFile, defaultTapConfig.EnforcePolicyFile, "Yaml file with policy rules")
} }

View File

@@ -3,6 +3,9 @@ package errormessage
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/up9inc/mizu/cli/mizu"
regexpsyntax "regexp/syntax" regexpsyntax "regexp/syntax"
k8serrors "k8s.io/apimachinery/pkg/api/errors" k8serrors "k8s.io/apimachinery/pkg/api/errors"
@@ -13,11 +16,11 @@ import (
func FormatError(err error) error { func FormatError(err error) error {
var errorNew error var errorNew error
if k8serrors.IsForbidden(err) { if k8serrors.IsForbidden(err) {
errorNew = fmt.Errorf("Insufficient permissions: %w. "+ errorNew = fmt.Errorf("insufficient permissions: %w. "+
"Supply the required permission or control Mizu's access to namespaces by setting MizuResourcesNamespace "+ "supply the required permission or control Mizu's access to namespaces by setting MizuResourcesNamespace "+
"in the config file or setting the tapped namespace with --set mizu-resources-namespace=<NAMEPSACE>.", err) "in the config file or setting the tapped namespace with --%s %s=<NAMEPSACE>", err, mizu.SetCommandName, mizu.MizuResourcesNamespaceConfigName)
} else if syntaxError, isSyntaxError := asRegexSyntaxError(err); isSyntaxError { } else if syntaxError, isSyntaxError := asRegexSyntaxError(err); isSyntaxError {
errorNew = fmt.Errorf("Regex %s is invalid: %w", syntaxError.Expr, err) errorNew = fmt.Errorf("regex %s is invalid: %w", syntaxError.Expr, err)
} else { } else {
errorNew = err errorNew = err
} }

View File

@@ -13,6 +13,7 @@ import (
"github.com/creasty/defaults" "github.com/creasty/defaults"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "github.com/spf13/pflag"
"github.com/up9inc/mizu/cli/mizu/configStructs"
"github.com/up9inc/mizu/cli/uiUtils" "github.com/up9inc/mizu/cli/uiUtils"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@@ -22,13 +23,22 @@ const (
SetCommandName = "set" SetCommandName = "set"
) )
var allowedSetFlags = []string{
AgentImageConfigName,
MizuResourcesNamespaceConfigName,
TelemetryConfigName,
DumpLogsConfigName,
configStructs.AnalysisDestinationTapName,
configStructs.SleepIntervalSecTapName,
}
var Config = ConfigStruct{} var Config = ConfigStruct{}
func (config *ConfigStruct) Validate() error { func (config *ConfigStruct) Validate() error {
if config.IsNsRestrictedMode() { if config.IsNsRestrictedMode() {
if config.Tap.AllNamespaces || len(config.Tap.Namespaces) != 1 || config.Tap.Namespaces[0] != config.MizuResourcesNamespace { if config.Tap.AllNamespaces || len(config.Tap.Namespaces) != 1 || config.Tap.Namespaces[0] != config.MizuResourcesNamespace {
return fmt.Errorf("Not supported mode. Mizu can't resolve IPs in other namespaces when running in namespace restricted mode.\n" + return fmt.Errorf("Not supported mode. Mizu can't resolve IPs in other namespaces when running in namespace restricted mode.\n" +
"You can use the same namespace for --namespace and --mizu-resources-namespace") "You can use the same namespace for --%s and --%s", configStructs.NamespacesTapName, MizuResourcesNamespaceConfigName)
} }
} }
@@ -95,7 +105,7 @@ func initFlag(f *pflag.Flag) {
if f.Name == SetCommandName { if f.Name == SetCommandName {
if setError := mergeSetFlag(sliceValue.GetSlice()); setError != nil { if setError := mergeSetFlag(sliceValue.GetSlice()); setError != nil {
Log.Infof(uiUtils.Red, "Invalid set argument") Log.Warningf(uiUtils.Red, fmt.Sprintf("%v", setError))
} }
return return
} }
@@ -117,6 +127,11 @@ func mergeSetFlag(setValues []string) error {
} }
argumentKey, argumentValue := split[0], split[1] argumentKey, argumentValue := split[0], split[1]
if !Contains(allowedSetFlags, argumentKey) {
return errors.New(fmt.Sprintf("invalid set flag name %s, allowed set flag names: \"%s\"", argumentKey, strings.Join(allowedSetFlags, "\", \"")))
}
mergeFlagValue(configElem, argumentKey, argumentValue) mergeFlagValue(configElem, argumentKey, argumentValue)
} }
@@ -141,7 +156,7 @@ func mergeFlagValue(currentElem reflect.Value, flagKey string, flagValue string)
parsedValue, err := getParsedValue(flagValueKind, flagValue) parsedValue, err := getParsedValue(flagValueKind, flagValue)
if err != nil { if err != nil {
Log.Warningf(uiUtils.Red, fmt.Sprintf("Invalid value %v for key %s, expected %s", flagValue, flagKey, flagValueKind)) Log.Warningf(uiUtils.Red, fmt.Sprintf("Invalid value %v for flag name %s, expected %s", flagValue, flagKey, flagValueKind))
return return
} }
@@ -169,7 +184,7 @@ func mergeFlagValues(currentElem reflect.Value, flagKey string, flagValues []str
for _, flagValue := range flagValues { for _, flagValue := range flagValues {
parsedValue, err := getParsedValue(flagValueKind, flagValue) parsedValue, err := getParsedValue(flagValueKind, flagValue)
if err != nil { if err != nil {
Log.Warningf(uiUtils.Red, fmt.Sprintf("Invalid value %v for key %s, expected %s", flagValue, flagKey, flagValueKind)) Log.Warningf(uiUtils.Red, fmt.Sprintf("Invalid value %v for flag name %s, expected %s", flagValue, flagKey, flagValueKind))
return return
} }

View File

@@ -6,6 +6,13 @@ import (
"github.com/up9inc/mizu/cli/mizu/configStructs" "github.com/up9inc/mizu/cli/mizu/configStructs"
) )
const (
AgentImageConfigName = "agent-image"
MizuResourcesNamespaceConfigName = "mizu-resources-namespace"
TelemetryConfigName = "telemetry"
DumpLogsConfigName = "dump-logs"
)
type ConfigStruct struct { type ConfigStruct struct {
Tap configStructs.TapConfig `yaml:"tap"` Tap configStructs.TapConfig `yaml:"tap"`
Fetch configStructs.FetchConfig `yaml:"fetch"` Fetch configStructs.FetchConfig `yaml:"fetch"`

View File

@@ -10,6 +10,8 @@ import (
) )
const ( const (
AnalysisDestinationTapName = "dest"
SleepIntervalSecTapName = "upload-interval"
GuiPortTapName = "gui-port" GuiPortTapName = "gui-port"
NamespacesTapName = "namespaces" NamespacesTapName = "namespaces"
AnalysisTapName = "analysis" AnalysisTapName = "analysis"

11
cli/mizu/sliceUtils.go Normal file
View File

@@ -0,0 +1,11 @@
package mizu
func Contains(slice []string, containsValue string) bool {
for _, sliceValue := range slice {
if sliceValue == containsValue {
return true
}
}
return false
}