Skip to content

Commit

Permalink
Add validation that kubeconfig is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
mszostok committed May 12, 2023
1 parent e155f82 commit a91fc3b
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions cmd/executor/gh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func (*GHExecutor) Metadata(context.Context) (api.MetadataOutput, error) {

// Execute returns a given command as a response.
func (e *GHExecutor) Execute(ctx context.Context, in executor.ExecuteInput) (executor.ExecuteOutput, error) {
if err := pluginx.CheckKubeConfigProvided(pluginName, in.Context.KubeConfig); err != nil {
return executor.ExecuteOutput{}, err
}

var cfg Config
err := pluginx.MergeExecutorConfigs(in.Configs, &cfg)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions internal/executor/helm/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ func (e *Executor) Metadata(context.Context) (api.MetadataOutput, error) {
// - history
// - get [all|manifest|hooks|notes]
func (e *Executor) Execute(ctx context.Context, in executor.ExecuteInput) (executor.ExecuteOutput, error) {
if err := pluginx.CheckKubeConfigProvided(PluginName, in.Context.KubeConfig); err != nil {
return executor.ExecuteOutput{}, err
}

cfg, err := MergeConfigs(in.Configs)
if err != nil {
return executor.ExecuteOutput{}, fmt.Errorf("while merging input configs: %w", err)
Expand Down
3 changes: 3 additions & 0 deletions internal/executor/helm/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ func TestExecutorConfigMergingErrors(t *testing.T) {
// when
_, err := hExec.Execute(context.Background(), executor.ExecuteInput{
Command: "helm install",
Context: executor.ExecuteInputContext{
KubeConfig: []byte("fake config"),
},
Configs: []*executor.Config{
{
RawYAML: mustYAMLMarshal(t, configA),
Expand Down
4 changes: 4 additions & 0 deletions internal/executor/kubectl/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func (e *Executor) Metadata(context.Context) (api.MetadataOutput, error) {

// Execute returns a given command as response.
func (e *Executor) Execute(ctx context.Context, in executor.ExecuteInput) (executor.ExecuteOutput, error) {
if err := pluginx.CheckKubeConfigProvided(PluginName, in.Context.KubeConfig); err != nil {
return executor.ExecuteOutput{}, err
}

cfg, err := MergeConfigs(in.Configs)
if err != nil {
return executor.ExecuteOutput{}, fmt.Errorf("while merging input configs: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/source/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (d *Dispatcher) Dispatch(dispatch PluginDispatch) error {
},
})
if err != nil {
return fmt.Errorf("while opening stream for %s: %w", dispatch.pluginName, err)
return fmt.Errorf(`while opening stream for "%s.%s" source: %w`, dispatch.sourceName, dispatch.pluginName, err)
}

go func() {
Expand Down
5 changes: 5 additions & 0 deletions internal/source/kubernetes/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/kubeshop/botkube/pkg/api"
"github.com/kubeshop/botkube/pkg/api/source"
pkgConfig "github.com/kubeshop/botkube/pkg/config"
"github.com/kubeshop/botkube/pkg/pluginx"
)

const (
Expand Down Expand Up @@ -64,6 +65,10 @@ func NewSource(version string) *Source {

// Stream streams Kubernetes events
func (*Source) Stream(ctx context.Context, input source.StreamInput) (source.StreamOutput, error) {
if err := pluginx.CheckKubeConfigProvided(PluginName, input.Context.KubeConfig); err != nil {
return source.StreamOutput{}, err
}

cfg, err := config.MergeConfigs(input.Configs)
if err != nil {
return source.StreamOutput{}, fmt.Errorf("while merging input configs: %w", err)
Expand Down
10 changes: 9 additions & 1 deletion pkg/pluginx/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/pkg/errors"
)

func PersistKubeConfig(ctx context.Context, kc []byte) (string, func(context.Context) error, error) {
func PersistKubeConfig(_ context.Context, kc []byte) (string, func(context.Context) error, error) {
if len(kc) == 0 {
return "", nil, fmt.Errorf("received empty kube config")
}
Expand All @@ -34,3 +34,11 @@ func PersistKubeConfig(ctx context.Context, kc []byte) (string, func(context.Con

return abs, deleteFn, nil
}

// CheckKubeConfigProvided returns an error if a given kubeconfig is empty or nil.
func CheckKubeConfigProvided(pluginName string, kubeconfig []byte) error {
if len(kubeconfig) != 0 {
return nil
}
return fmt.Errorf("The kubeconfig data is missing. Please make sure that you have specified a valid RBAC configuration for %q plugin. Learn more at https://docs.botkube.io/configuration/rbac.", pluginName)
}

0 comments on commit a91fc3b

Please sign in to comment.