Skip to content

rbac-tool: Add support for namespace-specific ServiceAccount lookups in the lookup command#124

Open
ravisastryk wants to merge 1 commit intoalcideio:masterfrom
ravisastryk:ravisastryk/support-lookup-servericeaccount-namespace-cmd
Open

rbac-tool: Add support for namespace-specific ServiceAccount lookups in the lookup command#124
ravisastryk wants to merge 1 commit intoalcideio:masterfrom
ravisastryk:ravisastryk/support-lookup-servericeaccount-namespace-cmd

Conversation

@ravisastryk
Copy link

Add support for namespace-specific ServiceAccount lookups in the lookup command.

This is an attempt to help the contributors/users using the rbac-tool to take advantage of command line option to lookup serviceaccount with specific namespace.

Problem

It was reported in the issue#123 where previously, the rbac-tool lookup command could search for ServiceAccounts by name, but did not allow users to specify a namespace. As a result:

  • Users could not distinguish between ServiceAccounts with the same name in different namespaces and have to employ out of box solutions like using yet another tool such as awk to lookup.
  • The output did not clearly indicate the namespace of the matched ServiceAccount, causing confusion, especially in clusters with multiple namespaces using the default ServiceAccount name.
  • There was no way to limit results to ServiceAccounts within a specific namespace, leading to broader and less relevant results.

That is, the following command failed to directly perform lookup

rbac-tool lookup default

Fix/Solution

This PR introduces a --namespace flag to the lookup command. When specified:

  • The command restricts the search to ServiceAccounts within the provided namespace.
  • The output table now explicitly displays the namespace for each matched ServiceAccount.
  • This flag is optional; omitting it preserves the previous behavior (searches all namespaces). Therefore, it maintains compatibility with existing behaviour for users/groups.
  • Refactors existing convoluted logic in preparing table rows in the final output.

Example Usage

rbac-tool lookup default --namespace=somenamespace

Output

rbac-tool lookup default --namespace=myns
Error: Failed to create kubernetes client - invalid configuration: no configuration has been provided, try setting KUBERNETES_MASTER environment variable
Usage:
  rbac-tool lookup [flags]

Aliases:
  lookup, look

Flags:
      --cluster-context string   Cluster Context .use 'kubectl config get-contexts' to list available contexts
  -h, --help                     help for lookup
      --namespace string         Namespace of the serviceaccount <<<<<<-------- Note that this is now honored with this flag
  -n, --not                      Inverse the regex matching. Use to search for users that do not match '^system:.*'
  -e, --regex string             Specify whether run the lookup using a regex match

Global Flags:
  -v, --v Level   number for the log level verbosity

Failed to create kubernetes client - invalid configuration: no configuration has been provided, try setting KUBERNETES_MASTER environment variable

@ravisastryk
Copy link
Author

@gadinaor Can you please review when you get a chance?

@gadinaor-r7 gadinaor-r7 self-requested a review August 13, 2025 13:36
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants