kubectx icon indicating copy to clipboard operation
kubectx copied to clipboard

Allow setting namespace when no permission to list namespaces

Open josalmi opened this issue 6 years ago • 19 comments

We have users that are only allowed to access single namespace in the cluster with very restricted permissions. It would be nice to have option to set namespace to context even when user doesn't have permission to list all namespaces. Ideally kubens could verify from user if they want to set namespace even if it can't verify the namespace exists or to have a flag like --force to set without verifying.

This it what happens now even if namespace exists but user doesn't have right to list namespaces:

$ kubens helloworld
Error from server (Forbidden): namespaces is forbidden: User "josalmi" cannot list namespaces at the cluster scope: Required "container.namespaces.list" permission.
error: no namespace exists with name "helloworld".

josalmi avatar Sep 05 '18 07:09 josalmi

This came up once before in https://github.com/ahmetb/kubectx/issues/19.

Primarily, not being able to list namespaces straight out makes the kubens (without args) command useless. Now with recently added interactive mode that ability more important than ever.

Secondarily, Kubernetes has no namespace validity checks for the set-context command, therefore kubens enforces this. There might be a better way to enforce this (like kubectl get ns [VALUE]==successful)

I'll think about what we can do here. There might be a low-hanging fruit like the one I mentioned above.

ahmetb avatar Sep 05 '18 21:09 ahmetb

I just re-read #19, and apparently in that person's case, users didn't have the namespaces.get permission). @josalmi are your users able to kubectl get ns [NAME]?

ahmetb avatar Sep 05 '18 21:09 ahmetb

It seems that the feature request in issue 19 is exactly the same as I'm requesting.

At the moment they are not able to run kubectl get ns [NAME] but I guess that could be arranged. My current workaround for the single cluster that I switch to using kubetx is running kubectl config set-context $(kubectl config current-context) --namespace=helloworld manually.

Being able to run kubens would make my workflow more consistent and being able to rely on kubens in all cases. As said I think it's very reasonable to validate namespace if that is possible, but it would be nice for users to have option to override this with say the --force flag when it's not possible.

josalmi avatar Sep 06 '18 08:09 josalmi

I wasn't aware that you can work in a namespace without having permission to get the namespace.

I've already worked on being able to switch to a namespace without listing all namespaces. Unluckily there's a validity check which needs to get namespace permission before. See my initial changes https://github.com/tomaaron/kubectx/commit/cfd480162a9be2d94afa1015d62008d68f610319.

@ahmetb If you have another idea what to do when a user isn't able to list/get a namespace but wants change to it, let me know! I'm happy to contribute or adapt my initial changes.

tomaaron avatar Mar 28 '19 15:03 tomaaron

@tomaaron My concerns around your implementation would be that the check permissions etc causes ~.3-.5s overhead (per kubectl call) times three –for all kubens users.

Again, I'm not entirely convinced that kubens is useful for people who can't list/get namespaces. These users seem to be a very small percentage, and in that case, I don't think we should try to satisfy them.

Maybe we can add a -f/--force flag as suggested above, but that would not only complicate the code, but also make the kubens command (without args) and its tab completion completely useless.

ahmetb avatar Mar 28 '19 16:03 ahmetb

If/when we rewrite this in Go (#115), we can solve this without introducing additional overhead to rest of the kubens users who don't have this RBAC problem.

ahmetb avatar Jun 05 '19 18:06 ahmetb

We have the same issue, no cluster wide option to list namespaces only access granted to known namespaces

pixie79 avatar Jun 07 '19 04:06 pixie79

Hello,

I have this need also. It would be nice if you can implement this feature or add --force We restrict the list namespace permission but user still can work normally on their namespace.

Thank you!

hungpr0 avatar Apr 23 '20 03:04 hungpr0

Hi, I implemented -f idea in #215. What do you think?

mory91 avatar May 02 '20 00:05 mory91

Could this issue receive more attention? This kills the whole idea of the tool

smnbbrv avatar Jun 04 '20 06:06 smnbbrv

This was fixed for the Go implementation in master with #236. I didn’t make a release for it yet, but feel free to give it a try.

ahmetb avatar Jun 04 '20 06:06 ahmetb

That's super! Do you plan a release soon? :)

smnbbrv avatar Jun 04 '20 06:06 smnbbrv

Hope I get try the new feature soon

TreeKat71 avatar Jun 10 '20 05:06 TreeKat71

I've tested this with latest version of kubens (go based, not the bash-one), and it's working ok! Maybe we should close this ticket now?

morhook avatar Jun 20 '20 21:06 morhook

OS: mac Install by homebrew and I replace the kubens and kubectx under kubectx/0.9.0/bin. But I still can't use kubens to list or switch to other namespace.

error: failed to query if namespace exists (is cluster accessible?): failed to list namespaces from k8s API: namespaces is forbidden: User "u-sau3pmriea" cannot list resource "namespaces" in API group "" at the cluster scope

Is it error message related?

TreeKat71 avatar Jun 22 '20 06:06 TreeKat71

@TreeKat71 likely not. Homebrew still distributes the bash implementation. Please make sure you're checking which executable you have (bash or go).

ahmetb avatar Jun 22 '20 17:06 ahmetb

Hi @ahmetb, I mean I install kubectx from homebrew and I replace kubectx and kubens with go version. kubectx is 2.6M and kubens is 30M, I think it is go version. Did I set it correctly?

TreeKat71 avatar Jun 23 '20 01:06 TreeKat71

Yes but as I said above the fix is in master, but not tagged as a version yet.

ahmetb avatar Jun 23 '20 01:06 ahmetb

I see. I used the release 0.9. Thanks!

I build from master and it works fine!

TreeKat71 avatar Jun 23 '20 01:06 TreeKat71