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

Passing namespace in the SelfSubjectAccessReviews function #82

Merged
merged 2 commits into from
Jul 29, 2022
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
4 changes: 2 additions & 2 deletions pkg/agent/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ AGENT_NAME:
return newAgent, nil
}

func ValidateSAPermissions(mode string, kubeconfig *string) {
func ValidateSAPermissions(namespace string, mode string, kubeconfig *string) {
var (
pems [2]bool
err error
Expand All @@ -228,7 +228,7 @@ func ValidateSAPermissions(mode string, kubeconfig *string) {
}

for i, resource := range resources {
pems[i], err = k8s.CheckSAPermissions(k8s.CheckSAPermissionsParams{Verb: "create", Resource: resource, Print: true}, kubeconfig)
pems[i], err = k8s.CheckSAPermissions(k8s.CheckSAPermissionsParams{Verb: "create", Resource: resource, Print: true, Namespace: namespace}, kubeconfig)
if err != nil {
utils.Red.Println(err)
}
Expand Down
10 changes: 8 additions & 2 deletions pkg/cmd/connect/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ var agentCmd = &cobra.Command{

// Check if user has sufficient permissions based on mode
utils.White_B.Print("\n🏃 Running prerequisites check....")
agent.ValidateSAPermissions(newAgent.Mode, &kubeconfig)
agent.ValidateSAPermissions(newAgent.Namespace, newAgent.Mode, &kubeconfig)

agents, err := apis.GetAgentList(credentials, newAgent.ProjectId)
utils.PrintError(err)
Expand Down Expand Up @@ -220,7 +220,7 @@ var agentCmd = &cobra.Command{

// Check if user has sufficient permissions based on mode
utils.White_B.Print("\n🏃 Running prerequisites check....")
agent.ValidateSAPermissions(modeType, &kubeconfig)
agent.ValidateSAPermissions(newAgent.Namespace, modeType, &kubeconfig)
newAgent, err = agent.GetAgentDetails(modeType, newAgent.ProjectId, credentials, &kubeconfig)
utils.PrintError(err)

Expand All @@ -239,6 +239,12 @@ var agentCmd = &cobra.Command{
utils.Red.Println("\n❌ Agent connection failed: " + err.Error() + "\n")
os.Exit(1)
}

if agent.Data.UserAgentReg.Token == "" {
utils.Red.Println("\n❌ failed to get the agent registration token: " + "\n")
os.Exit(1)
}

path := fmt.Sprintf("%s/%s/%s.yaml", credentials.Endpoint, utils.ChaosYamlPath, agent.Data.UserAgentReg.Token)
utils.White_B.Print("Applying YAML:\n", path)

Expand Down
10 changes: 6 additions & 4 deletions pkg/k8s/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,18 @@ func NsExists(namespace string, kubeconfig *string) (bool, error) {
}

type CheckSAPermissionsParams struct {
Verb string
Resource string
Print bool
Verb string
Resource string
Print bool
Namespace string
}

func CheckSAPermissions(params CheckSAPermissionsParams, kubeconfig *string) (bool, error) {

var o CanIOptions
o.Verb = params.Verb
o.Resource.Resource = params.Resource
o.Namespace = params.Namespace
client, err := ClientSet(kubeconfig)
if err != nil {
return false, err
Expand Down Expand Up @@ -160,7 +162,7 @@ start:
utils.White_B.Println("👍 Continuing with", namespace, "namespace")
}
} else {
if val, _ := CheckSAPermissions(CheckSAPermissionsParams{"create", "namespace", false}, kubeconfig); !val {
if val, _ := CheckSAPermissions(CheckSAPermissionsParams{"create", "namespace", false, namespace}, kubeconfig); !val {
utils.Red.Println("🚫 You don't have permissions to create a namespace.\n Please enter an existing namespace.")
goto start
}
Expand Down