Skip to content

Commit

Permalink
corrected commit and added only required files
Browse files Browse the repository at this point in the history
  • Loading branch information
girishg4t committed Feb 6, 2020
1 parent 20f035a commit 0fc5eba
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
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) {

type test struct {
input string
expected string
}

tests := []test{
{input: "get pods --cluster-name=minikube", expected: "minikube"},
{input: "--cluster-name minikube1", expected: "minikube1"},
{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-name minikube1", expected: "minikube1"},
}

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

0 comments on commit 0fc5eba

Please sign in to comment.