Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checked cluster name before executing kubectl command #243

Merged
merged 3 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions pkg/execute/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,16 @@ func NewDefaultExecutor(msg string, allowkubectl bool, restrictAccess bool, clus
// Execute executes commands and returns output
func (e *DefaultExecutor) Execute() string {
args := strings.Fields(e.Message)

if validKubectlCommands[args[0]] {
isClusterNamePresent := strings.Contains(e.Message, "--cluster-name")
if !e.AllowKubectl {
return fmt.Sprintf(kubectlDisabledMsg, e.ClusterName)
if isClusterNamePresent && e.ClusterName == utils.GetClusterNameFromKubectlCmd(e.Message) {
return fmt.Sprintf(kubectlDisabledMsg, e.ClusterName)
}
return ""
}
isClusterNamePresent := strings.Contains(e.Message, "--cluster-name")

if e.RestrictAccess && !e.IsAuthChannel && isClusterNamePresent {
return ""
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"fmt"
"os"
"regexp"
"strconv"
"time"

Expand Down Expand Up @@ -402,3 +403,15 @@ func ExtractAnnotaions(obj *coreV1.Event) map[string]string {

return map[string]string{}
}

//GetClusterNameFromKubectlCmd this will return cluster name from kubectl command
func GetClusterNameFromKubectlCmd(cmd string) string {
r, _ := regexp.Compile(`--cluster-name[=|' ']([^\s]*)`)
//this gives 2 match with cluster name and without
matchedArray := r.FindStringSubmatch(cmd)
var s string
if len(matchedArray) >= 2 {
s = matchedArray[1]
}
return s
}
32 changes: 32 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package utils

import (
"testing"
)

func TestGetClusterNameFromKubectlCmd(t *testing.T) {
sanketsudake marked this conversation as resolved.
Show resolved Hide resolved

type test struct {
input string
expected string
}

tests := []test{
{input: "get pods --cluster-name=minikube", expected: "minikube"},
{input: "--cluster-name minikube1", expected: "minikube1"},
PrasadG193 marked this conversation as resolved.
Show resolved Hide resolved
{input: "--cluster-name minikube2 -n default", expected: "minikube2"},
{input: "--cluster-name minikube -n=default", expected: "minikube"},
{input: "--cluster-name", expected: ""},
{input: "--cluster-name ", expected: ""},
{input: "--cluster-name=", expected: ""},
{input: "", expected: ""},
{input: "--cluster-nameminikube1", expected: ""},
}

for _, ts := range tests {
got := GetClusterNameFromKubectlCmd(ts.input)
if got != ts.expected {
t.Errorf("expected: %v, got: %v", ts.expected, got)
}
}
}