diff --git a/cmd/dump/dump.go b/cmd/dump/dump.go index bd67f593..bab90500 100644 --- a/cmd/dump/dump.go +++ b/cmd/dump/dump.go @@ -1,12 +1,10 @@ package dump import ( - "context" "os" "github.com/clevyr/kubedb/internal/actions/dump" "github.com/clevyr/kubedb/internal/config/flags" - "github.com/clevyr/kubedb/internal/config/namespace_filter" "github.com/clevyr/kubedb/internal/consts" "github.com/clevyr/kubedb/internal/util" "github.com/spf13/cobra" @@ -53,7 +51,6 @@ the generated filename might look like "` + dump.HelpFilename() + `"`, flags.RemoteGzip(cmd) flags.Spinner(cmd, &action.Spinner) - cmd.SetContext(namespace_filter.NewContext(context.Background(), namespace_filter.ReadOnly)) return cmd } diff --git a/cmd/exec/exec.go b/cmd/exec/exec.go index 51453153..4ebe20d3 100644 --- a/cmd/exec/exec.go +++ b/cmd/exec/exec.go @@ -1,11 +1,8 @@ package exec import ( - "context" - "github.com/clevyr/kubedb/internal/actions/exec" "github.com/clevyr/kubedb/internal/config/flags" - "github.com/clevyr/kubedb/internal/config/namespace_filter" "github.com/clevyr/kubedb/internal/consts" "github.com/clevyr/kubedb/internal/util" "github.com/spf13/cobra" @@ -32,7 +29,6 @@ func New() *cobra.Command { flags.Password(cmd) cmd.Flags().StringVarP(&action.Command, consts.CommandFlag, "c", "", "Run a single command and exit") - cmd.SetContext(namespace_filter.NewContext(context.Background(), namespace_filter.ReadWrite)) return cmd } diff --git a/cmd/port_forward/port_forward.go b/cmd/port_forward/port_forward.go index a0cb367d..dcc48fbf 100644 --- a/cmd/port_forward/port_forward.go +++ b/cmd/port_forward/port_forward.go @@ -1,12 +1,10 @@ package port_forward import ( - "context" "strconv" "github.com/clevyr/kubedb/internal/actions/port_forward" "github.com/clevyr/kubedb/internal/config/flags" - "github.com/clevyr/kubedb/internal/config/namespace_filter" "github.com/clevyr/kubedb/internal/consts" "github.com/clevyr/kubedb/internal/util" "github.com/spf13/cobra" @@ -57,7 +55,6 @@ func New() *cobra.Command { panic(err) } - cmd.SetContext(namespace_filter.NewContext(context.Background(), namespace_filter.ReadWrite)) return cmd } diff --git a/cmd/restore/restore.go b/cmd/restore/restore.go index cbae371c..0f2b437a 100644 --- a/cmd/restore/restore.go +++ b/cmd/restore/restore.go @@ -1,14 +1,12 @@ package restore import ( - "context" "errors" "os" "github.com/AlecAivazis/survey/v2" "github.com/clevyr/kubedb/internal/actions/restore" "github.com/clevyr/kubedb/internal/config/flags" - "github.com/clevyr/kubedb/internal/config/namespace_filter" "github.com/clevyr/kubedb/internal/consts" "github.com/clevyr/kubedb/internal/util" "github.com/spf13/cobra" @@ -55,7 +53,6 @@ Supported Input Filetypes: flags.Spinner(cmd, &action.Spinner) cmd.Flags().BoolVarP(&action.Force, consts.ForceFlag, "f", false, "Do not prompt before restore") - cmd.SetContext(namespace_filter.NewContext(context.Background(), namespace_filter.ReadWrite)) return cmd } diff --git a/cmd/status/status.go b/cmd/status/status.go index 239d29a5..6d127e9c 100644 --- a/cmd/status/status.go +++ b/cmd/status/status.go @@ -1,7 +1,6 @@ package status import ( - "context" "errors" "fmt" "os" @@ -10,7 +9,6 @@ import ( "github.com/clevyr/kubedb/internal/config" "github.com/clevyr/kubedb/internal/config/flags" - "github.com/clevyr/kubedb/internal/config/namespace_filter" "github.com/clevyr/kubedb/internal/database" "github.com/clevyr/kubedb/internal/kubernetes" "github.com/clevyr/kubedb/internal/util" @@ -34,7 +32,6 @@ func New() *cobra.Command { flags.Username(cmd) flags.Password(cmd) - cmd.SetContext(namespace_filter.NewContext(context.Background(), namespace_filter.ReadOnly)) return cmd } diff --git a/internal/config/flags/kubernetes.go b/internal/config/flags/kubernetes.go index a6048b9f..f5db51be 100644 --- a/internal/config/flags/kubernetes.go +++ b/internal/config/flags/kubernetes.go @@ -4,7 +4,6 @@ import ( "os" "path/filepath" - "github.com/clevyr/kubedb/internal/config/namespace_filter" "github.com/clevyr/kubedb/internal/consts" "github.com/clevyr/kubedb/internal/kubernetes" "github.com/spf13/cobra" @@ -68,11 +67,8 @@ func Namespace(cmd *cobra.Command) { return nil, cobra.ShellCompDirectiveError } names := make([]string, 0, len(namespaces.Items)) - access := namespace_filter.NewFromContext(cmd.Context()) for _, namespace := range namespaces.Items { - if access.Match(namespace.Name) { - names = append(names, namespace.Name) - } + names = append(names, namespace.Name) } return names, cobra.ShellCompDirectiveNoFileComp }) diff --git a/internal/config/namespace_filter/namespace_filter.go b/internal/config/namespace_filter/namespace_filter.go deleted file mode 100644 index 92803f78..00000000 --- a/internal/config/namespace_filter/namespace_filter.go +++ /dev/null @@ -1,62 +0,0 @@ -package namespace_filter - -import ( - "context" - "regexp" - - "github.com/clevyr/kubedb/internal/consts" - "github.com/spf13/viper" -) - -var ( - namespaceFilterReadOnly = ".*" - namespaceFilterReadWrite = "-(dev|stage|test|demo|temp[0-9]+|pr[0-9]+)$" -) - -type AccessLevel uint8 - -const ( - ReadWrite AccessLevel = iota - ReadOnly -) - -type NamespaceRegexp struct { - re *regexp.Regexp -} - -func (level NamespaceRegexp) Match(namespace string) bool { - if !viper.GetBool(consts.NamespaceFilterKey) { - return true - } - return level.re.MatchString(namespace) -} - -func NewFromContext(ctx context.Context) NamespaceRegexp { - accessLevel, _ := LevelFromContext(ctx) - - switch accessLevel { - case ReadOnly: - return NamespaceRegexp{re: regexp.MustCompile(namespaceFilterReadOnly)} - default: - return NamespaceRegexp{re: regexp.MustCompile(namespaceFilterReadWrite)} - } -} - -func init() { - viper.SetDefault(consts.NamespaceFilterKey, true) - viper.SetDefault(consts.NamespaceFilterROKey, namespaceFilterReadOnly) - viper.SetDefault(consts.NamespaceFilterRWKey, namespaceFilterReadWrite) -} - -type contextKey string - -var accessLevelKey = contextKey("accessLevel") - -func NewContext(ctx context.Context, level AccessLevel) context.Context { - return context.WithValue(ctx, accessLevelKey, level) -} - -func LevelFromContext(ctx context.Context) (AccessLevel, bool) { - filter, ok := ctx.Value(accessLevelKey).(AccessLevel) - return filter, ok -} diff --git a/internal/config/namespace_filter/namespace_filter_test.go b/internal/config/namespace_filter/namespace_filter_test.go deleted file mode 100644 index d14f1e05..00000000 --- a/internal/config/namespace_filter/namespace_filter_test.go +++ /dev/null @@ -1,76 +0,0 @@ -package namespace_filter - -import ( - "context" - "regexp" - "testing" - - "github.com/clevyr/kubedb/internal/consts" - "github.com/spf13/viper" - "github.com/stretchr/testify/assert" -) - -func TestNamespaceRegexp_Match(t *testing.T) { - type fields struct { - re *regexp.Regexp - } - type args struct { - namespace string - } - tests := []struct { - name string - fields fields - args args - enabled bool - want bool - }{ - {"dev-ro", fields{regexp.MustCompile(namespaceFilterReadOnly)}, args{"example-dev"}, true, true}, - {"dev-rw", fields{regexp.MustCompile(namespaceFilterReadWrite)}, args{"example-dev"}, true, true}, - {"stage-ro", fields{regexp.MustCompile(namespaceFilterReadOnly)}, args{"example-stage"}, true, true}, - {"stage-rw", fields{regexp.MustCompile(namespaceFilterReadWrite)}, args{"example-stage"}, true, true}, - {"test-ro", fields{regexp.MustCompile(namespaceFilterReadOnly)}, args{"example-test"}, true, true}, - {"test-rw", fields{regexp.MustCompile(namespaceFilterReadWrite)}, args{"example-test"}, true, true}, - {"demo-ro", fields{regexp.MustCompile(namespaceFilterReadOnly)}, args{"example-demo"}, true, true}, - {"demo-rw", fields{regexp.MustCompile(namespaceFilterReadWrite)}, args{"example-demo"}, true, true}, - {"pr-ro", fields{regexp.MustCompile(namespaceFilterReadOnly)}, args{"example-pr1"}, true, true}, - {"pr-rw", fields{regexp.MustCompile(namespaceFilterReadWrite)}, args{"example-pr1"}, true, true}, - {"prod-ro", fields{regexp.MustCompile(namespaceFilterReadOnly)}, args{"example-prod"}, true, true}, - {"prod-rw", fields{regexp.MustCompile(namespaceFilterReadWrite)}, args{"example-prod"}, true, false}, - {"other-ro", fields{regexp.MustCompile(namespaceFilterReadOnly)}, args{"example"}, true, true}, - {"other-rw", fields{regexp.MustCompile(namespaceFilterReadWrite)}, args{"example"}, true, false}, - {"disabled", fields{regexp.MustCompile(namespaceFilterReadWrite)}, args{"example-prod"}, false, true}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - level := NamespaceRegexp{ - re: tt.fields.re, - } - viper.Set(consts.NamespaceFilterKey, tt.enabled) - got := level.Match(tt.args.namespace) - assert.Equal(t, tt.want, got) - }) - } -} - -func TestNewFromContext(t *testing.T) { - roCtx := NewContext(context.Background(), ReadOnly) - rwCtx := NewContext(context.Background(), ReadWrite) - - type args struct { - ctx context.Context - } - tests := []struct { - name string - args args - want NamespaceRegexp - }{ - {"ro", args{roCtx}, NamespaceRegexp{re: regexp.MustCompile(namespaceFilterReadOnly)}}, - {"rw", args{rwCtx}, NamespaceRegexp{re: regexp.MustCompile(namespaceFilterReadWrite)}}, - {"unset", args{context.Background()}, NamespaceRegexp{re: regexp.MustCompile(namespaceFilterReadWrite)}}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - assert.Equal(t, tt.want, NewFromContext(tt.args.ctx)) - }) - } -} diff --git a/internal/consts/viper.go b/internal/consts/viper.go index 7adfb5c9..2ac70081 100644 --- a/internal/consts/viper.go +++ b/internal/consts/viper.go @@ -1,17 +1,14 @@ package consts const ( - AnalyzeKey = "restore.analyze" - SpinnerKey = "spinner.name" - KubeconfigKey = "kubernetes.kubeconfig" - JobPodLabelsKey = "kubernetes.job-pod-labels" - NoJobKey = "kubernetes.no-job" - LogLevelKey = "log.level" - LogFormatKey = "log.format" - LogRedactKey = "log.redact" - RemoteGzipKey = "remote-gzip" - NamespaceFilterKey = "namespace.filter" - NamespaceFilterROKey = "namespace.ro" - NamespaceFilterRWKey = "namespace.rw" - PortForwardAddrKey = "port-forward.address" + AnalyzeKey = "restore.analyze" + SpinnerKey = "spinner.name" + KubeconfigKey = "kubernetes.kubeconfig" + JobPodLabelsKey = "kubernetes.job-pod-labels" + NoJobKey = "kubernetes.no-job" + LogLevelKey = "log.level" + LogFormatKey = "log.format" + LogRedactKey = "log.redact" + RemoteGzipKey = "remote-gzip" + PortForwardAddrKey = "port-forward.address" ) diff --git a/internal/util/cmd_setup.go b/internal/util/cmd_setup.go index 7bf5fcf8..eb5520bd 100644 --- a/internal/util/cmd_setup.go +++ b/internal/util/cmd_setup.go @@ -10,7 +10,6 @@ import ( "github.com/AlecAivazis/survey/v2" "github.com/clevyr/kubedb/internal/config" - "github.com/clevyr/kubedb/internal/config/namespace_filter" "github.com/clevyr/kubedb/internal/consts" "github.com/clevyr/kubedb/internal/database" "github.com/clevyr/kubedb/internal/kubernetes" @@ -53,11 +52,6 @@ func DefaultSetup(cmd *cobra.Command, conf *config.Global, opts SetupOptions) (e conf.Context = conf.Client.Context conf.Namespace = conf.Client.Namespace - access := namespace_filter.NewFromContext(ctx) - if !access.Match(conf.Client.Namespace) { - return errors.New("The current action is disabled for namespace " + conf.Client.Namespace) - } - if _, err := conf.Client.Namespaces().Get(ctx, conf.Namespace, metav1.GetOptions{}); err != nil { log.WithError(err).Warn("namespace may not exist") }