Skip to content
Open
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
40 changes: 29 additions & 11 deletions cmd/lookup_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func NewCommandLookup() *cobra.Command {
clusterContext := ""
regex := ""
inverse := false
namespace := ""

// Support overrides
cmd := &cobra.Command{
Expand All @@ -44,6 +45,8 @@ rbac-tool lookup -e '^system:.*'
# Lookup all accounts that DO NOT start with system: )
rbac-tool lookup -ne '^system:.*'

# Lookup ServiceAccount "default" in "myns" namespace
rbac-tool lookup default --namespace=myns
`,
Hidden: false,
RunE: func(c *cobra.Command, args []string) error {
Expand Down Expand Up @@ -106,6 +109,17 @@ rbac-tool lookup -ne '^system:.*'
}

//Subject match
// Filter by namespace if subject is ServiceAccount and namespace flag is set
subjNamespace := subject.Namespace
if subject.Kind == "ServiceAccount" {
if subjNamespace == "" {
subjNamespace = binding.Namespace
}
if namespace != "" && subjNamespace != namespace {
continue
}
}

roleNamespace := binding.Namespace
if binding.RoleRef.Kind == "ClusterRole" {
roleNamespace = ""
Expand All @@ -115,16 +129,20 @@ rbac-tool lookup -ne '^system:.*'
continue
}

if binding.Namespace == "" {
row := []string{subject.Name, subject.Kind, "ClusterRole", "", binding.RoleRef.Name, binding.Name}
rows = append(rows, row)
} else if binding.Namespace != "" && roleNamespace == "" {
row := []string{subject.Name, subject.Kind, "ClusterRole", binding.Namespace, binding.RoleRef.Name, binding.Name}
rows = append(rows, row)
} else {
row := []string{subject.Name, subject.Kind, "Role", binding.Namespace, binding.RoleRef.Name, binding.Name}
rows = append(rows, row)
scope := "Role"
if binding.RoleRef.Kind == "ClusterRole" {
scope = "ClusterRole"
}

row := []string{
subject.Name,
subject.Kind,
scope,
subjNamespace,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original code tracks the role namespace, and here it changes to the subject namespace.

binding.RoleRef.Name,
binding.Name,
}
rows = append(rows, row)
}
}
}
Expand All @@ -133,7 +151,6 @@ rbac-tool lookup -ne '^system:.*'
if strings.Compare(rows[i][0], rows[j][0]) == 0 {
return (strings.Compare(rows[i][3], rows[j][3]) < 0)
}

return (strings.Compare(rows[i][0], rows[j][0]) < 0)
})

Expand All @@ -146,8 +163,9 @@ rbac-tool lookup -ne '^system:.*'

flags := cmd.Flags()
flags.StringVar(&clusterContext, "cluster-context", "", "Cluster Context .use 'kubectl config get-contexts' to list available contexts")

flags.StringVarP(&regex, "regex", "e", "", "Specify whether run the lookup using a regex match")
flags.BoolVarP(&inverse, "not", "n", false, "Inverse the regex matching. Use to search for users that do not match '^system:.*'")
flags.StringVar(&namespace, "namespace", "", "Namespace of the serviceaccount")

return cmd
}